简体   繁体   English

如何将背景颜色添加到Google Charts中的特定行

[英]How to add a background color to a specific row in Google Charts

I would like to add a background color to a specific row(s) in my Google Timeline Chart. 我想在Google时间线图表中为特定行添加背景颜色。 I know there's a global setting to set the background of the entire chart, but how would I do this for a specific row. 我知道有一个全局设置来设置整个图表的背景,但我如何为特定行执行此操作。 Here's an example of what I mean: 这是我的意思的一个例子:

在此输入图像描述

I would like the "Willow Room" row (and only that row) to have a background as follows: 我希望“柳树房间”行(并且只有该行)具有如下背景:

在此输入图像描述

Here is a jsfiddle example of the above (without the red background that I'm trying to get): https://jsfiddle.net/0f86vLrg/3/ . 这是上面的一个jsfiddle示例(没有我想要获得的红色背景): https ://jsfiddle.net/0f86vLrg/3/。

 google.charts.load("current", {packages:["timeline"]}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var container = document.getElementById('timeline'); var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); dataTable.addColumn({ type: 'string', id: 'Platform' }); dataTable.addColumn({ type: 'string', id: 'Status' }); dataTable.addColumn({ type: 'string', role: 'style' }); dataTable.addColumn({ type: 'date', id: 'Start' }); dataTable.addColumn({ type: 'date', id: 'End' }); // set the background of a missing item dataTable.addRows([ [ 'Magnolia Room', '', null, new Date(0,0,0,12,0,0), new Date(0,0,0,13,30,0) ], [ 'Magnolia Room', '', null, new Date(0,0,0,14,0,0), new Date(0,0,0,15,30,0) ], [ 'Willow Room', '', 'error', new Date(0,0,0,12,30,0), new Date(0,0,0,14,0,0) ], [ 'X Room', '', 'opacity: 0', new Date(0,0,0,0,0,0), new Date(0,0,1,0,0,0)]]); var options = { timeline: { colorByRowLabel: true }, backgroundColor: '#ffd' }; chart.draw(dataTable, { hAxis: { minValue: new Date(0,0,0,0,0,0), maxValue: new Date(0,0,0,24,0,0), } }); } 
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div id="timeline" style="height: 180px;"></div> 

How would I add in a background color for a specific row based on the "style" setting of that row (or if there's a better way to give it a class or something.) For example in the above code the style is called "error" for the "Willow Room" item. 如何根据该行的“样式”设置为特定行添加背景颜色(或者如果有更好的方法为其赋予类或其他东西。)例如,在上面的代码中,样式称为“错误” “为”柳树房“项目。

there are no options for styling a specific bar on the timeline chart. 没有选项可以在时间线图表上设置特定栏的样式。
and the data format does not support the 'style' role, only the 'tooltip' role. 并且数据格式不支持'style'角色,仅支持'tooltip'角色。

but we can still use the data table column to specify the error rows. 但我们仍然可以使用数据表列来指定错误行。
and we'll need to change their color manually, on the chart's 'ready' event. 我们需要在图表的'ready'事件中手动更改颜色。

the thing that makes this difficult, the chart will combine data table rows onto a single bar, 使这个困难的事情,图表将数据表行组合到一个单独的栏上,
depending on the row label or even possibly the date range. 取决于行标签甚至可能是日期范围。
so the number of bars in the chart will not always match the number of bars in the data table. 因此图表中的条形数量并不总是与数据表中的条形数量相匹配。
however, they will be drawn in the same order as found in the data table. 但是,它们将按照数据表中的顺序绘制。

as such, we must first find the corresponding data table row for the label. 因此,我们必须首先找到标签的相应数据表行。
then check to see if the label's row has an error. 然后检查标签的行是否有错误。
then find the bar with the same index as the label and change the color. 然后找到与标签具有相同索引的条形并更改颜色。

see following working snippet... 请参阅以下工作代码段...

 google.charts.load('current', { packages:['timeline'] }).then(function () { var container = document.getElementById('timeline'); var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); dataTable.addColumn({ type: 'string', id: 'Platform' }); dataTable.addColumn({ type: 'string', id: 'Status' }); dataTable.addColumn({ type: 'string', role: 'style' }); dataTable.addColumn({ type: 'date', id: 'Start' }); dataTable.addColumn({ type: 'date', id: 'End' }); dataTable.addRows([ ['Magnolia Room', '', null, new Date(0,0,0,12,0,0), new Date(0,0,0,13,30,0)], ['Magnolia Room', '', null, new Date(0,0,0,14,0,0), new Date(0,0,0,15,30,0)], ['Willow Room', '', 'error', new Date(0,0,0,12,30,0), new Date(0,0,0,14,0,0)], ['X Room', '', null, new Date(0,0,0,0,0,0), new Date(0,0,1,0,0,0)] ]); var options = { timeline: {colorByRowLabel: true}, backgroundColor: '#ffd', height: 180 }; // change bar color for errors google.visualization.events.addListener(chart, 'ready', function() { var rows; var barIndex; var labelIndex = 0; var labels = container.getElementsByTagName('text'); Array.prototype.forEach.call(labels, function(label) { // process bar labels if (label.getAttribute('text-anchor') === 'end') { // find data rows for label rows = dataTable.getFilteredRows([{ column: 0, test: function (value) { var labelFound; var labelText; // chart will cutoff label if not enough width if (label.textContent.indexOf('…') > -1) { labelText = label.textContent.replace('…', ''); labelFound = (value.indexOf(labelText) > -1); } else { labelText = label.textContent; labelFound = (value === labelText); } return labelFound; } }]); if (rows.length > 0) { // process label rows rows.forEach(function (rowIndex) { // check if row has error if (dataTable.getValue(rowIndex, 2) === 'error') { // change color of label label.setAttribute('fill', '#c62828'); // change color of bar barIndex = 0; var bars = container.getElementsByTagName('rect'); Array.prototype.forEach.call(bars, function(bar) { if ((bar.getAttribute('x') === '0') && (bar.getAttribute('stroke-width') === '0')) { if (barIndex === labelIndex) { bar.setAttribute('fill', '#ffcdd2'); } barIndex++; } }); } }); } labelIndex++; } }); }); chart.draw(dataTable, { hAxis: { minValue: new Date(0,0,0,0,0,0), maxValue: new Date(0,0,0,24,0,0) } }); }); 
 #timeline svg g text { font-weight: normal !important; } 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="timeline"></div> 

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

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