首先,下载highcharts js包文件,引入:
<script type="text/javascript" src="js/jquery.1.9.1.min.js"></script> <script type="text/javascript" src="js/highcharts.js"></script> <script type="text/javascript" src="js/modules/exporting.js"></script>
例子:
<script type="text/javascript"> $(document).ready(function () { var chart = new Highcharts.Chart({ chart: { renderTo: 'container',//放置图标的容器 type: 'scatter', //line 折线图 area 区域图 'column' 柱状图 bar条形图 scatter散列图 'bubble'气泡图 marginRight: 130, marginBottom: 25 }, title: { text: 'Monthly Average Temperature', x: -20 //center }, subtitle: { text: 'Source: WorldClimate.com', x: -20 }, //X 轴 xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, //Y轴 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'; } }, //划分选项 plotOptions: { line: { dataLabels: { enabled: true, style: { textShadow: '0 0 3px white, 0 0 3px white' } }, enableMouseTracking: false } }, legend: { layout: 'vertical',//显示样式,垂直或者水平 align: 'right', verticalAlign: 'top',//默认为底部 x: -10, y: 100, borderWidth: 0 }, series:<%=xx %> }); }); </script>
其中series对应的是数据部分,这一部分我可以通过对数据库的处理分析得到。其实就是得到一个json字符串。
protected string xx = " [{ name: 'Tokyo',data: [30.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] }, {name: 'New York',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] }]";
下面是另外一个例子柱状、曲线、饼图组合图,取自Highcharts配置手册。
效果图:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Chart.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="js/jquery.1.9.1.min.js"></script> <script type="text/javascript" src="js/highcharts.js"></script> <script type="text/javascript" src="js/modules/exporting.js"></script> <script type="text/javascript"> $(function () { $('#container').highcharts({ chart: { }, title: { text: 'Combination chart' }, xAxis: { categories: ['Apples', 'Oranges', 'Pears', 'Bananas', 'Plums'] }, tooltip: { formatter: function () { var s; if (this.point.name) { // the pie chart s = '' + this.point.name + ': ' + this.y + ' fruits'; } else { s = '' + this.x + ': ' + this.y; } return s; } }, labels: { items: [{ html: 'Total fruit consumption', style: { left: '40px', top: '8px', color: 'black' } }] }, series: [{ type: 'column', name: 'Jane', data: [3, 2, 1, 3, 4] }, { type: 'column', name: 'John', data: [2, 3, 5, 7, 6] }, { type: 'column', name: 'Joe', data: [4, 3, 3, 9, 0] }, { type: 'spline', name: 'Average', data: [3, 2.67, 3, 6.33, 3.33], marker: { lineWidth: 2, lineColor: Highcharts.getOptions().colors[3], fillColor: 'white' } }, { type: 'pie', name: 'Total consumption', data: [{ name: 'Jane', y: 13, color: Highcharts.getOptions().colors[0] // Jane's color }, { name: 'John', y: 23, color: Highcharts.getOptions().colors[1] // John's color }, { name: 'Joe', y: 19, color: Highcharts.getOptions().colors[2] // Joe's color }], center: [100, 80], size: 100, showInLegend: false, dataLabels: { enabled: false } }] }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> </div> </form> </body> </html>