首页 > 代码库 > Highcharts使用指南
Highcharts使用指南
统计分析报表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);
- });
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。