{"id":40392,"date":"2016-09-22T12:27:40","date_gmt":"2016-09-22T06:57:40","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=40392"},"modified":"2016-09-22T12:27:40","modified_gmt":"2016-09-22T06:57:40","slug":"creating-customized-line-chart-using-d3-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-customized-line-chart-using-d3-js\/","title":{"rendered":"Creating customized line chart using d3.js"},"content":{"rendered":"<p><a href=\"https:\/\/d3js.org\/\">D3<\/a> (Data-Driven Documents) is a JavaScript library to create\u00a0custom visualizations. It\u00a0combines powerful visualization and interaction techniques with a data-driven approach to DOM manipulation, giving you the full capabilities of modern browsers and the freedom to design the right visual interface for your data.<\/p>\n<p>D3.js graphs are for those who want to create complex, customized graphs.<\/p>\n<p><strong>Installing :<\/strong><br \/>\nIf you are using NPM, then you can install d3 by using the following command<span style=\"font-family: Consolas, Monaco, 'Lucida Console', monospace\"><span style=\"font-size: 12px;line-height: 20.5714px\"><br \/>\n<\/span><\/span><\/p>\n<p>[java]npm install d3[\/java]<\/p>\n<p>or,<br \/>\n<span style=\"line-height: 1.71429;font-size: 1rem\">you can simply download the <\/span><a style=\"line-height: 1.71429;font-size: 1rem\" href=\"https:\/\/cdnjs.com\/libraries\/d3\">latest version<\/a><span style=\"line-height: 1.71429;font-size: 1rem\"> and include it in your page.<\/span><\/p>\n<p><strong>Drawing a Line Chart :<\/strong><\/p>\n<p>Step 1 :\u00a0First, we\u2019ll need some data to plot. We\u2019re going to use the following data.<\/p>\n<p>[java] var lineData = [{a: 1, y: 5}, {a: 20, b: 20}, &#8230;&#8230; , {a: 100, b: 60}]; [\/java]<\/p>\n<p>Step 2 : Create an empty div which will enclose the chart<\/p>\n<p>[java]<br \/>\n&lt;div id=&quot;chartArea&quot;&gt;&lt;\/div&gt;<br \/>\n[\/java]<\/p>\n<p>Step 3\u00a0: Creating SVG\u00a0element<\/p>\n<p>[java]<br \/>\nvar margin =\u00a0{top: 30, left:30, right:30, bottom:30},\u00a0width = 800,\u00a0height = 500;<br \/>\nvar svg = d3.select(&quot;#chartArea&quot;).append(&quot;svg&quot;)<br \/>\n.attr(&#8216;id&#8217;, &#8216;mychart&#8217;)<br \/>\n.attr(&quot;width&quot;, width)<br \/>\n.attr(&quot;height&quot;, height)<br \/>\n.style(&#8216;border&#8217;, &#8216;1px solid #ccc&#8217;);<\/p>\n<p>[\/java]<\/p>\n<p>Step 4 :\u00a0Define scale and axes<\/p>\n<p>[java]<br \/>\nvar xScale = d3.scale.linear().range([margin.left, width &#8211; margin.right])<br \/>\n.domain([0, 100]),<br \/>\nyScale = d3.scale.linear().range([height &#8211; margin.top, margin.bottom])<br \/>\n.domain([0, 100]);\u00a0<\/p>\n<p>var xAxis = d3.svg.axis().scale(xScale),<br \/>\nyAxis = d3.svg.axis().scale(yScale).orient(&#8216;left&#8217;);<br \/>\n \/\/ y-axis it needs to be oriented to the left<br \/>\n[\/java]<\/p>\n<p>Here,\u00a0domain defines the minimum and maximum values displayed on the graph. Range is the area of the SVG we\u2019ll be covering.<br \/>\nScales are functions that map from an input domain to an output range.<\/p>\n<p>Step 5\u00a0: \u00a0Append both the axis to the SVG and apply the transformation<\/p>\n<p>[java]<br \/>\nvar mySVG = d3.select(&#8216;#mychart&#8217;) \/\/selects element with id equals to mychart, currently pointing to the above created SVG element.<br \/>\nmySVG.append(&quot;g&quot;) \/\/g element is used to group SVG shapes together<br \/>\n .attr(&quot;class&quot;, &quot;x-axis&quot;)<br \/>\n .attr(&quot;transform&quot;, &quot;translate(0,&quot; + (height &#8211; margin.bottom) + &quot;)&quot;) \/\/The transforms are SVG transforms<br \/>\n .call(xAxis)<br \/>\n .append(&quot;text&quot;)<br \/>\n .attr(&quot;y&quot;, &#8216;3em&#8217;)<br \/>\n .attr(&quot;x&quot;, &quot;30em&quot;)<br \/>\n .text(&quot;Quantity&quot;);<\/p>\n<p> mySVG.append(&quot;g&quot;) \/\/We create an SVG Group Element to hold all the elements that the axis function produces.<br \/>\n .attr(&quot;class&quot;, &quot;y-axis&quot;)<br \/>\n .attr(&quot;transform&quot;, &quot;translate(&quot; + (margin.left) + &quot;,0)&quot;)<br \/>\n .call(yAxis)<br \/>\n .append(&quot;text&quot;)<br \/>\n .attr(&quot;y&quot;, &quot;16em&quot;)<br \/>\n .attr(&quot;x&quot;, &quot;-5.5em&quot;)<br \/>\n .text(&quot;Price ($)&quot;);<\/p>\n<p>[\/java]<\/p>\n<p>Here, the translate() function takes one or two values which specify the horizontal and vertical translation values, respectively.<br \/>\ntx represents the translation value along the x-axis and\u00a0ty represents the translation value along the y-axis.<\/p>\n<p>We have transformed both the axes, keeping the defined margins in view so that the axes don\u2019t touch the SVG margins.<\/p>\n<p>Step 6 : Next we are going to plot coordinates and draw a line.<\/p>\n<p>[java]<\/p>\n<p>var lineFunc = d3.svg.line().x(function(obj) {return xScale(obj.quantity)})<br \/>\n.y(function(obj) {return yScale(obj.price);});<\/p>\n<p>var path = svg.append(&quot;path&quot;)<br \/>\n.attr(&quot;d&quot;, lineFunc(lineData))<br \/>\n.attr(&quot;stroke&quot;, &#8216;#87CEEB&#8217;)<br \/>\n.attr(&quot;stroke-width&quot;, 3)<br \/>\n.attr(&quot;fill&quot;, &quot;none&quot;);<\/p>\n<p>[\/java]<\/p>\n<p>d3.svg.line() constructs a new line generator with the default x and y accessor functions. The<br \/>\nreturned function generates path data for an open piece-wise linear curve.<\/p>\n<p>Now, we have successfully created a line chart, its time to\u00a0add some customization.<\/p>\n<p><strong>Highlighting the path on hover<\/strong><\/p>\n<p>Let&#8217;s change the color of the line, when user hovers it.<\/p>\n<p>[java]<\/p>\n<p>path.on(&#8216;mouseover&#8217;, function(obj) {<br \/>\n     d3.select(this).attr(&#8216;stroke&#8217;, &#8216;#00688B&#8217;);<br \/>\n}).on(&#8216;mouseout&#8217;, function(obj) {<br \/>\n     d3.select(this).attr(&#8216;stroke&#8217;, &#8216;#87CEEB&#8217;);<br \/>\n});<br \/>\n[\/java]<\/p>\n<p><strong>Adding tooltips<\/strong><\/p>\n<p>Step 1 : Create a empty div<\/p>\n<p>[java]<\/p>\n<p>var tooltipDiv = d3.select(&quot;body&quot;).append(&quot;div&quot;)<br \/>\n .attr(&quot;class&quot;, &quot;tooltip&quot;)<br \/>\n .style(&quot;opacity&quot;, 0)<br \/>\n .style(&quot;position&quot;, &quot;absolute&quot;)<br \/>\n .style(&quot;text-align&quot;, &quot;center&quot;)<br \/>\n .style(&quot;width&quot;, &quot;90px&quot;)<br \/>\n .style(&quot;height&quot;, &quot;45px&quot;)<br \/>\n .style(&quot;padding&quot;, &quot;2px&quot;)<br \/>\n .style(&quot;background&quot;, &quot;beige&quot;)<br \/>\n .style(&quot;pointer-events&quot;, &quot;none&quot;);<br \/>\n[\/java]<\/p>\n<p>Step 2 :\u00a0Plot circles.<\/p>\n<p>[java]<\/p>\n<p>var circles =\u00a0svg.selectAll(&quot;circle&quot;)<br \/>\n                 .data(lineData)<br \/>\n                 .enter().append(&quot;circle&quot;)<br \/>\n                 .style(&quot;fill&quot;, &#8216;#00688B&#8217;)<br \/>\n                 .attr(&quot;r&quot;, 5)<br \/>\n                 .attr(&quot;cx&quot;, function(obj) {<br \/>\n                     return xScale(obj.quantity);<br \/>\n                  })<br \/>\n                 .attr(&quot;cy&quot;, function(obj) {<br \/>\n                     return yScale(obj.price);<br \/>\n                  });<\/p>\n<p>[\/java]<\/p>\n<p>Step 3. Finally,\u00a0show\u00a0tooltip on hover<\/p>\n<p>[java]<\/p>\n<p>circles.on(&quot;mouseover&quot;, function(obj) {<br \/>\n    tooltipDiv.transition().duration(200)style(&quot;opacity&quot;, .9);<br \/>\n    tooltipDiv.html(&quot;Quantity : &quot; + obj.quantity + &quot;&lt;br\/&gt;Price : &quot; + obj.price)<br \/>\n              .style(&quot;left&quot;, (d3.event.pageX) + &quot;px&quot;)<br \/>\n              .style(&quot;top&quot;, (d3.event.pageY &#8211; 28) + &quot;px&quot;);<br \/>\n }).on(&quot;mouseout&quot;, function(obj) {<br \/>\n        tooltipDiv.transition().duration(500).style(&quot;opacity&quot;, 0);<br \/>\n });<\/p>\n<p>[\/java]<\/p>\n<p>And we&#8217;re done. I&#8217;ve also created a <a href=\"https:\/\/jsfiddle.net\/mansiarora\/jhynag08\/\">JsFiddle<\/a>\u00a0for the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>D3 (Data-Driven Documents) is a JavaScript library to create\u00a0custom visualizations. It\u00a0combines powerful visualization and interaction techniques with a data-driven approach to DOM manipulation, giving you the full capabilities of modern browsers and the freedom to design the right visual interface for your data. D3.js graphs are for those who want to create complex, customized graphs. [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1669,4021,4023,4006,4022],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/40392"}],"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\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=40392"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/40392\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=40392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=40392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=40392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}