Show line legend labels inside google chart - google-visualization

I'd like to show labels next to each line in a google chart line graph. Can someone point me to the options I should be setting? anything to do with annotations?

you can use annotations to label a point in a series.
annotations are added as a column.
data.addColumn({role: 'annotation', type: 'string'});
each annotation column should follow the value column it represents.
looking at the image in the question, it appears you only want one annotation, or label, per series.
as such, set the value for the annotation column to null, except for the last row.
see following example...
annotations have a few settings that can be customized, see the configuration options for the particular chart...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Week');
data.addColumn('number', 'NO');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'UK');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'NL');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'DK');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'SE');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'FI');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'IL');
data.addColumn({role: 'annotation', type: 'string'});
data.addColumn('number', 'US 2014');
data.addColumn({role: 'annotation', type: 'string'});
var currentWeek = 27;
var ticks = [];
for (var i = 0; i < 52; i++) {
ticks.push(i + 1);
var row = [];
if (i < currentWeek) {
row.push(i + 1);
for (var x = 1; x < data.getNumberOfColumns(); x=x+2) {
var value = x * (Math.pow(i, 3));
var annot = null;
if (i === (currentWeek - 1)) {
annot = data.getColumnLabel(x) + ' - ' + value;
}
row.push(value, annot);
}
data.addRow(row);
}
}
new google.visualization.LineChart(document.getElementById('chart_div')).draw(data, {
annotations: {
stem: {
length: 0
}
},
chartArea: {
width: '80%'
},
hAxis: {
format: 'Week 0',
ticks: ticks
},
height: 600
});
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

Google Chart bars half the correct height in wkhtmltopdf

My Google Chart works perfectly in the browser:
Works perfectly
However, when I try to render this as a PDF using:
wkhtmltopdf --javascript-delay 2000 --print-media-type chart_dummy.html chart_dummy.pdf
the heights of the bars get halved, and the colour code goes missing:
Rendered PDF with bars half height
I take it there must be some wkhtmltopdf option to correct this, but I can't for the life of me identify one.
I'm using Ubuntu 16.04.7 LTS (Xenial Xerus), and wkhtmltopdf 0.12.5 (with patched qt).
My visualization function is
function drawVisualization() {// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'focus name');
data.addColumn('number', 'Exceeded');
data.addColumn('number', 'Achieved');
data.addColumn('number', 'Not Achieved');
data.addColumn('number', 'Regressed');
var width = $($('.container-fluid')[0]).width();
var height = $($('.container-fluid')[0]).height();
var options = {
'width': width,
'height': height,
'chartArea': {top:10},
title: String(),
vAxis: {
title: '% Progress',
minValue: 0,
maxValue: 100,
format: '#\'%\'',
baseline: 0,
},
hAxis: {
title: ''
},
seriesType: 'bars',
series: {
5: {
type: 'line'
}
},
colors: ['#ffe400', '#03ea74', '#ffa000', '#0095ff'],
annotations: {
alwaysOutside: true,
textStyle: {
fontSize: 20,
auraColor: '#eee',
color: '#eee'
}
},
animation: {
duration: 2000,
easing: 'linear',
startup: true
},
legend: 'bottom'
};
reportData = {
"combined": {
"exceeded": "34",
"achieved": "72",
"not_achieved": "14",
"regressed": "8"
}
};
data.addRows([
['All Focus Areas', Number(reportData['combined']['exceeded']), Number(reportData['combined']['achieved']), Number(reportData['combined']['not_achieved']), Number(reportData['combined']['regressed'])]
]);
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
Has anyone a clue how to make the PDF the same as the Browser rendering?

Count number of rows after filter in Google Visualisation Table

I have implement various controls in a Google Visualisation Table for multiple filter: CategoryFilter, StringFilter and NumberRangeFilter like this exemple
http://jsfiddle.net/asgallant/Ena84/
Is it possible know the number of rows visualised in table after every filter settings?
Thank you.
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('string', 'Team');
data.addColumn('string', 'Nationality');
data.addColumn('number', 'Height');
data.addColumn('number', 'Weight');
data.addColumn('number', 'OverallRating');
data.addColumn('string', 'Foot');
data.addRows([
['MESSI','FC BARCELONA','ARGENTINA',169,67,25,'Left foot'],
['A. INIESTA','FC BARCELONA','SPAIN',170,65,28,'Right foot'],
['XAVI','FC BARCELONA','SPAIN',170,68,32,'Right foot']
]);
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard'));
var stringFilter = new google.visualization.ControlWrapper({
controlType: 'StringFilter',
containerId: 'string_filter_div',
options: {
filterColumnIndex: 0
}
});
var numberRangeFilter = new google.visualization.ControlWrapper({
controlType: 'NumberRangeFilter',
containerId: 'numnber_range_filter_div',
options: {
filterColumnIndex: 5,
minValue: 0,
maxValue: 100,
ui: {
label: 'Overall Rating'
}
}
});
var table = new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'table_div',
options: {
showRowNumber: true
}
});
dashboard.bind([stringFilter, numberRangeFilter], [table]);
dashboard.draw(data);
}
google.load('visualization', '1', {packages:['controls'], callback: drawTable});
once the 'ready' event fires on any of the controls, charts, or dashboard
you can use the getDataTable method on any of the controls or charts
to getNumberOfRows
see following working snippet...
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('string', 'Team');
data.addColumn('string', 'Nationality');
data.addColumn('number', 'Height');
data.addColumn('number', 'Weight');
data.addColumn('number', 'OverallRating');
data.addColumn('string', 'Foot');
data.addRows([
['MESSI','FC BARCELONA','ARGENTINA',169,67,25,'Left foot'],
['A. INIESTA','FC BARCELONA','SPAIN',170,65,28,'Right foot'],
['XAVI','FC BARCELONA','SPAIN',170,68,32,'Right foot']
]);
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard'));
var stringFilter = new google.visualization.ControlWrapper({
controlType: 'StringFilter',
containerId: 'string_filter_div',
options: {
filterColumnIndex: 0
}
});
var numberRangeFilter = new google.visualization.ControlWrapper({
controlType: 'NumberRangeFilter',
containerId: 'numnber_range_filter_div',
options: {
filterColumnIndex: 5,
minValue: 0,
maxValue: 100,
ui: {
label: 'Overall Rating'
}
}
});
var table = new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'table_div',
options: {
showRowNumber: true
}
});
dashboard.bind([stringFilter, numberRangeFilter], [table]);
google.visualization.events.addListener(dashboard, 'ready', function () {
// get number of rows
console.log(table.getDataTable().getNumberOfRows());
});
dashboard.draw(data);
}
google.load('visualization', '1', {packages:['controls'], callback: drawTable});
<script src="https://www.google.com/jsapi"></script>
<div id="string_filter_div"></div>
<div id="numnber_range_filter_div"></div>
<div id="table_div"></div>

google chart All series on a given axis must be of the same data type

I'm drowing a bar chart with dynamic data.
I couldn't figure out why do i still get the data type error.
This is my function:
function drawBasic(object,id,title) {
var array = ['publisher','price','date'];
var data = new google.visualization.DataTable();
for(var j = 0; j < array.length; j++){
if(array[j] == 'publisher'){
data.addColumn('string', array[j]);
} else if (array[j] == 'revenue'){
data.addColumn('number', array[j]);
} else if(array[j] == 'date'){
data.addColumn('date', 'X');
}
}
$.each(object,function (key,val) {
var temp = [];
var outterArray = [];
var day = new Date(key);
for(var y = 0; y < val.length; y++){
for(var x = 0; x < array.length; x++){
if(array[x] =='publisher'){
temp.push(val[y][array[x]]);
} else if(array[x] != 'date' && array[x] !='publisher'){
temp.push(parseObjToNum(val[y][array[x]]));
}
}
temp.push(day);
outterArray.push(temp);
temp = [];
}
data.addRows(outterArray);
});
var options = {
title: title,
hAxis: {
"format": "y-MM-d"
}
};
var chart = new google.visualization.ColumnChart(document.getElementById(id));
var formatter = new google.visualization.NumberFormat({fractionDigits: true});
formatter.format(data, 1, 2, 3);
chart.draw(data, options);
}
The column is structured as: publisher type string, price type number and X type date.
Row single data example:
['josh the great',12, Tue Sep 20 2016 03:00:00 GMT+0300 (IDT)]
The data is ordered according to the column.
I'm aiming to present for each date the set of publishers and their price.
I don't mind representing it in a different chart but i don't think it's the problem.
Any suggestions will be highly appreciated.
each chart type has a specific Data Format
for Bar Charts, and most others, a 'date' column is only valid as the first column
it cannot be a series column -- which can only be numbers
to display the date in the chart, recommend using a Column Role
using either 'annotation' or 'tooltip'
see following working snippet...
google.charts.load('current', {
callback: function () {
var container = document.getElementById('chart_div');
var chart = new google.visualization.BarChart(container);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({type: 'string', label: 'Publisher'});
dataTable.addColumn({type: 'number', label: 'Price'});
dataTable.addColumn({type: 'string', role: 'annotation'});
dataTable.addRows([
['josh the great', 12, 'Tue Sep 20 2016 03:00:00 GMT+0300 (IDT)'],
]);
chart.draw(dataTable);
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Column data label on google bar chart

I am trying to add a column data label to a google bar chart.
I have followed the instructions given in the API docs, and this is what I get:
google.charts.load('current', {'packages':['bar']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Searches'],
['1998', 9800],
['2000', 60000000],
['2007', 1200000000],
['2008', 1745000000],
['2009', 2610000000],
['2010', 3627000000],
['2011', 4717000000],
['2012', 5134000000],
['2013', 5922000000],
['2014', 5740000000]
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{ calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation" }]);
var options = {
chart: {
title: 'Google searches',
subtitle: 'Average searches per day 1998-2014',
}
};
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
chart.draw(view, options);
}
I must be doing something wrong as the chart fails to render any data labels.
What am I doing wrong?
https://jsfiddle.net/q9edfpte/1/
I did a number of changes*, and this one ultimately worked.
*Changes:
- 'packages':['bar'] --> 'packages':['corechart']
- google.charts.Bar --> google.visualization.ColumnChart
Working version (though needs beautifying):
google.charts.load("current", {packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Searches'],
['1998', 9800],
['2000', 60000000],
['2007', 1200000000],
['2008', 1745000000],
['2009', 2610000000],
['2010', 3627000000],
['2011', 4717000000],
['2012', 5134000000],
['2013', 5922000000],
['2014', 5740000000]
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{ calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation" }]);
var options = {
title: 'Google searches',
subtitle: 'Average searches per day 1998-2014',
};
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart_values'));
chart.draw(view, options);
}

Add additional data to google chart dataTable

I'm using google scatterchart to show where defects are on a surface. All defects have an ID, when I click on a point I want an event to fire from where I can get that ID and do other stuff with it.
In google chart one can wire an selecthandler from where I can get what's selected, but how do I add an ID (or any other data) in the dataTable to a point without it being displayed?
For example:
var dataTable = new google.visualization.DataTable();
dataTable.addColumn('number', 'Width');
dataTable.addColumn('number', 'Yellow');
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
dataTable.addColumn('number', 'Red');
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
dataTable.addColumn('number', 'Id'); <-- this doesn't work
The last column I want to add the ID of the defect and retrieve it via the selectHandler.
dataTable.addRow([123, 123, 'some tooltip', null, null, 999]);
Here I added ID 999 to the table. But I don't want the chart to display it. How do I add additional (hidden) data to a point?
You can add extra columns containing any information you want, and then hide them with a DataView:
var dataTable = new google.visualization.DataTable();
dataTable.addColumn('number', 'Width');
dataTable.addColumn('number', 'Yellow');
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
dataTable.addColumn('number', 'Red');
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
dataTable.addColumn('number', 'Id');
// populate dataTable
var view = new google.visualization.DataView(dataTable);
// exclude column 5 (Id)
view.setColumns([0, 1, 2, 3, 4]);
Then draw your chart using the DataView instead of the DataTable.
setRowProperties(rowIndex, properties) would work in this case, remove Id column and add dataTable.setRowProperties(0,{'id':'999'});
You can get the property of the row by using getRowProperties(rowIndex);