2016-04-26 168 views
1

nvd3 multichart yaxis可能从零开始,最大值由输入数据决定吗?nvd3 multichart yaxis从零开始

我试过chart.yDomain1([0,100]);但是图表会在最大值为100时被截断。我需要最大值是动态的。

回答

0

所以,你帮我弄清楚如何设置Y轴范围,所以我认为Id尝试和返回的青睐,即使是半年后
我开始从多图示例的源代码
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; 
});