The animation and visual effects code is new to RGraph as of August 2011. It greatly simplifies the process of animating your
graphs or using visual effects.
Example use
To use the effects you simply change your call to the .Draw() method to the appropriate effect function, like this:
CSS animation (which jQuery does) can be smoother than canvas animation (ie changing the CSS properties vs redrawing the
canvas). This is because CSS animation (ie changing the CSS properties) does not have the side
effect of clearing the canvas, and so does not incur a redraw.
An alternative to the widely known and understood setTimeout() function is the newer requestAnimationFrame().
This is a function which has been optimised for animation compared to the standard setTimeout() function. Some of
the animation functions use this over the setTimeout() function and going forward newer animation functions will
use it. RGraph has a fallback though so that if the function isn't available the older setTimeout() will be used.
Combining effects
Some of the animation effects (ie those that work on separate aspects of the chart) can be combined. As an example the
Fade in/out can be combined with most other animation effects, allowing you to add a fade effect to the other animation
types. An example of this is below - this chart uses the Grow, Fade and Reveal effects.
The .Animate() function function is similar to the jQuery .Animate() function. You can pass it a map of RGraph
properties and their TARGET values and the .Animate() function will move the properties towards those values.
In this way you can animate a variety of RGraph properties. The .Animate() function takes two or three arguments:
The graph object
A map of the target chart properties
(Optional) A function object which is called when the animation is complete (it is passed the chart object)
An example of it use is below. It should be used instead of calling the .Draw() method:
function myCallback (obj)
{
alert('A sample callback using the alert() function');
}
// myBar.Draw();
RGraph.Effects.Animate(myBar, {'chart.hmargin': 5, 'chart.gutter.left': 50}, myCallback);
Available effects
These are the effects available. You can achieve more effects by using the .Animate() function,
which allows you to animate any numeric RGraph property.
Fade in
A simple fade in effect where the canvas is initially drawn with an opacity of zero, increasing to one.
RGraph.Effects.Fade.In(obj);
Fade out
A simple fade out effect to accompany the fade in, where the canvas initially has an opacity of one, decreasing
to zero.
RGraph.Effects.Fade.Out(obj);
Expand
This effect starts initially with the canvases CSS width and height set to 0 and over a period of half a second
increases them to 100%.
RGraph.Effects.jQuery.Expand(obj);
Snap
This effect is similar to the tooltip effect "snap" in that it "snaps" to the correct position,
moving in from the top left.
RGraph.Effects.jQuery.Snap(obj);
Reveal
This effect starts with the the canvas covered and is slowly revealed from the center outwards. The canvas
is at all times the same size (ie it's not resizd as with the Expand effect).
RGraph.Effects.jQuery.Reveal(obj);
Horizontal Blinds (open)
This effect gradually reveals the canvas by "opening some horizontal blinds", which are positioned
over the canvas.
RGraph.Effects.jQuery.Blinds.Open(obj);
Horizontal Blinds (close)
This effect gradually conceals the canvas by "closing some horizontal blinds", which are positioned
over the canvas.
RGraph.Effects.jQuery.HBlinds.Close(obj);
Vertical Blinds (open)
This effect gradually reveals the canvas by "opening some vertical blinds", which are positioned
over the canvas.
RGraph.Effects.jQuery.VBlinds.Open(obj);
Vertical Blinds (Close)
This effect gradually conceals the canvas by "closing some vertical blinds", which are positioned
over the canvas.
RGraph.Effects.jQuery.VBlinds.Close(obj);
Pie chart Grow
This effect gradually increases the radius of the Pie chart, expanding out from the centre.
RGraph.Effects.Pie.Grow(obj);
Pie chart Implode
This effect gradually decreases the explosion of the chart - gradually bringing it together. Combined with
the RoundRobin effect (documented below) produces a nice combination, as seen on the frony page of this website.
This effect gradually increase both the circular size of the Pie chart and also the radius. Combining this effect
with the implode effect produces a nice effect.
This effect gradually increases the size of the bars on the bar chart until they are at full size.
RGraph.Effects.Bar.Grow(obj);
Fuel chart Grow
This effect gradually increases the indicated value until it reaches the correct value.
RGraph.Effects.Fuel.Grow(obj);
Slide out
This effect slides the whole canvas out. One potential use would be to slide the canvas out and then
slide another canvas with a different chart in. You can specify that the canvas slides out to by using the second
argument as shown below.
This effect "unfolds" the line from the origin, gradually increasing the X (and/or Y) values. There's also an example
of a combined Bar/Line chart with the Bar chart Grow, the Fade and the Line chart Unfold effects on the
combined charts page.
RGraph.Effects.Line.Unfold(obj);
UnfoldFromCenter
This effect "unfolds" the line from the center, gradually increasing or decreasing the Y values as appropriate.
RGraph.Effects.Line.UnfoldFromCenter(obj);
FoldTocenter
This effect "folds" the line to the center, gradually increasing or decreasing the Y values as appropriate. In
effect the opposite of the "unfold" effect.
RGraph.Effects.Line.FoldToCenter(obj);
Line chart Trace
This effect gradually traces the line across the chart - slowly revealing it in full. Not all Line chart options
(eg filled charts) work with this effect.
RGraph.Effects.Line.jQuery.Trace(myLine14);
Rose chart Grow
This effect gradually increases the radius and the radius of each segment. It is not available with
non-equi-angular Rose charts.
RGraph.Effects.Rose.Grow(obj);
Odometer Grow
This effect gradually increase the value of the Odometer until it reaches the desired value. The second
argument is not used, but the third can be a Javascript function and will be called when the animation
is finished.
RGraph.Effects.Odo.Grow(obj, null, myFunc);
Meter Grow
This effect gradually increases the value of the Meter until it reaches the desired value. You can optionally specify an object map
as the second argument of which the only option is frames - allowing to specify the number of frames in the animation.
More frames = longer animation. The animation is set to use a frame rate of roughly 60 frames a second.
RGraph.Effects.Meter.Grow(obj, {'frames': 90});
HBar Grow
This effect gradually increase the values of the HBar until it reaches the desired value.
RGraph.Effects.HBar.Grow(obj);
Gauge Grow
This effect gradually increases the indicated value until it is correct. It's also shown on the
Gauge chart example page. By constantly updating the Gauge (with some AJAX
for example) you could make a control panel of sorts.
RGraph.Effects.Gauge.Grow(obj);
Radar Grow
This effect gradually increases the magnitude of the Radar chart, growing outwards from the center. Multiple
data sets are supported.
RGraph.Effects.Radar.Grow(obj);
Waterfall Grow
This effect gradually Grows the waterfall chart in both X and Y respects.
RGraph.Effects.Waterfall.Grow(obj);
Things to note
With Fade in/out effects remember that just because you can't see the canvas (ie the opacity is 0) doesn't mean
that you can't interact with it, so tooltip hotspots will still be active. If you want to get rid of the canvas
entirely you need to remove it from the DOM. You can do this with the standard.removeNode function:
obj.canvas.parentNode.removeChild(obj.canvas);
This will leave a hole in the page though and will cause the page layout to change. If this is an issue you may
want to use the RGraph API function RGraph.Effects.ReplaceCanvasWithDIV(canvas). As the name
suggests, this function replaces the canvas with a DIV which has the same dimensions meaning your page structure
won't change.
It should be noted that in order to prevent the canvas being constantly drawn on, it is cleared at the
start of the animation.
In the jQuery animations here, the Google hosted jQuery is used:
Because canvas based animations may need to redraw the entire chart,
they can be slower than CSS based animations and depend on a variety of things - eg the speed of the users PC
and the work necessary to redraw the canvas.