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

View example on CodePen
<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

PropertyDescriptionDefault
marginTopThe top margin (where, by default, the title is positioned).35
marginBottomThe left margin.35
marginLeftThe left margin (where the title is positioned).35
marginRightThe right margin.35
marginTop
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

PropertyDescriptionDefault
backgroundBarsCountThe number of background bars.5
backgroundBarsColor1The color of the background bars, (1 of 2).rgba(0,0,0,0)
backgroundBarsColor2The color of the background bars, (2 of 2).rgba(0,0,0,0)
backgroundColorThe color of the background.NULL
backgroundGridWhether to draw the background grid or not.true
backgroundGridLinewidthThe width that the background grid lines are. Decimals (eg 0.5) are permitted.1
backgroundGridBorderDetermines whether a border is drawn around the grid.true
backgroundGridColorDetermines what color the background grid should be.#ddd
backgroundGridHlinesDetermines whether to draw the horizontal gridlines.true
backgroundGridVlinesDetermines whether to draw the vertical gridlines.true
backgroundGridHlinesCountWhen using the autofit option this allows you to specify how many horizontal gridlines you want.5
backgroundGridVlinesCountWhen using the autofit option this allows you to specify how many vertical gridlines you want.20
backgroundGridDashedIf you want to have your background grid dashed then set this to true.false
backgroundGridDottedIf you want to have your background grid dotted then set this to true. This takes precedence over dashed lines.false
backgroundImageIf 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.null
backgroundImageStretchBy 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.true
backgroundImageXThe X position of the image. The coordinates are the top left corner of the image.null
backgroundImageYThe Y position of the image. The coordinates are the top left corner of the image.null
backgroundImageWThe width of the image. If you have a large canvas with many charts - you may need to specify this.null
backgroundImageHThe height of the image. If you have a large canvas with many charts - you may need to specify this.null
backgroundImageAlignInstead 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
null
backgroundImageAlphaThe alpha value (the opacity) of the image.1
backgroundBarsCount
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:

Default: null

backgroundImageAlpha
The alpha value (the opacity) of the image.
Default: 1

X-axis properties

PropertyDescriptionDefault
xaxisTitleThis allows you to specify a title for the x-axis.none
xaxisTitleSizeThis allows you to specify a size for the x-axis title.null
xaxisTitleFontThis allows you to specify a font for the x-axis title.null
xaxisTitleBoldThis controls whether the x-axis title is bold or not.null
xaxisTitleItalicThis controls whether the x-axis title is italic or not.null
xaxisTitleColorThe color of the x-axis title.null
xaxisTitleXBy giving this you can specifically set the X position of the x-axis title.null
xaxisTitleYBy giving this you can specifically set the Y position of the x-axis title.null
xaxisTitlePosThis is multiplied with the margin to give the position of the x-axis title.null
xaxisTitle
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

PropertyDescriptionDefault
yaxisTitleThis allows you to specify a title for the y-axis.none
yaxisTitleSizeThis allows you to specify a size for the y-axis title.null
yaxisTitleFontThis allows you to specify a font for the y-axis title.null
yaxisTitleBoldThis controls whether the y-axis title is bold or not.null
yaxisTitleItalicThis controls whether the y-axis title is italic or not.null
yaxisTitleColorThis controls what color the y-axis is.black
yaxisTitlePosThis is multiplied with the margin to give the position of the y-axis title.null
yaxisTitleXBy giving this you can specifically set the X position of the y-axis title.null
yaxisTitleYBy giving this you can specifically set the Y position of the y-axis title.null
yaxisTitle
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

PropertyDescriptionDefault
textSizeThe size (in points) of the labels.12
textFontThe font used to render the text.Arial, Verdana, sans-serif
textColorThe color used to render the text.black
textBoldWhether the text is bold or not.false
textItalicWhether the text is italic or not.false
textAccessibleA 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
textThis allows you to add custom text to your chart if you want to. There's a dedicated page that describes this option here.null
textSize
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

PropertyDescriptionDefault
titleThe title of the chart, if any.null
titleFontThe font that the title is rendered in. If not specified the textFont setting is used (usually Arial).null
titleSizeThe size of the title. If not specified the size is usually 4pt bigger than the textSize setting.null
titleBoldWhether the title is bold or not.null
titleItalicWhether the title is italic or not.null
titleColorThe color of the title.null
titleXTo 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.null
titleYTo 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.null
titleHalignThe horizontal alignment of the title.center (can change depending on other options)
titleValignThe vertical alignment of the title.center (can change depending on other options)
titleOffsetxYou 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). 0
titleOffsetyYou 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). 0
titleSubtitleThe 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.null
titleSubtitleSizeThe size of the font used to render the subtitle.null
titleSubtitleColorThe color of the subtitle.#aaa
titleSubtitleFontThe font used to render the subtitle.null
titleSubtitleBoldWhether the subtitle is bold or not.null
titleSubtitleItalicWhether the subtitle is italic or not.null
titleSubtitleOffsetxUse this property to adjust the horizontal position of the subtitle. 0
titleSubtitleOffsetyUse this property to adjust the vertical position of the subtitle. 0
title
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

PropertyDescriptionDefault
tooltipsA numerically indexed array of tooltips that are shown when a bar is clicked. These can contain html.null
tooltipsEventThis is the event that triggers the tooltips. It can be either click or mousemove.click
tooltipsCssClassThis is the name of the css class the chart uses.RGraph_tooltip
tooltipsOverrideIf 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
tooltipsHighlightSet this to false if you don't want your charts to be highlighted.true
tooltipsNohideonclearNot 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
tooltipsFormattedPointWhen using formatted tooltip strings this is used as the point when using the %{value_formatted} option..
tooltipsFormattedThousandWhen using formatted tooltip strings this is used as the thousand separator when using the %{value_formatted} option.,
tooltipsFormattedDecimalsWhen using formatted tooltip strings this specifies the number of decimals when using the %{value_formatted} option. 0
tooltipsFormattedUnitsPreWhen using formatted tooltip strings these units are prepended to the number when using the %{value_formatted} option.(an empty string)
tooltipsFormattedUnitsPostWhen using formatted tooltip strings these units are appended to the number when using the %{value_formatted} option.(an empty string)
tooltipsFormattedListTypeWith this property you can switch between an unordered list (the default) and an ordered list. Possible values are ul and ol.ul
tooltipsFormattedListItemsThis 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
tooltipsFormattedTableHeadersWhen 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
tooltipsFormattedTableDataThis 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)
tooltipsPointerBy 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
tooltipsPointerCssIf 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
tooltipsPointerOffsetxThis allows you to adjust the vertical position of the tooltips pointer. 0
tooltipsPointerOffsetyThis allows you to adjust the vertical position of the tooltips pointer (for example, if you add a border you may need to move it down slightly). 0
tooltipsPositionStaticThe 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
tooltipsCssIf 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
tooltipsOffsetxThis property allows you to shift the tooltips left or right. 0
tooltipsOffsetyThis property allows you to shift the tooltips up or down. 0
tooltips
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

tooltipsPointerOffsetx
This allows you to adjust the vertical position of the tooltips pointer.
Default: 0

tooltipsPointerOffsety
This allows you to adjust the vertical position of the tooltips pointer (for example, if you add a border you may need to move it down slightly).
Default: 0

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

PropertyDescriptionDefault
highlightStyleBy 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.null
highlightStrokeIf you use tooltips, this controls the color of the highlight stroke.rgba(0,0,0,0)
highlightFillIf you use tooltips, this controls the color of the highlight fill.rgba(255,255,255,0.7)
highlightStyle
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.