You can't get an "x" to click, but you can make the bars disappear on click
with a bit of hackery:
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Show Sales', 'Expenses', 'Show Expenses'],
['2004', 1000, true, 400, true],
['2005', 1170, true, 460, true],
['2006', 660, true, 1120, true],
['2007', 1030, true, 540, true]
]);
var options = {
title: 'Company Performance',
vAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
};
var chart = new
google.visualization.BarChart(document.getElementById('chart_div'));
function updateChart () {
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: data.getColumnLabel(1),
calc: function (dt, row) {
// return a value from column 1 if column 2 is true, null
otherwise
return dt.getValue(row, 2) ? dt.getValue(row, 1) : null;
}
}, {
type: 'number',
label: data.getColumnLabel(3),
calc: function (dt, row) {
// return a value from column 3 if column 4 is true, null
otherwise
return dt.getValue(row, 4) ? dt.getValue(row, 3) : null;
}
}]);
chart.draw(view, options);
}
google.visualization.events.addListener(chart, 'select', function () {
var selection = chart.getSelection();
if (selection.length && selection[0].row != null &&
selection[0].column != null) {
data.setValue(selection[0].row, selection[0].column * 2, false);
}
updateChart();
});
updateChart();
}
google.load('visualization', '1', {packages:['corechart'], callback:
drawChart});
see it working here: http://jsfiddle.net/asgallant/c4Lnaseb/
On Tuesday, September 23, 2014 1:36:58 PM UTC-4, min ji wrote:
>
> Hello,
> I created a bar chart (codes are below). I want to add a function on the
> chart: if users want to hide a bar from the chart, how to realize it? Is
> there a "x" button in the each bar which can be clicked to hide the bar
> (the data don't be deleted, just hide)?
> Thank you very much.
>
> <html>
> <head>
> <script type="text/javascript"
> src="https://www.google.com/jsapi"></script>
> <script type="text/javascript">
> google.load("visualization", "1", {packages:["corechart"]});
> google.setOnLoadCallback(drawChart);
> function drawChart() {
> var data = google.visualization.arrayToDataTable([
> ['Year', 'Sales', 'Expenses'],
> ['2004', 1000, 400],
> ['2005', 1170, 460],
> ['2006', 660, 1120],
> ['2007', 1030, 540]
> ]);
>
> var options = {
> title: 'Company Performance',
> vAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
> };
>
> var chart = new
> google.visualization.BarChart(document.getElementById('chart_div'));
>
> chart.draw(data, options);
> }
> </script>
> </head>
> <body>
> <div id="chart_div" style="width: 900px; height: 500px;"></div>
> </body>
> </html>
>
>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.