具有离散值的 nvd3 堆积条形图 [英] nvd3 Stacked Bar Chart with discrete values
问题描述
我正在尝试使用 nvd3 创建垂直堆积条形图.我将使用离散数据值,而不是随机生成的值作为其网站上的示例.
我尝试使用组/堆积条形图的实时代码示例,并放入包含我自己的值的 JSON 数据.我试图做的是从水平条形图中取出JSON数据,并将其作为垂直条形图的数据.
这是我在实时代码示例中使用的数据代替组/堆积条形图中的数据:
<预><代码>[{"key": "系列 1","颜色": "#d62728",价值观":[{"label" : "A组" ,价值":-1.8746444827653} ,{"label" : "Group B" ,价值":-8.0961543492239} ,{"label" : "C组" ,价值":-0.57072943117674} ,{"label" : "Group D" ,价值":-2.4174010336624} ,{"label" : "Group E" ,价值":-0.72009071426284} ,{"label" : "Group F" ,价值":-0.77154485523777} ,{"label" : "Group G" ,价值":-0.90152097798131} ,{"label" : "H 组" ,价值":-0.91445417330854} ,{"label" : "Group I" ,价值":-0.055746319141851}]},{"key": "系列 2","颜色": "#1f77b4",价值观":[{"label" : "A组" ,价值":25.307646510375} ,{"label" : "Group B" ,价值":16.756779544553} ,{"label" : "C组" ,价值":18.451534877007} ,{"label" : "Group D" ,价值":8.6142352811805} ,{"label" : "Group E" ,价值":7.8082472075876} ,{"label" : "Group F" ,价值":5.259101026956} ,{"label" : "Group G" ,价值":0.30947953487127} ,{"label" : "H 组" ,价值":0} ,{"label" : "Group I" ,价值":0}]}]我将 javascript 中对 data() 的函数调用替换为 data:
nv.addGraph(function() {var chart = nv.models.multiBarChart();图表.xAxis.tickFormat(d3.format(',f'));图表.y轴.tickFormat(d3.format(',.1f'));d3.select('#chart svg').datum(数据).transition().duration(500).call(chart);nv.utils.windowResize(chart.update);返回图表;});
图表正确标记了我的系列,但没有显示条形图.这个图可以不取这种类型的数据吗?我的数据使用水平条,但如果可能,我想使用垂直条.
组/堆叠条形图(multiBarChart 模型)与 x
和 一起使用y
属性,其中 y
是条的高度,x
是标签,可能是一些数字或文本.
不幸的是,关于确切数据格式的文档并不多,我发现的所有示例都使用了一些令人困惑的函数来生成数据.所以这是我使用混合数字和文本标签工作的示例:
var chart = nv.models.multiBarChart();d3.select('#chart svg').datum([{键:S1",颜色:#51A351",价值观:[{ x : "A", y : 40 },{ x : "B", y : 30 },{ x : 5, y : 20 }]},{键:S2",颜色:#BD362F",价值观:[{ x : "A", y : 60 },{ x : "B", y : 50 },{ x : 5, y : 70 }]}]).transition().duration(500).call(chart);
<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>
现在您的代码有两个问题:
您需要删除要使用文本标记的轴的
tickFormat
.tickFormat
函数设置轴标签的数字格式.只有当标签可以转换为数字并且在您的情况下会导致NaN
时,它才会起作用.您需要将
label
和value
属性重命名为x
和y
或者您需要创建函数来提供这些值:var chart = nv.models.multiBarChart();chart.x(function(d) { return d.label; });chart.y(function(d) { return d.value; });d3.select('#chart svg').datum(/* 你的数据 */).transition().duration(500).call(chart);
I am trying to use nvd3 to create a vertical stacked bar chart. I will be using discrete data values, as opposed to randomly generated values as the example on their website.
I have tried to play around with the live code example of the Group / Stacked Bar Chart and put in JSON data containing my own values. What I tried to do was take the JSON data from the horizontal bar chart and put it in as the data for the vertical bar chart.
This is the data I used on the live code example in place of the data in the 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
}
]
}
]
I replaced the function call to data() in the javascript to 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;
});
The graph properly labels my series but does not show the bars. Can this graph not take this type of data? I got my data working with the horizontal bars, but I would like to use vertical bars if possible.
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);
<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>
Now there are two issues with your code:
You need to remove the
tickFormat
for axis you intend to label with texts.
ThetickFormat
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 inNaN
in your case.You need either to rename the
label
andvalue
attributes tox
andy
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);
这篇关于具有离散值的 nvd3 堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!