'XXX' below shows where I would like custom tooltip to be used - it should actually display a name from a row of data - 'XXX' is used to make the problem stand out here. This works fine on standard scatterplot but not on combo chart. Combo version displays default tooltip.
What am I missing?
Charts currently look like this...image link
`
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
callback: drawChart,
packages:['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'others_average');
data.addColumn('number', '');
// A column for custom tooltip content
data.addColumn({
type: 'string',
role: 'tooltip',
});
data.addRows([
<?php
$wa_startindex = 0;
$i=0;
while(!$chart->atEnd()) {
$wa_startindex = $chart->Index;
?>
[<?php echo($chart->getColumnVal("others_average")); ?>, <?php echo($chart->getColumnVal("band")); ?>, 'XXX']
<?php if($i<($chart->TotalRows)) echo ',';?>
<?php
$i++;
$chart->moveNext();
}
$chart->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?>
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
label: 'y1=x',
type: 'number',
calc: function (dt, row) {
return dt.getValue(row, 0)
}
}]);
var options = {
seriesType: 'scatter',
series: {
1: {
type: 'line'
}
},
hAxis: {title: 'Average Band (other courses)', direction:-1, viewWindow: { min: 1, max: 9 }, ticks: [1,2,3,4,5,6,7,8,9], viewWindowMode: "explicit"},
vAxis: {title: '<?php echo($chart->getColumnVal("course_title")); ?> Band', direction:-1, viewWindow: { min: 1, max: 9 }, ticks: [1,2,3,4,5,6,7,8,9], viewWindowMode: "explicit"},
legend: 'none'
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(view, options);
}
</script>
`
I don't find the google charts hard to modify for use with PHP/MySQL data - problem just seems to be with the combo chart implmentation..
You should also include the tooltip column (column #2) in the DataView: view.setColumns([0, 1, 2, {....
google.charts.load('current', {
callback: drawChart,
packages:['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'others_average');
data.addColumn('number', '');
// A column for custom tooltip content
data.addColumn({
type: 'string',
role: 'tooltip',
});
data.addRows([
[1, 2, 'xxx1'],
[2, 5, 'xxx2'],
[3, 1, 'xxx3'],
[4, 3, 'xxx4'],
[5, 2, 'xxx5']
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, 2, {
label: 'y1=x',
type: 'number',
calc: function (dt, row) {
return dt.getValue(row, 0)
}
}]);
var options = {
seriesType: 'scatter',
series: {
1: {
type: 'line'
}
},
hAxis: {title: 'Average Band (other courses)', direction:-1, viewWindow: { min: 1, max: 5 }, ticks: [1,2,3,4,5,6,7,8,9], viewWindowMode: "explicit"},
vAxis: {title: '... Band', direction:-1, viewWindow: { min: 1, max: 9 }, ticks: [1,2,3,4,5,6,7,8,9], viewWindowMode: "explicit"},
legend: 'none'
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(view, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div">
</div>
Related
I am looking to build the same as shown in the image, I checked chart.js documentation(https://www.chartjs.org/docs/3.0.2/) but didn't find anything like this. Is it possible to create a chart like this using chart.js?
Yes you can use the datalabels plugin to achieve what you want:
Example:
Chart.register(ChartDataLabels);
var options = {
type: 'bar',
data: {
labels: ["Category 1", "Category 2", "Category 3"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
animals: ['cat', 'dog', 'bear'],
borderWidth: 1,
backgroundColor: ['orange', 'blue', 'gray']
},
{
label: '# of Points',
data: [7, 11, 5],
animals: ['monkey', 'bird', 'fish'],
borderWidth: 1,
backgroundColor: ['yellow', 'cyan', 'purple']
}
]
},
options: {
plugins: {
title: {
display: true,
text: 'title'
},
datalabels: {
anchor: 'end', // remove this line to get label in middle of the bar
align: 'end',
formatter: (val, x) => (x.dataset.animals[x.dataIndex]),
labels: {
value: {
color: 'purple'
}
}
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels#2.0.0-rc"></script>
</body>
you can use Plotly.js for grouped chart please check below code
Reference URL : https://plotly.com/javascript/bar-charts/
var trace1 = {
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
name: 'SF Zoo',
type: 'bar'
};
var trace2 = {
x: ['giraffes', 'orangutans', 'monkeys'],
y: [12, 18, 29],
name: 'LA Zoo',
type: 'bar'
};
var data = [trace1, trace2];
var layout = {barmode: 'group'};
Plotly.newPlot('myDiv', data, layout);
<head>
<!-- Load plotly.js into the DOM -->
<script src='https://cdn.plot.ly/plotly-1.58.4.min.js'></script>
</head>
<body>
<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
I have used google charts for a web app. I want to remove its white background. I tried changing the background color to transparent, but it isn't working.
<script type="text/javascript">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
["Element", "Density", { role: "style" } ],
["LIC", 8.94, "#BCFFFF"],
["STAR", 10.49, "#FFC9C9"],
["UNIVERSAL", 19.30, "#F9FFD3"],
["APPOLLO", 21.45, "#C2FFBD"]
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{ calc: "stringify",
sourceColumn: 0,
type: "string",
role: "annotation" },
2]);
var options = {
title: "CUSTOMERS",
bar: {groupWidth: "70%"},
legend: { position: "none" },
};
var chart = new google.visualization.BarChart(document.getElementById("barchart_values"));
chart.draw(view, options);
}
var options = {
title: "CUSTOMERS",
bar: {groupWidth: "70%"},
backgroundColor: 'transparent',
legend: { position: "none" },
It works now, don't know why it didn't work before.
when i create a chart (with google chart) with all the values setted to zero, the haxis is in the middle of the chart.
How can i stuck it at the bottom ?
here is the code :
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Average Revenue');
data.addRows([
['2004', 0],
['2005', 0],
['2006', 0],
['2007', 0]
]);
var options = {
title: 'Revenue by Year',
seriesType: "bars",
series: {1: {type: "line"}},
vAxis: {title: 'Year',
titleTextStyle:{color: 'red'}},
colors:['red','black']
};
var chart = new google.visualization.ComboChart(document.getElementById('chart'));
chart.draw(data, options);
}
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
http://jsfiddle.net/boj6ztLo/
thanks
set following config option...
vAxis.viewWindow.min: 0
see following working snippet...
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Average Revenue');
data.addRows([
['2004', 0],
['2005', 0],
['2006', 0],
['2007', 0]
]);
var options = {
title: 'Revenue by Year',
seriesType: 'bars',
series: {
1: {
type: 'line'
}
},
vAxis: {
title: 'Year',
titleTextStyle: {
color: 'red'
},
viewWindow: {
min: 0
}
},
colors:['red','black']
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
note
recommend not using jsapi to load the library, according to release notes...
The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. Please use the new gstatic loader (loader.js) from now on.
<script src="https://www.gstatic.com/charts/loader.js"></script>
this will also change the load statement to...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
How can i hide the vertical bar that separates the values on the left and the bars on the right?
for bar charts, set hAxis.baselineColor to 'transparent', to hide the vertical bar
set hAxis.gridlines.count to zero, so the baseline isn't replaced with a gridline
see following working snippet...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart(transparent) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
data.addColumn('number', 'Value');
data.addRows([
['UK', 8.94],
['AF', 10.49],
['UN', 20.2],
['SK', 21.45],
]);
var options = {
legend: {
position: 'none'
},
hAxis: {
baselineColor: 'transparent',
gridlines: {
count: 0
}
},
vAxis: {
baselineColor: 'magenta'
},
width: 200
};
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
calc: 'stringify',
sourceColumn: 1,
type: 'string',
role: 'annotation'
}]);
var chartDiv = document.getElementById('chart_div');
var chart = new google.visualization.BarChart(chartDiv);
chart.draw(view, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
I'm trying to create a column chart with google charts. I could put values on top of the bar using the following:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Estacionamento');
data.addColumn('number', 'Valor');
data.addColumn({type: 'number', role:'annotation'});
and creating a view:
chart.draw(view, {
height: 600,
width: 600,
series: {
0: {
type: 'bars'
},
1: {
type: 'line',
color: 'grey',
lineWidth: 0,
pointSize: 0,
visibleInLegend: false
}
},
});
I would like to know if it's possible to also display a value inside the bar. Right now, it's working as a tooltip, but I want to display values without a tooltip like this image:
I have a solution for you problem.... check this example
My solution: simple, add a column with size 0 and after that add an annotation...
select color transparent for the last column...
Javascript Code:
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawStacked);
function drawStacked() {
var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Motivation Level');
data.addColumn({type:'string', role:'annotation'});
data.addColumn('number', 'Energy Level');
data.addColumn({type:'string', role:'annotation'});
data.addColumn('number', 'test Level');
data.addColumn({type:'string', role:'annotation'});
data.addColumn('number', 'Motivation Level');
data.addColumn({type:'string', role:'annotation'});
data.addRows([
[{v: [9, 0, 0], f: '8 am'},{v:40,f:"asd"},"q",{v:40,f:"asd"},"q",{v:40,f:"asd"},"q",{v:0},"q"],
[{v: [10, 0, 0], f: '8 am'},{v:40,f:"asd"},"q",{v:40,f:"asd"},"q",{v:40,f:"asd"},"q",{v:0},"q"],
[{v: [11, 0, 0], f: '8 am'},{v:100,f:"asd"},"q",{v:40,f:"asd"},"q",{v:40,f:"asd"},"q",{v:0},"q"],
]);
var options = {
title: 'Motivation and Energy Level Throughout the Day',
isStacked: true,
annotations: {
alwaysOutside : false,
textStyle: {
fontSize: 14,
color: '#000',
auraColor: 'none'
}
},
hAxis: {
title: 'Time of Day',
format: 'h:mm a',
},
vAxis: {
title: 'Rating (scale of 1-10)',
viewWindow:{
max:200,
min:0
}
},
colors: ["blue","red","green","transparent"],
bar: {groupWidth: "95%"},
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
HTML code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>