博客开了两个月了,今天终于有勇气写出第一篇了,希望对大家能有所帮助。
介绍一下HightCharts:
Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表。
Highcharts目前支持直线图、曲线图、面积图、柱状图、饼图、散点图等多达18种不同类型的图表。
HightCharts有一个中文网:http://www.hcharts.cn/,里面有各种图的模板和源代码,可以很好地学习
首先说一下柱状图:
在敲代码之前,要引入两个js文件
引过来之后就是这样
注意:一定要是jquery-1.8.2.min.js放上面,hightcharts.js放下面
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="jquery/highcharts.js"></script> <script type="text/javascript"> $(function(){ $("#container").highcharts({ //图表展示容器,与div的id保持一致 chart: { type:'column', //指定图表的类型,默认是折线图(line) }, title: { text:'这是我的图表', //指定图表标题 }, xAxis: { categories: ['柱', '状', '图'], //指定X分组 }, yAxis: { title: { text:'something', //指定Y轴的标题 }, }, series: [ //指定数据列,数据列里的数据是可以随业务的需求改变的 { name:'Jane', //数据列名 data:[1,2,4], //数据 },{ name:'John', data:[5,3,7], } ], }); }); </script> <body> <div id="container" style="width: 800px; height: 500px;"></div> //id="container",上面那个id选择器里写的就是这个id </body>
运行效果图:
折线图:
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="jquery/highcharts.js"></script> <script type="text/javascript"> $(function(){ $("#container").highcharts({ //图表展示容器,与div的id保持一致 //默认是折线图,所以chart: {type:'line',},不用写 title: { //头部 text: '我是标题', //text:标题的文本 x: -20 }, subtitle: { //副标题,写不写都行 text: '我是副标题', x: -20 }, xAxis: { //X坐标轴 categories类别 categories: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], plotLines: [{ //plotLines:标示线 value: 2, //定义在哪个值上显示标示线,这里是在x轴上刻度为3的值处垂直化一条线 width: 2, //标示线的宽度,2px dashStyle:'solid', //默认值是solid实线,这里定义为虚线 color: 'red',//线的颜色,定义为红色 }] }, yAxis: { //Y坐标轴 title: { text: 'Temperature (°C)' }, plotLines: [{ //plotLines:标示线 value: 2, //定义在哪个值上显示标示线,这里是在x轴上刻度为3的值处垂直化一条线 width: 1, //标示线的宽度,2px dashStyle:'solid', //默认值,这里定义为实线 color: '#808080',//线的颜色,定义为灰色 }] }, tooltip: { //数据提示框 valueSuffix: '$', //highcharts 提供了 valuePrefix(前缀)、valueSuffix(后缀) 来给数据添加前缀及后缀 }, //比如说 valuePrefix: '¥', valueSuffix: '元' legend: { //图例 layout: 'vertical', //图例内容布局方式,有水平布局及垂直布局可选,对应的配置值是: “horizontal(水平)”, “vertical(垂直)” align: 'left', //图例在图表中的对齐方式,有 “left”, "center", "right" 可选 verticalAlign: 'middle', //垂直对齐方式,有 'top', 'middle' 及 'bottom' 可选 borderWidth: 2 //边框宽度 }, series:[ //数据列 { //数据列中的 name 代表数据列的名字,并且会显示在数据提示框(Tooltip)及图例(Legend)中 name: 'John', 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] },{ 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] },{ name: 'Berlin', 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] },{ name: 'London', 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] }] }); }); </script> <body> <div id="container" style="width: 800px; height: 500px;"></div> </body>
运行效果图:
饼图:
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="jquery/highcharts.js"></script> <script type="text/javascript"> $(function () { $("#container").highcharts({ //图表展示容器,与div的id保持一致 chart: { //指定图表的类型,默认是折线图(line) plotBackgroundColor: null, //绘图区背景颜色 plotBorderWidth: null, //绘图区边框宽度 plotShadow: false //绘图投影 }, title: { //头部 text: 'Browser market shares at a specific website, 2010' }, tooltip: { //数据提示框 //单个点的格式化函数 pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { //图例事件,图例默认的点击行为是显示或隐藏当前数据列 pie: { allowPointSelect: false, //cursor: 'pointer', dataLabels: { //数据标签 enabled: true, //通过设置 legend.enabled = true | false 来打开或关闭图例 color: 'red', connectorColor: 'blue', //连接线颜色 format: '<b>{point.name}</b>: {point.percentage:.1f} %' } } }, series: [ { type: 'pie', //pie:饼图 name: 'Browser share', data: [ ['Firefox', 45.0], ['IE', 26.8], { name: 'Chrome', y: 12.8, sliced: false, //饼图各个部分分不分开,true为分开,false为不分开 selected: true }, ['Safari', 8.5], ['Opera', 6.2], ['Others', 0.7] ] }] }); }); </script> <body> <div id="container" style="width: 800px; height: 500px;"></div> </body>
运行效果图:
好了,HightCharts基本图形就到这里,下一篇介绍HightCharts与后台交互