The drawing API rect object

Summary: The drawing API rect object which be added to your chart and provides a way for you to give your charts extra interactivity

[No canvas support]

The rect object allows you to add a rectangle to your charts. The rectangle can be used as a way to provide an extra tooltip (for example) to your users.


Usage example

<script>
    line = new RGraph.Line({
        id: 'cvs',
        data: [4,9,1,3,2,6,5],
        options: {
            axes: false,
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            xaxisLabels: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
            marginInner: 5,
            tickmarksStyle: 'none',
            shadow: false,
            spline: true,
            colors: ['black']
        }
    }).draw()
    
    rect = new RGraph.Drawing.Rect({
        id: 'cvs',
        x: 35,
        y: line.canvas.height - 35 - 50,
        width: 50,
        height: 50,
        options: {
            colorsStroke: 'transparent',
            colorsFill: 'rgba(255,255,0,0.7)',
            shadow: true,
            shadowColor: '#aaa',
            shadowBlur: 2,
            shadowOffsetx: 2,
            shadowOffsety: 2,
            tooltips: ['<b>Example</b><br />This is an example tooltip'],
            highlightStroke: 'transparent'
        }
    }).draw()
</script>

Properties

Color properties

PropertyDescriptionDefault
colorsStrokeThe color used to stroke the circle.transparent
colorsFillThe color used to fill the circle.red
colorsStroke
The color used to stroke the circle.
Default: transparent

colorsFill
The color used to fill the circle.
Default: red

Shadow properties

PropertyDescriptionDefault
shadowThis controls whether the shadow is enabled or not.false
shadowColorThe color of the shadow.gray
shadowOffsetxThe X offset that the shadow is drawn at.3
shadowOffsetyThe Y offset that the shadow is drawn at.3
shadowBlurThe severity of the shadow blur.5
shadow
This controls whether the shadow is enabled or not.
Default: false

shadowColor
The color of the shadow.
Default: gray

shadowOffsetx
The X offset that the shadow is drawn at.
Default: 3

shadowOffsety
The Y offset that the shadow is drawn at.
Default: 3

shadowBlur
The severity of the shadow blur.
Default: 5

Interactive features properties

PropertyDescriptionDefault
tooltipsThe tooltip for the circle. Even though you can only have one - this should still be an array: obj.set('tooltips', ['The tooltip']);null
tooltipsEventThis can be click or mousemove and controls what event is used to trigger the tooltip.click
tooltipsHighlightThis stipulates whether the circle will be highlighted when the tooltip is shown.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
tooltips
The tooltip for the circle. Even though you can only have one - this should still be an array: obj.set('tooltips', ['The tooltip']);
Default: null

tooltipsEvent
This can be click or mousemove and controls what event is used to trigger the tooltip.
Default: click

tooltipsHighlight
This stipulates whether the circle will be highlighted when the tooltip is shown.
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

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
highlightStrokeThis is the color that the circle is highlighted (the stroke) in when the tooltip is shown.black
highlightFillThis is the color that the circle is highlighted in (the fill) when the tooltip is shown.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
This is the color that the circle is highlighted (the stroke) in when the tooltip is shown.
Default: black

highlightFill
This is the color that the circle 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 rectangle when it has been clicked on or hovered over. It returns an object which has the following indexes available:
object The chart object.
x The X coordinate of the rectangle.
y The Y coordinate of the rectangle.
width The width of the rectangle.
height The height of the rectangle.
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 Rect 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>
    rect.canvas.onclick = function (e)
    {
        RGraph.redraw();
    
        var canvas = e.target,
            obj    = canvas.__object__,
            shape  = obj.getShape(e);

        if (shape) {            
            // Highlight the rectangle.
            RGraph.path({
                object: obj,
                  path: 'lw 10 b r % % % % s black f rgba(255,0,0,0.25)',
                  args: [shape.x, shape.y, shape.width, shape.height]
            });
        }
    }
</script>

obj.on(event, function)

This method can be used to set an event listener on an object. It operates in a similar way 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.Rect({

        id: 'cvs',
         x: 50,
         y: 50,
     width: 100,
    height: 100,

}).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.

Custom RGraph events that are available

Custom RGraph events are events that RGraph fires when certain actions occur. There is a more comprehensive list of these events here . Here's some example code that show you how to add these events to your chart.

There's more documentation about events available here: Summary of events documentation

<script>
    rect = new RGraph.Drawing.Rect({
        id: 'cvs',
        x: 5,
        y: 5,
        width: 100,
        height: 100,
        options: {
        }
    }).draw().on('click', function (e, shape)
    {
        // Add your click event listener code here

    }).on('mousemove', function (e, shape)
    {
        // Add your mousemove event listener code here

    }).on('mouseover', function (e, shape)
    {
        // Add your mouseover event listener code here

    }).on('mouseout', function (obj)
    {
        // Add your mouseout event listener code here
    });
</script>