A customised, adjustable Bar chart
This demo shows how you can make use of the
so that when you adjust one bar on the chart the others are updated
with the remainder - be it a plus or minus figure - (so that they all add up to 300).
As shown below, the code to do this is not a lot and is added in the configuration using the
on() function after the
The first thing the code does is get the
shape object for the bar that's being adjusted
from the RGraph registry. It gets the new value of the bar, takes it off the specified maximum value
(which is 300 in this example) and then shares the remainder out between the other bars and sets
Finally, it redraws the chart so that the new values are shown on the chart.
It should be feasible to maintain the differences between the bars so that instead of the rest of the bars being set to the same value they would be set to their original value + their bit of the adjusted bar - be it a plus or a negative.
But since that's more involved I'll leave it as an exercise for the reader!
There's a Horizontal Bar demo in the download called
demonstrates this concept, albeit with only two bars.
responsive() function reduces the size of the canvas, reduces the size of the
text on the canvas and moves the X-axis labels down a little bit. The CSS
also removed. The dynamic nature of the chart is not altered.
This goes in the documents header:
<script src="RGraph.common.core.js"></script> <script src="RGraph.common.dynamic.js"></script> <script src="RGraph.bar.js"></script>Put this where you want the chart to show up:
<div> <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):