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。
@WhiteHat ...非常感謝。這確實有助於與數據同步軸標籤。最初我有一個以日期表示爲字符串的折線圖。有了這個,我無法獲得Y軸(基線)。所以我現在試着將日期字符串轉換爲日期對象,並將網格線顯示爲Y軸(基線)。 我的最終目標是擁有Y軸 - 可否請您告訴我這是可能的日期字符串還是需要轉換爲日期對象。 – Farhan
正確,使用離散軸(字符串),然後不控制網格線等 - 必須是連續軸(日期,數字等等) - [離散與連續](https://開發人員.google.com/chart/interactive/docs/customizing_axes#discrete-vs-continuous) - 查看[this answer](http://stackoverflow.com/a/39862037/5090771)瞭解更多軸標籤格式化技巧... – WhiteHat
感謝您的優秀解釋!根據您的建議進行更改。 – Farhan