Google Piechart Tooltip when enableInteractivity disabled - charts

I want to show Google Pie Chart with tooltip. But, interactivity (like click) not required.
But, enableInteractivity = false removes toolTip by default. Is there anyway to show toolTip when enableInteractivity = false

enableInteractivity must be true for tooltips to work
but you can "cancel" the 'select' event by setting an empty selection
chart.setSelection([]);
google.charts.load('current', {
callback: function () {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Percent', 10],
['Rest', 90],
]);
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'select', function () {
chart.setSelection([]);
});
chart.draw(data);
},
packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

Google Line Charts. Remove extra gridlines or change their color

I've created google line chart with the following settings:
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawCharts);
function drawCharts() {
var options = {
backgroundColor:{fill:'transparent'},
legend:'none',
series:{0:{color:'#aa8e57'}},
lineWidth:4,
pointSize:7,
chartArea:{width: '86%'},
hAxis:{
textStyle:{color:'#ffffff',fontSize:12},
gridlines:{color:'#2a261d'},
baselineColor:'#b19c72'
},
vAxis:{
textStyle:{color:'#ffffff',fontSize:12},
gridlines:{color:'#2a261d'},
baselineColor:'#b19c72'
},
};
var dataMembers = new google.visualization.DataTable();
dataMembers.addColumn('string', 'Date');
dataMembers.addColumn('number', 'Users');
dataMembers.addRows([
['13.11.2018',5], ['14.11.2018',7], ['15.11.2018',10]
]);
var membersChart = new google.visualization.LineChart(document.getElementById('membersChart'));
membersChart.draw(dataMembers, options);
}
I read all docs and cant find out how to customize those white extra lines:
I've done a lot of experiments, but i can't find out how they appears and how to remove them from the chart. Or at least, change their color to match rest of gridlines.
those are --> minorGridlines
for the same color...
minorGridlines:{color:'#2a261d'},
to remove...
minorGridlines:{count:0},
see following working snippet...
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawCharts);
function drawCharts() {
var options = {
backgroundColor:{fill:'transparent'},
legend:'none',
series:{0:{color:'#aa8e57'}},
lineWidth:4,
pointSize:7,
chartArea:{width: '86%'},
hAxis:{
textStyle:{color:'#ffffff',fontSize:12},
gridlines:{color:'#2a261d'},
minorGridlines:{color:'#2a261d'},
baselineColor:'#b19c72'
},
vAxis:{
textStyle:{color:'#ffffff',fontSize:12},
gridlines:{color:'#2a261d'},
minorGridlines:{color:'#2a261d'},
baselineColor:'#b19c72'
},
};
var dataMembers = new google.visualization.DataTable();
dataMembers.addColumn('string', 'Date');
dataMembers.addColumn('number', 'Users');
dataMembers.addRows([
['13.11.2018',5], ['14.11.2018',7], ['15.11.2018',10]
]);
var membersChart = new google.visualization.LineChart(document.getElementById('membersChart'));
membersChart.draw(dataMembers, options);
}
body {
background-color: #000000;
}
#membersChart {
height: 600px;
width: 800px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="membersChart"></div>

Google charts - setting the right locale to display values in £s

I've got a chart Google bar chart with currency values and I simply want to have the Y-axis formatted with £s. I've tried to explicitly set the locale using 'en' but it still displays as $. If I set other locales it does then display in the local currency for that locale ... but it doesn't seem to work for English £s.
google.charts.load('current', {packages:['corechart'], language: 'en'});
e.g. https://jsfiddle.net/ynsqwe6o/5/
use language code --> 'en-GB'
see following working snippet...
google.charts.load('current', {
packages: ['corechart'],
language: 'en-GB'
});
google.charts.setOnLoadCallback(drawStuff);
function drawStuff() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
data.addColumn('number', 'GDP');
data.addRows([
['US', 16768100],
['China', 9181204],
['Japan', 4898532],
['Germany', 3730261],
['France', 2678455]
]);
var options = {
chartArea: {
left: 108
},
title: 'GDP of selected countries, in \u00A3pounds',
width: 500,
height: 300,
legend: 'none',
bar: {
groupWidth: '95%'
},
vAxis: {
format: "currency",
gridlines: {
count: 4
}
}
};
var chart = new google.visualization.ColumnChart(document.getElementById('number_format_chart'));
chart.draw(data, options);
};
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="number_format_chart"></div>

Google visualisation with Chartrangeslider and data import from Google docs spreadsheet

I have the problem, that I dont manage to implement my data from a google spread sheet when using dashboard environment with chartwrapper and controlwrapper. I used the piechart example from the google charts website (https://developers.google.com/chart/interactive/docs/gallery/controls) and tried to modify without success. Maybe somebody can provide a pointer ! Thanks in advance (link to jsfiddle : https://jsfiddle.net/Chriseif/08mk90hu/1/#&togetherjs=MHq11Kn3hl)
<html>
<head>
<!--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']});
// 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() {
var query = new
google.visualization.Query("https://docs.google.com/spreadsheets/d/
1uJNf8RgPjcjm3pUWig0VL4EEww1PG-bNL8mtcxI6SYI/edit");
query.send(response);
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' '
+ response.getDetailedMessage());
return;
}
var data1 = response.getDataTable();
}
// 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': 'ChartRangeFilter',
'containerId': 'filter_div',
'options': {
'filterColumnIndex ': 2
}
});
// Create a pie chart, passing some options
var pieChart = new google.visualization.ChartWrapper({
'chartType': 'LineChart',
'containerId': 'chart_div',
'options': {
'width': 900,
'height': 300,
'pieSliceText': 'value',
'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, pieChart);
// Draw the dashboard.
dashboard.draw(data1);
}
</script>
</head>
<body>
<!--Div that will hold the dashboard-->
<div id="dashboard_div">
<!--Divs that will hold each control and chart-->
<div id="filter_div"></div>
<div id="chart_div"></div>
</div>
</body>
</html>
first, the query.send method is asynchronous, and accepts an argument for a callback function
because you need to wait for the data from the query,
all of the dashboard code should be inside the callback
else it will run before the data is returned...
// the argument should be the name of a function
query.send(handleQueryResponse);
// callback function to be called when data is ready
function handleQueryResponse(response) {
//draw dashboard here...
next, the range filter should operate on the column used for the x-axis
unless you are using a view, it will be column index 0 (zero is the first index)
var rangeSlider = new google.visualization.ControlWrapper({
controlType: 'ChartRangeFilter',
containerId: 'filter_div',
options: {
filterColumnIndex: 0 // <-- x-axis column
}
});
also, when drawing charts from a spreadsheet,
you need to set a specific number format on each axis,
otherwise, it will display the word general as part of the number...
var chart = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'chart_div',
options: {
width: 900,
height: 300,
pieSliceText: 'value',
legend: 'right',
// set number formats
hAxis: {
format: '#,##0'
},
vAxis: {
format: '#,##0'
}
}
});
see following working snippet...
google.charts.load('current', {
packages: ['corechart', 'controls']
}).then(function () {
var query = new google.visualization.Query("https://docs.google.com/spreadsheets/d/1uJNf8RgPjcjm3pUWig0VL4EEww1PG-bNL8mtcxI6SYI/edit");
query.send(handleQueryResponse);
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data1 = response.getDataTable();
var dashboard = new google.visualization.Dashboard(
document.getElementById('dashboard_div')
);
var rangeSlider = new google.visualization.ControlWrapper({
controlType: 'ChartRangeFilter',
containerId: 'filter_div',
options: {
filterColumnIndex: 0
}
});
var chart = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'chart_div',
options: {
width: 900,
height: 300,
pieSliceText: 'value',
legend: 'right',
hAxis: {
format: '#,##0'
},
vAxis: {
format: '#,##0'
}
}
});
dashboard.bind(rangeSlider, chart);
dashboard.draw(data1);
}
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dashboard_div">
<div id="filter_div"></div>
<div id="chart_div"></div>
</div>

How to give style to x-axis of google charts?

I am implementing google charts and I want to give style to x-axis data. I found there the following code to do this.
hAxis:{
title: 'Month',
textStyle :{
fontSize : 10
}
}
I want to underline the text data and change cursor style to the pointer. I searched on the google chart website but didn't get.
you can modify the text elements directly,
when the 'ready' event fires on the chart
you can select which axis labels to modify by using the 'text-anchor' attribute
x-axis labels have a value of 'middle', y-axis 'end'
see following working snippet...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Category');
data.addColumn('number', 'Value');
data.addRows([
['Category A', 200],
['Category B', 110],
['Category D', 310],
['Category E', 280],
['Category F', 175]
]);
var options = {
hAxis: {
textStyle: {
fontSize : 10
},
title: 'Month'
},
height: 400,
width: 600
};
var chartContainer = document.getElementById('chart_div');
var chart = new google.visualization.ComboChart(chartContainer);
google.visualization.events.addListener(chart, 'ready', function () {
// modify x-axis labels
var labels = chartContainer.getElementsByTagName('text');
Array.prototype.forEach.call(labels, function(label) {
if (label.getAttribute('text-anchor') === 'middle') {
label.style.textDecoration = 'underline';
label.style.cursor = 'pointer';
}
});
});
chart.draw(data, options);
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Export google chart in a dashboard as png

I am trying to export google chart in a dashboard to png image through a button. But I get the following error-
One or more participants failed to draw()
undefined is not a function
Here is the code:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the controls package.
google.load('visualization', '1.0', {'packages':['controls']});
// Set a callback to run when the Google Visualization API is loaded.
google.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([
['Name', 'Donuts eaten'],
['Michael' , 5],
['Elisa', 7],
['Robert', 3],
['John', 2],
['Jessica', 6],
['Aaron', 1],
['Margareth', 8]
]);
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(
document.getElementById('dashboard_div'));
// Create a range slider, passing some options
var select = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'filter_div',
'options': {
'filterColumnLabel': 'Donuts eaten'
}
});
// Create a pie chart, passing some options
var chart = new google.visualization.ChartWrapper({
'chartType': 'ColumnChart',
'containerId': 'chart_div',
'options': {
'width': 500,
'height': 300,
'legend': 'right'
}
});
google.visualization.events.addListener(chart, 'ready', function(e) {
document.getElementById('png').outerHTML = 'Printable version';
});
// 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(select, chart);
// Draw the dashboard.
dashboard.draw(data);
}
</script>
</head>
<body>
<div id='png'></div>
<!--Div that will hold the dashboard-->
<div id="dashboard_div">
<!--Divs that will hold each control and chart-->
<div id="filter_div"></div>
<div id="chart_div"></div>
</div>
</body>
</html>
Please help me where I am going wrong.
A fiddle always helps!
https://jsfiddle.net/kujxsn7z/
The things I changed:
Created a div to hold the PNG output Your attempt to call
getImageURI() was failing because you were applying it on a
chartWrapper, not on a chart object. So to fix this you need to call,
in your instance:
chart.getChart().getImageURI()
So you reference your chartWrapper, use getChart to refer to the chart it contains, and then execute getImageIRI() on the chart object itself.