Tweet about RGraph
Follow for HTML5 news
Bookmark and share:

# RGraph: HTML5 canvas graph library - Line chart documentation

The line chart, along with the bar chart, is probably the most configurable of all the charts available.

The example file is here.

- Properties
- Methods
- Combining the Line and Bar charts
- Alternative colors
- Custom tickmarks

    <script>
        window.onload = function ()
        {
            var data = [10,4,17,50,25,19,20,25,30,29,30,29];

            var line = new RGraph.Line("myLine", data);
            line.Set('chart.background.barcolor1', 'rgba(255,255,255,1)');
            line.Set('chart.background.barcolor2', 'rgba(255,255,255,1)');
            line.Set('chart.background.grid.color', 'rgba(238,238,238,1)');
            line.Set('chart.colors', ['rgba(255,0,0,1)']);
            line.Set('chart.linewidth', 2);
            line.Set('chart.filled', true);
            line.Set('chart.hmargin', 5);
            line.Set('chart.labels', ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
            line.Set('chart.gutter', 40);
            line.Draw();
        }
    </script>

## Properties

You can use these properties to control how the bar graph apears. You can set them by using the Set() method. Eg:

myLine.Set('name', 'value');

- Background
- Labels and text
- Margins
- Colors
- Shadow
- Interactive features
- Titles
- Key
- Scale
- Axis properties
- Zoom
- Miscellaneous

### Background

chart.background.barcolor1
The color of the background bars (1 of 2).
Default: rgba(0,0,0,0)
chart.background.barcolor2
The color of the background bars (2 of 2).
Default: rgba(0,0,0,0)
chart.background.grid
Whether to show the background grid or not.
Default: true
chart.background.grid.color
The color of the background grid.
Default: #eee
chart.background.hbars
An array of information stipulating horizontal colored bars. You can use these to indicate limits. Eg: myLine.Set('hbars', [[75, 10, 'yellow'], [85, 15, 'red']]); This would give you two bars, one red and a lower yellow bar. The units correspond to your scale, and are the starting point and the height.
Default: null
chart.background.grid.hsize
The horizontal size of the grid.
Default: 25
chart.background.grid.vsize
The vertical size of the grid.
Default: 25
chart.background.grid.width
The width of the background grid.
Default: 1
chart.background.grid.border
Determines whether a border line is drawn around the grid.
Default: true
chart.background.grid.hlines
Determines whether to draw the horizontal grid lines.
Default: true
chart.background.grid.vlines
Determines whether to draw the vertical grid lines.
Default: true
chart.background.grid.autofit
Instead of specifying a pixel width/height for the background grid, you can use autofit and specify how many horizontal and vertical lines you want.
Default: false
chart.background.grid.autofit.numhlines
When using autofit this allows you to specify how many horizontal grid lines you want.
Default: 7
chart.background.grid.autofit.numvlines
When using autofit this allows you to specify how many vertical grid lines you want.
Default: 20
chart.backdrop
When enabled this specifies that the line(s) will have a backdrop effect. You can control the transparency with the other backdrop settings).
Default: false
chart.backdrop.size
This controls the size/extent of the backdrop effect.
Default: 30
chart.backdrop.alpha
This controls how much transparency the backdrop effect has. It can go from 0 - 1.
Default: 0.2

### Labels and text

chart.labels.above
Whether the values are shown in labels drawn above the line.
Default: false
chart.labels.above.size
The size of the labels which are drawn above the line.
Default: 8
chart.labels
An array of the X labels for the graph.
Default: [] (An empty array)
chart.labels.ingraph
An array of labels for the graph which are drawn "inside" the graph. If you have 5 data points then this should have a corresponding number of elements, though there is a shorthand available.
Default: null
chart.ylabels
Can be true or false and determines whether the chart has Y axis labels.
Default: true
chart.ylabels.invert
Reverses the Y axis so that 0 is at the top, instead of the bottom.
Default: false
chart.ylabels.count
A value (1, 3 or 5) that controls how many Y labels there are.
Default: 5
chart.ylabels.inside
This controls whether the Y labels are drawn inside the Y axis or not. If your labels are large, this may help.
Default: false
chart.ylabels.inside.color
If the Y labels are to be drawn inside the Y axis, this is used as the background color.
Default: rgba(255,255,255,0.5)
chart.xlabels.inside
This controls whether the X labels are drawn inside the X axis or not. By using this you can significantly reduce the size of the gutter needed.
Default: false
chart.xlabels.inside.color
If the X labels are to be drawn inside the X axis, this is used as the background color.
Default: rgba(255,255,255,0.5)
chart.text.size
The size of the text (in points).
Default: 10
chart.text.angle
The angle of the horizontal text labels (at the bottom of the graph). Previously this could be 0, 45 or 90, but now (31st July 2010) this can be any angle.
Default: 0 (Horizontal)
chart.text.font
The font used to render the text.
Default: Verdana
chart.text.color
The color of the labels.
Default: black

### Margins

chart.gutter
The gutter of the graph. This is the bit of the graph that the labels are in.
Default: 25
chart.hmargin
The size of the horizontal margin. This is on the inside of the axes.
Default: 0

### Colors

chart.colors
An array of line colors.
Default: ['#f00', '#0f0', '', '#00f', '#f0f', '#ff0', '#0ff'] 9
chart.colors.alternate
Set this to true if you want your line color(s) to be alternated. See note
Default: false
chart.fillstyle
A single color or an array of colors that filled line charts will use.
Important: This used to be a string, and still can be, but can now also be an array.
Default: null
chart.filled
Whether the area under the graph is filled or not. This looks best when there is no horizontal margin.
Default: false
chart.filled.range
This is useful for indicating a range. Exactly two datasets are required, with the space between them filled. This is useful for indicating a range.The line chart examples page demonstrates this in action.
Default: false

### Shadow

chart.shadow
If true a shadow will be applied to the line.
Default: false
chart.shadow.color
The color of the shadow. As well as a single color definition, this can also be an array of colors. This means that if you have multiple lines on your chart, each can have a different shadow color.
Default: rgba(0,0,0,0.5)
chart.shadow.offsetx
The X offset in pixels for the shadow.
Default: 3
chart.shadow.offsety
The Y offset in pixels for the shadow.
Default: 3
chart.shadow.blur
The severity of the shadow blurring effect.
Default: 3

### Interactive features

chart.tooltips
These are tooltips for the line(s). It should be an array of tooltips. If you have multiple lines, simply pass multiple arrays to the Set() method.
Default: [] (An empty array)
chart.tooltips.effect
The animated effect used for showing tooltips. Can be either fade or expand.
Default: fade
chart.tooltips.coords.adjust
If you translate() before drawing your graph to reduce wasted space, the coords used for tooltips may need adjusting by how much you translate() by. This setting is how to do it. It should be an array of the X/Y adjustments. There's an example of this on the misc documentation page.
Default: [0,0]
chart.tooltips.css.class
This is the name of the CSS class the graph uses.
Default: RGraph_tooltip
chart.tooltips.override
If you wish to handle showing tooltips yourself, this should be a function object which does just that. There's more information on the tooltips documentation page
Default: null
chart.tooltips.highlight
If you don't want/need the graph to be highlighted and thus avoid redrawing, (eg When combining charts), then set this to false.
Default: true
chart.crosshairs
If true, you will get a crosshair centering on the current mouse position.
Default: false
chart.crosshairs.linewidth
This controls the linewidth of the crosshairs.
Default: 1
chart.crosshairs.color
The color of the crosshairs.
Default: #333
chart.contextmenu
An array of context menu items. Unlike the bar chart, you CAN have context menus at the same time as tooltips. More information on context menus is here.
Default: [] (An empty array)
chart.annotatable
Whether annotations are enabled for the chart (ie you can draw on the chart interactively.
Default: false
chart.annotate.color
If you do not allow the use of the palette, then this will be the only color allowed for annotations.
Default: black
chart.resizable
Defaulting to false, this determines whether your graph will be resizable. Because of the numerous event handlers this has to install code on, This feature is unlikely to work with other dynamic features (the context menu is fine however).
Default: false
chart.adjustable
Defaulting to false, this determines whether your graph will be adjustable (click a point and drag it).
Default: false

### Titles

chart.title
The title of the chart.
Default: none
chart.title.vpos
This allows you to completely override the vertical positioning of the title. It should be a number between 0 and 1, and is multiplied with the gutter and then used as the vertical position. It can be useful if you need to have a large gutter.
Default: null
chart.title.color
The color of the title.
Default: black
chart.title.xaxis
This allows to specify a title for the X axis.
Default: none
chart.title.yaxis
This allows to specify a title for the Y axis.
Default: none
chart.title.xaxis.pos
This is multiplied with the gutter to give the position of the X axis title.
Default: 0.25
chart.title.yaxis.pos
This is multiplied with the gutter to give the position of the Y axis title.
Default: 0.25

### Key

chart.key
An array of key information.
Default: [] (An empty array)
chart.key.background
The color of the key background. Typically white, you could set this to something like rgba(255,255,255,0.7) to allow people to see things behind it.
Default: white
chart.key.position
Determines the position of the key.Either graph (default), or gutter.
Default: graph
chart.key.shadow
Whether a small drop shadow is applied to the key.
Default: false
chart.key.shadow.color
The color of the shadow.
Default: #666
chart.key.shadow.blur
The extent of the blurring effect used on the shadow.
Default: 3
chart.key.shadow.offsetx
The X offset of the shadow.
Default: 2
chart.key.shadow.offsety
The Y offset of the shadow.
Default: 2
chart.key.rounded
This controls whether the corners of the key (in graph mode) are curved. If the key is gutter mode, this has no effect.
Default: false

### Scale

chart.units.post
The units (if any) that the Y axis is measured in (gets appended to the number)
Default: none
chart.units.pre
The units (if any) that the Y axis is measured in (gets preppended to the number)
Default: none
chart.scale.decimals
Determines the precision of the numbers used as the scale.
Default: 0
chart.scale.point
The character used as the decimal point.
Default: .
chart.scale.thousand
The character used as the thousand separator
Default: ,
chart.scale.round
Whether to round the maximum scale value up or not. This will produce slightly better scales in some instances.
Default: null
chart.ymin
The optional minimum Y scale value. If not specified then it will be zero.
Default: null
chart.ymax
The optional maximum Y scale value. If not specified then it will be calculated.
Default: null (It's calculated)
chart.outofbounds
Normally, out-of-bounds values are not drawn. By setting this to true you can change this behaviour.
Default: false

### Axis properties

chart.xticks
The number of X tickmarks.
Default: null (linked to number of datapoints)
chart.ticksize
The size of the tick marks. This only affects certain styles of tickmarks.
Default: 3
chart.tickdirection
Whether the ticks are above or below the axis.
Default: -1 (-1 is below, 1 is above)
chart.axis.color
The color of the axes.
Default: black
chart.xaxispos
The position of the X axis. It can be either bottom or center.
Default: bottom
chart.yaxispos
Specifies the Y axis position. Can be left or right.
Default: left
chart.axesontop
A minor option, this sets the axes to be redrawn after the graph has been drawn. This is only useful in a certain set of circumstances - the graph is filled and the line width is small.
Default: false
chart.noaxes
Whether the axes are drawn
Default: false (the axes ARE drawn)

### Zoom

chart.zoom.mode
Can be used to control whether the zoom is in thumbnail or canvas mode. Possible values are: thumbnail and canvas.
Default: canvas
chart.zoom.factor
This is the factor that the graph will be zoomed by (bigger values means more zoom)
Default: 1.5
chart.zoom.fade.in
Whether the zoomed canvas fades in or not. This also can be used to control the fade in for the zoom in thumbnail mode.
Default: true
chart.zoom.fade.out
Whether the zoomed canvas fades out or not. This also can be used to control the fade in for the zoom in thumbnail mode.
Default: true
chart.zoom.hdir
The horizontal direction of the zoom. Possible values are: left, center, right
Default: right
chart.zoom.vdir
The vertical direction of the zoom. Possible values are: up, center, down
Default: down
chart.zoom.delay
The delay (in milliseconds) between frames.
Default: 50
chart.zoom.frames
The number of frames in the zoom animation.
Default: 10
chart.zoom.shadow
Whether or not the zoomed canvas has a shadow or not.
Default: true
chart.zoom.thumbnail.width
When the zoom is in thumbnail mode, this is the width (in pixels) of the thumbnail.
Default: 75
chart.zoom.thumbnail.height
When the zoom is in thumbnail mode, this is the height (in pixels) of the thumbnail.
Default: 75
chart.zoom.background
Defaulting to true, this determines whether the zoom has a dark, semi-opaque background that covers the entire web page.
Default: true

### Miscellaneous

chart.tickmarks
What kind of tickmarks to use on the graph. This can be:

- dot
- circle
- filledcircle
- endcircle
- square
- endsquare
- filledsquare
- filledendsquare
- tick
- halftick
- endtick
- cross
- borderedcircle (same as dot)
- arrow
- filledarrow

Note that "arrow" and "filledarrow" look better with a thinner (1 or 2) linewidth setting. Also note that now (10th August 2010) as well as a string, this can be an array of different tickmark styles.
Default: null
chart.tickmarks.dot.color
This is the color of the BORDER around the dot/borderedcircle style tickmarks.
Default: #fff
chart.stepped
Draws the line as stepped. Useful for showing stock performance for example.
Default: false
chart.linewidth
The width of the line (ie the actual line on the line graph). Note: If your line is stepped and filled, and you don't want a trailing line indicating the last value, you can set this to zero.
Default: 1
chart.variant
At present this can only be 3d, and gives a small 3D effect.
Default: null
chart.chromefix
Since version 6, Chrome has had a shadow bug, which becomes apparent when you use shadow blurring. This value defaults to true and means that RGraph will skirt the bug with a small fix.
Default: true

## Methods

obj.getPoint()
This method makes it easier to get hold of which point on the Line chart has been hovered over. It returns an array of:

- The graph object
- The X coordinate
- The Y coordinate
- The numerical index of the point. This corresponds (for example) to the tooltips array, and the coordinates array

An example usage is:

    <canvas id="cvs" width="600" height="300">[No canvas support]</canvas>

    <script src="RGraph.common.core.js"></script>
    <script src="RGraph.line.js"></script>

    <script>
        myGraph = new RGraph.Line('cvs', [10,4,2,4,1]);
        myGraph.Set('chart.hmargin', 10);
        myGraph.Set('chart.tickmarks', 'endcircle');
        myGraph.Set('chart.labels', ['Fred','John','Kev','Lou','Pete']);
        myGraph.Draw();

        RGraph.Register(myGraph);

        myGraph.canvas.onmousemove = function (e)
        {
            RGraph.FixEventObject(e);

            var canvas  = e.target;
            var context = canvas.getContext('2d');
            var obj     = e.target.__object__;

            // This is the new method which simplifies getting coordinates
            var point = obj.getPoint(e);

            if (point) {

                canvas.style.cursor = 'pointer';

                // Is this the same tooltip as the one (if any) that's already being shown
                if (RGraph.Registry.Get('chart.tooltip') && RGraph.Registry.Get('chart.tooltip').__index__ == point[3]) {
                    return;
                }

                // Start afresh
                RGraph.Redraw();

                // Show the tooltip
                RGraph.Tooltip(canvas, obj.Get('chart.labels')[point[3]], e.pageX, e.pageY, point[3]);

                // Highlight the point
                context.strokeStyle = 'gray';
                context.fillStyle = 'white';
                context.beginPath();
                context.moveTo(point[1], point[2]);
                context.arc(point[1], point[2], 2, 0, 6.26, 0);
                context.stroke();
                context.fill();

                return;
            }

            canvas.style.cursor = 'default';
        }

        window.onclick = function ()
        {
            RGraph.Redraw();
        }
    </script>

## Combining the Line and Bar charts

You can combine the Bar and Line charts with a bit of magic. It's actually quite easy to do. Find out more here. In the same vein, you can have Y axes on both the left and right.

## Alternative colors

Instead of a string stipulating the color, each element of the colors array can itself be a two element array, stipulating the up color, and the down color. To use alternating colors you must also stipulate the alternate property:

    myLine.Set('chart.colors.alternate', true);
    myLine.Set('chart.colors', ['red', ['blue', 'yellow'], 'green]);

## Custom tickmarks

If none of the available tickmark styles are suitable, you can instead specify a function object that draws the tickmark, enabling you to draw the tickmark yourself. For example:

    <script>
        line.Set('chart.tickmarks', myTick);

        /**
        * The function that is called once per tickmark, to draw it
        *
        * @param object obj   The graph object
        * @param array  data  The entire line data
        * @param number value The individual points value
        * @param number index The current index, in the data array
        * @param number x     The X coordinate
        * @param number y     The Y coordinate
        * @param string color The color of the line
        * @param number prevX The previous X coordinate
        * @param number prevY The previous Y coordinate
        */
        function myTick (obj, data, value, index, x, y, color, prevX, prevY)
        {
            // Draw your custom tick here
        }
    </script>