使用highcharts插件

       highcharts是一个功能强大、开源、美观、图表丰富、兼容绝大多数浏览器的纯js图表库,通过highcharts插件,可以封装很个性而且强大的图形。

      首先,下载highcharts插件。导入项目。

      后台,封装的数据,因为显示图形的封装都是一样的,只有数据不一样;

     highcharts属性:

  1.         renderTo: 'container',//设置显示图表的容器  

  2.             type: 'line',//设置图表样式,可以为line,spline, scatter, splinearea bar,pie,area,column  

  3. //          defaultSeriesType: 'column', //图表的默认样式  

  4. //          margin:[21, 23, 24, 54],//整个图表的位置(上下左右的空隙)  

  5.             marginRight: 200,//右边间距  

  6.             marginBottom: 25//底部间距/空隙  

  7. //          inverted: false,//可选,控制显示方式,默认上下正向显示  

  8. //          shadow:true,//外框阴影  

  9. //          backgroundColor:"#FFF",  

  10. //          animation:true,  

  11. //          borderColor:"#888",  

  12. //          borderRadius:5,  

  13. //          borderWidth:1,  

  14. //          ignoreHiddenSeries:true,  

  15. //          reflow:true,  

  16. //          plotBorderWidth:1,  

  17. //          alignTicks:true  

  18.         },  

  19.         labels:{//在报表上显示的一些文本  

  20.             items:[{  

  21.                 html:'本图表数据有误,仅用于说明相应的属性',  

  22.                 style:{left:'100px',top:'60px'}  

  23.             }, {  

  24.                 html:'http://www.highcharts.com/demo',  

  25.                 style:{left:'100px',top:'100px'}  

  26.             }]  

  27.         },  

  28.         credits:{//右下角的文本  

  29.             enabled: true,  

  30.             position: {//位置设置  

  31.                 align: 'right',  

  32.                 x: -10,  

  33.                 y: -10  

  34.             },  

  35.             href: "http://www.highcharts.com",//点击文本时的链接  

  36.             style: {  

  37.                 color:'blue'  

  38.             },  

  39.             text: "Highcharts Demo"//显示的内容  

  40.         },  

  41. //        plotOptions:{//绘图线条控制  

  42. //            spline:{  

  43. //                allowPointSelect :true,//是否允许选中点  

  44. //                animation:true,//是否在显示图表的时候使用动画  

  45. //                cursor:'pointer',//鼠标移到图表上时鼠标的样式  

  46. //                dataLabels:{  

  47. //                   enabled :true,//是否在点的旁边显示数据  

  48. //                    rotation:0  

  49. //                },  

  50. //                enableMouseTracking:true,//鼠标移到图表上时是否显示提示框  

  51. //                events:{//监听点的鼠标事件  

  52. //                    click: function() {  

  53. //                    }  

  54. //                },  

  55. //                marker:{  

  56. //                    enabled:true,//是否显示点  

  57. //                   radius:3,//点的半径  

  58. //                      fillColor:"#888"  

  59. //                    lineColor:"#000"  

  60. //                    symbol: 'url(http://highcharts.com/demo/gfx/sun.png)',//设置点用图片来显示  

  61. //                    states:{  

  62. //                        hover:{  

  63. //                            enabled:true//鼠标放上去点是否放大  

  64. //                                                    },  

  65. //                        select:{  

  66. //                            enabled:false//控制鼠标选中点时候的状态  

  67. //                        }  

  68. //                    }  

  69. //               },  

  70. //                states:{  

  71. //                    hover:{  

  72. //                        enabled:true,//鼠标放上去线的状态控制  

  73. //                        lineWidth:3  

  74. //                    }  

  75. //                },  

  76. //                stickyTracking:true,//跟踪  

  77. //                visible:true,  

  78. //                lineWidth:2//线条粗细  

  79. //                pointStart:100,  

  80. //            }  

  81. //        },  

  82.   

  83.         title: {  

  84.             text: 'Monthly Average Temperature',//标题  

  85.             x: -20 //center设置标题的位置  

  86.         },  

  87.         subtitle: {  

  88.             text: 'Source: WorldClimate.com',//副标题  

  89.             x: -20//副标题位置  

  90.         },  

  91.         xAxis: {//横轴的数据  

  92.             categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',  

  93.                 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']  

  94. //          lineWidth:1,//纵轴一直为空所对应的轴,即X轴  

  95. //          plotLines: [{//一条竖线  

  96. //               color: '#FF0000',  

  97. //               width: 2,  

  98. //               value: 5.5  

  99. //           }]  

  100. //          labels: {//设置横轴坐标的显示样式  

  101. //              rotation: -45,//倾斜度  

  102. //              align: 'right',  

  103. //              step:1,//刻度间隔  

  104. //              style: {  

  105. //                   font: 'normal 13px Verdana, sans-serif'  

  106. //                   color: 'white'  

  107. //              }  

  108. //          }  

  109.   

  110.         },  

  111.         yAxis: {  

  112. //          tickInterval: 200,  //自定义刻度  

  113. //          max:1000,//纵轴的最大值  

  114. //          min: 0,//纵轴的最小值  

  115.             title: {//纵轴标题  

  116.                 text: '百分数'  

  117.             },  

  118.             labels : {  

  119.                 formatter : function() {//设置纵坐标值的样式  

  120.                  return this.value + '%';  

  121.                 }  

  122.                },   

  123.             plotLines: [{  

  124.                 value: 0,  

  125.                 width: 1,  

  126.                 color: '#808080'  

  127.             }]  

  128.         },  

  129.         tooltip: {//鼠标移到图形上时显示的提示框  

  130.             formatter: function() {  

  131.                     return '<b>'+ this.series.name +'</b><br/>'+  

  132.                     this.x +': '+ this.y +'°C';  

  133.             }  

  134. //          crosshairs:[{//控制十字线  

  135. //              width:1,  

  136. //              color:"#CCC",  

  137. //              dashStyle:"longdash"  

  138. //          }  

  139.   

  140.         },  

  141.         legend: {//方框所在的位置(不知道怎么表达)  

  142.             layout: 'vertical',  

  143.             align: 'right',  

  144.             verticalAlign: 'top',  

  145.             x: -10,  

  146.             y: 100,  

  147.             borderWidth: 0  

  148.         },  

  149.         series: [{//以下为纵轴数据  

  150.             name: 'Tokyo',  

  151.             data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]  

  152.         }, {  

  153.             name: 'New York',  

  154.             data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]  

  155.         }, {  

  156.             name: 'Berlin',  

  157.             data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]  

  158.         }, {  

  159.             name: 'London',  

  160.             data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]  

  161.         }]  

  162.     }); 

        前面的属性的一样的。所以可以写一个接口。

       传入json字符串到前台:

一:  饼图:

{"type":"pie","options3d":{"enabled":true,"alpha":50,"beta":0}},"title":{"text":""},"tooltip":{"pointFormat":"<b>{point.percentage:.1f}%</b>"},"plotOptions":{"pie":{"depth":45,"allowPointSelect":true,"cursor":"pointer","dataLabels":{"distance":1,"format":"{point.name}<br>{point.percentage:.1f}%"},"showInLegend":true}},"credits":{"enabled":false},"legend":{"layout":"vertical","align":"right","verticalAlign":"middle"},"colors":['#3CB371','#3CC371','#66CDAA','#B0E0E6'],

"series":[{data:[['name1',1],['name2',1],['name3',1],['name4',27],['name5',1],['name6',3],['name7',2],['name8',1],['name10',1],['name11',17]]}]}

series:是数据的参数,series前面的属性都可以是固定的,关键是series后面的data参数的封装。格式是

[{data:[['name',num]]}


二:普通 柱状图:

{"type":"column","options3d":{"enabled":true,"alpha":1,"beta":1,"depth":500,"viewDistance":2300}},"title":{"text":""},"plotOptions":{"column":{"depth":500,"animation":true,"groupPadding":"0.1","dataLabels":{"enabled":true,"inside":true}}},"credits":{"enabled":false},"xAxis":{"title":{"text":""},"categories":['unknownUser0','453','jack','liulin','Michael','testpass2','Tim'],"gridLineWidth":0,"labels":{"rotation":0,"align":"right"}},"colors":['#E1BCFB','#87CEEB','#7CB5EC','#85EB0C','#FF6B01'],"yAxis":{"title":{"text":""}},"series":[{name:'Vulnerability',data:[1,2,1146,3,122,1,243]}]}

普通柱状图有两个数据参数:

第一个是categories,是显示X轴的,也就是横坐标的参数:封装格式是['name1','num2','name3']

第二个是series:跟上面一样,是显示Y轴的,格式是

{name:'name',data:[1,2,1146,3,122,1,243]


三:叠加柱状图

{"type":"column","options3d":{"enabled":true,"alpha":1,"beta":1,"depth":400,"viewDistance":2300}},"title":{"text":""},"plotOptions":{"column":{"depth":400,"animation":true,"groupPadding":"0.2","dataLabels":{"enabled":true,"inside":true},"stacking":"normal"}},"credits":{"enabled":false},"xAxis":{"title":{"text":""},"categories":['453','jack','liulin','Tim','unknownUser','testpass2','Michael'],"gridLineWidth":0,"labels":{"rotation":0,"align":"right"}},"colors":['#FFBBFF','#7cb5ec','#87CEEB','#32CD32'],"yAxis":{"title":{"text":""}},"series":[{name:'Low',data:[1,677,0,162,0,0,84]},{name:'Medium',data:[1,417,2,70,1,1,37]},{name:'High',data:[0,52,1,11,0,0,1]}]}

   叠加柱状图也有两个数据参数:

第一个是categories,是显示X轴的,也就是横坐标的参数:封装格式是['name1','num2','name3']

第二个是series:跟上面一样,是显示Y轴的,data格式和上面不一样,是这样

{name:'类型1',data:[1,677,0,162,0,0,84]},{name:'类型2',data:[1,417,2,70,1,1,37]}


四:条形图

{"type":"column","inverted":true,"options3d":{"enabled":true,"alpha":1,"beta":1,"depth":300,"viewDistance":2300}},"title":{"text":""},"plotOptions":{"column":{"depth":300,"animation":true,"groupPadding":"0.1","dataLabels":{"enabled":true,"inside":true}}},"credits":{"enabled":false},"xAxis":{"title":{"text":""},"categories":['LogInsight Log 3357'],"gridLineWidth":0,"labels":{"rotation":0,"align":"right"}},"colors":['#7957A4','#97BD45','#C83C39','#3C7AC8'],"yAxis":{"title":{"text":""}},"series":[{name:'HighVulerabilery ',data:[1]}]}

条形图数据参数也是两个,categories和series

categories:x轴,封装格式是['name1','name2',...]

是:series ,封装格式是 [{name:'name1',data:[1,2]}]}

五:折线图

{"type":'spline'},"title":{"text":' '},"subtitle":{"text":''},"xAxis":{"categories":['10-20','10-21','10-22','10-23','10-24','10-25','10-26'],"title":{"text":''}},"yAxis":{"title":{"text":''}},"tooltip":{"enabled":false,"formatter":function() { return '<b>'+ this.series.name +'</b><br>'+this.x +': '+ this.y; }},"plotOptions":{"line":{"dataLabels":{"enabled":true},"enableMouseTracking":false}},"series":[{name:'Case Create Trends',data:[1,1,1,0,0,0,1]}]

折线图也是两个参数:

  1. xAxis: X轴参数

    格式: ['name1','name2'...]

  2. series:Y轴参数

    格式:[{name:'',data[num1,num2,num3]}]


需要注意的是:1,封装的格式,中括号[],大括号{},这些都是不能错的。

                     2,data里面的值要跟X轴的一样匹配,如果数据乱了,或者少一个多一个,就好出现数据错误,或者不能显示

  


js页面显示HighChart图片。导入HighChart插件


//1.通过json或者其他请求获得数据

    $.jsonpGet({
        url:url,  //请求路径
        async:false,
        callbackName:"sss",//回调函数
       fun:function(data){
            if(data.succeed){
                console.log(data.data.chart);
                if(data.data==null)
                {

           //这一段是显示图片,前面是取到数据

             var chart=$.showCharts({
              data:highChartsStr,//封装好的数据
              id:dashboardPanelNo,//显示的div的ID,注意,不能带#号的
              legend:'right',
              remotion:30,
              error:function(){//如果错误执行的方法
               alert('It has no data ...');
              }


                }//判断结束

  });//请求结束


你可能感兴趣的:(使用highcharts插件)