Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:24:59+00:00 2026-06-02T00:24:59+00:00

I need to create a d3 bar chart that can have negative values. Ideally

  • 0

I need to create a d3 bar chart that can have negative values. Ideally the axis zero position should be calculated based on the extent of the data, but I’d settle for a solution that assumes symmetric positive and negative extent, i.e. that it would be always in the middle of the chart.

Here’s an example of what I’d like to achieve.

Bar chart with negative values

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:25:01+00:00Added an answer on June 2, 2026 at 12:25 am

    OK, let’s say you have an array of numbers as your dataset, and this includes some positive and negative values:

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

    You’ll want two scales to construct a bar chart. You need one quantitative scale (typically a linear scale) to compute the bar positions along the x-axis, and a second ordinal scale to compute the bar positions along the y-axis.

    For the quantitative scale, you typically need to compute the domain of your data, which is based on the minimum and maximum value. An easy way to do that is via d3.extent:

    var x = d3.scale.linear()
        .domain(d3.extent(data))
        .range([0, width]);
    

    You might also want to nice the scale to round the extent slightly. As another example, sometimes you want the zero-value to be centered in the middle of the canvas, in which case you’ll want to take the greater of the minimum and maximum value:

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

    Alternatively, you can hard-code whatever domain you want.

    var x = d3.scale.linear()
        .domain([-30, 30])
        .range([0, width]);
    

    For the y-axis, you’ll want to use rangeRoundBands to divide the vertical space into bands for each bar. This also lets you specify the amount of padding between bars. Often an ordinal scale is used with some identifying data—such as a name or a unique id. However, you can also use ordinal scales in conjunction with the data’s index:

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

    Now that you’ve got your two scales, you can create the rect elements to display the bars. The one tricky part is that in SVG, rects are positioned (the x and y attributes) based on their top-left corner. So we need to use the x– and y-scales to compute the position of the top-left corner, and that depends 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 here:

    svg.selectAll(".bar")
        .data(data)
      .enter().append("rect")
        .attr("class", "bar")
        .attr("x", function(d, i) { return x(Math.min(0, d)); })
        .attr("y", function(d, i) { return y(i); })
        .attr("width", function(d, i) { return Math.abs(x(d) - 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. Putting it all together:

    • Bar Chart with Negative Values
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a Stacked Bar Chart. My requirement is I need
I have a bar chart created using flot jsFiddle Code Now i need to
I need to write a method to create a simple text bar chart given
I have a bunch of divs that the user can create and which represent
I need to create a Gold bar for my windows form similar to what
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I am trying to create horizontal bar chart to look like a pill chart.
I'm trying to create a sample bar chart or pie chart from a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.