d3.js - Problems with creating a y axis in d3 -
i'm having significant difficulties getting y axis appear on graph. you'll have forgive me because i'm relative beginner @ this, , code not par standards. of challenge of helping me out this. i've gotten x axis work out fine xscale, , when append see @ bottom. y axis not working though, , can't life of me figure out how y axis appear. doing wrong? xscale , yscale 2 first scales, , try append axes @ bottom. in advance this.
<!doctype html> <html> <head> <title> digital humanities latin graph</title> <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script> </head> <body> <div id="graph"> </div> <script type="text/javascript"> var romandataset= [ {"year": 1800, "freq": 10.45 }, {"year": 1850, "freq": 9.49 }, {"year": 1900, "freq": 7.16 }, {"year": 1950, "freq": 6.28 }, {"year": 2000, "freq": 5.25 }, {"year": 2008, "freq": 5.18 } ]; var greekdataset= [ {"year": 1800, "freq": 9.1 }, {"year": 1850, "freq": 7.86 }, {"year": 1900, "freq": 7.07 }, {"year": 1950, "freq": 8.00 }, {"year": 2000, "freq": 8.43 }, {"year": 2008, "freq": 8.49} ]; var padding = 200 var w = 1100 var h = 800 var xscale = d3.scale.linear() .domain([1800, 2010]) .range([padding, w - padding]) var yscale = d3.scale.linear() .domain([5.00, 11.00]) .range([h - padding, padding]) var romancolorscale = d3.scale.linear() .domain([3.00, 11]) .range(["white", "blue"]) var radiusscale = d3.scale.linear() .domain([5.18, 10.45]) .range([10, 20]) var greekcolorscale = d3.scale.linear() .domain([3.00, 12.00]) .range(["white", "green"]) var xaxis = d3.svg.axis() .scale(xscale) .ticks(6); var yaxis = d3.svg.axis() .scale(yscale) .orient("left") .ticks(5); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); svg.selectall("circle") .data(romandataset) .enter() .append("circle") .attr("cx", function (d) {return xscale(d.year) }) .attr("cy", function (d) {return yscale(d.freq) }) .attr("r", function (d) {return radiusscale(d.freq) }) .style("stroke", "gray") .style("fill", function (d) { return romancolorscale(d.freq)}); svg.selectall("rect") .data(greekdataset) .enter() .append("circle") .attr("cx", function (d) {return xscale(d.year) }) .attr("cy", function (d) {return yscale(d.freq) }) .attr("r", function (d) {return radiusscale(d.freq) }) .style("stroke", "gray") .style("fill", function (d) { return greekcolorscale(d.freq)}); svg.append("g") .attr("transform", "translate(0," + (h - padding) + ")") .call(xaxis) .orient[("bottom")]; svg.append("g") .attr("class", "axis") .attr("transform", "translate(" + padding + ",0)") .call(yaxis); </script> </body> </html>
you're trying orient appended g
element x axis, gives error , following code (which draw y axis) isn't executed.
simply remove last line from
svg.append("g") .attr("transform", "translate(0," + (h - padding) + ")") .call(xaxis) .orient[("bottom")];
and put axis defined:
var xaxis = d3.svg.axis() .scale(xscale) .orient("bottom") .ticks(6);
(you don't need square brackets around it.)
working code (that needs more work) here.
Comments
Post a Comment