Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - d3.js histogram with positive and negative values

I can't figure out how to properly create a histogram where there are both positive and negative values in the data array.

I've used the histogram example here http://bl.ocks.org/mbostock/3048450 as a base, and while the x axis values and ticks are correct, the bars are out to lunch.

Data

var values = [-15, -20, -22, -18, 2, 6, -26, -18, -15, -20, -22, -18, 2, 6, -26, -18];

X Scale

var x0 = Math.max(-d3.min(values), d3.max(values));
            
var x = d3.scale.linear()
    .domain([-x0, x0])
    .range([0, width])
    .nice();

Check the jfiddle here: http://jsfiddle.net/tNdJj/2/

I assume it's something missing from the "rect" creations but I am not seeing it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the example of histogram from the following question: Bar chart with negative values

I inversed x and y and adapted the display. Now you have a nice basis.

enter image description here

Here is the corresponding jsFiddle: http://jsfiddle.net/chrisJamesC/tNdJj/4/

Here is the relevant code:

var data = [-15, -20, -22, -18, 2, 6, -26, -18];

var margin = {top: 30, right: 10, bottom: 10, left: 30},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var y0 = Math.max(Math.abs(d3.min(data)), Math.abs(d3.max(data)));

var y = d3.scale.linear()
    .domain([-y0, y0])
    .range([height,0])
    .nice();

var x = d3.scale.ordinal()
    .domain(d3.range(data.length))
    .rangeRoundBands([0, width], .2);

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.selectAll(".bar")
    .data(data)
  .enter().append("rect")
    .attr("class", function(d) { return d < 0 ? "bar negative" : "bar positive"; })
    .attr("y", function(d) { return y(Math.max(0, d)); })
    .attr("x", function(d, i) { return x(i); })
    .attr("height", function(d) { return Math.abs(y(d) - y(0)); })
    .attr("width", x.rangeBand());

svg.append("g")
    .attr("class", "x axis")
    .call(yAxis);

svg.append("g")
    .attr("class", "y axis")
  .append("line")
    .attr("y1", y(0))
    .attr("y2", y(0))
    .attr("x1", 0)
    .attr("x2", width);

Note: For simple visualizations like this, I would recommand using nvd3.js


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...