Share: Bookmark with delicious tweet this site Post to Facebook

RGraph: HTML5 canvas graph library - Scatter graph documentation

A scatter graph. Used to represent many data points over a period of time. For example, events occurring in a given year.

The example file is here.

<script>
    window.onload = function ()
    {
        var data = [
                    [67,78,null, 'The winner!'], [67,40,'red'], [58,12], [78,56], [365,90], [360,300], [320,150], [15,45],
                    [16,43], [84,12], [67,89,'green'], [90,23,'green'], [23,80], [80,66], [55,66], [88,12], [43,45], [61,12],
                    [15,89], [13,16]
                   ];
        var sg = new RGraph.Scatter('myScatter', data);
        sg.Set('chart.background.barcolor1','rgba(255,255,255,1)');
        sg.Set('chart.background.barcolor2', 'rgba(255,255,255,1)');
        sg.Set('chart.grid.color', 'rgba(238,238,238,1)');
        sg.Set('chart.gutter', 30);
        sg.Set('chart.labels', ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
        sg.Set('chart.xmax', 365); // Important!
        sg.Draw();
    }
</script>

As you can see each mark on the Scatter chart is made by supplying an array of up to 4 elements:

If you wish to specify a tooltip, but not a color (ie use the default color instead), you can pass null instead of a color.

Properties

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

myScatter.Set('chart.xmax', 365);

Chart configuration

chart.width
This controls the width of the graph. You can set this to (for example) 600 and have your canvas 650px wide giving an extra 50px for labels.
Default: null (By default the canvas width is used)

chart.height
This controls the height of the graph. You can set this to (for example) 250 and have your canvas 300px heigh giving an extra 50px for labels.
Default: null (By default the canvas height is used)

Background

chart.background.barcolor1
The color of the background bars.
Default: rgba(0,0,0,0)

chart.background.barcolor2
The color of the background bars.
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 coloured bars. You can use these to indicate limits. Eg:
myScatter.Set('chart.background.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.vbars
An array of vertical bar information. They're specified like his:
myScatter.Set('chart.background.vbars', [[0, 181, 'rgba(0,255,0,0.5)']]);
Default: null

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.background.grid.autofit.align
If you want to have your grid lines line up with the labels (both X and Y axes), you can set this to true and RGraph will attempt to make the grid lines line up. If you have a chart.hmargin set then the alignment will be thrown out.
Default: false

Labels and text

chart.labels
An array of the X labels for the graph.
Default: [] (An empty array)

chart.ylabels
A boolean (true or false) that controls whether the graph has Y labels.
Default: true

chart.ylabels.count
A value (1, 3 or 5) that controls how many Y labels there are.
Default: 5

chart.text.font
The font used to render the text.
Default: Verdana

chart.text.color
The color of the labels.
Default: black

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). This can be from 0-90 (degrees)
Default: 0 (Horizontal)

chart.labels.specific.align
If you are using specific points for X labels, by default the labels are left aligned within its section. With this setting you can center the labels within its section.
Default: left

Margins

chart.gutter
The gutter on the graph (the area outside of the axes).
Default: 25

Axis properties

chart.xmax
The maximum X axis value you wish to set. For example if you're displaying products sold in a year, you might use 365.
Default: none - must be supplied

chart.ymax
The optional maximum Y scale value. If not specified then it will be calculated.
Default: null (It's calculated)

chart.ymin
The optional minimum Y scale value. If not specified it will be zero.
Default: null

chart.ticksize
The size of the tickmarks.
Default: 3

chart.tickmarks
The style of the tickmarks. Can be: cross, plus, circle, diamond, square or null (no tickmarks).
Default: cross

chart.xticks
This controls whether the X axis tickmarks are drawn.
Default: true

chart.noendxtick
Whether to draw an end X tick. Usually used when combining Scatter charts
Default: false

chart.xaxis
This controls whether the X axis is drawn.
Default: true

chart.xaxispos
Where the X axis should be drawn. Can be either center or bottom.
Default: bottom

chart.yaxispos
Where the Y axis should be drawn. Can be either left or right.
Default: left

chart.yaxispos
Where the Y axis should be drawn. Can be either left or right.
Default: left

chart.axis.color
The color of the axes.
Default: black

chart.noaxes
If this is set to true, no axes will be drawn.
Default: false

Titles

chart.title
The title of the scatter graph.
Default: none

chart.title.background
The background color (if any) for the title.
Default: null

chart.title.hpos
This allows you to completely override the horizontal positioning of the title. It should be a number between 0 and 1, and is multiplied with the whole width of the canvas and then used as the horizontal position.
Default: null

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

chart.title.yaxis.position
Instead of using the option above you can instead use this option, specifying left or right.
Default: left

Scale

chart.scale.formatter
To allow thoroughly custom formats of numbers in the scale, you can use this option to specify a function that is used by RGraph to format numbers. This function should handle ALL of the formatting. Eg:

function myFormatter(obj, num)
{
    return num + 'F'; // An example of formatting
}
myGraph.Set('chart.scale.formatter', myFormatter);

Default: null

chart.scale.decimals
The number of decimal places to display for the Y 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.units.pre
The units (if any) that the Y axis is measured in (these are preppended to the number).
Default: none

chart.units.post
The units (if any) that the Y axis is measured in (these are appended to the number).
Default: none

Interactive features

Note: Unlike other charts, the Scatter chart tooltips are specified as part of the data array. See above.

chart.tooltips.effect
The animated effect used for showing tooltips.
Default: fade

chart.tooltips.hotspot
This controls the size of the hotspot on the chart for tooltips.
Default: 3

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.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.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.crosshairs.coords
If true, this will show the coordinates that the crosshairs are currently over.
Default: false

chart.crosshairs.fixed
If true, this makes the coordinates static (attached), instead of following the pointer around.
Default: false

chart.crosshairs.fadeout
If true, when you move your mouse off of the canvas the coordinates will fade out.
Default: false

chart.crosshairs.labels.x
Instead ox X and Y, you can specify more meaningful labels.
Default: X

chart.crosshairs.labels.y
Instead ox X and Y, you can specify more meaningful labels.
Default: Y

chart.contextmenu
An array of context menu items. More information on context menus is here.
Default: null

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 colour allowed for annotations.
Default: #000

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.resize.handle.adjust
If you're right aligning your chart then you may need this to adjust the position of the resize handle.
Default: [0,0]

chart.resize.handle.background
With this you can specify the background color for the resize handle. If you're adjusting the position of the handle then you may need this to make the handle stand out more.
Default: null

Line properties

chart.line
Whether to show a connecting line (like in the sixth example).
Default: false

chart.line.colors
The colors of the lines connecting the tick marks.
Default: ['green', 'red']

chart.line.shadow.color
The color of the lines shadow (if any).
Default: rgba(0,0,0,0) (invisible)

chart.line.shadow.offsetx
The X offset of the lines shadow.
Default: 3

chart.line.shadow.offsety
The Y offset of the lines shadow.
Default: 3

chart.line.shadow.blur
The severity of the line shadows blurring effect.
Default: 2

chart.line.stepped
Like the line chart, Scatter chart lines can be stepped by setting this to true
Default: false

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.halign
Instead of specifying the exact x/y coordinates, you can use this property to simply specify whether the key hould be aligned left or right.
Default: right

chart.key.position
Determines the position of the key.Either graph (default), or gutter.
Default: graph

chart.key.position.x
This allows you to specify a specific X coordinate for the key.
Default: null

chart.key.position.y
This allows you to specify a specific Y coordinate for the key.
Default: null

chart.key.position.gutter.boxed
If you have the key in gutter mode (ie horizontal), this allows you to give a background color.
Default: true

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

chart.key.color.shape
This can be square, circle or line and controls how the color indicators in the key appear.
Default: square

chart.key.linewidth
The line width of the surrounding border on the key.
Default: 1

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.defaultcolor
This is the default color of tick marks, which is used if a color isn't given.
Default: #000

chart.boxplot.width
This stipulates the default width of boxes. For more on boxplots, see here.
Default: 8



Box plots

Instead of a single Y value, you have the ability to specify an array of 5, 6, 7 or 8 values, which will be used to make a box plot. The example page shows a box plot. These values are (in order):

<script>
    scatter8 = new RGraph.Scatter('scatter8', [
                                               [10,[1,1,16,24,24, 'red', 'green']],
                                               [105,[5,10,15,25,25, 'red', 'green']],
                                               [125,[10,15,25,35,45, 'red', 'green']],
                                               [325,[10,15,25,35,45, 'red', 'green', 30]]
                                              ]);
    scatter8.Set('chart.title', 'An example of a boxplot');
    scatter8.Set('chart.labels', ['Q1', 'Q2', 'Q3', 'Q4']);
    scatter8.Set('chart.xmax', 365);
    scatter8.Set('chart.ymax', 50);
    scatter8.Set('chart.boxplot.width', 12); // The default width
    scatter8.Draw();
</script>

Specific points for labels on the X axis

Instead of a simple string which is used as the label, each entry of the chart.labels array can be a two element array consisting of the label, and the X value that the label should be placed at. For example:

scatter.Set('chart.labels', [
                             ['Quarter 1', 0],
                             ['Quarter 2', 90],
                             ['Quarter 3', 181],
                             ['Quarter 4', 273]
                            ]);

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 object data  The graph data
    * @param number x     The X coordinate
    * @param number y     The Y coordinate
    * @param number xVal  The X value
    * @param number yVal  The Y value
    * @param number xMax  The maximum X scale value
    * @param number xMax  The maximum Y scale value
    * @param string color The color of the tickmark
    */
    function myTick (obj, data, x, y, xVal, yVal, xMax, yMax, color)
    {
        // Draw your custom tick here
    }
</script>

Note about colors and the key

If you're using a key you may need to set chart.line.colors to allow it to use the correct colors. For example:

myObj.Set('chart.line.colors', ['red','green','blue']);