{"id":19996,"date":"2015-05-26T11:46:12","date_gmt":"2015-05-26T06:16:12","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19996"},"modified":"2017-04-25T10:00:13","modified_gmt":"2017-04-25T04:30:13","slug":"adding-interactive-charts-to-web-pages-using-highcharts","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/adding-interactive-charts-to-web-pages-using-highcharts\/","title":{"rendered":"Adding interactive charts to web pages using Highcharts"},"content":{"rendered":"<p>I recently had to add charts to a web page in my project. I\u00a0used\u00a0<a href=\"http:\/\/www.highcharts.com\/\">Highcharts<\/a> library for this.\u00a0It is very dynamic allowing you to change graphs even after the chart\u00a0has been drawn.<\/p>\n<p>To display\u00a0a graph we need a div with an ID. Let&#8217;s say we have\u00a0div with ID &#8220;chartDivId&#8221; and we want to display two lines in the chart. We can use the below for doing this.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n&lt;div id=&quot;chartDivId&quot;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;script&gt;<br \/>\nvar myChart = new Highcharts.Chart({<br \/>\n            chart: {<br \/>\n                renderTo: &#8216;chartDivId&#8217;<br \/>\n            },<br \/>\n            series: [{<br \/>\n                data: [[1, 2], [2, 9], [3, 4], [5, 15]]<br \/>\n            },<br \/>\n            {<br \/>\n                data: [5, 6, 7, 9, 15, 2, 5]<br \/>\n            }]<br \/>\n        });<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>Using the above I was able to get this chart. Quite easy? Wasn&#8217;t it?<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/charts2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-20013\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/charts2.png\" alt=\"charts2\" width=\"1837\" height=\"425\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>A few things about the above code<\/p>\n<ul>\n<li>Here the series element is to add series to the chart. Each one needs to have a\u00a0<a href=\"http:\/\/api.highcharts.com\/highcharts#series&lt;line&gt;.data\">data<\/a>\u00a0element which is used to add particular points to the series.<\/li>\n<li>renderTo specifies the divId to which chart needs to be rendered.<\/li>\n<\/ul>\n<p>Changing xAxis, yAxis, Chart title, changing legend positions etc. can be done by simply changing the above to this.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nvar myChart = new Highcharts.Chart({<br \/>\n            chart: {<br \/>\n                renderTo: &#8216;chartDivId&#8217;<br \/>\n            },<br \/>\n            series: [<br \/>\n                {<br \/>\n                    data: [[1, 2], [2, 9], [3, 4], [5, 15]]<br \/>\n                },<br \/>\n                {<br \/>\n                    data: [5, 6, 7, 9, 15, 2, 5]<br \/>\n                }<br \/>\n            ],<br \/>\n            xAxis: {<br \/>\n                title: {<br \/>\n                    text: &#8216;x-axis label&#8217;<br \/>\n                }<br \/>\n            },<br \/>\n            yAxis: {<br \/>\n                title: {<br \/>\n                    text: &#8216;y-axis label&#8217;<br \/>\n                }<br \/>\n            },<br \/>\n            title: {<br \/>\n                text: &#8216;Chart Title&#8217;<br \/>\n            },<br \/>\n            legend: {<br \/>\n                align: &#8216;right&#8217;,<br \/>\n                verticalAlign: &#8216;top&#8217;,<br \/>\n                layout: &#8216;vertical&#8217;<br \/>\n            },<br \/>\n            tooltip: {<br \/>\n                pointFormat: &#8216;&lt;span style=&quot;color:{series.color}&quot;&gt;{series.name}&lt;\/span&gt;: &lt;b&gt;{point.y}&lt;\/b&gt;&lt;br\/&gt;&#8217;,<br \/>\n                shared: true<br \/>\n            }<br \/>\n        });<br \/>\n[\/code]<\/p>\n<p>Just doing this I was able to change the chart to below. A tooltip for comparing the various lines.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/charts.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-20010\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/charts.png\" alt=\"charts\" width=\"1813\" height=\"404\" \/><\/a><\/p>\n<p>Even after the chart has been drawn we can change it. \u00a0The variable mychart that we have used contains\u00a0a Chart\u00a0object which allows us to access the chart which is currently on screen. I ran the below via the console and I added \u00a0a new series to the chart.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nmyChart.addSeries({ data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]});<br \/>\n[\/code]<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/charts21.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-20016\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/charts21.png\" alt=\"charts2\" width=\"1839\" height=\"409\" \/><\/a><\/p>\n<p>You can see how convenient this can be.<\/p>\n<p>The Chart object can\u00a0also be used for accessing all data points which can be useful for functional testing.<\/p>\n<p>Note\/Reference &#8211;<\/p>\n<ul>\n<li>Highcharts is free for non-commercial use only.<\/li>\n<\/ul>\n<ul>\n<li><a href=\"http:\/\/api.highcharts.com\/highcharts\">Highcharts API References<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I recently had to add charts to a web page in my project. I\u00a0used\u00a0Highcharts library for this.\u00a0It is very dynamic allowing you to change graphs even after the chart\u00a0has been drawn. To display\u00a0a graph we need a div with an ID. Let&#8217;s say we have\u00a0div with ID &#8220;chartDivId&#8221; and we want to display two lines [&hellip;]<\/p>\n","protected":false},"author":161,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[1669,4005,1794,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19996"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/161"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19996"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19996\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}