From a research, I got that the tooltip that appears on Gannt Google Chart is not customizable, so I decided to override it capturing the hover event triggered by my gannt's rectangles as follow:
google.visualization.events.addListener(chart, 'onmouseover', function (e) {
openTooltip(data, e.row);
});
But now the problem is that the default tooltip popup still opens, how can I disable the default tooltip? (chart options/ custom jquery)
there aren't any options to remove the default tooltip on a Gantt chart
the tooltip is drawn using svg elements, which are added dynamically when you hover a bar
we can use a MutationObserver to know when the tooltip has been added
we can't remove the tooltip because it will break the chart
but we can make it invisible / all the elements transparent
see following working snippet...
google.charts.load('current', {
packages:['gantt']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
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([
['Research', 'Find sources',
new Date(2015, 0, 1), new Date(2015, 0, 5), null, 100, null],
['Write', 'Write paper',
null, new Date(2015, 0, 9), daysToMilliseconds(3), 25, 'Research,Outline'],
['Cite', 'Create bibliography',
null, new Date(2015, 0, 7), daysToMilliseconds(1), 20, 'Research'],
['Complete', 'Hand in paper',
null, new Date(2015, 0, 10), daysToMilliseconds(1), 0, 'Cite,Write'],
['Outline', 'Outline paper',
null, new Date(2015, 0, 6), daysToMilliseconds(1), 100, 'Research']
]);
var options = {
height: 275
};
var container = document.getElementById('chart_div');
var chart = new google.visualization.Gantt(container);
google.visualization.events.addOneTimeListener(chart, 'ready', function () {
var observer = new MutationObserver(function (nodes) {
Array.prototype.forEach.call(nodes, function(node) {
if (node.addedNodes.length > 0) {
Array.prototype.forEach.call(node.addedNodes, function(addedNode) {
if ((addedNode.tagName === 'rect') && (addedNode.getAttribute('fill') === 'white')) {
addedNode.setAttribute('fill', 'transparent');
addedNode.setAttribute('stroke', 'transparent');
Array.prototype.forEach.call(addedNode.parentNode.getElementsByTagName('text'), function(label) {
label.setAttribute('fill', 'transparent');
});
}
});
}
});
});
observer.observe(container, {
childList: true,
subtree: true
});
});
chart.draw(data, options);
function daysToMilliseconds(days) {
return days * 24 * 60 * 60 * 1000;
}
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Related
I Have tried all the links but no one works with my code
I am trying to add a button to google chart hover tooltip
this is my code
result is data that im bringing to chart
my data need to be grouped all works fine but how i can add a button in
bottom of my tooltip
see the image
`<script>
google.charts.load('current', {packages: ['corechart']});
function drawChart() {
// Define the chart to be drawn.
var data = google.visualization.arrayToDataTable(result, true);
var groupData = google.visualization.data.group(
data,
[1, 0, 2], // group on both columns, age group first
[{
column: 1,
aggregation: google.visualization.data.count,
type: 'number'
}]
);
var view = new google.visualization.DataView(groupData);
// init column arrays
// use age group as first column
var viewColumns = [0];
var aggColumns = [];
// build view & agg columns for each gender
groupData.getDistinctValues(1).forEach(function (surveyName, index) {
// add view column for each gender
viewColumns.push({
calc: function (dt, row) {
if (dt.getValue(row, 1) === surveyName) {
return dt.getValue(row, 2);
}
return null;
},
label: surveyName,
type: 'number'
});
// add agg column
aggColumns.push({
aggregation: google.visualization.data.sum,
column: index + 1,
label: surveyName,
type: 'number'
});
});
// set view columns
view.setColumns(viewColumns);
// agg view by age group
var aggData = google.visualization.data.group(
view,
[0],
aggColumns
);
var options = {
title: '',
titleTextStyle: {
color: '#98D015',
fontName: 'Baloo Paaji 2',
fontSize: '25',
margin: 10,
},
titlePosition: 'center',
colors: ['#B42987','3E5F27','#A58C21','#7030A0','#C59EE2','#243254','#F8CBAD','#00B0F0','#B4C7E7','#FFC000'],
theme: 'material',
legend: {
maxLines: 2,
position: 'bottom'
},
chartArea: {top: 20 , width: '80%', height: '60%'},
tooltip: {
isHtml: true,
trigger: 'both'
},
};
// Instantiate and draw the chart.
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(aggData, options);
}
google.charts.setOnLoadCallback(drawChart);
`
this is what i am trying to achieve
enter image description here
i am using Google Visulization Charts (donut Charts )
The Chart has got tool Tips
On CLick of a Button can we show tool tips dynamically ??
google.load("visualization", "1", {
packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task');
data.addColumn('number', 'Hours per Day');
data.addRows([
['A', roundNumber(11 * Math.random(), 2)],
['B', roundNumber(2 * Math.random(), 2)],
['C', roundNumber(2 * Math.random(), 2)],
['D', roundNumber(2 * Math.random(), 2)],
['E', roundNumber(7 * Math.random(), 2)]
]);
var options = {
width: 450,
height: 300,
colors: ['#ECD078','#ccc','#C02942','#542437','#53777A'],
legend: {position:'none'},
pieHole: 0.4,
animation: {duration:800,easing:'in'}
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
function roundNumber(num, dec) {
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
}
$(document).ready(function(){
$("#button").click(function(){
alert('show tool tips')
});
});
http://jsfiddle.net/kcjr90sx/1/
first, need to use the latest version of google charts,
the jsapi loader is outdated, and should no longer be used.
use instead --> <script src='https://www.gstatic.com/charts/loader.js'></script>
this will only change the load statement.
see following snippet and update library loader code for more info.
next, we can use the following option to allow tooltips to be shown when a slice is selected...
tooltip: {
trigger: 'both'
}
and we can use the following option to select multiple slices...
selectionMode: 'multiple',
then, when we click the button, we select the slices, which displays the tooltips.
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task');
data.addColumn('number', 'Hours per Day');
data.addRows([
['A', roundNumber(11 * Math.random(), 2)],
['B', roundNumber(2 * Math.random(), 2)],
['C', roundNumber(2 * Math.random(), 2)],
['D', roundNumber(2 * Math.random(), 2)],
['E', roundNumber(7 * Math.random(), 2)]
]);
var options = {
width: 450,
height: 300,
colors: ['#ECD078','#ccc','#C02942','#542437','#53777A'],
legend: {position:'none'},
pieHole: 0.4,
animation: {duration:800,easing:'in'},
selectionMode: 'multiple',
tooltip: {
trigger: 'both'
}
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'ready', function () {
$("#button").click(function(){
var selection = [];
$.each(new Array(data.getNumberOfRows()), function (rowIndex) {
if (data.getValue(rowIndex, 1) > 0) {
selection.push({row: rowIndex});
}
});
chart.setSelection(selection);
});
});
chart.draw(data, options);
});
function roundNumber(num, dec) {
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://www.gstatic.com/charts/loader.js'></script>
<div id="button" data-role="button">Show Tool Tips</div>
<div id='chart_div'></div>
note: you should also wait for the chart's 'ready' event,
before assigning the button click event.
otherwise, an error will be thrown.
elow you will find my grouped and stacked barchart, this works so far fine. But how can I add the information of the grouped value.
If I roll over each block I get the value of this block, but I need the cumulation of the blocks.
Example: Block1: 1.000 Block2: 1.500 Block3: 1.000 Block4: 2.000
If I roll over the third block the value must be 3.500.
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {'packages': ['bar']});
google.setOnLoadCallback(drawStuff);
function drawStuff() {
var data = new google.visualization.DataTable();
data.addColumn('string', '');
data.addColumn('number', 'level 1');
data.addColumn('number', 'level 2');
data.addColumn('number', 'level 3');
data.addColumn('number', 'level 4');
data.addColumn('number', 'current value');
data.addRows([
['Team1', {!Team1_l1}, {!Team1_l2}, {!Team1_l3}, {!Team1_l4}, {!Team1_cv}],
['Team2', {!Team2_l1}, {!Team2_l2}, {!Team2_l3}, {!Team2_l4}, {!Team2_cv}],
]);
var options = {
isStacked: true,
width: 890,
height: 500,
backgroundColor: '#F8F8F8',
chartArea: { backgroundColor: '#F8F8F8' },
chart: {
subtitle: 'current view of the incentive'
},
vAxis: {
format: 'decimal',
viewWindow: {
min: 0,
max: 30000000
}
},
series: {
4: { targetAxisIndex: 1 },
5: { targetAxisIndex: 1 }
}
};
var chart = new google.charts.Bar(document.getElementById('chart_div'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
</script>
<div id="chart_div"></div>
only way to change the tooltip is to provide a custom one using a tooltip column role
however, column roles are not support by Material charts (along with many other options)
google.charts.Bar --- packages: ['bar']
using a Classic chart is the only option...
google.visualization.ColumnChart --- packages: ['corechart']
to aggregate the values of each stack, provide a custom tooltip
use a DataView and the setColumns() method to dynamically add columns for the tooltips
to use custom html tooltip, must set property --> html: true -- on the column,
and set chart option --> tooltip: {isHtml: true}
there is a minor bug with DataView, it does not respect column properties
must convert back to a DataTable before drawing --> dataView.toDataTable()
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', '');
data.addColumn('number', 'level 1');
data.addColumn('number', 'level 2');
data.addColumn('number', 'level 3');
data.addColumn('number', 'level 4');
data.addColumn('number', 'current value');
data.addRows([
['Team1', 1000, 1500, 1000, 2000, 1800],
['Team2', 2000, 2500, 2000, 3000, 2800]
]);
var options = {
isStacked: true,
width: 890,
height: 500,
backgroundColor: '#F8F8F8',
chartArea: {
backgroundColor: '#F8F8F8'
},
chart: {
subtitle: 'current view of the incentive'
},
colors: ['#2196f3', '#42a5f5', '#64b5f6', '#90caf9', '#bbdefb'],
tooltip: {
isHtml: true
},
vAxis: {
format: 'decimal',
viewWindow: {
min: 0,
max: 15000
}
}
};
// number formatter
var formatNumber = new google.visualization.NumberFormat({
pattern: options.vAxis.format
});
// build data view columns
var viewColumns = [];
for (var col = 0; col < data.getNumberOfColumns(); col++) {
addColumn(col);
}
function addColumn(col) {
// add data table column
viewColumns.push(col);
// add tooltip column
if ((col > 0) && (col < (data.getNumberOfColumns() - 1))) {
viewColumns.push({
type: 'string',
role: 'tooltip',
calc: function (dt, row) {
// calculate aggregate
var aggregateValue = 0;
for (var aggCol = 1; aggCol <= col; aggCol++) {
aggregateValue += dt.getValue(row, aggCol);
}
// build custom tooltip
var tooltip = '<div class="ggl-tooltip"><div><span>';
tooltip += dt.getFormattedValue(row, 0) + '</span></div>';
tooltip += '<div>' + dt.getColumnLabel(col) + ': ';
tooltip += '<span>' + formatNumber.formatValue(aggregateValue) + '</span></div></div>';
return tooltip;
},
p: {html: true}
});
}
}
var dataView = new google.visualization.DataView(data);
dataView.setColumns(viewColumns);
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
// use data view to draw chart
chart.draw(dataView.toDataTable(), options);
});
.ggl-tooltip {
background-color: #ffffff;
border: 1px solid #e0e0e0;
font-family: Arial, Helvetica;
font-size: 14px;
padding: 12px 12px 12px 12px;
}
.ggl-tooltip div {
margin-top: 4px;
}
.ggl-tooltip span {
font-weight: bold;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
note: jsapi should no longer be used to load the charts library,
according to the release notes...
The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. The last update, for security purposes, was with a pre-release of v45. Please use the new gstatic loader.js from now on.
this will only change the load statement, see above snippet...
If I use date values for the hAxis of a Google Charts column chart, the labels appear below the bar group divider (see pic below). This does not happen if I use strings.
Has anyone figured out how to make the date label to appear centered below each bar group?
you can provide your own ticks for the hAxis option...
use the dates from the data for each tick / label...
var ticks = [];
for (var i = 0; i < data.getNumberOfRows(); i++) {
ticks.push(data.getValue(i, 0));
}
then assign in the options...
hAxis: {
ticks: ticks
},
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'x');
data.addColumn('number', 'y0');
data.addColumn('number', 'y1');
data.addRows([
[new Date(2017, 07, 08), 200, 210],
[new Date(2017, 07, 15), 190, 220],
[new Date(2017, 07, 22), 205, 200]
]);
var ticks = [];
for (var i = 0; i < data.getNumberOfRows(); i++) {
ticks.push(data.getValue(i, 0));
}
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, {
chartArea: {
bottom: 24,
height: '100%',
left: 48,
right: 96,
top: 24,
width: '100%'
},
hAxis: {
ticks: ticks
},
height: '100%',
title: 'Title',
width: '100%'
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
I've alreayd checked here:
https://groups.google.com/forum/#!topic/google-visualization-api/4qCeUgE-OmU
https://developers.google.com/chart/interactive/docs/querylanguage#Format
https://developers.google.com/chart/interactive/docs/reference#numberformatter
I want to display the number on the x-axis in a graph drawn with Google Charts with no decimals and a . as the thousand separator.
var options = {
hAxis: {
title: 'Month'
},
vAxis: {
title: 'Price (€)', format: '#.###'
}
};
So 3200 should be shown as 3.200
I've already tried for the format variable:
#.#
#.###
#
since the format option will not work in this situation,
you can provide your own ticks for vAxis...
use object notation for each tick
{
v: value,
f: formattedValue
}
use google's NumberFormat class...
var formatNumber = new google.visualization.NumberFormat({
fractionDigits: 0,
groupingSymbol: '.'
});
format each tick using the formatValue method
see following working snippet...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y');
data.addRows([
[1, 3000],
[2, 3100],
[3, 3200],
[4, 3300],
[5, 3400],
]);
var formatNumber = new google.visualization.NumberFormat({
fractionDigits: 0,
groupingSymbol: '.'
});
var columnRange = data.getColumnRange(1);
var ticks = [];
for (var i = columnRange.min; i <= columnRange.max; i=i+100) {
ticks.push({
v: i,
f: formatNumber.formatValue(i)
});
}
var options = {
hAxis: {
title: 'Month'
},
vAxis: {
ticks: ticks,
title: 'Price (€)'
}
};
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data, options);
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>