我正在尝试在Google Chart's website上找到的代码,还有一个额外的选项,将存储桶设置为每个代表10%的百分比:
var options = {
title: 'Lengths of dinosaurs, in meters',
legend: { position: 'none' },
histogram: { bucketSize: rawData.length / 10}
};
但是,我无法通过查看文档来改变单个数据点的颜色,从而使图形类似于以下内容:
是否有使用Javascript和/或Google Charts公开方法的变通办法,以实现这一目标?
Pastebin链接到我的代码(上面有详细的更改):https://pastebin.com/NeGnEwKY
答案 0 :(得分:0)
没有任何标准选项可用于更改特定块的颜色
但是您可以手动更改颜色
图表将按值('Length'
)绘制块
首先,按值对数据进行排序
// sort data by 'Length'
var data = google.visualization.arrayToDataTable(rawData);
data.sort([{column: 1}]);
现在,数据表的顺序将与用于绘制图表的<rect>
元素相同
我们可以使用getFilteredRows
设置要突出显示的行索引
// find data row to highlight
var highlightRows = data.getFilteredRows([{
column: 0,
value: 'Ultrasaurus (ultra lizard)'
}]);
接下来,我们可以使用colors
选项来识别块元素(<rect>
)
然后更改'fill'
属性
但是我们必须使用MutationObserver
,因为图表将恢复为原始颜色,
随时进行活动(例如悬停),或选择...
请参阅以下工作片段...
google.charts.load('current', {packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var rawData = [
['Dinosaur', 'Length'],
['Acrocanthosaurus (top-spined lizard)', 12.2],
['Albertosaurus (Alberta lizard)', 9.1],
['Allosaurus (other lizard)', 12.2],
['Apatosaurus (deceptive lizard)', 22.9],
['Archaeopteryx (ancient wing)', 0.9],
['Argentinosaurus (Argentina lizard)', 36.6],
['Baryonyx (heavy claws)', 9.1],
['Brachiosaurus (arm lizard)', 30.5],
['Ceratosaurus (horned lizard)', 6.1],
['Coelophysis (hollow form)', 2.7],
['Compsognathus (elegant jaw)', 0.9],
['Deinonychus (terrible claw)', 2.7],
['Diplodocus (double beam)', 27.1],
['Dromicelomimus (emu mimic)', 3.4],
['Gallimimus (fowl mimic)', 5.5],
['Mamenchisaurus (Mamenchi lizard)', 21.0],
['Megalosaurus (big lizard)', 7.9],
['Microvenator (small hunter)', 1.2],
['Ornithomimus (bird mimic)', 4.6],
['Oviraptor (egg robber)', 1.5],
['Plateosaurus (flat lizard)', 7.9],
['Sauronithoides (narrow-clawed lizard)', 2.0],
['Seismosaurus (tremor lizard)', 45.7],
['Spinosaurus (spiny lizard)', 12.2],
['Supersaurus (super lizard)', 30.5],
['Tyrannosaurus (tyrant lizard)', 15.2],
['Ultrasaurus (ultra lizard)', 30.5],
['Velociraptor (swift robber)', 1.8]
];
// sort data by 'Length'
var data = google.visualization.arrayToDataTable(rawData);
data.sort([{column: 1}]);
// find data row to highlight
var highlightRows = data.getFilteredRows([{
column: 0,
value: 'Ultrasaurus (ultra lizard)'
}]);
var options = {
colors: ['#3366cc', '#dc3912'], // <-- 1st color used to identify, 2nd to highlight
title: 'Lengths of dinosaurs, in meters',
legend: { position: 'none' },
histogram: { bucketSize: rawData.length / 10}
};
var container = document.getElementById('chart_div');
var chart = new google.visualization.Histogram(container);
google.visualization.events.addListener(chart, 'ready', function () {
var observer = new MutationObserver(function () {
var index = 0;
Array.prototype.forEach.call(container.getElementsByTagName('rect'), function (rect) {
if (options.colors.indexOf(rect.getAttribute('fill')) > -1) {
if (highlightRows.indexOf(index) > -1) {
rect.setAttribute('fill', options.colors[1]);
}
index++;
}
});
});
observer.observe(container, {
childList: true,
subtree: true
});
});
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>