in google charts, the first column of the data table represents the x-axis values.
each additional column represents the y-axis values.
to plot two lines, requires three data table columns.
data.addColumn('number', 'Day');
data.addColumn('number', 'Jan')
data.addColumn('number', 'Feb')
and each line would need the same value for the x-axis.
you could just use 1 for the first day of the month.
[1, 50, 75]
google charts does have some features we can use to customize the appearance of the chart.
for instance, if you would like the tooltips for each line to display a different x-axis value,
you can use object notation when providing the data.
we can provide the value (v:
) and the formatted value (f:
).
the tooltips display the formatted value.
as such, the following data would plot on the same x-axis value,
however, the tooltips for each would display a different date.
[{v: 1, f: '2020-01-01'}, 50, null],
[{v: 1, f: '2020-02-01'}, null, 75],
see following working snippet for an example...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'Jan')
data.addColumn('number', 'Feb')
data.addRows([
[{v: 1, f: '2020-01-01'}, 50, null],
[{v: 1, f: '2020-02-01'}, null, 75],
[{v: 2, f: '2020-01-02'}, 25, null],
[{v: 2, f: '2020-02-02'}, null, 35],
[{v: 3, f: '2020-01-03'}, 20, null],
[{v: 3, f: '2020-02-03'}, null, 50],
]);
var xAxisTicks = [];
for (var i = 1; i <= 31; i++) {
xAxisTicks.push(i);
}
var options = {
aggregationTarget: 'none',
chartArea: {
left: 64,
top: 48,
right: 32,
bottom: 64,
height: '100%',
width: '100%'
},
hAxis: {
ticks: xAxisTicks,
title: 'Day of Month'
},
height: '100%',
interpolateNulls: true,
legend: {
alignment: 'start',
position: 'top'
},
selectionMode: 'multiple',
tooltip: {
trigger: 'both'
},
vAxis: {
title: 'Daily Ticket Sales'
},
width: '100%'
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
google.visualization.events.addListener(chart, 'ready', function () {
chart.setSelection([
{row: 4, column: 1},
{row: 5, column: 2}
]);
});
chart.draw(data, options);
window.addEventListener('resize', function () {
chart.draw(data, options);
});
});
html, body {
height: 100%;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
#chart {
height: 100%;
min-height: 400px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
EDIT
another option is to use the column role feature,
which allows us to add multiple domain columns.
if not specified, the first column of the data table is used as the domain column.
by explicitly setting the role type,
we can have multiple domains, or x-axis values.
in this scenario, we do not have to perform any manipulation,
in order to display the correct values in the tooltips.
and all the values can be set in the same row...
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Jan', type: 'number'});
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Feb', type: 'number'});
data.addRows([
['2020-02-01', 75, '2020-01-01', 50],
['2020-02-02', 35, '2020-01-02', 25],
['2020-02-03', 50, '2020-01-03', 20],
]);
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Jan', type: 'number'});
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Feb', type: 'number'});
data.addRows([
['2020-02-01', 75, '2020-01-01', 50],
['2020-02-02', 35, '2020-01-02', 25],
['2020-02-03', 50, '2020-01-03', 20],
]);
var xAxisTicks = [];
for (var i = 1; i <= 31; i++) {
xAxisTicks.push(i);
}
var options = {
aggregationTarget: 'none',
chartArea: {
left: 64,
top: 48,
right: 32,
bottom: 48,
height: '100%',
width: '100%'
},
hAxis: {
ticks: xAxisTicks,
title: 'Day of Month'
},
height: '100%',
legend: {
alignment: 'start',
position: 'top'
},
selectionMode: 'multiple',
tooltip: {
trigger: 'both'
},
vAxis: {
title: 'Daily Ticket Sales'
},
width: '100%'
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
google.visualization.events.addListener(chart, 'ready', function () {
chart.setSelection([
{row: 2, column: 1},
{row: 2, column: 3}
]);
});
chart.draw(data, options);
window.addEventListener('resize', function () {
chart.draw(data, options);
});
});
html, body {
height: 100%;
margin: 0px 0px 0px 0px;
overflow: hidden;
padding: 0px 0px 0px 0px;
}
#chart {
height: 100%;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>