Creating multi line graphs using Google Charts API - charts

I am attempting to draw multiple lines on a line graph using google charts, using JSON data coming from an API. I am able to draw a single line using the point data from the API, however, am unsure of the data format for multiple lines.
This is my client side code that uses google charts
var express = require('express');
var router = express.Router();
var request = require('request');
router.get('/', function(req, res){
res.send(JSON.stringify({
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushroooooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":2,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}))
console.log('done with the sample api 1')
req.flash('success_msg', 'You successfully retrieved all goals');
console.log('done with the sample api 2')
});
module.exports = router;
The chart I observe is as follows
However, I am attempting to generate an image that looks like this
Would somebody be able to help me with the format of JSON data that would generate an image like so, with multiple lines.
Also, is there a way to specify that I'd like to see certain lines dotted and some other solid, in a multi line graph, as below.
PS : I am aware that you could hardcode points as follows, to generate multi lines. However, I'd rather it come from a json object as I show above, so I can replace this with a REST API endpoint that emits a JSON object.
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
EDIT : Based on #WhiteHat's response below, I have tried to create a chart with 4 lines, two of which are dotted, and two solid. Each of these line would have five points in the chart, for Week 17, Week 18, Week 19, Week 20 and Week 21. However, I see that only two lines get drawn.
This is my json input below
res.send({
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Solid-Series-1","pattern":"","type":"number"},
{"id":"","label":"Solid-Series-2","pattern":"","type":"number"},
{"id":"","label":"Dotted-Series-1","pattern":"","type":"number"},
{"id":"","role":"certainty","type":"boolean"},
{"id":"","label":"Dotted-Series-2","pattern":"","type":"number"},
{"id":"","role":"certainty","type":"boolean"}
],
"rows": [
{"c":[{"v":"Week 17","f":null},{"v":6,"f":null},{"v":5,"f":null},{"v":4,"f":null},{"v":3,"f":null},{"v":false}]},
{"c":[{"v":"Week 18","f":null},{"v":12,"f":null},{"v":11,"f":null},{"v":8,"f":null},{"v":7,"f":null},{"v":false}]},
{"c":[{"v":"Week 19","f":null},{"v":18,"f":null},{"v":15,"f":null},{"v":12,"f":null},{"v":12,"f":null},{"v":false}]},
{"c":[{"v":"Week 20","f":null},{"v":24,"f":null},{"v":20,"f":null},{"v":16,"f":null},{"v":14,"f":null},{"v":false}]}
]
})
EDIT for adding color
This is my attempt at specifying color in the JSON input for the chart. However, I am unable to get the color of choice. Kindly advise on how I can specify the color to add.
res.send({
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Series 1 solid","pattern":"","type":"number"},
{"id":"","label":"Series 2 solid","pattern":"","type":"number"},
{"id":"","label":"Series 1 dotted","pattern":"","type":"number"},
{"id":"","role":"certainty","type":"boolean"},
{"id":"","label":"Series 2 dotted","pattern":"","type":"number"},
{"id":"","role":"certainty","type":"boolean"},
{"id":"","role":"style"}
],
"rows": [
{"c":[{"v":"Week 17"},{"v":6},{"v":5},{"v":4},{"v":false},{"v":3},{"v":false},{"color":"red"}]},
{"c":[{"v":"Week 18"},{"v":12},{"v":11},{"v":8},{"v":false},{"v":7},{"v":false},{"color":"red"}]},
{"c":[{"v":"Week 19"},{"v":18},{"v":15},{"v":12},{"v":false},{"v":12},{"v":false},{"color":"red"}]},
{"c":[{"v":"Week 20"},{"v":24},{"v":20},{"v":16},{"v":false},{"v":14},{"v":false},{"color":"red"}]}
]
})

to add multiple lines, add multiple columns to the data table
the data table should have one column for the x-axis,
each additional column will be for the y-axis
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"}, // x-axis
{"id":"","label":"Slices 1","pattern":"","type":"number"}, // y-axis - series 0 - line 1
{"id":"","label":"Slices 2","pattern":"","type":"number"}, // y-axis - series 1 - line 2
{"id":"","label":"Slices 3","pattern":"","type":"number"}, // y-axis - series 2 - line 3
],
"rows": [
{"c":[{"v":"Mushroooooms","f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":5,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null},{"v":2,"f":null},{"v":3,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":4,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null},{"v":2,"f":null},{"v":3,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":4,"f":null}]}
]
}
you can also add columns for roles, such as style or certainty
the role will be applied to the series column it follows
the certainty role will change lines to dotted when the role value is false,
in the following example, the third line will be dotted...
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"}, // x-axis
{"id":"","label":"Slices","pattern":"","type":"number"}, // y-axis - series 0 - line 1
{"id":"","label":"Slices","pattern":"","type":"number"}, // y-axis - series 1 - line 2
{"id":"","label":"Slices","pattern":"","type":"number"}, // y-axis - series 2 - line 3
{"id":"","role":"certainty","type":"boolean"}, // certainty role - false = dotted
],
"rows": [
{"c":[{"v":"Mushroooooms","f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":5,"f":null},{"v":false}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":false}]},
{"c":[{"v":"Olives","f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":false}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":false}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":false}]}
]
}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable({
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"}, // x-axis
{"id":"","label":"Slices","pattern":"","type":"number"}, // y-axis - series 0 - line 1
{"id":"","label":"Slices","pattern":"","type":"number"}, // y-axis - series 1 - line 2
{"id":"","label":"Slices","pattern":"","type":"number"}, // y-axis - series 2 - line 3
{"id":"","role":"certainty","type":"boolean"}, // certainty role - false = dotted
],
"rows": [
{"c":[{"v":"Mushroooooms","f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":5,"f":null},{"v":false}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":false}]},
{"c":[{"v":"Olives","f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":false}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":false}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null},{"v":3,"f":null},{"v":4,"f":null},{"v":false}]}
]
});
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
to add colors, use the colors configuration option...
colors: ['cyan', 'magenta', 'lime', 'yellow']
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable({
"cols": [
{"id":"","label":"x","pattern":"","type":"string"}, // x-axis
{"id":"","label":"y0","pattern":"","type":"number"}, // y-axis - series 0 - line 1
{"id":"","label":"y1","pattern":"","type":"number"}, // y-axis - series 1 - line 2
{"id":"","label":"y2","pattern":"","type":"number"}, // y-axis - series 2 - line 3
{"id":"","role":"certainty","type":"boolean"}, // certainty role - false = dotted
{"id":"","label":"y3","pattern":"","type":"number"}, // y-axis - series 3 - line 4
{"id":"","role":"certainty","type":"boolean"}, // certainty role - false = dotted
],
"rows": [
{"c":[{"v":"A"}, {"v":3}, {"v":4}, {"v":5},{"v":false}, {"v":6},{"v":false}]},
{"c":[{"v":"B"}, {"v":4}, {"v":5}, {"v":6},{"v":false}, {"v":7},{"v":false}]},
{"c":[{"v":"C"}, {"v":3}, {"v":4}, {"v":5},{"v":false}, {"v":6},{"v":false}]}
]
});
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, {
colors: ['cyan', 'magenta', 'lime', 'yellow']
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

Related

Google Charts display options

My questions (short style)
Can you customize a y-axis labeling interval ?
Can you display the extreme values of a series as an horizontal line ?
The detailed explanations
I have a combo chart made with Google Charts : the first set of data uses an area style, and the second a line style. The second one is the one that matters here :
it represents a percentage
i don't want it from 0 to 1 (or 0 to 100 in percentage), but from its min to its max (or something near)
and i want to display those min and max values
If i modify the scale so :
PHP
$min_reject_percentage = 5 * floor($min_reject_percentage / 5);
$max_reject_percentage = 5 * ceil($max_reject_percentage / 5);
JS
var options = {
...
vAxes: {
...
1: {
format:"##%",
viewWindow: {
min: <?php echo ($min_taux_rejet / 100); ?>,
max: <?php echo ($max_taux_rejet / 100); ?>,
},
},
},
series: {
0: {
targetAxisIndex: 0,
type: 'area',
},
1: {
targetAxisIndex: 1,
type: 'line',
},
}
}
The vertical axis is limited to the nearest multiple of 5 for min and max values, but :
the interval shown on the axis is from 10 to 10, which is too big. Since i have a real max of 31.5 and a real min of 17.1, axis min is 15 is 15 and axis max is 35, but the only graduation labeled are 20 and 30.
i can't see the real min and max on the graph
you can use config option ticks, which is an array of values to be used for the labels...
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['x', 'y0', 'y1'],
[0, 18, 0.171],
[1, 28, 0.315],
]);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var axisMin = 0.15;
var axisMax = 0.35;
var ticks = [];
for (var i = axisMin; i <= axisMax; i = i + 0.05) {
ticks.push(i);
}
var options = {
vAxes: {
1: {
format: '##%',
ticks: ticks,
viewWindow: {
min: axisMin,
max: axisMax,
},
},
},
series: {
0: {
targetAxisIndex: 0,
type: 'area',
},
1: {
targetAxisIndex: 1,
type: 'line',
},
}
};
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Issue when using ticks option on h-axis: google charts

I am using google chart's combo stacked bar. I want to display explicit values for h-axis, so I used ticks option. But, after using this option, my graph is displaying all bar values at once along the h-axis. But, I want to limit the bars to let's say 5 at a time and display the rest dynamically by sliding the charRangeFilter. The bars are almost appearing as straight lines when I use ticks option as shown in the image. Could some help me in resolving this issue? Any help is highly appreciated
Below is the code for the chart options
let hTicks = new Array<Date>(); // I already populated this array with Dates
let chart = new google.visualization.ChartWrapper({
'chartType': 'ComboChart',
'containerId': 'chartRangeFilter_chart_div',
'options': { .
// Use the same chart area width as the control for . axis alignment.
'width': '100%',
'height': '100%',
'hAxis': { 'title': 'DATES', 'gridlines': { 'color': 'transparent' }, 'ticks': hTicks, 'slantedText': true, 'slantedTextAngle': hTicks.length <= 10 ? 45 : 90 },
'legend': 'none',
'vAxis': { 'title': 'CABINS', 'viewWindow': { 'min': 0, 'max': (Number(cabinTotal) * 1.5) } },
'seriesType': 'bars',
'chartArea': { 'height': '50%', 'width': '85%' },
'bar': { 'groupWidth': '15%' },
'series': { 4: { 'type': 'line' } },
'isStacked': true,
'colors': ['#95d631', '#d7d7d7', '#f44336', '#00bcd4', '#ffc107']
}
Below are the charts
Chart with Tick option Chart Without Tick option
Solved by updating the ticks dynamically as the user slides the chartFilter

Google Gauge Chart - Add Text Label for each gauge section

I am trying to use Google gauge charts in my page. Now I want to add text in the chart for each section or colors. Can I do that inside the chart. I tried doing some html modification but with no help.
Trying from this link -https://developers.google.com/chart/interactive/docs/gallery/gauge
So for example, I would like to add text1 for white color and so on.
use the majorTicks config option to provide labels for the major ticks
see following working snippet...
google.charts.load('current', {
callback: function () {
var container = document.getElementById('chart_div');
var chart = new google.visualization.Gauge(container);
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Memory', 80],
['CPU', 55],
['Network', 68]
]);
var options = {
width: 600, height: 200,
redFrom: 90, redTo: 100,
yellowFrom:75, yellowTo: 90,
majorTicks: ['A', 'B', 'C', 'D', 'E'],
minorTicks: 3
};
chart.draw(data, options);
},
packages: ['gauge']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Google Charts area chart custom tooltip not working as expected

I'm trying to add tooltips to a Google Area Chart, but I'm getting unexpected behavior. I took the Area Chart JSFiddle example and modified it to add a custom tooltip as described here. My content looks like this:
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses', { type: 'string', role: 'tooltip'}],
['2013', 1000, 400, 'wtf'],
['2014', 1170, 460, 'hithere'],
['2015', 660, 1120, 'doh'],
['2016', 1030, 540, 'moohaha']
]);
var options = {
title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#333'}},
vAxis: {minValue: 0},
focusTarget: 'category'
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
JSFiddle here: https://jsfiddle.net/accelerate/y18tb8eq/
Rather than replacing the entire tooltip content, like I expected, the 'expenses' line in the tooltip gets replaced with my tooltip data. For example, the tooltip for the first set of data looks like:
2013
Sales: 1,000
Expenses: wtf
Can someone explain to me what I'm doing wrong?
I figured it out. Which column the tooltip is in matters. I needed to put the tooltip column immediately after the first column. So my header column has to look like this:
['Year', { type: 'string', role: 'tooltip'}, 'Sales', 'Expenses']

Change X Axis using PHP data

I am using using Highcharts to show some charts. I want to change the X Axis points with data from MYSQL. I have 4 columns in my MYSQL server: ampere, Voltage, Temperature and seconds. I want to change the X AXis by using the seconds-column. The other works fine when I implement the data from MySQL like so:
series: [{
name: 'Ampere',
data: [<?php echo join($Ampere_array, ',') ?>]
}, {
name: 'Voltage',
data: [<?php echo join($Voltage_array, ',') ?>]
}, {
name: 'Temperatuur',
data: [<?php echo join($Temperatuur_array, ',') ?>]
}]
How can I change the X Axis? I tried using the same method as the other 3 MYSQL data. BTW I am using a basic area chart.
The X Axis should return the seconds of the type double, but the code returns nothing.
xAxis: {
allowDecimals: true,
categories: {
[<?php echo join($Seconden_array, ',') ?>]
},
style: {
color: 'white'
}
}
},