Centering date labels on Google Charts hAxis - charts

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>

Related

Google Charts - How do I create the attached image gauge within a semicircle

I need this type of gauge chart
Image :
How do I create the above guage chart
I have the speedometer gauge working, but it doesn't meet the needs.
Is there a way within the google visualization api to use a triangle for the gauge and not the speedometer?
google.charts.load('current', {'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Memory', 80],
['CPU', 55],
['Network', 68]
]);
var options = {
width: 400, height: 120,
redFrom: 90, redTo: 100,
yellowFrom:75, yellowTo: 90,
minorTicks: 5
};
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
chart.draw(data, options);
setInterval(function() {
data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
chart.draw(data, options);
}, 13000);
setInterval(function() {
data.setValue(1, 1, 40 + Math.round(60 * Math.random()));
chart.draw(data, options);
}, 5000);
setInterval(function() {
data.setValue(2, 1, 60 + Math.round(20 * Math.random()));
chart.draw(data, options);
}, 26000);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 400px; height: 120px;"></div>

Google Chart Gannt avoid tooltip

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>

Google barchart grouped and stacked

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...

google chart show only integer on vaxis that starts with zero, equal spacing, non repeat data points

I am using google Chart:
I have to show only integer on vaxis that starts with zero, equal spacing, non repeat data points.
I tried following
vAxis: {
gridlines: {
count: -1
},
viewWindow: {
min: 0
},
format: '#'
},
but getting -1, 0, 1 data points on vaxis.
use option --> vAxis.ticks -- to show specific axis labels
see following working snippet...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y');
var ticksY = [];
for (var i = 0; i <= 20; i++) {
data.addRow([i, i]);
ticksY.push(i);
}
var options = {
legend: 'none',
vAxis: {
ticks: ticksY
},
height: 600
};
var container = document.getElementById('chart_div');
var chart = new google.visualization.ScatterChart(container);
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Google barchart side label cuts off

Right so I have my chart set up but the side labels or bar titles are cut off if they don't fit in the div of the graph, so my question is, is there anyway to overflow the bar titles of the graph(the font size is getting too small to read so I can't make it any smaller) or even wrap the text.
Well seeing as I don't think the code will help I'll show it regardless, mind you I have limited space and I am showing 2 graphs in that space. All I need is the side labels to have overflow: show(so if the specific bar label overflows the area it displays..
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>
</head>
<body>
<div>
<div id="myChart" style="width:50%"></div>
</div>
<script type="text/javascript">
$(function(){
var id = "myChart";
var chartData= [["The big label that is the issue in this case we need this to display its overflow","74","",""],["Louise","71","",""],["Louise.v.2","0","",""],["member1","72","",""],["member3","67","",""]];
var defaultColors = ["#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00",
"#b82e2e", "#316395", "#994499", "#22aa99", "#aaaa11", "#6633cc", "#e67300", "#8b0707", "#651067", "#329262",
"#5574a6", "#3b3eac", "#b77322", "#16d620", "#b91383", "#f4359e", "#9c5935", "#a9c413", "#2a778d", "#668d1c",
"#bea413", "#0c5922", "#743411"];
var counter = 0;
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', '');
data.addColumn({ type: 'string', role: "style" });
data.addColumn({ type: 'string', role: 'annotation' });
if (chartData[0][3].length > 0) {
data.addColumn('number', '');
}
data.addRows(chartData.length + 1);
for (var i = 0; i < chartData.length; i++) {
var thisItem = chartData[i];
data.setCell(i, 0, thisItem[0]);
data.setCell(i, 1, thisItem[1]);
if (thisItem[2].length > 0) {
data.setCell(i, 2, "color: #000000");
} else {
data.setCell(i, 2, "color: " + defaultColors[counter]);
}
data.setCell(i, 3, thisItem[1] + "%");
if (thisItem[3].length > 0) {
data.setCell(i, 4, thisItem[3]);
}
counter = counter + 1;
if (counter == 31) {
counter = 0;
}
}
var barChart = null;
var options = null;
barChart = new google.visualization.ComboChart(document.getElementById(id));
var fullHeight = ((chartData.length + 1) * 20) + 50;
var minHieght = 200;
options = {
height: fullHeight,
tooltip: { isHtml: true },
max: 100,
label: 'value',
orientation: 'vertical',
fontSize: 15,
width: (((($(window).width() / 3) * 2) / 5) * 3),
legend: { position: 'none' },
bar: { groupWidth: 15, width: 20 },
chartArea: { height: fullHeight - 50, width: "47%", left: "50%", top: 0 },
backgroundColor: 'transparent',
enableInteractivity: false,
legend: 'none',
seriesType: 'bars',
series: { 1: { type: 'line', lineWidth: 5, enableInteractivity: false, color: 'grey' } },
annotations: {
alwaysOutside: true
}
};
barChart.draw(data, options);
});
</script>
</body>
</html>