I'm using a bar chart and I need that the bars to be gradient rather than solid colors.In the code, you can see that I add "role: style" and after a solid color, but I need that the to have a linear gradient.
function drawChart2() {
var data = google.visualization.arrayToDataTable([
["Element", "Difference", {
role: "style"
}],
['Mg', 1.2 , "#1aab54"]
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"
},
2]);
var options = {
title: "",
legend: {position: 'none'},
bar: {
groupWidth: "50%"
},
hAxis: {
viewWindowMode:'explicit',
viewWindow: {
max:2,
min:0.5
},
ticks: [{ v: 0.5, f: 'low'}, { v: 1, f: 'middle'}, {v: 1.5, f: 'high'}],
},
};
var chart_area = document.getElementById("barchart_values2");
var chart = new google.visualization.BarChart(chart_area);
google.visualization.events.addListener(chart, 'ready', function(){
chart_area.innerHTML = '<img src="' + chart.getImageURI() + '" class="img-responsive">';
});
chart.draw(view, options);
}
no options for gradient fill,
but you can add your own...
first, add your gradient definition to the html somewhere.
this element should not be hidden with
display: none
,otherwise, some browsers may ignore it.
setting the size to zero pixels seems to work.
next, we need to be able to identify the
<rect>
element used for the bar.we can use the color style.
find the
<rect>
element and set the fill attribute.normally, we could set the gradient fill on the chart's
'ready'
event,but we have to use a
MutationObserver
, and set the fill every time the svg is mutated (drawn / hovered).however, I was not able to get the gradient to come thru in the image, maybe html2canvas will do a better job.
see following working snippet...