javascript - trying to generate bar chart in d3 using object-oriented approach -


ploti'm new d3 , new javascript (most of experience python). went trough tutorial in book interactive data visualization: web scott murray on creating bar chart. didn't have nay issues completing tutorial, approach takes in book seems procedural using global variables. create similar chart, use object-oriented approach able encapsulate variables , reuse code generate multiple charts in 1 script without having repeat code on , on again. i've tried far without luck:

        function d3chart(id, data, width, height, padding) {             this.id = id             this.data = data             this.width = width             this.height = height             this.padding = padding             this.svg =  d3.select("body")                 .append("svg")                 .attr("width", width)                 .attr("height", height)                 .attr("class", "chart")                 .attr("id". this.id);             this.yscale = d3.scale.linear()                 .domain([0, d3.max(data)])                 .range([h - padding, padding])             this.xscale = d3.scale.ordinal()                 .domain(d3.range(data.length))                 .rangeroundbands([padding,w-padding], 0.05)         };          d3chart.prototype.rect = function() {              this.rect = this.svg.selectall("rect")                 .data(this.data)                 .enter()                 .append("rect");         }          d3chart.prototype.plot = function() {               this.rect.attr("x", function(d, i) {                     return this.xscale(i);                 })                 .attr("y", function (d) {                     return this.yscale(d);                 })                 .attr("width", this.xscale.rangeband())                 .attr("height", function(d) {                     return this.height - this.padding - this.yscale(d);                 })                 .attr("fill", function(d) {                     return "rgb(" + (100 - d) + ", 0, " + (d*10) + ")";                 });             }          var chart = new d3chart("chart1", [5, 15, 10, 30, 20, 45, 15, 10, 5], 600, 400, 40);         chart.rect()         chart.plot() 

i'm sure there's pretty simple i'm doing wrong, know why isn't working, or if right approach take? appreciated. thanks.

the first problem code have couple of typos in there, e.g. . instead of , , h instead of height. second problem you're making attribute of this, different in different contexts. example, in

this.rect.attr("x", function(d, i) {                 return this.xscale(i);             }) 

the 2 this refer different objects -- in second case this actual dom element you're operating on , not have attribute you're referring to.

so need save attributes of global object, window. of course not better using global variables. more object-oriented approach, need create object contains information , pass around, or implement methods can call on it.

i've put fixed code in jsfiddle here.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -