2016-10-13 150 views
2

我一直在研究具有日期軸的Google折線圖。我注意到軸標籤的滴答是由網格線的數量決定的,與我傳遞的數據不一樣。有人能告訴我如何強制軸標籤與我傳遞的數據同步。 請找到鏈接,小提琴:https://jsfiddle.net/FarhanI/5ga6xu44/Google Charts日期軸標籤不正確

function drawChart() { 

    var data = new google.visualization.DataTable(); 
    data.addColumn('date', 'Time of Day'); 
    data.addColumn('number', 'Rating'); 

    data.addRows([ 
     [new Date(2015, 0, 1), 5], 
     [new Date(2015, 0, 7), 3],[new Date(2015, 0, 14), 3], [new Date(2015, 0, 21), 2],[new Date(2015, 0, 28), 8], 
    ]); 


    var options = { 
     title: 'Rate the Day on a Scale of 1 to 10', 
     width: 900, 
     height: 500, 
     hAxis: { 
     format: 'M/d/yy', 
     gridlines: {count: 9} 
     }, 
     vAxis: { 
     gridlines: {color: 'none'}, 

     } 
    }; 

    var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 

    chart.draw(data, options); 

    var button = document.getElementById('change'); 

    button.onclick = function() { 

     // If the format option matches, change it to the new option, 
     // if not, reset it to the original format. 
     options.hAxis.format === 'M/d/yy' ? 
     options.hAxis.format = 'MMM dd, yyyy' : 
     options.hAxis.format = 'M/d/yy'; 

     chart.draw(data, options); 
    }; 
    } 

我也想有網格線顯示爲y軸,因爲我無法用折線圖來獲得Y軸。我嘗試將網格線指定爲1,但我只能在X軸中間獲得1個網格線。

有人請讓我知道,如果我可以得到Y軸的折線圖。

欣賞幫助, Farhan。

回答

3

可以提供定製hAxis.ticks

與數據同步,建立與各行的日期數組

// load custom ticks 
var ticks = []; 
for (var i = 0; i < data.getNumberOfRows(); i++) { 
    ticks.push(data.getValue(i, 0)); 
} 

然後使用數組中的配置選項

hAxis: { 
    format: 'M/d/yy', 
    ticks: ticks 
    } 

請注意:不遵循y軸所需要的,請您澄清一下...
- >獲得與折線圖

Y軸,如果你只是單純的想看到網格線,從配置選項刪除以下...

vAxis: { 
    gridlines: {color: 'none'} 
    } 

看到下面的工作片段...

google.charts.load('current', { 
 
    callback: function() { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('date', 'Time of Day'); 
 
    data.addColumn('number', 'Rating'); 
 

 
    data.addRows([ 
 
     [new Date(2015, 0, 1), 5], 
 
     [new Date(2015, 0, 7), 3], 
 
     [new Date(2015, 0, 14), 3], 
 
     [new Date(2015, 0, 21), 2], 
 
     [new Date(2015, 0, 28), 8] 
 
    ]); 
 

 
    // load custom ticks 
 
    var ticks = []; 
 
    for (var i = 0; i < data.getNumberOfRows(); i++) { 
 
     ticks.push(data.getValue(i, 0)); 
 
    } 
 

 
    var options = { 
 
     title: 'Rate the Day on a Scale of 1 to 10', 
 
     width: 900, 
 
     height: 500, 
 
     hAxis: { 
 
     format: 'M/d/yy', 
 
     ticks: ticks 
 
     } 
 
    }; 
 

 
    var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
 
    chart.draw(data, options); 
 

 
    var button = document.getElementById('change'); 
 
    button.onclick = function() { 
 
     // If the format option matches, change it to the new option, 
 
     // if not, reset it to the original format. 
 
     options.hAxis.format === 'M/d/yy' ? 
 
     options.hAxis.format = 'MMM dd, yyyy' : 
 
     options.hAxis.format = 'M/d/yy'; 
 
     chart.draw(data, options); 
 
    }; 
 
    }, 
 
    packages:['corechart'] 
 
});
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<input type="button" id="change" value="Change" /> 
 
<div id="chart_div"></div>

+0

@WhiteHat ...非常感謝。這確實有助於與數據同步軸標籤。最初我有一個以日期表示爲字符串的折線圖。有了這個,我無法獲得Y軸(基線)。所以我現在試着將日期字符串轉換爲日期對象,並將網格線顯示爲Y軸(基線)。 我的最終目標是擁有Y軸 - 可否請您告訴我這是可能的日期字符串還是需要轉換爲日期對象。 – Farhan

+0

正確,使用離散軸(字符串),然後不控制網格線等 - 必須是連續軸(日期,數字等等) - [離散與連續](https://開發人員.google.com/chart/interactive/docs/customizing_axes#discrete-vs-continuous) - 查看[this answer](http://stackoverflow.com/a/39862037/5090771)瞭解更多軸標籤格式化技巧... – WhiteHat

+0

感謝您的優秀解釋!根據您的建議進行更改。 – Farhan