Post-processing on a Bar chart
A Bar chart that shows you that by using the draw event along with the coordinates you can do post-processing on your charts
This Bar chart uses the custom RGraph draw event to add
some highlighting to each bar.
There's an SVG version of this chart in
the download archive.
The highlighting is added using the draw event (which is triggered at the end of the draw function) so the coordinates of the bars are available.
The gradient is created using this bit of code, which uses an RGraph function for creating gradients easily:
obj.context.fillStyle = RGraph.linearGradient({
object: obj, // The chart object
x1: 0, y1: 0, x2: 0, y2: 250, // strartX, startY, endX, endY
colors: [
'rgba(255,255,255,.75)', // Start color
'rgba(255,255,255,0)' // End color
]
});
The responsive function reduces the size of the text, turns off axes and the shadow and removes the css float from the canvas tag.
This goes in the documents header:
<script src="RGraph.common.core.js"></script> <script src="RGraph.bar.js"></script>Put this where you want the chart to show up:
<div style="float: left">
<canvas id="cvs" width="600" height="250">[no canvas support]</canvas>
</div>
This is the code that generates the chart - it should be placed AFTER the canvas tag(s):
<script>
// Create the bar chart just like a normal grouped bar chart
new RGraph.Bar({
id: 'cvs',
data: [[47,75],[32,74],[71,85],[25,19],[23,71],[81,59],[43,130],[23,20]],
options: {
marginLeft: 50,
colors: ['#494949','#35A0DA'],
xaxisLabels: ['Alf','Bert','Craig','Dan','Edgar','Fred','Gary','Harry'],
yaxisLabelsCount: 3,
backgroundGridHlinesCount: 3,
responsive: [
{maxWidth: null,width:600,height:300,options: {textSize:14,marginInner: 5}, parentCss: {'float': 'right',textAlign:'none'}},
{maxWidth: 900, width:500,height:250,options: {textSize:10,marginInner: 2}, parentCss: {'float': 'none',textAlign:'center'}}
],
// Now use the draw event and the coordinates that were created when
// the chart was drawn to add highlighting to the left side of each
// bar. The highlighting is graduated from fully opaque white at the
// top to semi-transparent white at the bottom.
events: {
draw: function (obj)
{
var len = obj.coords.length;
for (var i=0; i<len; ++i) {
// Get the coordinates for each bar - full height but only half
// width.
var x = obj.coords[i][0],
y = obj.coords[i][1],
w = obj.coords[i][2] / 2,
h = obj.coords[i][3];
// Create the gradient using an RGraph API function
obj.context.fillStyle = RGraph.linearGradient({
object: obj,
x1: 0, y1: 35, x2: 0, y2: 250,
colors: ['white','rgba(255,255,255,.25)']
});
// Draw the highlight rectangle
obj.context.fillRect(x,y,w,h)
}
}
}
}
}).draw();
</script>