zoukankan      html  css  js  c++  java
  • Highcharts.js -纯javasctipt图表库初体验

    一.highcharts简介以及引入

      highcharts作为免费提供给个人学习、个人网站和非商业用途使用的前端图表演示插件的确使用起来十分方便和轻便。在我最近完成一个需求的时候用到了它, 它的兼容性也很强,其在标准(W3C标准)浏览器中使用SVG技术渲染图形,在遗留的IE浏览器中使用VML技术来绘图。我们在使用highcharts的时候只需要引入highcharts.js 及 jQuery 、 MooTools 、Prototype 、Highcharts Standalone Framework 常用 JS 框架中的一个,在此我们使用jQuery。

    只需在你的项目中如此引用就能方便的调用它的各种函数

    <script src="./jquery-1.8.3.min.js"></script>
    <script src="./highcharts.js"></script>

    二.常用图表介绍

      HighCharts支持图表类型,包括曲线图、区域图、柱状图、饼状图、散状点图和综合图表等,在此主要介绍一下饼图,柱状图以及线性图的属性和调用方法。

    首先要确保如上的代码正确引用,新建一个index.html,加入以下代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script type="text/javascript" src="js/jquery-2.1.4.js"></script>
        <script src="js/highcharts.js"></script>
     
     <script>
     var params ={
    
     };
    $(function () {
        $('.pieChart').highcharts({
            chart: {
                 plotBackgroundColor: null, //绘制图形区域的背景颜色
                    plotBorderWidth: null, //边框颜色
                    plotShadow: true, //绘图区投影
                     params.width || 200, //
                    height: params.height || 200, //
                    margin: [0, 0, 0, 0],
                    reflow: false,//自动缩放
                    //animation:false
            },
            title: {
                text: '饼图'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            credits: {//去掉图标
                    enabled: false
                }, 
            plotOptions: {
               pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        size:params.size || 100,//pie size
                        dataLabels: {
                            enabled: true,
                            color: '#000000',
                            connectorColor: '#000000',
                            format: '<b>{point.name}</b>: <br>{point.percentage:.1f} %',
                            distance: -5
                        },
                    }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: [
                    ['Firefox',   45.0],
                    ['IE',       26.8],
                    {
                        name: 'Chrome',
                        y: 12.8,
                        sliced: true,
                        selected: true
                    },
                    ['Safari',    8.5],
                    ['Opera',     6.2],
                    ['Others',   0.7]
                ]
            }]
        });
    
    
        $('#lineChart').highcharts({
                chart: {                                                                
                        type: 'spline', 
                        200,//
                        height:200,//                                                
                        animation: Highcharts.svg, // don't animate in old IE               
                        marginRight: 10,                                                    
                        events: {                                                           
                            load: function() {                                              
                                                                                            
                                                                              
                            }                                                               
                        }                                                                   
                    },
    
                credits: {
                    enabled: false
                },
                plotOptions: {
                    line: {
                        animation: false
                    },
                    series: {
                        animation: false
                    }
                },
                title: {
                    text: '线性图'
                },
                xAxis: {
                    type: 'datetime',
                    // dateTimeLabelFormats: { // don't display the dummy year
                    //     second: '%H:%M:%S'
                    },
                yAxis: {                                                                
                        title: {                                                            
                            text: '单位(Mbit/s)'                                                   
                        },                                                                  
                        plotLines: [{                                                       
                            value: 0,                                                       
                             1,                                                       
                            color: '#808080'                                                
                        }] ,
    
                        min: 0,
                        allowDecimals:false
                                                                               
                    },
                series: [{
                    name:'网站流量',
                    data:[ [ 1453443752602, 30.2 ], [ 1453443753602, 47.9 ], [ 1453443754602, 38.2 ], [ 1453443755602, 59.8 ], [ 1453443756602, 43.3 ], [ 1453443757602, 57.1 ], [ 1453443758602, 52.2 ], [ 1453443759602, 48 ] ]
                }]
    
            });
    
        $('.barChart').highcharts({
          
                    chart: {
                        type: 'column',
                        height:200,
                        params.width || 250,
                    },
                    credits: {
                        enabled: false
                    },
                    legend:{
                        enabled: false
    
                    },
                    title: {
                        text: params.title
                    },
                    subtitle: {
                        text: ''
                    },
                    xAxis: {
                        categories:params.categoriesArr|| ['当前','','']
                    },
                    yAxis: {
                        min: 0,
                        title: {
                            text: params.yUnit ||'(个)'
                        }
                    },
                    tooltip: {
                        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                            '<td style="padding:0"><b>{point.y:.1f} '+'(个)'+'</b></td></tr>',
                        footerFormat: '</table>',
                        shared: true,
                        useHTML: true
                    },
                    plotOptions: {
                        column: {
                            pointPadding: 0.2,
                            borderWidth: 0,
                            pointWidth:params.pointWidth||30 //宽度
                        }
                    },
                    series: params.series||
                    [{
                         name: ['数量'],
                        data: [213,321,112]
                        }
                       
                    ]
    
                });
    
    
    });
    </script>
    </head>
    <body>
    
        <div class="pieChart"></div>
        <div id="lineChart"></div>
        <div class="barChart"></div>
    </body>
    
    </html>

    运行就可以看到常用的饼图,条形图柱状图已经顺利生成了。

    我们可以看到在highchart中,是利用jquery选择器去选择相应的元素进行绘图,所以我们可以灵活的使用id,class等选择器为我们绑定图表。

    三.常用属性介绍

    成功了运行上面的图表生成代码,那么下面我来为大家介绍一下其中常用的属性,以满足一般开发要求。

    1.chart 顾名思义图表属性,通过改变它去改变图表的样式等

    type: 'spline', //图表类型

    plotBackgroundColor: null, //绘制图形区域的背景颜色

    plotBorderWidth: null, //边框颜色

    plotShadow: true, //绘图区投影

    params.width || 200, //整个绘图区域宽度

    height: params.height || 200, //这个绘图去高度

    margin: [0, 0, 0, 0],//绘图区域margin

    reflow: false,//自动缩放

    events:  //图表事件监听器

    2.title

    text: params.title// title的文字信息

    3.credits

    enabled: false// 不展示logo

    4.plotOptions  对应的图形样式设定

    pie: {//饼图
    allowPointSelect: true,
    cursor: 'pointer',
    size:params.size || 100,//饼图中,饼图自身半径
    dataLabels: {
    enabled: true,
    color: '#000000',
    connectorColor: '#000000',
    format: '<b>{point.name}</b>: <br>{point.percentage:.1f} %',
    distance: -5  //用于设置饼图上描述文字的位置
    },
    }
    
    
    
    plotOptions: {// 折线图
    
    line: {
    animation: false
    },
    series: {
    animation: false
    }
    },
    
    
    
    plotOptions: {//柱形图
    column: {
    pointPadding: 0.2,
    borderWidth: 0,
    pointWidth:params.pointWidth||30 //宽度
    }
    }
    

      

    5.series  图表数据,注意尽量用number类型而不是string,因为除了饼图外,string值不被识别

     

    四.参考

    highcharts

  • 相关阅读:
    java定时读取文件
    Java面试:投行的15个多线程和并发面试题(转)
    读取一个文件,给定一个字符串,判断这个字符串在文件中出现的次数
    随机产生10个数,并每个数给定一个序号,然后将这10个数按照从小到大的顺序输出来,并带上序号输出
    找出给定字符串中出现最多的字符和次数
    公司开发部门GIT与SVN 之争
    浅谈Hibernate中的三种数据状态
    MyBatis框架的XML数据访问Dao层接口的组合使用
    浅谈WebLogic和Tomcat
    为什么我们不要 .NET 程序员
  • 原文地址:https://www.cnblogs.com/gabrielchen/p/5153395.html
Copyright © 2011-2022 走看看