最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Grouped and stacked multiBarChart with NVD3 - Stack Overflow

programmeradmin1浏览0评论

Possible duplicate question to Bar chart in Javascript: stacked bars + grouped bars

I'm trying to create a stacked bar chart that lets you pare 2 values (dark and mid blue) to last week's data points (the secondary light blues 'behind').

  1. Starting with multiBarChart() with .stacked(true) first I tried merging both weeks into a single array of 14 bars, where the x position could help group the bars. I tried to form my bined array of objects where .x properties' values are 0, 0.3, 1, 1.3, 2, 2.3, etc. Unfortunately unlike lineChart() it doesn't use the x value for positioning.

  2. Another idea is to exploit the group .stacked(false), providing 4 items (instead of 2) with the same x value. These then appear overlaid on top of each other instead of stacked. Here the spacing looks good, but how do I stack these 2 by 2?

Possible duplicate question to Bar chart in Javascript: stacked bars + grouped bars

I'm trying to create a stacked bar chart that lets you pare 2 values (dark and mid blue) to last week's data points (the secondary light blues 'behind').

  1. Starting with multiBarChart() with .stacked(true) first I tried merging both weeks into a single array of 14 bars, where the x position could help group the bars. I tried to form my bined array of objects where .x properties' values are 0, 0.3, 1, 1.3, 2, 2.3, etc. Unfortunately unlike lineChart() it doesn't use the x value for positioning.

  2. Another idea is to exploit the group .stacked(false), providing 4 items (instead of 2) with the same x value. These then appear overlaid on top of each other instead of stacked. Here the spacing looks good, but how do I stack these 2 by 2?

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Oct 11, 2012 at 23:58 Gon ZifroniGon Zifroni 2135 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Hey I just developed grouped+stacked bar chart on d3.js. It is not NVD3 but it may help you.

  • Source

  • Demo

Let me just say up front that I am SO not an nvd3 expert. I'm barely past the getting-started stage myself.

That said, it looks like you're making this too hard on yourself.

I think you really want to send nvd3 two sets of data, with the x's matching between the two. (E.g., (1,y1a) corresponding to (1,y2a), then (2,y2a) with (2,y2b), etc.)

You can see this more clearly by the following:

  1. Head to their Live Code page
  2. Select the Group/Stacked Bar Chart.
  3. Select the Data (JSON) tab.
  4. Replace the first function with the following, and observe the resulting x values.:

    function() {
      return stream_layers(2,10,.1).map(function(data, i) {
        alert( 'Stream '+i+': '+JSON.stringify(data));
        return {
          key: 'Stream' + i,
          values: data
        };
      });
    }

Best as I understand it, that's the model you're looking for.

发布评论

评论列表(0)

  1. 暂无评论