如果你正在寻找如何创建图表,那我们这篇文章就是为你准备的。我曾经在网上找了很多的资料,怎样去完美的解决创建图表的方案,让我惊喜的是发现了一个很好的很强悍的Javascript图表库:Highcharts。 这是一个纯Javascript库,它主要包括两个部分: 主要的特性有:
我认为在现有的阶段来说,这是最好的方式来为用户表达图表信息了。今天就准备几个例子,分享给大家,一起来见证这精彩的时刻吧。先看看demo效果图,也一并提供附件,有需要的童鞋请猛击! 第一步:HTML<!-- add scripts -->
<script src="http://code./jquery-1.7.1.min.js"></script>
<script src="js/highcharts.js"></script>
<script src="js/gray.js"></script>
<script src="js/main.js"></script>
其实Highcharts也是一个jQuery库,所以在顶部还是要引用jQuery库。在我们的附件里面, <!-- Chart type switchers --> <div> <button id="column">column</button> <button id="area">area</button> <button id="line">line</button> <button id="spline">Spine</button> <button id="areaspline">areaspline</button> </div> <!-- two different charts --> <div id="chart_1"></div> <div id="chart_2"></div> 第二步:CSS现在的图标是没有任何的样式风格,我们需要给图表加上一些固定的宽度和按钮的样式: css/main.css .actions, .chart { margin: 15px auto; width: 820px; } button { background: none repeat scroll 0 0 #E3E3E3; border: 1px solid #BBBBBB; border-radius: 3px 3px 3px 3px; box-shadow: 0 0 1px 1px #F6F6F6 inset; color: #333333; font: bold 12px; margin: 0 5px; padding: 8px 0 9px; text-align: center; text-shadow: 0 1px 0 #FFFFFF; width: 150px; } button:hover { background: none repeat scroll 0 0 #D9D9D9; box-shadow: 0 0 1px 1px #EAEAEA inset; color: #222222; cursor: pointer; } button:active { background: none repeat scroll 0 0 #D0D0D0; box-shadow: 0 0 1px 1px #E3E3E3 inset; color: #000000; } 第三步:JSjs/main.js 最后,让我们一起看看我们的初始化javascript代码: // Change Chart type function function ChangeChartType(chart, series, newType) { newType = newType.toLowerCase(); for (var i = 0; i < series.length; i++) { var srs = series[0]; try { srs.chart.addSeries({ type: newType, stack: srs.stack, yaxis: srs.yaxis, name: srs.name, color: srs.color, data: srs.options.data }, false); series[0].remove(); } catch (e) { } } } // Two charts definition var chart1, chart2; // Once DOM (document) is finished loading $(document).ready(function() { // First chart initialization chart1 = new Highcharts.Chart({ chart: { renderTo: 'chart_1', type: 'column', height: 350, }, title: { text: 'Tools developers plans to use to make html5 games (in %)' }, xAxis: { categories: ['Processing.js', 'Impact.js', 'Other', 'Ease.js', 'Box2D.js', 'WebGL', 'DOM', 'CSS', 'Canvas', 'Javascript'] }, yAxis: { title: { text: 'Interviewed' } }, series: [{ name: 'Dev #1', data: [5, 10, 20, 22, 25, 28, 30, 40, 80, 90] }, { name: 'Dev #2', data: [15, 15, 18, 40, 30, 25, 60, 60, 80, 70] }, { name: 'Dev #3', data: [1, 3, 6, 0, 50, 25, 50, 60, 30, 100] }] }); // Second chart initialization (pie chart) chart2 = new Highcharts.Chart({ chart: { renderTo: 'chart_2', plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, height: 350, }, title: { text: 'Pie chart diagram for the first developer' }, tooltip: { pointFormat: '<b>{point.percentage}%</b>', percentageDecimals: 1 }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: false }, showInLegend: true } }, series: [{ type: 'pie', name: 'Dev #1', data: [ ['Processing.js', 5], ['Impact.js', 10], ['Other', 20], ['Ease.js', 22], ['Box2D.js', 25], ['WebGL', 28], ['DOM', 30], ['CSS', 40], ['Canvas', 80], ['Javascript', 90] ] }] }); // Switchers (of the Chart1 type) - onclick handler $('.switcher').click(function () { var newType = $(this).attr('id'); ChangeChartType(chart1, chart1.series, newType); }); }); 在一开始的时候我定义了一个函数: 今天就到这里。我希望这个非常强大的图表highcharts将会对你有帮助。祝你好运,欢迎回来。有不懂的可以给我们留言,我们将一起探讨更多的知识! |
|