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 8726475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:15:14+00:00 2026-06-13T08:15:14+00:00

I need a very basic example to create a stack chart like this example

  • 0

I need a very basic example to create a stack chart like this example with a predefined array like this:

var data = [[ 1,  4,  2,  7],
            [21,  2,  5, 10],
            [ 1, 17, 16,  6]]

Whatever I do ends creating errors. E.g. TypeError: data is undefined.
Here is my simplified code so far:

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v2.js?2.9.5"></script>
<script>

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

var data = [[ 1,  4,  2,  7],
            [21,  2,  5, 10],
            [ 1, 17, 16,  6]];

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

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

var z = d3.scale.category20c();

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 + ")");

var stack = d3.layout.stack(data)
      .offset("zero")
      .values(function(d) { return d; })
      .x(function(d, i) { return i; })
      .y(function(d) { return d; });

var area = d3.svg.area()
    .x(function(d, i) { return x(i); })
    .y0(function(d) { return y(d.y0); })
    .y1(function(d) { return y(d.y0 + d.y); });

svg.selectAll(".layer")
      .data(stack)
      .enter().append("path")
      .attr("class", "layer")
      .attr("d", function(d) { return area(d.values); })
      .style("fill", function(d, i) { return colors[i]; });

</script>
  • 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-13T08:15:15+00:00Added an answer on June 13, 2026 at 8:15 am

    You can see a working example at http://jsfiddle.net/DyrsK/2/.

    Generally I find it easier to permute the input data a little bit to get into the default form that d3 expects. For this example I took your data and computed the x, y and y0 values and then passed that to the default stack layout.

    var data = [[ 1,  4,  2,  7],
                [21,  2,  5, 10],
                [ 1, 17, 16,  6]];
    
    // permute the data
    data = data.map(function(d) { 
        return d.map(function(p, i) { 
            return {x:i, y:p, y0:0};
        });
    });
    

    I think there is an easier way to do this, but I had trouble with the out() function of d3.layout.stack() since your input data was a value but the out() function expects it to be a reference value. This meant that there wasn’t a way to update the values with the computed offsets and so the data being returned by stack() was always equal to the input values.

    After getting the stack function to work, the rest pretty much followed from the example you linked to:

    var stack = d3.layout.stack()
          .offset("zero")
    
    var layers = stack(data);
    
    var area = d3.svg.area()
        .interpolate('cardinal')
        .x(function(d, i) { return x(i); })
        .y0(function(d) { return y(d.y0); })
        .y1(function(d) { return y(d.y0 + d.y); });
    
    svg.selectAll(".layer")
          .data(layers)
          .enter().append("path")
          .attr("class", "layer")
          .attr("d", function(d) { return area(d); })
          .style("fill", function(d, i) { return colors(i); });
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need very basic template engine, which is incapsulated in single class. So I
visual basic editor in autocad is very low level , i need a more
I need a very fast SQLite database acces. Setting parameters this way: PRAGMA synchronize
I need some very basic advice on how to define a working -main function
Possible Duplicate: Absolute path & Relative Path This is a very basic question, but
I'm attempting to create an iOS 5 app with some very basic FTP functionality
I'm building a solution for a client which allows them to create very basic
I need a very basic step by step walk through on calling a webservice
I need very basic help (hopefully) on an Artificial Intelligence Project. My question is
I really need help implementing a continuous tanh-sigmoid activation function in a very basic

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.