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>
Am using google API to implement chart in my Project. Am using Scatter chart.
I need to implement chart like below. How can Achieve this.? Is there any other way to Achieve this by using any other Open source chart.?
Sample Google Chart which I need
Additional requirement
you can use a ComboChart to combine scatter and area series
the area series should be stacked
set the color of the first layer to 'transparent'
use null for values where the series do not coincide
see following working snippet...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'area bottom');
data.addColumn('number', 'area top');
data.addColumn('number', 'scatter');
data.addRows([
[1.5, null, null, 1.5],
[3, 3, 3, null],
[6, 3, 3, null]
]);
var options = {
areaOpacity: 1,
colors: ['transparent', '#ff9900', '#3366cc'],
hAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
height: 320,
legend: {
position: 'none'
},
isStacked: true,
seriesType: 'area',
series: {
2: {
type: 'scatter'
}
},
title: 'Final Score',
vAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
width: 320
};
var chart = new google.visualization.ComboChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
UPDATE
just add another area layer for the new requirement...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'area bottom');
data.addColumn('number', 'area middle');
data.addColumn('number', 'area top');
data.addColumn('number', 'scatter');
data.addRows([
[1.5, null, null, null, 1.5],
[3, 3, 3, null, null],
[4.5, 3, 3, null, null],
[4.5, 3, 1.5, 1.5, null],
[6, 3, 1.5, 1.5, null]
]);
var options = {
areaOpacity: 1,
colors: ['transparent', '#ff9900', '#f8bbd0', '#3366cc'],
hAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
height: 320,
legend: {
position: 'none'
},
isStacked: true,
seriesType: 'area',
series: {
3: {
type: 'scatter'
}
},
title: 'Final Score',
vAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
width: 320
};
var chart = new google.visualization.ComboChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Set to show only the 100 and -100 grids ticks: [-100, 100], but the horizontal 0 (zero) grid appears. How to hide?
<table class="columns">
<tr>
<th>Linear Scale</th>
</tr>
<tr>
<td><div id="linear_div"></div></td>
</tr>
</table>
<script>
google.charts.load('current', {'packages':['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Population');
data.addRows([
[new Date(1400, 1, 1), -44],
[new Date(1500, 1, 1), 33],
[new Date(1600, 1, 1), 88],
[new Date(1700, 1, 1), 100],
[new Date(1750, 1, 1), 200],
]);
var linearOptions = {
title: 'World Population Since 1400 A.D. in Linear Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
ticks: [-100, 100]
}
};
var linearChart = new google.visualization.LineChart(document.getElementById('linear_div'));
linearChart.draw(data, linearOptions);
}
</script>
https://jsfiddle.net/dnsaudd8/
use following option to hide 0 (baseline)
baselineColor: 'transparent'
see following working snippet...
google.charts.load('current', {'packages':['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Population');
data.addRows([
[new Date(1400, 1, 1), -44],
[new Date(1500, 1, 1), 33],
[new Date(1600, 1, 1), 88],
[new Date(1700, 1, 1), 100],
[new Date(1750, 1, 1), 200]
]);
var linearOptions = {
baselineColor: 'transparent',
title: 'World Population Since 1400 A.D. in Linear Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
ticks: [-100, 100]
}
};
var linearChart = new google.visualization.LineChart(document.getElementById('linear_div'));
linearChart.draw(data, linearOptions);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="linear_div"></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'