Related
While I have read several similar SO posts (most notably this one, which is the reason for the first, commented-out dataArray in my snippet), I still cannot seem to make my gridlines along the horizontal axis align with my actual data points. As you can see from the snippet below, I have specified the hAxis.ticks. While it's no longer reflected in this snippet, I've also played around with hAxis.gridlines.count, hAxis.minorGridlines.count, and several other options, all to no avail.
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>
appears to be an issue with the 'current' version of google charts.
if you go back to version '45', it appears to work as expected.
see following working snippet...
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>
Can someone explain to me what makes the example on the left show a solid line for the x-axis and what makes the example on the right show no line for the x-axis?
I want to show the x-axis like the example on the left, but I am not sure how Google Charts decides to show it. I am using the google-charts library from npm. To clarify: I want to show the x and y axes as solid lines in my chart.
var linearOptions = { //This one shows x-axis
title: 'World Population Since 1400 A.D. in Linear Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
ticks: [0, 1000, 2000, 4000, 6000]
}
};
var logOptions = { //This one does not show x-axis
title: 'World Population Since 1400 A.D. in Log Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
scaleType: 'log',
ticks: [0, 1000, 2000, 4000, 6000]
}
};
https://jsfiddle.net/zbiernat/ppLy8esg/
from the documentation...
https://developers.google.com/chart/interactive/docs/customizing_axes
according to the documentation for vAxis.scaleType...
'log' - Logarithmic scaling. Negative and zero values are not plotted. This option is the same as setting vAxis: { logscale: true }.
since zero values are not plotted, the first tick in the options is ignored
in order to show a solid line for the x-axis when using 'log',
use a number greater than zero for the first tick under vAxis
see following working snippet,
data table method getColumnRange(colIndex) is used to find the min for the y-axis column
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Population');
data.addRows([
[new Date(1400, 1, 1), 374],
[new Date(1500, 1, 1), 460],
[new Date(1600, 1, 1), 579],
[new Date(1700, 1, 1), 679],
[new Date(1750, 1, 1), 770],
[new Date(1800, 1, 1), 954],
[new Date(1850, 1, 1), 1241],
[new Date(1900, 1, 1), 1633],
[new Date(1910, 1, 1), 1750],
[new Date(1920, 1, 1), 1860],
[new Date(1930, 1, 1), 2070],
[new Date(1940, 1, 1), 2300],
[new Date(1950, 1, 1), 2558],
[new Date(1951, 1, 1), 2595],
[new Date(1952, 1, 1), 2637],
[new Date(1953, 1, 1), 2682],
[new Date(1954, 1, 1), 2730],
[new Date(1955, 1, 1), 2782],
[new Date(1956, 1, 1), 2835],
[new Date(1957, 1, 1), 2891],
[new Date(1958, 1, 1), 2948],
[new Date(1959, 1, 1), 3001],
[new Date(1960, 1, 1), 3043],
[new Date(1961, 1, 1), 3084],
[new Date(1962, 1, 1), 3140],
[new Date(1963, 1, 1), 3210],
[new Date(1964, 1, 1), 3281],
[new Date(1965, 1, 1), 3350],
[new Date(1966, 1, 1), 3421],
[new Date(1967, 1, 1), 3490],
[new Date(1968, 1, 1), 3562],
[new Date(1969, 1, 1), 3637],
[new Date(1970, 1, 1), 3713],
[new Date(1971, 1, 1), 3790],
[new Date(1972, 1, 1), 3867],
[new Date(1973, 1, 1), 3942],
[new Date(1974, 1, 1), 4017],
[new Date(1975, 1, 1), 4089],
[new Date(1976, 1, 1), 4160],
[new Date(1977, 1, 1), 4232],
[new Date(1978, 1, 1), 4304],
[new Date(1979, 1, 1), 4379],
[new Date(1980, 1, 1), 4451],
[new Date(1981, 1, 1), 4534],
[new Date(1982, 1, 1), 4615],
[new Date(1983, 1, 1), 4696],
[new Date(1984, 1, 1), 4775],
[new Date(1985, 1, 1), 4856],
[new Date(1986, 1, 1), 4941],
[new Date(1987, 1, 1), 5027],
[new Date(1988, 1, 1), 5115],
[new Date(1989, 1, 1), 5201],
[new Date(1990, 1, 1), 5289],
[new Date(1991, 1, 1), 5372],
[new Date(1992, 1, 1), 5456],
[new Date(1993, 1, 1), 5538],
[new Date(1994, 1, 1), 5619],
[new Date(1995, 1, 1), 5699],
[new Date(1996, 1, 1), 5779],
[new Date(1997, 1, 1), 5858],
[new Date(1998, 1, 1), 5935],
[new Date(1999, 1, 1), 6012],
[new Date(2000, 1, 1), 6089],
[new Date(2001, 1, 1), 6165],
[new Date(2002, 1, 1), 6242],
[new Date(2003, 1, 1), 6319],
[new Date(2004, 1, 1), 6396],
[new Date(2005, 1, 1), 6473],
[new Date(2006, 1, 1), 6551],
[new Date(2007, 1, 1), 6630],
[new Date(2008, 1, 1), 6709],
[new Date(2009, 1, 1), 6788]
]);
var populationRange = data.getColumnRange(1);
var logOptions = {
title: 'World Population Since 1400 A.D. in Log Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
scaleType: 'log',
ticks: [populationRange.min, 1000, 2000, 4000, 6000],
}
};
var logChart = new google.visualization.LineChart(document.getElementById('log_div'));
logChart.draw(data, logOptions);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="log_div"></div>
I am using a google AreaChart, but it doesn't display the x axis as I like it to be.
This is the data I like to plot
[new Date(2017, 02,10),1,'1'],
[new Date(2017, 02,21),1,'1'],
[new Date(2017, 02,28),1,'1'],
[new Date(2017, 03,07),1,'1'],
[new Date(2017, 03,14),1,'1'],
[new Date(2017, 03,23),1,'1'],
[new Date(2017, 03,31),1,'1'],
[new Date(2017, 04,07),1,'1'],
[new Date(2017, 04,26),1,'1'],
[new Date(2017, 05,03),1,'1'],
[new Date(2017, 05,10),1,'1'],
[new Date(2017, 05,17),1,'1'],
[new Date(2017, 05,25),1,'1'],
[new Date(2017, 06,05),1,'1'],
[new Date(2017, 06,12),0.5,'0.5']
I like the date intervals on the X-axis
The options for this chart are
var options = {
legend: {position: 'top', alignment: 'start', textStyle: {fontSize: 12, bold: false}},
width: 800,
height: 500,
hAxis: {showTextEvery: 0, slantedText: 'true', slantedTextAngle: 45},
vAxis: {minValue: 0},
colors: ['#C3D5BC'],
trendlines: {0: {
labelInLegend: 'Trendline (Test)',
type: 'linear',
showR2: false,
visibleInLegend: true,
color: '#344F35'
}} // Draw a trendline for data series 0.
};
var container = document.getElementById("test-div");
var chart = new google.visualization.AreaChart(container);
chart.draw(data, options);
And this is my current output, but I like all date intervals on the x-axis???
you can provide custom axis labels using option --> ticks
ticks should be an array of values with the same type as the axis,
in this case 'date'
ticks: [new Date(2017, 02,10), new Date(2017, 02,11), ...]
see following working snippet...
here, data table method --> getDistinctValues
is used to build an array of unique dates from the data
this will only show the dates for which there is data
hAxis: {
format: 'M/d/yy',
ticks: data.getDistinctValues(0),
...
(also added format option to change format of the label)
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Date', 'Value', {role: 'tooltip'}],
[new Date(2017, 02,10),1,'1'],
[new Date(2017, 02,21),1,'1'],
[new Date(2017, 02,28),1,'1'],
[new Date(2017, 03,07),1,'1'],
[new Date(2017, 03,14),1,'1'],
[new Date(2017, 03,23),1,'1'],
[new Date(2017, 03,31),1,'1'],
[new Date(2017, 04,07),1,'1'],
[new Date(2017, 04,26),1,'1'],
[new Date(2017, 05,03),1,'1'],
[new Date(2017, 05,10),1,'1'],
[new Date(2017, 05,17),1,'1'],
[new Date(2017, 05,25),1,'1'],
[new Date(2017, 06,05),1,'1'],
[new Date(2017, 06,12),0.5,'0.5']
]);
var options = {
legend: {position: 'top', alignment: 'start', textStyle: {fontSize: 12, bold: false}},
width: 800,
height: 500,
hAxis: {
format: 'M/d/yy',
ticks: data.getDistinctValues(0),
slantedText: 'true',
slantedTextAngle: 45
},
vAxis: {minValue: 0},
colors: ['#C3D5BC'],
trendlines: {0: {
labelInLegend: 'Trendline (Test)',
type: 'linear',
showR2: false,
visibleInLegend: true,
color: '#344F35'
}}
};
var container = document.getElementById('test-div');
var chart = new google.visualization.AreaChart(container);
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="test-div"></div>
if you want to show all dates, whether there is data or not,
use data table method --> getColumnRange
getColumnRange will return the min and max dates from the data
use this to build an array of all the dates
var dateRange = data.getColumnRange(0);
var oneDay = (1000 * 60 * 60 * 24);
var ticks = [];
for (var i = dateRange.min.getTime(); i < dateRange.max.getTime(); i = i + oneDay) {
ticks.push(new Date(i));
}
note: there must be enough room on the chart,
or all the labels will not be displayed...
UPDATE
object notation can be used in place of literal values,
in the data table, and other places such as chart option ticks...
object notation accepts keys for...
v: - value
f: - formatted value
p: - custom properties (not displayed on the chart)
{v: new Date(2017, 1, 10), f: 'Feb 28, 17', p: {custom: 'value'}}
when used in the data table,
the default tooltip will show the formatted value
when used in ticks,
the axis label will display the formatted value
see following working snippet...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Date', 'Value'],
// use object notation here to change the default tooltip
[{v: new Date(2017, 1, 10), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 1, 21), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 1, 28), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 2, 7), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 2, 14), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 2, 23), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 2, 31), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 3, 7), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 3, 26), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 4, 3), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 4, 10), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 4, 17), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 4, 25), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 5, 5), f: 'Feb 28, 17'},1],
[{v: new Date(2017, 5, 12), f: 'Feb 28, 17'},0.5]
]);
var options = {
legend: {position: 'top', alignment: 'start', textStyle: {fontSize: 12, bold: false}},
width: 800,
height: 500,
hAxis: {
format: 'M/d/yy',
ticks: [
// use object notation here to change the axis label
{v: new Date(2017, 1, 10), f: 'Feb 28, 17'},
{v: new Date(2017, 1, 21), f: 'Feb 28, 17'},
{v: new Date(2017, 1, 28), f: 'Feb 28, 17'},
{v: new Date(2017, 2, 7), f: 'Feb 28, 17'},
{v: new Date(2017, 2, 14), f: 'Feb 28, 17'}
],
slantedText: 'true',
slantedTextAngle: 45
},
vAxis: {minValue: 0},
colors: ['#C3D5BC'],
trendlines: {0: {
labelInLegend: 'Trendline (Test)',
type: 'linear',
showR2: false,
visibleInLegend: true,
color: '#344F35'
}}
};
var container = document.getElementById('test-div');
var chart = new google.visualization.AreaChart(container);
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="test-div"></div>
data table method --> getDistinctValues(colIndex)
returns a simple array, with the values from the column
the first column in this example would return an array of the date values...
[new Date(2017, 1, 10), new Date(2017, 1, 21), ...]
in order to convert this array to object notation,
you could use the map method, or a number of other routines...
the following snippet creates object notation using the date values from the data,
all with the same formatted value
var ticks = data.getDistinctValues(0);
ticks = ticks.map(function (xDate) {
return {
v: xDate,
f: 'Feb 28, 2017'
};
});
see following working snippet...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Date', 'Value', {role: 'tooltip'}],
[new Date(2017, 1, 10),1,'1'],
[new Date(2017, 1, 21),1,'1'],
[new Date(2017, 1, 28),1,'1'],
[new Date(2017, 2, 7),1,'1'],
[new Date(2017, 2, 14),1,'1'],
[new Date(2017, 2, 23),1,'1'],
[new Date(2017, 2, 31),1,'1'],
[new Date(2017, 3, 7),1,'1'],
[new Date(2017, 3, 26),1,'1'],
[new Date(2017, 4, 3),1,'1'],
[new Date(2017, 4, 10),1,'1'],
[new Date(2017, 4, 17),1,'1'],
[new Date(2017, 4, 25),1,'1'],
[new Date(2017, 5, 5),1,'1'],
[new Date(2017, 5, 12),0.5,'0.5']
]);
var ticks = data.getDistinctValues(0);
ticks = ticks.map(function (xDate) {
return {
v: xDate,
f: 'Feb 28, 2017'
};
});
var options = {
legend: {position: 'top', alignment: 'start', textStyle: {fontSize: 12, bold: false}},
width: 800,
height: 500,
hAxis: {
format: 'M/d/yy',
ticks: ticks,
slantedText: 'true',
slantedTextAngle: 45
},
vAxis: {minValue: 0},
colors: ['#C3D5BC'],
trendlines: {0: {
labelInLegend: 'Trendline (Test)',
type: 'linear',
showR2: false,
visibleInLegend: true,
color: '#344F35'
}}
};
var container = document.getElementById('test-div');
var chart = new google.visualization.AreaChart(container);
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="test-div"></div>
How to mix line and scatter plot on one chart?
Column 1: Y (date)
Column 2: X1 (number, as points/dots)
Column 3: X2 (number, as line)
Example JsFiddle: https://jsfiddle.net/e64y5wfj/4/
It's doesn't seems a way or example on the documentation.
use the series option to change the style of Column 2
series: {
0: {
pointSize: 8,
lineWidth: 0
}
}
see following working snippet...
google.charts.load('current', {
callback: drawMultipleTrendlineChart,
packages:['corechart']
});
function drawMultipleTrendlineChart() {
var chart;
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Sales value A');
data.addColumn('number', 'Sales value B');
data.addRows([
[new Date(2013, 3, 11), 200, 1000],
[new Date(2013, 4, 02), 500, 650],
[new Date(2013, 5, 03), 700, 550],
[new Date(2013, 6, 04), 800, 95],
[new Date(2013, 7, 05), 500, 400],
[new Date(2013, 8, 06), 900, 250],
[new Date(2014, 0, 07), 800, 300],
[new Date(2014, 1, 08), 2000, 200],
[new Date(2014, 2, 09), 1000, 312]
]);
var formatter = new google.visualization.NumberFormat({
fractionDigits: 2,
prefix: 'R$:'
});
formatter.format(data, 1);
var dateFormatter = new google.visualization.NumberFormat({
pattern: 'MMM yyyy'
});
dateFormatter.format(data, 0);
var chartHeight = 400;
var chartWidth = 600;
var chartOptions = {
tooltip: {
isHtml: true
},
title: 'multiple lines',
isStacked: true,
width: chartWidth,
height: chartHeight,
colors: ['#0000D8', '#00dddd'],
hAxis: {
title: 'example title',
slantedText: false,
slantedTextAngle: 45,
textStyle: {
fontSize: 10
},
format: 'dd-MM-yyyy'
},
chartArea: {
left: 50,
top: 20,
width: (chartWidth - 10),
height: (chartHeight - 90)
},
series: {
0: {
pointSize: 8,
lineWidth: 0
}
}
};
chart = new google.visualization.LineChart(document.getElementById('multipleTrendChart'));
chart.draw(data, chartOptions);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="multipleTrendChart"></div>
I read the API reference from google developer website. For material line chart, there is example from here. Could anyone tell me how to remove the bottom "Month"? I think it should be worked by some code like:
hAxis: {
title: ''
}
In addition, most of the hAxis and vAxis features don't work.
that is correct, the following option will remove the x-axis title...
hAxis: {
title: ''
},
just be sure to use the options conversion method for material charts...
//convert options
materialChart.draw(data, google.charts.Line.convertOptions(materialOptions));
see following working snippet...
google.charts.load('current', {'packages':['line', 'corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var button = document.getElementById('change-chart');
var chartDiv = document.getElementById('chart_div');
var data = new google.visualization.DataTable();
data.addColumn('date', 'Month');
data.addColumn('number', "Average Temperature");
data.addColumn('number', "Average Hours of Daylight");
data.addRows([
[new Date(2014, 0), -.5, 5.7],
[new Date(2014, 1), .4, 8.7],
[new Date(2014, 2), .5, 12],
[new Date(2014, 3), 2.9, 15.3],
[new Date(2014, 4), 6.3, 18.6],
[new Date(2014, 5), 9, 20.9],
[new Date(2014, 6), 10.6, 19.8],
[new Date(2014, 7), 10.3, 16.6],
[new Date(2014, 8), 7.4, 13.3],
[new Date(2014, 9), 4.4, 9.9],
[new Date(2014, 10), 1.1, 6.6],
[new Date(2014, 11), -.2, 4.5]
]);
var materialOptions = {
chart: {
title: 'Average Temperatures and Daylight in Iceland Throughout the Year'
},
width: 900,
height: 500,
series: {
// Gives each series an axis name that matches the Y-axis below.
0: {axis: 'Temps'},
1: {axis: 'Daylight'}
},
hAxis: {
title: ''
},
axes: {
// Adds labels to each axis; they don't have to match the axis names.
y: {
Temps: {label: 'Temps (Celsius)'},
Daylight: {label: 'Daylight'}
}
}
};
var classicOptions = {
title: 'Average Temperatures and Daylight in Iceland Throughout the Year',
width: 900,
height: 500,
// Gives each series an axis that matches the vAxes number below.
series: {
0: {targetAxisIndex: 0},
1: {targetAxisIndex: 1}
},
vAxes: {
// Adds titles to each axis.
0: {title: 'Temps (Celsius)'},
1: {title: 'Daylight'}
},
hAxis: {
ticks: [new Date(2014, 0), new Date(2014, 1), new Date(2014, 2), new Date(2014, 3),
new Date(2014, 4), new Date(2014, 5), new Date(2014, 6), new Date(2014, 7),
new Date(2014, 8), new Date(2014, 9), new Date(2014, 10), new Date(2014, 11)
]
},
vAxis: {
viewWindow: {
max: 30
}
}
};
function drawMaterialChart() {
var materialChart = new google.charts.Line(chartDiv);
materialChart.draw(data, google.charts.Line.convertOptions(materialOptions));
button.innerText = 'Change to Classic';
button.onclick = drawClassicChart;
}
function drawClassicChart() {
var classicChart = new google.visualization.LineChart(chartDiv);
classicChart.draw(data, classicOptions);
button.innerText = 'Change to Material';
button.onclick = drawMaterialChart;
}
drawMaterialChart();
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<button id="change-chart">Change to Classic</button>
<br><br>
<div id="chart_div"></div>
note: the conversion method will not work for all options,
many simply aren't supported...
see --> Tracking Issue for Material Chart Feature Parity
another route would be to use a classic chart with the following option...
theme: 'material'