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 Poly object API reference

The Poly object (which is short for polygon) is a drawing api object which you can use to add regular or irregular shapes to your charts.


Usage example

View example on CodePen
<script>
    line = new RGraph.Line({
        id: 'cvs',
        data: [4,9,1,3,2,6,5],
        options: {
            spline: true,
            backgroundGridVlines:false,
            backgroundGridBorder:false,
            xaxis: false,
            yaxis: false,
            xaxisLabels: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
            marginInner: 5,
            tickmarksStyle: 'endcircle',
            tickmarksSize: 10,
            colors: ['black'],
            textSize: 16
        }
    }).draw();

    new RGraph.Drawing.Poly({
        id: 'cvs',
        coords: [[50,150],[75,25],[100,150]],
        options: {
            colorsFill: 'rgba(255,0,0,0.5)',
            colorsStroke: 'rgba(0,0,0,0)',
            shadow: true,
            shadowColor: '#000',
            shadowBlur: 2,
            shadowOffsetx: 3,
            shadowOffsety: 3,
            tooltips: ['<b>The Drawing API Poly object</b>'],
            tooltipsCss: {
                fontWeight: 'bold',
                fontSize: '16pt',
                textAlign: 'left'
            }
        }
    }).draw();
</script>

Properties

Color properties

Name: colorsStroke
Description: 
The stroke color used to draw the shape.
Default: 'black'
Name: colorsFill
Description: 
The fill color used to draw the shape.
Default: 'red'

Interactive features properties

Name: tooltips
Description: 
This can be an array of tooltips (only the first element is used) that are shown when the axis is clicked on.
Default: null
Name: tooltipsEvent
Description: 
This is the event that triggers the tooltips. It can be either click or mousemove.
Default: click
Name: tooltipsEffect
Description: 
The effect used for showing tooltips. Can be either fade or none.
Default: fade
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: 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: tooltipsEvent
Description: 
This can be click or mousemove and controls what event is used to trigger the tooltip.
Default: onclick
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

Shadow properties

Name: shadow
Description: 
This controls whether the shadow is enabled or not.
Default: false
Name: shadowColor
Description: 
The color of the shadow.
Default: rgba(0,0,0,0.2)
Name: shadowOffsetx
Description: 
The X offset that the shadow is drawn at.
Default: 3
Name: shadowOffsety
Description: 
The Y offset that the shadow is drawn at.
Default: 3
Name: shadowBlur
Description: 
The severity of the shadow blur.
Default: 5

Other text properties

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

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: 
This is the color that the poly is highlighted in (the stroke) when the tooltip is shown.
Default: rgba(0,0,0,0)
Name: highlightFill
Description: 
This is the color that the poly is highlighted in (the fill) when the tooltip is shown.
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 Poly object when it has been clicked on or hovered over. It returns an object which has the following indexes available:
object The chart object.
coords The coordinates of each point in the poly.
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 Poly 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>
    poly.canvas.onclick = function (e)
    {
        RGraph.redraw();
    
        var canvas = e.target,
            obj    = canvas.__object__,
            shape  = obj.getShape(e);

        if (shape) {
            // Highlight the Poly object.
            obj.path('lw 10 b m % %', shape.coords[0][0], shape.coords[0][1]);

            // Loop through the coordinates of the Poly object
            for (var i=1; i<shape.coords.length; ++i) {
                obj.context.lineTo(shape.coords[i][0]. shape.coords[i][1]);
            }

            // Close and stroke/fill the poly
            obj.path('c s black f red');
        }
    }
</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:

obj = new RGraph.Drawing.Poly({
    id: 'cvs',
    coords: [[50,150],[75,25],[100,150]],
}).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.