About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 15 years old) and has a wealth of features making it an ideal choice to show charts on your website.

More »

 

Download
Get the latest version of RGraph (version 6.17) from the download page. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

More »

 

License
RGraph can be used for free under the GPL or if that doesn't suit your situation there's an inexpensive (£99) commercial license available.

More »

The drawing API Background object API reference

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

Name: marginTop
Description: 
The top margin (where, by default, the title is positioned).
Default: 35
Name: marginBottom
Description: 
The left margin.
Default: 35
Name: marginLeft
Description: 
The left margin (where the title is positioned).
Default: 35
Name: marginRight
Description: 
The right margin.
Default: 35

Background properties

Name: backgroundBarsCount
Description: 
The number of background bars.
Default: 5
Name: backgroundBarsColor1
Description: 
The color of the background bars, (1 of 2).
Default: rgba(0,0,0,0)
Name: backgroundBarsColor2
Description: 
The color of the background bars, (2 of 2).
Default: rgba(0,0,0,0)
Name: backgroundColor
Description: 
The color of the background.
Default: NULL
Name: backgroundGrid
Description: 
Whether to draw the background grid or not.
Default: true
Name: backgroundGridLinewidth
Description: 
The width that the background grid lines are. Decimals (eg 0.5) are permitted.
Default: 1
Name: backgroundGridBorder
Description: 
Determines whether a border is drawn around the grid.
Default: true
Name: backgroundGridColor
Description: 
Determines what color the background grid should be.
Default: #ddd
Name: backgroundGridHlines
Description: 
Determines whether to draw the horizontal gridlines.
Default: true
Name: backgroundGridVlines
Description: 
Determines whether to draw the vertical gridlines.
Default: true
Name: backgroundGridHlinesCount
Description: 
When using the autofit option this allows you to specify how many horizontal gridlines you want.
Default: 5
Name: backgroundGridVlinesCount
Description: 
When using the autofit option this allows you to specify how many vertical gridlines you want.
Default: 20
Name: backgroundGridDashed
Description: 
If you want to have your background grid dashed then set this to true.
Default: false
Name: backgroundGridDotted
Description: 
If you want to have your background grid dotted then set this to true. This takes precedence over dashed lines.
Default: false
Name: backgroundImage
Description: 
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
Name: backgroundImageStretch
Description: 
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
Name: backgroundImageX
Description: 
The X position of the image. The coordinates are the top left corner of the image.
Default: null
Name: backgroundImageY
Description: 
The Y position of the image. The coordinates are the top left corner of the image.
Default: null
Name: backgroundImageW
Description: 
The width of the image. If you have a large canvas with many charts - you may need to specify this.
Default: null
Name: backgroundImageH
Description: 
The height of the image. If you have a large canvas with many charts - you may need to specify this.
Default: null
Name: backgroundImageAlign
Description: 
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
Name: backgroundImageAlpha
Description: 
The alpha value (the opacity) of the image.
Default: 1
Name: backgroundBorder
Description: 
You can use this property to specify that the chart area has a border around it.
Default: false
Name: backgroundBorderLinewidth
Description: 
The linewidth of the border that goes around the chart area.
Default: 1
Name: backgroundBorderColor
Description: 
The color of the border that goes around the chart area.
Default: #aaa
Name: backgroundBorderDashed
Description: 
Whether the border around the chart area is dashed or not.
Default: false
Name: backgroundBorderDotted
Description: 
Whether the border around the chart area is dotted or not.
Default: false
Name: backgroundBorderDashArray
Description: 
If the default dot or dash style is not to your liking you can use this property to specify your own style. It should be an array of two or more elements that specify the dashes and spaces - for example: backgroundBorderDashArray: [5,10],
Default: null

X-axis properties

Name: xaxisTitle
Description: 
This allows you to specify a title for the x-axis.
Default: none
Name: xaxisTitleSize
Description: 
This allows you to specify a size for the x-axis title.
Default: null
Name: xaxisTitleFont
Description: 
This allows you to specify a font for the x-axis title.
Default: null
Name: xaxisTitleBold
Description: 
This controls whether the x-axis title is bold or not.
Default: null
Name: xaxisTitleItalic
Description: 
This controls whether the x-axis title is italic or not.
Default: null
Name: xaxisTitleColor
Description: 
The color of the x-axis title.
Default: null
Name: xaxisTitleX
Description: 
By giving this you can specifically set the X position of the x-axis title.
Default: null
Name: xaxisTitleY
Description: 
By giving this you can specifically set the Y position of the x-axis title.
Default: null
Name: xaxisTitlePos
Description: 
This is multiplied with the margin to give the position of the x-axis title.
Default: null

Y-axis properties

Name: yaxisTitle
Description: 
This allows you to specify a title for the y-axis.
Default: none
Name: yaxisTitleSize
Description: 
This allows you to specify a size for the y-axis title.
Default: null
Name: yaxisTitleFont
Description: 
This allows you to specify a font for the y-axis title.
Default: null
Name: yaxisTitleBold
Description: 
This controls whether the y-axis title is bold or not.
Default: null
Name: yaxisTitleItalic
Description: 
This controls whether the y-axis title is italic or not.
Default: null
Name: yaxisTitleColor
Description: 
This controls what color the y-axis is.
Default: black
Name: yaxisTitlePos
Description: 
This is multiplied with the margin to give the position of the y-axis title.
Default: null
Name: yaxisTitleX
Description: 
By giving this you can specifically set the X position of the y-axis title.
Default: null
Name: yaxisTitleY
Description: 
By giving this you can specifically set the Y position of the y-axis title.
Default: null

Labels and text properties

Name: textSize
Description: 
The size (in points) of the labels.
Default: 12
Name: textFont
Description: 
The font used to render the text.
Default: Arial, Verdana, sans-serif
Name: textColor
Description: 
The color used to render the text.
Default: black
Name: textBold
Description: 
Whether the text is bold or not.
Default: false
Name: textItalic
Description: 
Whether the text is italic or not.
Default: false
Name: textAccessible
Description: 
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
Name: text
Description: 
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

Name: title
Description: 
The title of the chart, if any.
Default: null
Name: titleFont
Description: 
The font that the title is rendered in. If not specified the textFont setting is used (usually Arial).
Default: null
Name: titleSize
Description: 
The size of the title. If not specified the size is usually 4pt bigger than the textSize setting.
Default: null
Name: titleBold
Description: 
Whether the title is bold or not.
Default: null
Name: titleItalic
Description: 
Whether the title is italic or not.
Default: null
Name: titleColor
Description: 
The color of the title.
Default: null
Name: titleX
Description: 
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
Name: titleY
Description: 
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
Name: titleHalign
Description: 
The horizontal alignment of the title.
Default: center (can change depending on other options)
Name: titleValign
Description: 
The vertical alignment of the title.
Default: center (can change depending on other options)
Name: titleOffsetx
Description: 
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
Name: titleOffsety
Description: 
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
Name: titleSubtitle
Description: 
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
Name: titleSubtitleSize
Description: 
The size of the font used to render the subtitle.
Default: null
Name: titleSubtitleColor
Description: 
The color of the subtitle.
Default: #aaa
Name: titleSubtitleFont
Description: 
The font used to render the subtitle.
Default: null
Name: titleSubtitleBold
Description: 
Whether the subtitle is bold or not.
Default: null
Name: titleSubtitleItalic
Description: 
Whether the subtitle is italic or not.
Default: null
Name: titleSubtitleOffsetx
Description: 
Use this property to adjust the horizontal position of the subtitle.
Default:  0
Name: titleSubtitleOffsety
Description: 
Use this property to adjust the vertical position of the subtitle.
Default:  0

Interactive features properties

Name: tooltips
Description: 
A numerically indexed array of tooltips that are shown when a bar is clicked. These can contain html.
Default: null
Name: tooltipsEvent
Description: 
This is the event that triggers the tooltips. It can be either click or mousemove.
Default: click
Name: tooltipsCssClass
Description: 
This is the name of the css class the chart uses.
Default: RGraph_tooltip
Name: tooltipsOverride
Description: 
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
Name: tooltipsHighlight
Description: 
Set this to false if you don't want your charts to be highlighted.
Default: true
Name: tooltipsNohideonclear
Description: 
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
Name: tooltipsFormattedPoint
Description: 
When using formatted tooltip strings this is used as the point when using the %{value_formatted} option.
Default: .
Name: tooltipsFormattedThousand
Description: 
When using formatted tooltip strings this is used as the thousand separator when using the %{value_formatted} option.
Default: ,
Name: tooltipsFormattedDecimals
Description: 
When using formatted tooltip strings this specifies the number of decimals when using the %{value_formatted} option.
Default:  0
Name: tooltipsFormattedUnitsPre
Description: 
When using formatted tooltip strings these units are prepended to the number when using the %{value_formatted} option.
Default: (an empty string)
Name: tooltipsFormattedUnitsPost
Description: 
When using formatted tooltip strings these units are appended to the number when using the %{value_formatted} option.
Default: (an empty string)
Name: tooltipsFormattedListType
Description: 
With this property you can switch between an unordered list (the default) and an ordered list. Possible values are ul and ol.
Default: ul
Name: tooltipsFormattedListItems
Description: 
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
Name: tooltipsFormattedTableHeaders
Description: 
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
Name: tooltipsFormattedTableData
Description: 
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)
Name: tooltipsPointer
Description: 
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
Name: tooltipsPointerCss
Description: 
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
Name: tooltipsPointerOffsetx
Description: 
This allows you to adjust the vertical position of the tooltips pointer.
Default:  0
Name: tooltipsPointerOffsety
Description: 
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
Name: tooltipsPositionStatic
Description: 
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
Name: tooltipsCss
Description: 
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
Name: tooltipsOffsetx
Description: 
This property allows you to shift the tooltips left or right.
Default:  0
Name: tooltipsOffsety
Description: 
This property allows you to shift the tooltips up or down.
Default:  0

Miscellaneous properties

Name: highlightStyle
Description: 
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
Name: highlightStroke
Description: 
If you use tooltips, this controls the color of the highlight stroke.
Default: rgba(0,0,0,0)
Name: highlightFill
Description: 
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.