RGraph: HTML5 Javascript charts library - Scatter charts documentation
Scatter charts. 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.left', 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:
- The X value (required)
- The Y value (required)
- The color (optional)
- The tooltip for this mark (optional)
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);
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: true
chart.background.grid.autofit.numhlines
When using autofit this allows you to specify how many horizontal grid lines you want.
Default: 5
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
chart.background.image
If you want to specify a background image to use on your chart, specify it with this property.
Default: null
Labels and text
chart.labels
An array of the X labels for the chart.
Default: [] (An empty array)
chart.labels.above
If true this will show the values of the points above them.
Default: false
chart.labels.above.size
If you're showing the labels above the points, this controls their size.
Default: 8
chart.labels.above.decimals
If your points have lots of decimals, you use this option to curtail them.
Default: 0
chart.ylabels
A boolean (true or false) that controls whether the chart has Y labels.
Default: true
chart.ylabels.count
A value (1, 3, 5 or 10) that controls how many Y labels there are.
Default: 5
chart.ylabels.invert
Reverses the Y axis so that 0 is at the top, instead of the bottom.
Default: false
chart.ylabels.specific
You can use this option to give your own Y labels (eg ['Low', 'Medium', 'High'].
Default: null
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 chart). 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.left
The left gutter of the chart, (the gutter is where the labels and title are)).
Default: 25
chart.gutter.right
The right gutter of the chart, (the gutter is where the labels and title are).
Default: 25
chart.gutter.top
The top gutter of the chart, (the gutter is where the labels and title are).
Default: 25
chart.gutter.bottom
The bottom gutter of the chart, (the gutter is where the labels and title are).
Default: 25
Axis properties
chart.xmin
The minimum X axis value you wish to set.
Default: 0
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.noendytick
Whether to draw an end Y tick. Usually used when combining Scatter charts
Default: true
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.axis.color
The color of the axes.
Default: black
chart.noaxes
If this is set to true, no axes will be drawn.
Default: false
chart.xscale
If true then an X scale is drawn instead of X labels.
Default: false
chart.xscale.units.pre
These are units that are prepended to the number.
Default: none
chart.xscale.units.post
These are units that are appended to the number.
Default: none
chart.xscale.formatter
This can be a function, which is passed the object and the number and should return a formatted number (formatted as you wish).
Default: null
chart.xscale.numlabels
This is a number representing how many X labels there should be when you're using the X scale.
Default: 10
Titles
chart.title
The title of the scatter chart.
Default: none
chart.title.font
The font that the title is rendered in. If not specified the chart.text.font setting is used (usually Verdana)
Default: null
chart.title.size
The size of the title. If not specified the size is usually 2pt bigger than the chart.text.size setting.
Default: null
chart.title.bold
Whather the title is bold or not.
Default: true
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.xaxis.size
This allows you to specify a size for the X axis title.
Default: null
chart.title.xaxis.font
This allows to specify a font for the X axis title.
Default: null
chart.title.xaxis.bold
This controls whether the X axis title is bold or not.
Default: true
chart.title.yaxis
This allows to specify a title for the Y axis.
Default: none
chart.title.yaxis.size
This allows you to specify a size for the Y axis title.
Default: null
chart.title.yaxis.font
This allows to specify a font for the Y axis title.
Default: null
chart.title.yaxis.bold
This controls whether the Y axis title is bold or not.
Default: true
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.highlight
This controls whether points are highlighted when a tooltip is shown. If you're combining the Scatter with another chart
you may need to set this to false.
Default: true
chart.tooltips.css.class
This is the name of the CSS class the chart 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.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.coords.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.crosshairs.hlines
This determines whether the horizontal crosshair is shown.
Default: true
chart.crosshairs.vlines
This determines whether the vertical crosshair is shown.
Default: true
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 chart 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.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
chart.line.linewidth
This can be either a number or an array of numbers (for multiple lines) and controls the
linewidth that is used for the lines.
Default: 1
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 chart 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
chart.boxplot.capped
This stipulates whether the boxplot will have caps on the end of whickers. This could be useful if you're
creating a stock chart for example.For more on boxplots, see here.
Default: true
Methods
obj.getPoint(e)
This method makes it easier to get hold of which point on the Scatter chart has been hovered over. It returns an array of:
- The X coordinate of the point
- The Y coordinate of the point
- The data set. This corresponds to the appropriate dataset (since mutiple datasets can be given to the constructor)
- The index of the appropriate data point in the set
- The tooltip (if any) for that particular point
(If the return value is null, then no point is being hovered over.) An example usage is:
<script src="RGraph.common.core.js"></script>
<script src="RGraph.line.js"></script>
<canvas id="cvs" width="600" height="250" style="border: 1px gray dashed">[No canvas support]</canvas>
<script>
scatter = new RGraph.Scatter('cvs', [[5,1, 'red', 'A'],[4,2,, 'B'],[3,9,,'C'],[10,12,,'D'],[300,2,,'E']],
[[5,6, 'red', 'F'],[2,9,,'G'],[13,19,,'H'],[18,17,,'I'],[340,12,,'J']]);
scatter.Set('chart.xmax', 365);
scatter.Set('chart.line', true);
scatter.Set('chart.labels', ['Q1', 'Q2', 'Q3', 'Q4']);
scatter.Draw();
scatter.canvas.onmousemove = function (e)
{
var canvas = e.target;
var context = canvas.getContext('2d');
var obj = canvas.__object__;
var point = obj.getPoint(e);
if (point) {
p('\r\nX Coordinate: ' + point[0] + '\r\n' +
'Y Coordinate: ' + point[1] + '\r\n' +
'Dataset: ' + point[2] + '\r\n' +
'Datapoint: ' + point[3] + '\r\n' +
'Tooltip: ' + point[4] + '\r\n');
}
}
</script>
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):
- Lower whisker
- Bottom of box
- Middle value (which isn't necessarily in the center of the box - this depends on your data)
- Top of box
- Upper whisker
- Upper color (optional)
- Lower color (optional)
- Width (optional)
<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>
Capped ends
For better compatibility with stock charts, you can now specify that the horizontal lines at the ends of the box plot
are not shown with chart.boxplot.capped.
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 chart object
* @param object data The chart 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']);
Note about the oncrosshairs event
With the Scatter chart you can use the oncrosshairs custom event to pull out the crosshair coordinates, like this:
The coordinates are only available if you have them displayed.
function myFunc (obj)
{
var xCoord = obj.canvas.__crosshairs_x__;
var yCoord = obj.canvas.__crosshairs_y__;
}
RGraph.AddCustomEventListener(myScatter, 'oncrosshairs', myFunc);