If you are looking how to create active charts (or diagrams) with information – our new article is for you. I have browsed the web, and found one good and serious solution – Highcharts library. This is pure javascript library which offers interactive and intuitive charts. This library supports various of possible charts: area, line, spline, areaspline, pie, column and other. I think that this is the best way to produce information for viewers. In today’s demo I prepared several examples with different charts.
[sociallocker]
[/sociallocker]
Ok, let’s download the source files and start coding !
Step 1. HTML
In the beginning we have to add all the necessary scripts in the header section:
3 | < script src = "js/highcharts.js" ></ script > |
4 | < script src = "js/gray.js" ></ script > |
5 | < script src = "js/main.js" ></ script > |
This are jQuery library, highcharts, gray.js is a way to customize chart design. In our package you can see several more small javascript files: dark-blue.js, dark-green.js, grid.js and skies.js. All of them – different designs of charts. You can link one of them (instead current gray.js) to see different chart designs. The last one javascript file – main.js – our own file with initialize code. In our demonstration I prepared two different charts, plus – I added a possibility to change chart type on-fly for the first one chart, let’s look at the result html code:
03 | < button class = "switcher" id = "column" >column</ button > |
04 | < button class = "switcher" id = "area" >area</ button > |
05 | < button class = "switcher" id = "line" >line</ button > |
06 | < button class = "switcher" id = "spline" >Spine</ button > |
07 | < button class = "switcher" id = "areaspline" >areaspline</ button > |
10 | < div id = "chart_1" class = "chart" ></ div > |
11 | < div id = "chart_2" class = "chart" ></ div > |
Step 2. CSS
css/main.css
There are no any special styles for our charts, but anyway, our demo contains a few customizations (certain width for chart plus customized buttons):
06 | background : none repeat scroll 0 0 #E3E3E3 ; |
07 | border : 1px solid #BBBBBB ; |
08 | border-radius: 3px 3px 3px 3px ; |
09 | box-shadow: 0 0 1px 1px #F6F6F6 inset ; |
15 | text-shadow : 0 1px 0 #FFFFFF ; |
19 | background : none repeat scroll 0 0 #D9D9D9 ; |
20 | box-shadow: 0 0 1px 1px #EAEAEA inset ; |
25 | background : none repeat scroll 0 0 #D0D0D0 ; |
26 | box-shadow: 0 0 1px 1px #E3E3E3 inset ; |
Step 3. JS
Finally, let’s check our initialize javascript code:
js/main.js
002 | function ChangeChartType(chart, series, newType) { |
003 | newType = newType.toLowerCase(); |
004 | for ( var i = 0; i < series.length; i++) { |
007 | srs.chart.addSeries({ |
013 | data: srs.options.data |
024 | $(document).ready( function () { |
026 | chart1 = new Highcharts.Chart({ |
033 | text: 'Tools developers plans to use to make html5 games (in %)' |
036 | categories: [ 'Processing.js' , 'Impact.js' , 'Other' , 'Ease.js' , 'Box2D.js' , 'WebGL' , 'DOM' , 'CSS' , 'Canvas' , 'Javascript' ] |
045 | data: [5, 10, 20, 22, 25, 28, 30, 40, 80, 90] |
048 | data: [15, 15, 18, 40, 30, 25, 60, 60, 80, 70] |
051 | data: [1, 3, 6, 0, 50, 25, 50, 60, 30, 100] |
055 | chart2 = new Highcharts.Chart({ |
058 | plotBackgroundColor: null , |
059 | plotBorderWidth: null , |
064 | text: 'Pie chart diagram for the first developer' |
067 | pointFormat: '<b>{point.percentage}%</b>' , |
068 | percentageDecimals: 1 |
072 | allowPointSelect: true , |
084 | [ 'Processing.js' , 5], |
098 | $( '.switcher' ).click( function () { |
099 | var newType = $( this ).attr( 'id' ); |
100 | ChangeChartType(chart1, chart1.series, newType); |
In the beginning I prepared a function ‘ChangeChartType’ which will change type of our first chart on-fly. When we click at the buttons – it evokes onClick event, and we call the ‘ChangeChartType’ function and pass the value of ID attribute into this function (as a name of desired chart type). Now, please pay attention how we initialize Highcharts.Chart object. We have to define the object where it should render the chart, type, also we can define xAxis, yAxis and series (this is an array of source data for our chart). You can also follow this link to find official API reference for this library.
Conclusion
That is all for today. We have just developed a few really powerful charts with Highcharts. I’m sure that this material will be very useful for you. Good luck and welcome back.