Skip to main content
100%

Bar Chart with Negative Values

✓ Published0🌍 Public
RRoxIv
Last edited Jul 29, 2018
Created on Jul 29, 2018

This bar chart displays both positive and negative values using horizontal bars, with positive values in steelblue extending right from the zero axis and negative values in darkorange extending left. The chart loads tab-separated data from a local file and renders it as SVG elements. Using D3 v3, the code employs a linear scale to position bars along the x-axis and an ordinal scale with rangeRoundBands for the y-axis. Two conditional logic paths compute the bar’s x-coordinate and width based on the value’s sign, while axes are drawn using d3.svg.axis.

AI-generated description

Say your dataset is an array of numbers, and includes both positive and negative values. Use two scales to construct the bar chart: a quantitative scale (such as a linear scale) to compute the bar positions along the x-axis, and an ordinal scale with rangeBands to compute the bar positions along the y-axis.

For the quantitative scale, compute the data domain (the minimum and maximum value) using d3.extent:

var x = d3.scale.linear()
    .domain(d3.extent(data, function(d) { return d.value; }))
    .range([0, width]);

Nicing the scale will extend the extent slightly to the nearest round numbers. If you want the zero-value to be centered in the middle of the canvas, take the greater of the minimum and maximum value by magnitude, or simply hard-code the desired domain.

For the y-axis, use rangeRoundBands to divide the vertical space into bands for each bar and specify the amount of padding between bars. The input (domain) to the ordinal scale is some identifying data—such as a name or a unique id. A simple such identifier is the data’s index:

var y = d3.scale.ordinal()
    .domain(data.map(function(d) { return d.name; }))
    .rangeRoundBands([0, height], .2);

Use both scales to position the bars. This is made slightly tricky in that SVG rects are positioned (the x and y attributes) by their top-left corner and cannot have a negative width or height. So, we must use the x- and y-scales to compute the position of the top-left corner, depending on whether the associated value is positive or negative: if the value is positive, then the data value determines the right edge of the bar, while if it’s negative, it determines the left edge of the bar. Hence the conditionals:

svg.selectAll(".bar")
    .data(data)
  .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(Math.min(0, d.value)); })
    .attr("y", function(d) { return y(d.name); })
    .attr("width", function(d) { return Math.abs(x(d.value) - x(0)); })
    .attr("height", y.rangeBand());

Lastly, you can add an axis to display tick marks on top. You might also compute a fill style (or even a gradient) to alter the differentiate the appearance of positive and negative values.

forked from <a href='http://bl.ocks.org/mbostock/'>mbostock</a>'s block: <a href='http://bl.ocks.org/mbostock/2368837'>Bar Chart with Negative Values</a>

forked from <a href='http://bl.ocks.org/RoxIv/'>RoxIv</a>'s block: <a href='http://bl.ocks.org/RoxIv/2f1833af2033a21c05041c764cb934b5'>Bar Chart with Negative Values</a>

forked from <a href='http://bl.ocks.org/RoxIv/'>RoxIv</a>'s block: <a href='http://bl.ocks.org/RoxIv/3910db8ebecce17d0ab634ea51c8beef'>Bar Chart with Negative Values</a>

gpl-3.0 Licensed

Similar vizzes