Google Vis: Gantt Chart with odd lines - google-visualization

Trying to creat a gantt chart but the lines are odd:
https://jsfiddle.net/Khrys/zyfbsy67/1/
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
data.addRows([
['Project', 'Project 1', null, new Date(2015, 0, 1), new Date(2015, 6, 5), null, 100, null],
['Task1', 'Task 1', 'Tarefa', new Date(2015, 0, 1), new Date(2015, 2, 5), null, 20, 'Project'],
['Task2', 'Task 2', 'Tarefa', new Date(2015, 2, 1), new Date(2015, 3, 5), null, 60, 'Project'],
['Task3', 'Task 3', 'Tarefa', new Date(2015, 4, 1), new Date(2015, 5, 5), null, 40, 'Project']
]);
var options = {
height: 475
};
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(data, options);
}

The data you supply to the chart needs to be corrected.
All Tasks are dependent on the overall Project, which ends after all the Tasks.
You can remove the dependencies, or shorten the Project end date to before the end of the first Task.
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
data.addRows([
['Project', 'Project 1', null, new Date(2015, 0, 1), new Date(2015, 6, 5), null, 100, null],
['Task1', 'Task 1', 'Tarefa', new Date(2015, 0, 1), new Date(2015, 2, 5), null, 20, null],
['Task2', 'Task 2', 'Tarefa', new Date(2015, 2, 1), new Date(2015, 3, 5), null, 60, null],
['Task3', 'Task 3', 'Tarefa', new Date(2015, 4, 1), new Date(2015, 5, 5), null, 40, null]
]);
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(data, {});
drawChart2();
}
function drawChart2() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
data.addRows([
['Project', 'Project 1', null, new Date(2015, 0, 1), new Date(2015, 0, 9), null, 100, null],
['Task1', 'Task 1', 'Tarefa', new Date(2015, 0, 1), new Date(2015, 2, 5), null, 20, 'Project'],
['Task2', 'Task 2', 'Tarefa', new Date(2015, 2, 1), new Date(2015, 3, 5), null, 60, 'Project'],
['Task3', 'Task 3', 'Tarefa', new Date(2015, 4, 1), new Date(2015, 5, 5), null, 40, 'Project']
]);
var chart = new google.visualization.Gantt(document.getElementById('chart_div2'));
chart.draw(data, {});
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
<br/>
<div id="chart_div2"></div>

Related

Google Vis Dashboard with a working ready listener

<html>
<head>
<title>Dashboard Evelien</title>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load the Visualization API and the controls package.
google.charts.load('current', {'packages':['corechart', 'controls', 'table']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawDashboard);
// Callback that creates and populates a data table,
// instantiates a dashboard, a range slider and a pie chart,
// passes in the data and draws it.
function drawDashboard() {
// Create our data table.
var data = google.visualization.arrayToDataTable([
['geo_Stad', 'Name', 'Leningdelen', 'Hypotheek', 'Straat', 'Woonplaats', 'Datum_start', 'Datum_eind', 'Mv', 'looptijd'],
['Netherlands, Amsterdam', 'Aap' , 5, 500000, 'Kalverstraat', 'Amsterdam', new Date(2014, 3, 22), new Date(2016, 2, 28), 'm', 123],
['Netherlands, Rotterdam', 'Noot', 1, 70000, 'Beursplein', 'Rotterdam', new Date(2014, 10, 11), new Date(2017, 3, 20), 'm', 234],
['Netherlands, Rotterdam', 'Mies', 3, 300000, 'Stationsplein', 'Rotterdam', new Date(2013, 10, 1), new Date(2013, 12, 23), 'v', 564],
['Netherlands, Amsterdam', 'Wim' , 2, 222222, 'Dorpsstraat', 'Amsterdam', new Date(2010, 1, 2), new Date(2016, 10, 23), 'm', 456],
['Netherlands, Amsterdam', 'Zus' , 7, 600000, 'Stationsplein', 'Amsterdam', new Date(2007, 5, 22), new Date(2009, 2, 2), 'v', 385],
['Netherlands, Rotterdam', 'Jet' , 2, 100000, 'Dorpsstraat', 'Rotterdam', new Date(2014, 7, 7), new Date(2015, 2, 16), 'v', 964],
['Netherlands, Rotterdam', 'Teun', 1, 85670, 'Kerkstraat', 'Rotterdam', new Date(2014, 3, 22), new Date(2016, 11, 12), 'm', 356],
['Netherlands, Utrecht', 'Gijs', 1, 53400, 'Stationsstraat', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 6, 18), 'm', 356],
['Netherlands, Utrecht', 'Does', 1, 77200, 'Vreeburg', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 8, 8), 'm', 768],
['Netherlands', 'Does1', 0, 0, 'Amsterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does2', 0, 0, 'Rotterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 563],
['Netherlands', 'Does3', 0, 0, 'Utrecht', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does4', 0, 0, 'Nederland', null, new Date(2014, 3, 22), new Date(2016, 8, 8), '', 963]
]);
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));
// Create a range slider, passing some options
var donutRangeSlider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'filter_div',
'options': {
'filterColumnLabel': 'Leningdelen'
}
});
var tableE = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'tableE_div',
'view': {'rows' : data.getFilteredRows([{column: 2, minValue: 1}])},
'options': {
'showRowNumber': 'true',
'width': '100%',
'height': '33%',
}
});
// Create a pie chart, passing some options
var pieChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'chart_div',
'view': {'columns': [1, 2],
'rows' : data.getFilteredRows([{column: 2, minValue: 1}])},
'options': {
'width': 400,
'height': 300,
'pieSliceText': 'value',
'legend': 'right'
}
});
// Create a bar chart, passing some options
var barChart = new google.visualization.ChartWrapper({
'chartType': 'BarChart',
'containerId': 'chartB_div',
'view': {'columns': [1, 2],
'rows' : data.getFilteredRows([{column: 2, minValue: 1}])},
'options': {
'width': 500,
'height': 300,
'legend': 'right'
}
});
// Establish dependencies, declaring that 'filter' drives 'pieChart',
// so that the pie chart will only display entries that are let through
// given the chosen slider range.
dashboard.bind(donutRangeSlider, [tableE, pieChart, barChart]);
// Draw the dashboard.
dashboard.draw(data);
}
</script>
</head>
<body>
<!--Div that will hold the dashboard-->
<div id="dashboard_div">
<table class="columns">
<tr>
<td><div id="filter_div" style="border: 1px solid #ccc"></div></td>
</tr>
<tr>
<td><div id="chart_div" style="border: 1px solid #ccc"></div></td>
<td><div id="chartB_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
<table>
<tr>
<td><div id="tableE_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
</div>
</body>
</html>
Hi,
I am trying to make a dashboard with many differend graphs, but I cannot solve the issue 'Invalid row index 5. Should be in the range [0-4].'
Now I have a very simple dashboard, it works when I use only the data columns the 2 simple graphs need. But I want more graphs with other columns. When I add more columns to the data, the error pops up. I think I need a ready Listener somewhere in the code, but I tried so many times using the answeres for others, but without success.
So will you please complete my code with a ready listener so the error will be solved? Thanks !
a 'ready' listener will not resolve the problem
the problem stems from...
'rows' : data.getFilteredRows([{column: 2, minValue: 1}])}
once the dashboard is drawn and the filter value changes,
there are rows that exist in the data, that do not exist in the dashboard
so getFilteredRows is returning more rows than the dashboard has access to
thus, the error is thrown --> Invalid row index
to correct, just use an overall DataView to draw the dashboard
var view = new google.visualization.DataView(data);
view.setRows(data.getFilteredRows([{column: 2, minValue: 1}]));
dashboard.draw(view);
see following working snippet...
google.charts.load('current', {'packages':['corechart', 'controls', 'table']});
google.charts.setOnLoadCallback(drawDashboard);
function drawDashboard() {
var data = google.visualization.arrayToDataTable([
['geo_Stad', 'Name', 'Leningdelen', 'Hypotheek', 'Straat', 'Woonplaats', 'Datum_start', 'Datum_eind', 'Mv', 'looptijd'],
['Netherlands, Amsterdam', 'Aap' , 5, 500000, 'Kalverstraat', 'Amsterdam', new Date(2014, 3, 22), new Date(2016, 2, 28), 'm', 123],
['Netherlands, Rotterdam', 'Noot', 1, 70000, 'Beursplein', 'Rotterdam', new Date(2014, 10, 11), new Date(2017, 3, 20), 'm', 234],
['Netherlands, Rotterdam', 'Mies', 3, 300000, 'Stationsplein', 'Rotterdam', new Date(2013, 10, 1), new Date(2013, 12, 23), 'v', 564],
['Netherlands, Amsterdam', 'Wim' , 2, 222222, 'Dorpsstraat', 'Amsterdam', new Date(2010, 1, 2), new Date(2016, 10, 23), 'm', 456],
['Netherlands, Amsterdam', 'Zus' , 7, 600000, 'Stationsplein', 'Amsterdam', new Date(2007, 5, 22), new Date(2009, 2, 2), 'v', 385],
['Netherlands, Rotterdam', 'Jet' , 2, 100000, 'Dorpsstraat', 'Rotterdam', new Date(2014, 7, 7), new Date(2015, 2, 16), 'v', 964],
['Netherlands, Rotterdam', 'Teun', 1, 85670, 'Kerkstraat', 'Rotterdam', new Date(2014, 3, 22), new Date(2016, 11, 12), 'm', 356],
['Netherlands, Utrecht', 'Gijs', 1, 53400, 'Stationsstraat', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 6, 18), 'm', 356],
['Netherlands, Utrecht', 'Does', 1, 77200, 'Vreeburg', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 8, 8), 'm', 768],
['Netherlands', 'Does1', 0, 0, 'Amsterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does2', 0, 0, 'Rotterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 563],
['Netherlands', 'Does3', 0, 0, 'Utrecht', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does4', 0, 0, 'Nederland', null, new Date(2014, 3, 22), new Date(2016, 8, 8), '', 963]
]);
var view = new google.visualization.DataView(data);
view.setRows(data.getFilteredRows([{column: 2, minValue: 1}]));
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));
var donutRangeSlider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'filter_div',
'options': {
'filterColumnLabel': 'Leningdelen'
}
});
var tableE = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'tableE_div',
'options': {
'showRowNumber': 'true',
'width': '100%',
'height': '33%',
}
});
var pieChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'chart_div',
'view': {
'columns': [1, 2]
},
'options': {
'width': 400,
'height': 300,
'pieSliceText': 'value',
'legend': 'right'
}
});
var barChart = new google.visualization.ChartWrapper({
'chartType': 'BarChart',
'containerId': 'chartB_div',
'view': {
'columns': [1, 2]
},
'options': {
'width': 500,
'height': 300,
'legend': 'right'
}
});
dashboard.bind(donutRangeSlider, [tableE, pieChart, barChart]);
dashboard.draw(view);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dashboard_div">
<table class="columns">
<tr>
<td><div id="filter_div" style="border: 1px solid #ccc"></div></td>
</tr>
<tr>
<td><div id="chart_div" style="border: 1px solid #ccc"></div></td>
<td><div id="chartB_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
<table>
<tr>
<td><div id="tableE_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
</div>

Setting Time as range value instead of date in google charts

Trying to work with Google charts and ended up with a range slider thru few code snippets provided by developer docs.
<https://jsfiddle.net/hari41980/gsvr18hw/11/>
Now I need someone to help me on getting the date range from date to TIME.
Expected:expected output
Regards
just need to set the desired format in the options for the slider...
'ui': {
'format': {
'pattern': 'h:mm a'
},
'labelStacking': 'vertical'
}
see following example...
google.charts.load('current', {
callback: function () {
var dashboard = new google.visualization.Dashboard(
document.getElementById('programmatic_dashboard_div'));
var programmaticSlider = new google.visualization.ControlWrapper({
'controlType': 'DateRangeFilter',
'containerId': 'programmatic_control_div',
'options': {
'filterColumnLabel': 'Time Stamp',
'ui': {
'format': {
'pattern': 'h:mm a'
},
'labelStacking': 'vertical'
}
}
});
var programmaticChart = new google.visualization.ChartWrapper({
'chartType': 'AreaChart',
'containerId': 'programmatic_chart_div',
'options': {
'width': 600,
'height': 300,
'legend': 'none',
}
});
var data = google.visualization.arrayToDataTable([
['Time Stamp', 'Bulk'],
[new Date(2014, 9, 15, 10, 30) , 5],
[new Date(2014, 9, 15, 11, 30) , 10],
[new Date(2014, 9, 15, 12, 30) , 15],
[new Date(2014, 9, 15, 13, 30) , 5],
[new Date(2014, 9, 15, 14, 30) , 10],
[new Date(2014, 9, 15, 15, 30) , 8],
[new Date(2014, 9, 15, 16, 30) , 12],
[new Date(2014, 9, 15, 17, 30) , 15],
[new Date(2014, 9, 15, 18, 30) , 25]
]);
dashboard.bind(programmaticSlider, programmaticChart);
dashboard.draw(data);
},
packages: ['corechart', 'controls']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="programmatic_control_div"></div>
<div id="programmatic_chart_div"></div>

How to make a custom tooltip for each series in Google Line Chart

I am currently testing google charts and having difficulty in specifying my own custom tooltips.
my data is declared as below
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addColumn('number', 'Cats');
data.addColumn({type: 'string', role: 'tooltip', p: {'html':true}});
data.addRows([0, 0, 0, 'hello'], [1, 10, 5, 'hello'],
[2, 23, 15, 'hello'],[3, 17, 9, 'hello'], [4, 18, 10, 'hello'],
[5, 9, 5, 'hello'],
see this jsFiddle http://jsfiddle.net/SecretSquirrel/rbwyhx1q/
It appears the custom tooltip is only affecting the first data column?
I thought it was pretty limited to only allow 1 tooltip per row, but if this is only 1 tooltip for the first column this is really quite useless.
Well you have to add another tooltip column for your second graft.
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addColumn({type: 'string', role: 'tooltip', p: {'html':true}});
data.addColumn('number', 'Cats');
data.addColumn({type: 'string', role: 'tooltip', p: {'html':true}});
data.addRows([
[0, 0,'custom', 0, 'hello'], [1, 10,'custom', 5, 'hello'], [2, 23,'custom', 15, 'hello'],
[3, 17,'custom', 9, 'hello'], [4, 18,'custom', 10, 'hello'], [5, 9,'custom', 5, 'hello'],
[6, 11,'custom', 3, 'hello'], [7, 27,'custom', 19, 'hello'], [8, 33,'custom', 25, 'hello'],
[9, 40,'custom', 32, 'hello'], [10, 32,'custom', 24, 'hello'], [11, 35,'custom', 27, 'hello'],
[12, 30,'custom', 22, 'hello'], [13, 40,'custom', 32, 'hello'], [14, 42,'custom', 34, 'hello'],
[15, 47,'custom', 39,'hello'], [16, 44,'custom', 36,'hello'], [17, 48,'custom', 40, 'hello'],
[18, 52,'custom', 44,'hello'], [19, 54,'custom', 46,'hello'], [20, 42,'custom', 34, 'hello'],
[21, 55,'custom', 47,'hello'], [22, 56,'custom', 48,'hello'], [23, 57,'custom', 49,'hello']
]);
var options = {
width: 1000,
height: 563,
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Popularity'
},
series: {
1: {curveType: 'function'}
}
};
var chart = new google.visualization.LineChart(document.getElementById('ex2'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<div id="ex2"></div>

how to connect selected points in scatter plot in google api

I'm using scatter plot for my application, so I selected google API for scatter charts. For my application I need to connect some points with line marker. I followed this
link for test. please help me to improve.
If you need to connect points on your ScatterChart, you can do so by setting either the lineWidth option (creates lines connecting points for all data series) or the series.<series index>.lineWidth option (which creates lines connecting the points of a single series). Here are some examples:
Use the lineWidth option to connect points in all series (jsfiddle example):
function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Y1');
data.addColumn('number', 'Y2');
data.addRows([
[0, 2, 5],
[1, 6, 6],
[2, 5, 9],
[3, 6, 5],
[4, 5, 4],
[7, 9, 2],
[8, 4, 6],
[9, 3, 7]
]);
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, {
height: 400,
width: 600,
lineWidth: 1
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
Connect all the points in one data series using the series.<series index>.lineWidth option (jsfiddle example):
function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Y1');
data.addColumn('number', 'Y2');
data.addRows([
[0, 2, 5],
[1, 6, 6],
[2, 5, 9],
[3, 6, 5],
[4, 5, 4],
[7, 9, 2],
[8, 4, 6],
[9, 3, 7]
]);
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, {
height: 400,
width: 600,
series: {
0: {
// connect the points in Y1 with a line
lineWidth: 1
}
}
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
If you want to connect only certain points in a data series, you must insert null values between all points that you do not want lines to connect. Here's an example connecting points (2, 5) and (3, 6) in series Y1 (jsfiddle example):
function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Y1');
data.addColumn('number', 'Y2');
data.addRows([
[0, 2, 5],
[null, null, null],
[1, 6, 6],
[null, null, null],
[2, 5, 9],
[3, 6, 5],
[null, null, null],
[4, 5, 4],
[null, null, null],
[7, 9, 2],
[null, null, null],
[8, 4, 6],
[null, null, null],
[9, 3, 7]
]);
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, {
height: 400,
width: 600,
series: {
0: {
// connect the points in Y1 with a line
lineWidth: 1
}
}
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
The important thing to note here is that points which are adjacent in the DataTable will be connected with lines. If you want to connect points which are not adjacent in the chart, you need to re-arrange the data. Here's an example that connects the points (2, 5) and (8, 4) in Y1 and (4, 4) and (8, 6) in Y2 (jsfiddle example):
function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Y1');
data.addColumn('number', 'Y2');
data.addRows([
[null, null, null],
// Y1 data
// make (2, 5) and (8, 4) adjacent in the DataTable
[2, 5, null],
[8, 4, null],
// split all others with nulls
[null, null, null],
[0, 2, null],
[null, null, null],
[1, 6, null],
[null, null, null],
[3, 6, null],
[null, null, null],
[4, 5, null],
[null, null, null],
[7, 9, null],
[null, null, null],
[9, 3, null],
// Y2 data
// make (4, 4) and (8, 6) adjacent in the DataTable
[4, null, 4],
[8, null, 6],
// split all others with nulls
[null, null, null],
[0, null, 5],
[null, null, null],
[1, null, 6],
[null, null, null],
[2, null, 9],
[null, null, null],
[3, null, 5],
[null, null, null],
[7, null, 2],
[null, null, null],
[9, null, 7]
]);
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, {
height: 400,
width: 600,
series: {
0: {
// connect the points in Y1 with a line
lineWidth: 1
},
1: {
// connect the points in Y2 with a line
lineWidth: 1
}
}
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
That should get you started on connecting points with lines in ScatterCharts.

Google Chart: How to draw the vertical axis for LineChart?

I want to draw a Google's line chart in my web page! Here is my js code:
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['x', 'Cats', 'Blanket 1'],
['A', 1, 10],
['B', 2, 5],
['C', 4, 12],
['D', 8, 5]
]);
var options = {
curveType: 'function',
lineWidth: 2,
hAxis: {
baselineColor: 'red',
textStyle: {color: '#000', fontName: 'Arial', fontSize: 10},
gridlines: { color: '#f3f3f3', count: 5}
},
vAxis: {
baseline: 0,
viewWindowMode: "explicit",
viewWindow:{ min: 0 },
gridlines: { color: '#f3f3f3', count: 6}
}
};
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, options);
}
However, the result chart is drawing without any vertical axis line. How I can add the vertical axis lines as below images:
Thank you so much!
Gridlines are not supported for category axes. Because the data in question is using strings as the categories (X-axis labels), there is no way to tell how they "should" be split. You can get around this, however, with the following techniques.
Trick 1: Turn your Data Numerical
So right now you have strings which means no gridlines. We want gridlines though, so we have to correct that small problem. So we turn our first column (X) in to a number, with a format on it so it says 'A' or 'B' or 'C' when you mouse over it (and in the legend):
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'Cats');
data.addColumn('number', 'Blanket 1');
data.addRows([
[{v: 1, f:'A'}, 1, 10],
[{v: 2, f:'B'}, 2, 5],
[{v: 3, f:'C'}, 4, 12],
[{v: 4, f:'D'}, 8, 5]
]);
This doesn't solve the problem though. Now we have numbers on the bottom axis, cut off at weird 0.8 intervals. So we want to fix that. Unfortunately, with the hAxis, you can't set a min/max value and have it stick (I don't know why). We can fix the min by adding baseline: 0 to our options.
To get the max, we have to add a dummy series.
Put it all together, and we get this:
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'Cats');
data.addColumn('number', 'Blanket 1');
// This dummy series is to extend the chart from 0-5 for padding
data.addColumn('number', null);
data.addRows([
[{v: 1, f:'A'}, 1, 10, null],
[{v: 2, f:'B'}, 2, 5, null],
[{v: 3, f:'C'}, 4, 12, null],
[{v: 4, f:'D'}, 8, 5, null],
[{v: 5, f:''}, null, null, {v: 0, f:''}]
]);
options = {
curveType: 'function',
lineWidth: 2,
hAxis: {
// Show a baseline at 0
baseline: 0,
// 6 Gridlines, 4 labels + left and right for padding
gridlines: {
count: 6
},
},
vAxis: {
baseline: 0,
},
series: [
{},
{},
// Hide our dummy series
{
lineWidth: 0,
pointsize: 0,
visibleInLegend: false
},
]
};
chart1 = new google.visualization.LineChart(document.getElementById('visualization'));
chart1.draw(data, options);
}
Now it's looking more like what you want. There are two main issues. One is that if you mouseover the bottom right of the chart, a blank tooltip pops up (this is not a huge issue I'd hope, though you may have to do some error trapping if you make the chart interactive with events). The other is that the bottom of our chart is showing numbers, not letters.
Unfortunately, there is no easy way to format numbers as letters (at least until Google implements the entire ICU pattern set rather than just dates/numbers). So we need to make another workaround. Basically, what I do is create an entirely new chart just to make the labels. I then format it so that it hides everything but the labels, and make sure that it lines up horizontally with the chart above.
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'Cats');
data.addColumn('number', 'Blanket 1');
// This dummy series is to extend the chart from 0-5 for padding
data.addColumn('number', null);
data.addRows([
[{v: 1, f:'A'}, 1, 10, null],
[{v: 2, f:'B'}, 2, 5, null],
[{v: 3, f:'C'}, 4, 12, null],
[{v: 4, f:'D'}, 8, 5, null],
[{v: 5, f:''}, null, null, {v: 0, f:''}]
]);
options = {
curveType: 'function',
lineWidth: 2,
hAxis: {
// Show a baseline at 0
baseline: 0,
// 6 Gridlines, 4 labels + left and right for padding
gridlines: {
count: 6
},
// Hide our labels
textPosition: 'none'
},
vAxis: {
baseline: 0,
},
series: [
{},
{},
// Hide our dummy series
{
lineWidth: 0,
pointsize: 0,
visibleInLegend: false
},
]
};
// Add dummy data for the axis labels
var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'x');
data2.addColumn('number', 'dummy');
data2.addRows([
['A', null],
['B', null],
['C', null],
['D', null]
]);
chart1 = new google.visualization.LineChart(document.getElementById('visualization'));
chart1.draw(data, options);
chart2 = new google.visualization.LineChart(document.getElementById('visualization2'));
chart2.draw(data2,
{
chartArea: {
top:0,
height:"0%"
},
min: 0,
max: 0,
hAxis: {
baselineColor: '#FFFFFF'
},
vAxis: {
baselineColor: '#FFFFFF',
direction: -1,
textPosition: 'none',
gridlines: {
color: '#FFFFFF'
}
}
});
}
Just make another below the first one, and use CSS to align it properly (float it around the same position, or whatnot), and it looks like the labels belong to the chart above.
It ain't glorious, but it works.
There is a more elegant solution by using xticks:
To do that we define a datatable like this:
var data = new google.visualization.DataTable();
data.addColumn('number', 'Month');
data.addColumn('number', 'Sales');
data.addRows([
[{v: 0, f:'Jan'}, 1000],
[{v: 1, f:'Feb'}, 1170],
[{v: 2, f:'Mar'}, 660],
[{v: 3, f:'Apr'}, 1030]
]);
Where we set number values but also string labels for the Month axis.
With just this and the format attribute removed, we would get vertical lines but numbers instead of the strings for the x axis labels, which is not what we want.
To fix this we can set xticks to force the correct labels in the plot.
var options = {
title: '',
hAxis: {
title: 'Month',
titleTextStyle: {
color: '#333'
},
baseline: 0,
gridlines: {
color: '#f3f3f3',
count: 4
},
ticks: [{v: 0, f:'Jan'},{v: 1, f:'Feb'},{v: 2, f:'Mar'},{v: 3, f:'Apr'}], // <------- This does the trick
},
vAxis: {
minValue: 0,
gridlines: {
color: '#f3f3f3',
count: 5
}
}
};
Hereby a complete working fiddle to show you how it can be done: http://jsfiddle.net/j29Pt/417/