The Background object
The background object allows you to add a background to your charts should
you
need to. An example use might be when you want the background grid to be
clickable (ie have a tooltip). In this case, you
can use the Background drawing API
object (and its own margin settings) to
make a grid that spans the whole of the desired area.
The Background object can have its own tooltip and also its own
click
and mousemove
events so if you want to make the whole
chart area clickable this is one way that you can do that.
Usage example
<script> new RGraph.Drawing.Background({ id: 'cvs', options: { title: 'The background object', tooltips: ['The drawing API background object'], tooltipsCss: { fontSize: '16pt' }, textSize: 18, textBold: true } }).draw(); </script>
Properties
- Margin properties
- Background properties
- X-axis properties
- Y-axis properties
- Labels and text properties
- Title properties
- Interactive features properties
- Miscellaneous properties
Margin properties
The top margin (where, by default, the title is positioned).
Default: 35
marginBottom
The left margin.
Default: 35
marginLeft
The left margin (where the title is positioned).
Default: 35
marginRight
The right margin.
Default: 35
Background properties
The number of background bars.
Default: 5
backgroundBarsColor1
The color of the background bars, (1 of 2).
Default: rgba(0,0,0,0)
backgroundBarsColor2
The color of the background bars, (2 of 2).
Default: rgba(0,0,0,0)
backgroundColor
The color of the background.
Default: NULL
backgroundGrid
Whether to draw the background grid or not.
Default: true
backgroundGridLinewidth
The width that the background grid lines are. Decimals (eg 0.5) are permitted.
Default: 1
backgroundGridBorder
Determines whether a border is drawn around the grid.
Default: true
backgroundGridColor
Determines what color the background grid should be.
Default: #ddd
backgroundGridHlines
Determines whether to draw the horizontal gridlines.
Default: true
backgroundGridVlines
Determines whether to draw the vertical gridlines.
Default: true
backgroundGridHlinesCount
When using the autofit option this allows you to specify how many horizontal gridlines you want.
Default: 5
backgroundGridVlinesCount
When using the autofit option this allows you to specify how many vertical gridlines you want.
Default: 20
backgroundGridDashed
If you want to have your background grid dashed then set this to true.
Default: false
backgroundGridDotted
If you want to have your background grid dotted then set this to true. This takes precedence over dashed lines.
Default: false
backgroundImage
If you want to specify a background image to use on your chart, specify it with this property. If you use effects with a background image on your chart it may make the effect flicker. Using a background image with the draw event may mean that the event fires twice. There is a property that you can check though that is set when the image has loaded:
obj.__rgraph_background_image_loaded__
Simply check this flag in your draw event. If it's true then the background image has loaded.Default: null
backgroundImageStretch
By default, your background image is stretched (if necessary) to cover the whole chart area (margins not included). If this is not what you want then set this property to false.
Default: true
backgroundImageX
The X position of the image. The coordinates are the top left corner of the image.
Default: null
backgroundImageY
The Y position of the image. The coordinates are the top left corner of the image.
Default: null
backgroundImageW
The width of the image. If you have a large
canvas
with many charts - you may need to specify this.Default: null
backgroundImageH
The height of the image. If you have a large
canvas
with many charts - you may need to specify this.Default: null
backgroundImageAlign
Instead of specifying the coordinates of the image, you can instead simply align it top, bottom, left or right. Examples are:
- top left
- bottom right
- bottom
- right
Default: null
backgroundImageAlpha
The alpha value (the
opacity
) of the image.Default: 1
X-axis properties
This allows you to specify a title for the
X-axis
.Default: none
xaxisTitleSize
This allows you to specify a size for the
X-axis
title.Default: null
xaxisTitleFont
This allows you to specify a font for the
X-axis
title.Default: null
xaxisTitleBold
This controls whether the
X-axis
title is bold or not.Default: null
xaxisTitleItalic
This controls whether the
X-axis
title is italic or not.Default: null
xaxisTitleColor
The color of the
X-axis
title.Default: null
xaxisTitleX
By giving this you can specifically set the X position of the
X-axis
title.Default: null
xaxisTitleY
By giving this you can specifically set the Y position of the
X-axis
title.Default: null
xaxisTitlePos
This is multiplied with the margin to give the position of the
X-axis
title.Default: null
Y-axis properties
This allows you to specify a title for the
Y-axis
.Default: none
yaxisTitleSize
This allows you to specify a size for the
Y-axis
title.Default: null
yaxisTitleFont
This allows you to specify a font for the
Y-axis
title.Default: null
yaxisTitleBold
This controls whether the
Y-axis
title is bold or not.Default: null
yaxisTitleItalic
This controls whether the
Y-axis
title is italic or not.Default: null
yaxisTitleColor
This controls what color the
Y-axis
is.Default: black
yaxisTitlePos
This is multiplied with the margin to give the position of the
Y-axis
title.Default: null
yaxisTitleX
By giving this you can specifically set the X position of the
Y-axis
title.Default: null
yaxisTitleY
By giving this you can specifically set the Y position of the
Y-axis
title.Default: null
Labels and text properties
Property | Description | Default |
---|---|---|
textSize | The size (in points) of the labels. | 12 |
textFont | The font used to render the text. | Arial, Verdana, sans-serif |
textColor | The color used to render the text. | black |
textBold | Whether the text is bold or not. | false |
textItalic | Whether the text is italic or not. | false |
textAccessible | A new feature in 2016 that allows you to use DOM text in place of canvas text. It makes for much higher quality text that you can also select if desired (for copy/paste operations). It won't fit all situations and you can read more about the DOM text feature here. A good way to control borders/margins/padding etc is not to set them on the canvas but to wrap the canvas in a div and set them on that like this:
<div style="margin-left: 50px; display: inline-block"> <canvas id="cvs" width="650" height="250"></canvas> </div> | false |
text | This allows you to add custom text to your chart if you want to. There's a dedicated page that describes this option here. | null |
The size (in points) of the labels.
Default: 12
textFont
The font used to render the text.
Default: Arial, Verdana, sans-serif
textColor
The color used to render the text.
Default: black
textBold
Whether the text is bold or not.
Default: false
textItalic
Whether the text is italic or not.
Default: false
textAccessible
A new feature in 2016 that allows you to use
DOM
text in place of canvas
text. It makes for much higher quality text that you can also select if desired (for copy/paste operations). It won't fit all situations and you can read more about the DOM text feature here. A good way to control borders/margins/padding etc is not to set them on the canvas
but to wrap the canvas
in a div
and set them on that like this:
<div style="margin-left: 50px; display: inline-block"> <canvas id="cvs" width="650" height="250"></canvas> </div>
Default: false
text
This allows you to add custom text to your chart if you want to. There's a dedicated page that describes this option here.
Default: null
Title properties
The title of the chart, if any.
Default: null
titleFont
The font that the title is rendered in. If not specified the
textFont
setting is used (usually Arial
).Default: null
titleSize
The size of the title. If not specified the size is usually
4pt
bigger than the textSize
setting.Default: null
titleBold
Whether the title is bold or not.
Default: null
titleItalic
Whether the title is italic or not.
Default: null
titleColor
The color of the title.
Default: null
titleX
To give the exact X coordinate for the title - use this. This can also be a string like this:
"-5"
- in which case it's converted to a number and added to the calculated coordinate - allowing you to adjust the calculated coordinate.Default: null
titleY
To give the exact Y coordinate for the title - use this. This can also be a string like this:
"-5"
- in which case it's converted to a number and added to the calculated coordinate - allowing you to adjust the calculated coordinate.Default: null
titleHalign
The horizontal alignment of the title.
Default: center (can change depending on other options)
titleValign
The vertical alignment of the title.
Default: center (can change depending on other options)
titleOffsetx
You can use this property to adjust the positioning of the title in the horizontal direction (positive values adjust the title to the right and negative values adjust it to the left).
Default: 0
titleOffsety
You can use this property to adjust the positioning of the title in the vertical direction (positive values adjust the title downwards and negative values adjust it upwards).
Default: 0
titleSubtitle
The subtitle of the chart. If a subtitle is specified the title is moved up to accommodate it. As such you might need to give a larger
marginTop
value.Default: null
titleSubtitleSize
The size of the font used to render the subtitle.
Default: null
titleSubtitleColor
The color of the subtitle.
Default: #aaa
titleSubtitleFont
The font used to render the subtitle.
Default: null
titleSubtitleBold
Whether the subtitle is bold or not.
Default: null
titleSubtitleItalic
Whether the subtitle is italic or not.
Default: null
titleSubtitleOffsetx
Use this property to adjust the horizontal position of the subtitle.
Default: 0
titleSubtitleOffsety
Use this property to adjust the vertical position of the subtitle.
Default: 0
Interactive features properties
Property | Description | Default |
---|---|---|
tooltips | A numerically indexed array of tooltips that are shown when a bar is clicked. These can contain HTML . | null |
tooltipsEvent | This is the event that triggers the tooltips. It can be either click or mousemove . | click |
tooltipsCssClass | This is the name of the CSS class the chart uses. | RGraph_tooltip |
tooltipsOverride | 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. | null |
tooltipsHighlight | Set this to false if you don't want your charts to be highlighted. | true |
tooltipsNohideonclear | Not an option that you'll need particularly often if at all. Setting this to true means that when you call the RGraph.clear API function the tooltip DOES NOT get hidden. | false |
tooltipsFormattedPoint | When using formatted tooltip strings this is used as the point when using the %{value_formatted} option. | . |
tooltipsFormattedThousand | When using formatted tooltip strings this is used as the thousand separator when using the %{value_formatted} option. | , |
tooltipsFormattedDecimals | When using formatted tooltip strings this specifies the number of decimals when using the %{value_formatted} option. | 0 |
tooltipsFormattedUnitsPre | When using formatted tooltip strings these units are prepended to the number when using the %{value_formatted} option. | (an empty string) |
tooltipsFormattedUnitsPost | When using formatted tooltip strings these units are appended to the number when using the %{value_formatted} option. | (an empty string) |
tooltipsFormattedListType | With this property you can switch between an unordered list (the default) and an ordered list. Possible values are ul and ol . | ul |
tooltipsFormattedListItems | This should be a two-dimensional array of the list items that are to be shown for all of the tooltips. An example of this property is:tooltipsFormattedListItems: [ ['Bill','Jerry','Berty'], // First tooltip ['Gill','Carrie','Lucy'], // Second tooltip ['Pob','Nobby','Hilda'] // Third tooltip ]You can use CSS to style this list - for example:.RGraph_tooltip ul#rgraph_formatted_tooltips_list li { text-align: left; color: yellow; } | null |
tooltipsFormattedTableHeaders | When showing a table in the tooltips this can be an array of headers for the table. These are added to the tooltip using th tags. | null |
tooltipsFormattedTableData | This is the data that is added to the table. This is a 3-dimensional array so it's easy to make a mistake. See the example, copy the code from it and then modify it suit. You'll create fewer bugs this way. | null) |
tooltipsPointer | By default the tooltips have a small triangular pointer that points to the shape that was clicked on. You can turn this off with this property. | true |
tooltipsPointerCss | If you want any CSS values applied to the tooltips pointer (a CSS border, for example) then specify an object containing those values to this property. For example: tooltips: { borderLeft: 'gray 2px solid', borderBottom: 'gray 2px solid' } | null |
tooltipsPositionStatic | The new default (as of August 2020) is for tooltips to be positioned statically and not be dependent on the mouse position. If you don't want this for whatever reason, you can disable it with this setting. When you set it to false tooltips are positioned next to the mouse pointer. | true |
tooltipsCss | If you want to specify some CSS that gets applied to all of the tooltips, but don't want to use the RGraph.tooltips.style object (which gets applied to all of the tooltips on the page for every chart) you can use this property to give some per-object CSS for the tooltips. These are CSS styles that get applied to all of the tooltips for the specific object only. It should look like this:tooltipsCss: { fontFamily: 'Verdana', fontSize: '20pt' } | null |
tooltipsOffsetx | This property allows you to shift the tooltips left or right. | 0 |
tooltipsOffsety | This property allows you to shift the tooltips up or down. | 0 |
A numerically indexed array of tooltips that are shown when a bar is clicked. These can contain
HTML
.Default: null
tooltipsEvent
This is the event that triggers the tooltips. It can be either
click
or mousemove
.Default: click
tooltipsCssClass
This is the name of the
CSS
class the chart uses.Default: RGraph_tooltip
tooltipsOverride
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
tooltipsHighlight
Set this to false if you don't want your charts to be highlighted.
Default: true
tooltipsNohideonclear
Not an option that you'll need particularly often if at all. Setting this to true means that when you call the
RGraph.clear
API
function the tooltip DOES NOT get hidden.Default: false
tooltipsFormattedPoint
When using formatted tooltip strings this is used as the point when using the
%{value_formatted}
option.Default: .
tooltipsFormattedThousand
When using formatted tooltip strings this is used as the thousand separator when using the
%{value_formatted}
option.Default: ,
tooltipsFormattedDecimals
When using formatted tooltip strings this specifies the number of decimals when using the
%{value_formatted}
option.Default: 0
tooltipsFormattedUnitsPre
When using formatted tooltip strings these units are prepended to the number when using the
%{value_formatted}
option.Default: (an empty string)
tooltipsFormattedUnitsPost
When using formatted tooltip strings these units are appended to the number when using the
%{value_formatted}
option.Default: (an empty string)
tooltipsFormattedListType
With this property you can switch between an unordered list (the default) and an ordered list. Possible values are
ul
and ol
.Default: ul
tooltipsFormattedListItems
This should be a two-dimensional array of the list items that are to be shown for all of the tooltips. An example of this property is:
tooltipsFormattedListItems: [ ['Bill','Jerry','Berty'], // First tooltip ['Gill','Carrie','Lucy'], // Second tooltip ['Pob','Nobby','Hilda'] // Third tooltip ]You can use
CSS
to style this list - for example:.RGraph_tooltip ul#rgraph_formatted_tooltips_list li { text-align: left; color: yellow; }
Default: null
tooltipsFormattedTableHeaders
When showing a table in the tooltips this can be an array of headers for the table. These are added to the tooltip using
th
tags.Default: null
tooltipsFormattedTableData
This is the data that is added to the table. This is a 3-dimensional array so it's easy to make a mistake. See the example, copy the code from it and then modify it suit. You'll create fewer bugs this way.
Default: null)
tooltipsPointer
By default the tooltips have a small triangular pointer that points to the shape that was clicked on. You can turn this off with this property.
Default: true
tooltipsPointerCss
If you want any
CSS
values applied to the tooltips pointer (a CSS
border, for example) then specify an object containing those values to this property. For example: tooltips: { borderLeft: 'gray 2px solid', borderBottom: 'gray 2px solid' }
Default: null
tooltipsPositionStatic
The new default (as of August 2020) is for tooltips to be positioned statically and not be dependent on the mouse position. If you don't want this for whatever reason, you can disable it with this setting. When you set it to
false
tooltips are positioned next to the mouse pointer.Default: true
tooltipsCss
If you want to specify some
CSS
that gets applied to all of the tooltips, but don't want to use the RGraph.tooltips.style
object (which gets applied to all of the tooltips on the page for every chart) you can use this property to give some per-object CSS
for the tooltips. These are CSS
styles that get applied to all of the tooltips for the specific object only. It should look like this:tooltipsCss: { fontFamily: 'Verdana', fontSize: '20pt' }
Default: null
tooltipsOffsetx
This property allows you to shift the tooltips left or right.
Default: 0
tooltipsOffsety
This property allows you to shift the tooltips up or down.
Default: 0
Miscellaneous properties
By default this is
null
but you can set it to a function if you wish so that function is called to do the chart highlighting. It's passed the shape object as an argument.Default: null
highlightStroke
If you use tooltips, this controls the color of the highlight stroke.
Default: rgba(0,0,0,0)
highlightFill
If you use tooltips, this controls the color of the highlight fill.
Default: rgba(255,255,255,0.7)
Methods
obj.get(name)
An accessor that you can use to retrieve the values of properties.
obj.set(name, value)
An accessor that you can use to set the values of properties.
obj.getShape(event)
This method makes it easy to get hold of the background when it has been clicked on or hovered over. It returns an object which has the following indexes available:
object |
The chart object. |
dataset |
As there's only ever one element this is always zero. |
index |
As there's only ever one element this is always zero. |
sequentialIndex |
As there's only ever one element this is always zero. |
tooltip |
If a tooltip is associated with the Background object this will be it.id:
strings are expanded for you (where the tooltip text is retrieved from the HTML
tag with the matching ID).
|
<script>
background.canvas.onclick = function (e)
{
RGraph.redraw();
var canvas = e.target,
obj = canvas.__object__,
shape = obj.getShape(e),
prop = obj.properties,
width = canvas.width - prop.marginLeft - prop.marginRight,
height = canvas.height - prop.marginTop - prop.marginBottom;
if (shape) {
// Highlight the main body of the Background object.
obj.path(
'lw 10 b r % % % % s black f rgba(255,0,0,0.25)',
prop.marginLeft, prop.marginTop, width, height
);
}
}
</script>
obj.on(event, function)
This method can be used to set an event listener on an object.
It operates similarly to the jQuery
on
function.
The first argument is the event that you wish to attach to and the second
is the handler function. For example:
obj.on('draw', function (obj)
{
// Put your event code here
});
The function is useful if you use method chaining when creating your charts:
var obj = new RGraph.Drawing.Background({ id: 'cvs', }).on('draw', function (obj) { // Put your draw event code here }).on('click', function (e, shape) { // Handle the click event }).draw();
obj.exec(function)
The exec function is documented here.