2016-04-26 87 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; 
});