Post-processing on a Bar chart
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, yaxisTickmarksCount: 3, backgroundGridHlinesCount: 3, backgroundGridVlines: false, backgroundGridBorder: false, colorsStroke: 'rgba(0,0,0,0)', shadowOffsety: -3 } // 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 semi-opaque white at the top // to fully transparent white at the bottom. }).on('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: 0, x2: 0, y2: 250, colors: ['rgba(255,255,255,.75)','rgba(255,255,255,0)'] }); // Draw the highlight rectangle obj.context.fillRect(x,y,w,h) } }).draw().responsive([ {maxWidth: null,width:600,height:300,options: {textSize:14,marginInner: 5,shadow:true, xaxis: false,yaxis: false,}, parentCss: {'float': 'left',textAlign:'none'}}, {maxWidth: 900, width:400,height:200,options: {textSize:10,marginInner: 2,shadow:false,xaxis: false,yaxis: false,}, parentCss: {'float': 'none',textAlign:'center'}} ], {delay: 0}); </script>