如何将 Google 图表网格线与我的数据点对齐?
How do I align Google Chart gridlines with my data points?
虽然我已经阅读了几篇类似的 SO 帖子(最著名的是 ,这是我的代码片段中第一个被注释掉的 dataArray
的原因),但我似乎仍然无法使我的沿水平轴的网格线与我的实际数据点对齐。从下面的代码片段中可以看出,我指定了 hAxis.ticks
。虽然它不再反映在这个片段中,但我也试过 hAxis.gridlines.count
、hAxis.minorGridlines.count
和其他几个选项,但都无济于事。
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Ratings');
/* const dataArray = [
[new Date('2020-12-22'), 2],
[new Date('2020-01-05'), 6],
[new Date('2020-01-05'), 7],
[new Date('2020-02-02'), 8],
[new Date('2020-02-16'), 10],
[new Date('2020-03-01'), 5],
[new Date('2020-03-15'), 9],
] */
const dataArray = [
[new Date('12/22/2019'), 2],
[new Date('01/05/2020'), 6],
[new Date('01/19/2020'), 7],
[new Date('02/02/2020'), 8],
[new Date('02/16/2020'), 10],
[new Date('03/01/2020'), 5],
[new Date('03/15/2020'), 9],
];
data.addRows(dataArray);
const dataView = new google.visualization.DataView(data);
const chart = new google.visualization.LineChart(document.querySelector('#chart'));
chart.draw(dataView, {
pointSize: 5,
lineWidth: 3,
hAxis: {
format: "MMM d, yyyy",
ticks: dataArray.map(d => d[0])
}
});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
google 图表的 'current'
版本似乎存在问题。
如果您返回版本 '45'
,它似乎可以按预期工作。
请参阅以下工作片段...
google.charts.load('45', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Ratings');
/* const dataArray = [
[new Date('2020-12-22'), 2],
[new Date('2020-01-05'), 6],
[new Date('2020-01-05'), 7],
[new Date('2020-02-02'), 8],
[new Date('2020-02-16'), 10],
[new Date('2020-03-01'), 5],
[new Date('2020-03-15'), 9],
] */
const dataArray = [
[new Date('12/22/2019'), 2],
[new Date('01/05/2020'), 6],
[new Date('01/19/2020'), 7],
[new Date('02/02/2020'), 8],
[new Date('02/16/2020'), 10],
[new Date('03/01/2020'), 5],
[new Date('03/15/2020'), 9],
];
data.addRows(dataArray);
const dataView = new google.visualization.DataView(data);
const chart = new google.visualization.LineChart(document.querySelector('#chart'));
chart.draw(dataView, {
pointSize: 5,
lineWidth: 3,
hAxis: {
format: "MMM d, yyyy",
ticks: dataArray.map(d => d[0])
}
});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
虽然我已经阅读了几篇类似的 SO 帖子(最著名的是 dataArray
的原因),但我似乎仍然无法使我的沿水平轴的网格线与我的实际数据点对齐。从下面的代码片段中可以看出,我指定了 hAxis.ticks
。虽然它不再反映在这个片段中,但我也试过 hAxis.gridlines.count
、hAxis.minorGridlines.count
和其他几个选项,但都无济于事。
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Ratings');
/* const dataArray = [
[new Date('2020-12-22'), 2],
[new Date('2020-01-05'), 6],
[new Date('2020-01-05'), 7],
[new Date('2020-02-02'), 8],
[new Date('2020-02-16'), 10],
[new Date('2020-03-01'), 5],
[new Date('2020-03-15'), 9],
] */
const dataArray = [
[new Date('12/22/2019'), 2],
[new Date('01/05/2020'), 6],
[new Date('01/19/2020'), 7],
[new Date('02/02/2020'), 8],
[new Date('02/16/2020'), 10],
[new Date('03/01/2020'), 5],
[new Date('03/15/2020'), 9],
];
data.addRows(dataArray);
const dataView = new google.visualization.DataView(data);
const chart = new google.visualization.LineChart(document.querySelector('#chart'));
chart.draw(dataView, {
pointSize: 5,
lineWidth: 3,
hAxis: {
format: "MMM d, yyyy",
ticks: dataArray.map(d => d[0])
}
});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
google 图表的 'current'
版本似乎存在问题。
如果您返回版本 '45'
,它似乎可以按预期工作。
请参阅以下工作片段...
google.charts.load('45', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Ratings');
/* const dataArray = [
[new Date('2020-12-22'), 2],
[new Date('2020-01-05'), 6],
[new Date('2020-01-05'), 7],
[new Date('2020-02-02'), 8],
[new Date('2020-02-16'), 10],
[new Date('2020-03-01'), 5],
[new Date('2020-03-15'), 9],
] */
const dataArray = [
[new Date('12/22/2019'), 2],
[new Date('01/05/2020'), 6],
[new Date('01/19/2020'), 7],
[new Date('02/02/2020'), 8],
[new Date('02/16/2020'), 10],
[new Date('03/01/2020'), 5],
[new Date('03/15/2020'), 9],
];
data.addRows(dataArray);
const dataView = new google.visualization.DataView(data);
const chart = new google.visualization.LineChart(document.querySelector('#chart'));
chart.draw(dataView, {
pointSize: 5,
lineWidth: 3,
hAxis: {
format: "MMM d, yyyy",
ticks: dataArray.map(d => d[0])
}
});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>