nvd3 multichart yaxis是否可以从零开始,最大值由输入数据确定?
我试过了chart.yDomain1([0,100]);但是图表在最多100处被切断。我需要最大值是动态的。
答案 0 :(得分:0)
所以你帮我弄清楚如何设置y轴范围,所以我想我尝试并返回优惠,即使是六个月后 我从多图表示例的源代码开始 https://github.com/nvd3-community/nvd3/blob/gh-pages/examples/multiChart.html
您要做的是计算数据的最大值,然后在chart.yDomain1()
函数中使用它。
示例小提琴 - https://jsfiddle.net/q72tzyaL/1/
var data = [{
"key": "key",
"type": "bar",
"values": [
{ x: 2012, y: 40 },
{ x: 2013, y: 36 }
]
}, {
"key": "key",
"type": "bar",
"values": [
{ x: 2012, y: 40 },
{ x: 2013, y: 36 }
]
}];
// each item is an object from the array with an array of values
// get the values that we need from that array and then get the max of that
function getMax(item) {
return d3.max(item.values.map(function(d){ return d.y; }));
}
// get the max. Pass in your data array and the function to get max
var max = d3.max(data, getMax);
nv.addGraph(function() {
var chart = nv.models.multiChart()
.margin({top: 30, right: 60, bottom: 50, left: 70})
.yDomain1([0, max]);
d3.select('#chart1 svg')
.datum(data)
.transition().duration(500).call(chart);
return chart;
});