统计分析报表Highcharts使用指南
一、前言(Preface)阅览本文,您可以了解:
1、Highcharts使用方法
2、Highcharts数据动态加载
3、Highcharts自动刷新数据
4、Highcharts常用模板
二、引用Highcharts的JS
- <script type="text/javascript" src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>
- <script type="text/javascript" src="http://cdn.hcharts.cn/highcharts/4.0.1/highcharts.js"></script>
- <script type="text/javascript"
src="http://cdn.hcharts.cn/highcharts/4.0.1/modules/exporting.js"></script>
复制代码
三、静态图表示例
- var chart1; // 全局变量
- $(document).ready(function() {
- chart1 = new Highcharts.Chart({
- chart: {
- renderTo: ‘container‘,
- type: ‘bar‘
- },
- title: {
- text: ‘Fruit Consumption‘
- },
- xAxis: {
- categories: [‘Apples‘, ‘Bananas‘, ‘Oranges‘]
- },
- yAxis: {
- title: {
- text: ‘Fruit eaten‘
- }
- },
- series: [{
- name: ‘Jane‘,
- data: [1, 0, 4]
- }, {
- name: ‘John‘,
- data: [5, 7, 3]
- }]
- });
- });
复制代码
四、动态图表示例(动态加载数据)(1)定义基本的初始的参数
- var options = {
- chart: {
- renderTo: ‘container‘,
- defaultSeriesType: ‘column‘
- },
- title: {
- text: ‘Fruit Consumption‘
- },
- xAxis: {
- categories: []
- },
- yAxis: {
- title: {
- text: ‘Units‘
- }
- },
- series: []
- };
复制代码
(2)动态加入数据
- $("#b1").click(function(){
- var ddate={
- name: ‘China‘,
- data: [Math.random()*10, Math.random()*10, Math.random()*10]
- };
- options.series.push(ddate);
- var chart = new Highcharts.Chart(options);
- });
复制代码
五、自动刷新
- function requestData() {
- $.ajax({
- url: ‘‘,
- success: function(data) {
- var series = chart.series[0],
- shift = series.data.length > 20; // shift if the series is longer than 20
- // add the point
- chart.series[0].addPoint(point, true, shift);
-
- // call it again after one second
- setTimeout(requestData, 1000);
- },
- cache: false
- });
- }
复制代码
六、常用模板(1)折线图
- var options = {
- chart: {
- renderTo: ‘container‘,
- type: ‘line‘,
- marginRight: 130,
- marginBottom: 25
- },
- title: {
- text: ‘Monthly Average Temperature‘,
- x: -20 //center
- },
- subtitle: {
- text: ‘Source: WorldClimate.com‘,
- x: -20
- },
- xAxis: {
- categories: [‘Jan‘, ‘Feb‘, ‘Mar‘, ‘Apr‘, ‘May‘, ‘Jun‘,
- ‘Jul‘, ‘Aug‘, ‘Sep‘, ‘Oct‘, ‘Nov‘, ‘Dec‘]
- },
- yAxis: {
- title: {
- text: ‘Temperature (°C)‘
- },
- plotLines: [{
- value: 0,
- width: 1,
- color: ‘#808080‘
- }]
- },
- tooltip: {
- formatter: function() {
- return ‘<b>‘+ this.series.name +‘</b><br/>‘+
- this.x +‘: ‘+ this.y +‘°C‘;
- }
- },
- legend: {
- layout: ‘vertical‘,
- align: ‘right‘,
- verticalAlign: ‘top‘,
- x: -10,
- y: 100,
- borderWidth: 0
- },
- series: []
- };
复制代码
调用方法:
- $("#b1").click(function(){
- var chart = new Highcharts.Chart(options);//或者 $(‘#container‘).highcharts(options);
- });
复制代码
(2)柱状图
- var options={
- chart: {
- type: ‘column‘
- },
- title: {
- text: ‘Monthly Average Rainfall‘
- },
- subtitle: {
- text: ‘Source: WorldClimate.com‘
- },
- xAxis: {
- categories: [
- ‘Jan‘,
- ‘Feb‘,
- ‘Mar‘,
- ‘Apr‘,
- ‘May‘,
- ‘Jun‘,
- ‘Jul‘,
- ‘Aug‘,
- ‘Sep‘,
- ‘Oct‘,
- ‘Nov‘,
- ‘Dec‘
- ]
- },
- yAxis: {
- min: 0,
- title: {
- text: ‘Rainfall (mm)‘
- }
- },
- 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} mm</b></td></tr>‘,
- footerFormat: ‘</table>‘,
- shared: true,
- useHTML: true
- },
- plotOptions: {
- column: {
- pointPadding: 0.2,
- borderWidth: 0
- }
- },
- series: [{
- name: ‘Tokyo‘,
- data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
- }, {
- name: ‘New York‘,
- data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]
- }, {
- name: ‘London‘,
- data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]
- }, {
- name: ‘Berlin‘,
- data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]
- }]
- }
复制代码
调用方法:
- $("#b1").click(function(){
- $(‘#container‘).highcharts(options);
- });
复制代码
(3)饼图
- var options = {
- chart: {
- plotBackgroundColor: null,
- plotBorderWidth: null,
- plotShadow: false
- },
- title: {
- text: ‘Monthly Average Temperature‘,
- x: -20 //center
- },
- subtitle: {
- text: ‘Source: WorldClimate.com‘,
- x: -20
- },
- tooltip: {
- pointFormat: ‘{series.name}: <b>{point.percentage:.1f}%</b>‘
- },
- plotOptions: {
- pie: {
- allowPointSelect: true,
- cursor: ‘pointer‘,
- dataLabels: {
- enabled: true,
- color: ‘#000000‘,
- connectorColor: ‘#000000‘,
- format: ‘<b>{point.name}</b>: {point.percentage:.1f} %‘
- }
- }
- },
- 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]
- ]
- }]
- };
复制代码
调用方法:
- $("#b1").click(function(){
- $(‘#container‘).highcharts(options);
- });
复制代码 Highcharts使用指南,布布扣,bubuko.com
Highcharts使用指南
原文:http://www.cnblogs.com/wxblog/p/3848156.html