normal bars positioning on d3.js bar chart -
I try to create a chart based on the 2D data array (I initially want to use the 2D array, hence there is a function "merging AR" using D.JS, which merges them is). Here's the code:
.bar {fill: steelblue; } .bar: hover {fill: brown; } Var arr1 = [399200,100000, 352108, 600150, 39000, 17005, 4278]; Var arr2 = [839, 14 9, 146, 200, 200, 121, 63]; Function merge AR (array 1, array 2) {var i, out = []; / Literal new array {out.push ([array1 [i], array2 [i]]) for // (i = 0; i & lt; array1.length; i ++); } Exit; } Var data = Merge AR (arr1, arr2); Margin = {top: 20, right: 20, bottom: 30, on the left: 40}, width = 960 - margin. Left - margin. Right, height = 500 - margin.Top - margin.bottom; Var x = d3.scale.linear () .domain ([0, d3.max (data, function (d) {return d [0];})) .ange ([0, width]); Var y = d3.scale.linear () .domain ([0, d3.max (data, function (d) {return d [1];})) .ange ([height, 0]); Var xAxis = d3.svg.axis () .scale (x) .orient ("bottom"); Var yAxis = d3.svg.axis () .scale (y) .orient ("left"); Var svg = d3.select ("body"). Attached ("svg") .attr ("width", width + margin.left + margin.right) .attr ("height", height + margin.top + margin below). Append ("g") .attr ("conversion", "translation (" + margin. Left + "," + margin.Top + ")"); Svg.append ("g") .attr ("class", "x axis") .attr ("transform", "translate (0," + height + ")"). Call (xAxis); Svg.append ("g") .attr ("class", "y axis") .color (yAxis) .append ("text") .attr ("transform", "rotate (-90)") .attr ( "Y", 6) .attr ("dy", ".71em") Style ("text-anchor", "end") svg.selectAll (". Bar"). Data (data) Center () ("square", "bar") .attr ("x", function (d) {returns x (d [0]);} // //.attr("width ", x .rrangeband ( ) .attr ("width", width / a1.length) .attr ("y", function (d) {return y (d [1]);}) .attr ("height", function (d) { Height Height - y (D [1]););
The problem is that the bars are separated from each other, there is no distance between them, even if I have used the range band.
There are 2 problems with your code
The first one is that data array To sort it, you can do this:
outside = out.support (function (a, b) {return d3ccending (a [0], b [0])})
Before returning outside
to your mergeAt
function. Sorting arrays ensures that you do process repeatedly in the correct order
The second issue is that your intervals are not the same, for remedieting for this, I have a Block width (but you want to separate something):
.attr ("width", function (D, i) {if (i! = (Data.length-1) ) {Return x (data [i + 1] [0]) - x (data [i] [0])} and {return 10; / / last block width is 10. To find out that at the end of ARI, A // is a cleaner way to add a marker, // how to complete the axis}})
JSField:
edit
To keep the same interval between each time and one width, you have to change the scale to an ordinal:
var x = d3.scale.ordinal () .rangeRoundBands ([0, width], .1) .domain (data.map (function (d) {return d [0]}))
Again, the way you count the width You need this to change it:
.attr ("Width", x.rangeBand ())
JSfield: