{"id":1599,"date":"2014-12-04T23:04:54","date_gmt":"2014-12-04T23:04:54","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1599"},"modified":"2014-12-04T23:06:47","modified_gmt":"2014-12-04T23:06:47","slug":"simple-bar-xy-line-graph-d3-js","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/12\/simple-bar-xy-line-graph-d3-js\/","title":{"rendered":"Simple Bar and XY line graph in D3.JS"},"content":{"rendered":"<p>A basic bargraph, as simple as it can be made, largely hacked from various tutorials. It looks like this:<\/p>\n<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/bar.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/bar.png\" alt=\"bar\" width=\"295\" height=\"360\" class=\"aligncenter size-full wp-image-1601\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/bar.png 295w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/bar-245x300.png 245w\" sizes=\"auto, (max-width: 295px) 100vw, 295px\" \/><\/a><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;svg class=&quot;output&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;\r\n&lt;\/svg&gt;\r\n\r\n&lt;script src=&quot;http:\/\/d3js.org\/d3.v3.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n\r\nvar svg = d3.select(&quot;.output&quot;)\r\n\r\nvar mydata = &#x5B;127, 61, 256, 71];\r\n\r\nvar selection = svg.selectAll(&quot;rect&quot;)\r\n  .data(mydata)\r\n\r\nselection.enter().append(&quot;rect&quot;)\r\n  .attr(&quot;x&quot;, 0)\r\n  .attr(&quot;y&quot;, function(d,i) { return i*90+50 })\r\n  .attr(&quot;width&quot;, function(d,i) { return d; })\r\n  .attr(&quot;height&quot;, 20)\r\n  .style(&quot;fill&quot;, &quot;steelblue&quot;)\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>The linegraph looks like this:<br \/>\n<a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/line.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/line.png\" alt=\"line\" width=\"144\" height=\"269\" class=\"aligncenter size-full wp-image-1600\" \/><\/a><\/p>\n<p>And is again as simple as I could get it. The data contains no X axis coordinate, and is just a flat array. In order to plot this a function provided to D3 infers the X axis from the datapoints index.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n\r\n&lt;style&gt;\r\npath {\r\n    stroke: steelblue;\r\n    stroke-width: 2;\r\n    fill: none; \/\/important\r\n}\r\n&lt;\/style&gt;\r\n\r\n&lt;body&gt;\r\n\r\n\r\n&lt;svg class=&quot;output&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;\r\n&lt;\/svg&gt;\r\n\r\n&lt;script src=&quot;http:\/\/d3js.org\/d3.v3.min.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;script&gt;\r\nvar svg = d3.select(&quot;.output&quot;)\r\n\r\nvar mydata = &#x5B;127, 61, 256, 71, 10,10,10,10,10,10,100];\r\n\r\nvar selection = svg.selectAll(&quot;line&quot;)\r\n  .data(mydata)\r\n\r\nvar valueline = d3.svg.line()\r\n    .x(function(d,i) { return i*10; })\r\n    .y(function(d,i) { return d; });\r\n\r\nselection.enter().append(&quot;path&quot;)\r\n  .attr(&quot;class&quot;, &quot;line&quot;)\r\n  .attr(&quot;d&quot;, valueline(mydata))\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A basic bargraph, as simple as it can be made, largely hacked from various tutorials. It looks like this: &lt;html&gt; &lt;body&gt; &lt;svg class=&quot;output&quot; width=&quot;500&quot; height=&quot;500&quot;&gt; &lt;\/svg&gt; &lt;script src=&quot;http:\/\/d3js.org\/d3.v3.min.js&quot;&gt;&lt;\/script&gt; &lt;script&gt; var svg = d3.select(&quot;.output&quot;) var mydata = &#x5B;127, 61, 256, 71]; var selection = svg.selectAll(&quot;rect&quot;) .data(mydata) selection.enter().append(&quot;rect&quot;) .attr(&quot;x&quot;, 0) .attr(&quot;y&quot;, function(d,i) { return i*90+50 }) .attr(&quot;width&quot;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1599","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-pN","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1599"}],"version-history":[{"count":2,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1599\/revisions"}],"predecessor-version":[{"id":1603,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1599\/revisions\/1603"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}