nvd3带有离散值的堆积条形图

rhl*_*hrm 19 nvd3.js

我正在尝试使用nvd3创建垂直堆积条形图.我将使用离散数据值,而不是随机生成的值作为其网站上的示例.

我试图使用Group/Stacked Bar Chart的实时代码示例,并输入包含我自己的值的JSON数据.我试图做的是从水平条形图中获取JSON数据并将其作为垂直条形图的数据.

这是我在实时代码示例中使用的数据,而不是Group/Stacked Bar Chart中的数据:

[
  {
    "key": "Series1",
    "color": "#d62728",
    "values": [
      { 
        "label" : "Group A" ,
        "value" : -1.8746444827653
      } , 
      { 
        "label" : "Group B" ,
        "value" : -8.0961543492239
      } , 
      { 
        "label" : "Group C" ,
        "value" : -0.57072943117674
      } , 
      { 
        "label" : "Group D" ,
        "value" : -2.4174010336624
      } , 
      {
        "label" : "Group E" ,
        "value" : -0.72009071426284
      } , 
      { 
        "label" : "Group F" ,
        "value" : -0.77154485523777
      } , 
      { 
        "label" : "Group G" ,
        "value" : -0.90152097798131
      } , 
      {
        "label" : "Group H" ,
        "value" : -0.91445417330854
      } , 
      { 
        "label" : "Group I" ,
        "value" : -0.055746319141851
      }
    ]
  },
  {
    "key": "Series2",
    "color": "#1f77b4",
    "values": [
      { 
        "label" : "Group A" ,
        "value" : 25.307646510375
      } , 
      { 
        "label" : "Group B" ,
        "value" : 16.756779544553
      } , 
      { 
        "label" : "Group C" ,
        "value" : 18.451534877007
      } , 
      { 
        "label" : "Group D" ,
        "value" : 8.6142352811805
      } , 
      {
        "label" : "Group E" ,
        "value" : 7.8082472075876
      } , 
      { 
        "label" : "Group F" ,
        "value" : 5.259101026956
      } , 
      { 
        "label" : "Group G" ,
        "value" : 0.30947953487127
      } , 
      { 
        "label" : "Group H" ,
        "value" : 0
      } , 
      { 
        "label" : "Group I" ,
        "value" : 0 
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

我将函数调用替换为javascript中的data()数据:

nv.addGraph(function() {
    var chart = nv.models.multiBarChart();

    chart.xAxis
        .tickFormat(d3.format(',f'));

    chart.yAxis
        .tickFormat(d3.format(',.1f'));

    d3.select('#chart svg')
        .datum(data)
      .transition().duration(500).call(chart);

    nv.utils.windowResize(chart.update);

    return chart;
});
Run Code Online (Sandbox Code Playgroud)

该图表正确标记了我的系列,但未显示条形图.这个图表可以不采用这种类型的数据吗?我的数据使用水平条,但我想尽可能使用垂直条.

kap*_*pex 43

The group/stacked bar chart (the multiBarChart model) is used with x and y attributes, where y is the height of the bar and x is the label and may be some number or a text.

Unfortunately there isn't much documentation on the exact data format and all examples I found use some confusing functions to generate data. So here's an example I got working using mixed numeric and text labels:

var chart = nv.models.multiBarChart();
d3.select('#chart svg').datum([
  {
    key: "S1",
    color: "#51A351",
    values:
    [      
      { x : "A", y : 40 },
      { x : "B", y : 30 },
      { x : 5,   y : 20 }  
    ]
  },
  {
    key: "S2",
    color: "#BD362F",
    values:
    [      
      { x : "A", y : 60 },
      { x : "B", y : 50 },
      { x : 5,   y : 70 } 
    ]
  }
]).transition().duration(500).call(chart);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css">

<div id="chart" style="height: 200px;"><svg></svg></div>
Run Code Online (Sandbox Code Playgroud)

组/堆积条形图


Now there are two issues with your code:

  1. You need to remove the tickFormat for axis you intend to label with texts.
    The tickFormat function sets a number format for the axis' labels. It will only work if the label can be converted to a number and would result in NaN in your case.

  2. You need either to rename the label and value attributes to x and y or you need to create functions to provide those values:

    var chart = nv.models.multiBarChart();
    
    chart.x(function(d) { return d.label; });
    chart.y(function(d) { return d.value; });
    
    d3.select('#chart svg')
      .datum( /* your data */ )
      .transition().duration(500).call(chart); 
    
    Run Code Online (Sandbox Code Playgroud)