简体   繁体   English

如何使用事件更改图表颜色(谷歌图表)

[英]How can I change the chart color using events (google charts)

Is it possible to change the color of the series through the event?是否可以通过活动改变系列的颜色?

In this example I trigger an alert and a log just to see how it works, but I would like to know if it is possible to change the color of the selected option.在此示例中,我触发警报和日志只是为了查看它是如何工作的,但我想知道是否可以更改所选选项的颜色。

Simple example: click on sales and the event changes the color from blue to green简单示例:点击销售,事件将颜色从蓝色变为绿色

 google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Year', 'Sales', 'Expenses'], ['2013', 1000, 400], ['2014', 1170, 460], ['2015', 660, 1120], ['2016', 1030, 540] ]); var options = { title: 'Company Performance', hAxis: {title: 'Year', titleTextStyle: {color: '#333'}}, vAxis: {minValue: 0} }; var chart = new google.visualization.AreaChart(document.getElementById('chart_div')); chart.draw(data, options); google.visualization.events.addListener(chart, 'select', selectHandler); function selectHandler(e) { console.log(chart.getSelection()[0]); alert('A table row was selected'); } }
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div" style="width: 100%; height: 500px;"></div>

first, a couple things...首先,有几件事...

  1. chart events should be applied, before the chart is drawn.在绘制图表之前,应应用图表事件。
  2. the 'select' event is called both when a point is selected, and when it is unselected.选择一个点和取消选择一个点时都会调用'select'事件。
    as such, you need to check the length of the selection, before trying to access the contents of the array.因此,在尝试访问数组的内容之前,您需要检查选择的长度。
    otherwise, when a point is unselected, an error would occur, here --> chart.getSelection()[0]否则,当一个点被取消选择时,会发生错误,这里 --> chart.getSelection()[0]

to set the series color, we can use the following config option.要设置系列颜色,我们可以使用以下配置选项。

series: {
  0: {  // <-- series number
    color: 'green'
  }
}

when the 'select' event fires, we can use the column property from the selection to find the series number.'select'事件触发时,我们可以使用选择中的column属性来查找序列号。 it will be the column value - 1.它将是列值 - 1。

however, in order to change the series color, the chart must be re-drawn.但是,为了更改系列颜色,必须重新绘制图表。
which means the selection will be removed.这意味着选择将被删除。

we can use the 'ready' event to reset the selection.我们可以使用'ready'事件来重置选择。
but in order to show the tooltip when the selection is reset,但为了在重置选择时显示工具提示,
we must use the following config option...我们必须使用以下配置选项...

tooltip: {
  trigger: 'both'
}

see following working snippet,请参阅以下工作片段,
when a point is selected, the selected series color is set to green, and the chart is re-drawn in the 'select' event.当一个点被选中时,选中的系列颜色设置为绿色,并且图表在'select'事件中重新绘制。
then the selection is reset in the 'ready' event...然后在'ready'事件中重置选择...

 google.charts.load('current', { packages: ['corechart'] }).then(function drawChart() { var data = google.visualization.arrayToDataTable([ ['Year', 'Sales', 'Expenses'], ['2013', 1000, 400], ['2014', 1170, 460], ['2015', 660, 1120], ['2016', 1030, 540] ]); var options = { title: 'Company Performance', hAxis: { title: 'Year', titleTextStyle: { color: '#333' } }, vAxis: { minValue: 0 }, tooltip: { trigger: 'both' } }; var chart = new google.visualization.AreaChart(document.getElementById('chart_div')); var selection = []; google.visualization.events.addListener(chart, 'ready', readyHandler); function readyHandler() { chart.setSelection(selection); } google.visualization.events.addListener(chart, 'select', selectHandler); function selectHandler() { selection = chart.getSelection(); options.series = {}; if (selection.length > 0) { options.series[selection[0].column - 1] = { color: 'green' }; } chart.draw(data, options); } chart.draw(data, options); window.addEventListener('resize', function () { chart.draw(data, options); }); });
 html, body { height: 100%; margin: 0px 0px 0px 0px; overflow: hidden; padding: 0px 0px 0px 0px; } #chart_div { height: 100%; }
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM