The drawing API Image object

Summary: The drawing API Image object which be added to your chart and provides a way for you to add images to your charts. These images can be interactive so can provide a way for you to give your users extra information

[No canvas support]

The image object allows you to add images to your charts. The image can, for example, be used as a way to provide an extra tooltip to your users or it can be used as a background image instead of using the built-in backgroundImage option.

The advantage in this case is that by using the image drawing object as the background image instead of specifying a background image for the Line chart (which you can do with the backgroundImage option) is that you can click the background and have the click event triggered or show a tooltip.


Usage example

<script>
    new RGraph.Line({
        id: 'cvs',
        data: [4,9,1,3,2,6,5],
        options: {
            xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
            marginInner: 5,
            tickmarksStyle: 'endcircle',
            colors: ['black'],
            tooltips: ['A','B','C','D','E','F','G'],
            marginBottom: 35,
            textSize: 10
        }
    }).draw();

    image = new RGraph.Drawing.Image({
        id: 'cvs',
        x: 40,
        y: 40,
        src: '/images/logo.png',
        options: {
            width: 32,
            height: 32,
            tooltips: ['Your logo!'],
            textSize: 10
        }
    }).draw();
</script>

Properties

Configuration properties

PropertyDescriptionDefault
widthIf you want to stretch or shrink your image - use this to set the width.null
heightIf you want to stretch or shrink your image - use this to set the height.null
halignThis determines the horizontal alignment of the image (left/center/right).left
valignThis determines the vertical alignment of the image (bottom/center/top).top
width
If you want to stretch or shrink your image - use this to set the width.
Default: null

height
If you want to stretch or shrink your image - use this to set the height.
Default: null

halign
This determines the horizontal alignment of the image (left/center/right).
Default: left

valign
This determines the vertical alignment of the image (bottom/center/top).
Default: top

Background properties

PropertyDescriptionDefault
backgroundColorThe background color for the image. This is effective if your image uses transparency.rgba(0,0,0,0)
backgroundColor
The background color for the image. This is effective if your image uses transparency.
Default: rgba(0,0,0,0)

Color properties

PropertyDescriptionDefault
colorsAlphaUse this to set the alpha value (the transparency) of the image.1
colorsAlpha
Use this to set the alpha value (the transparency) of the image.
Default: 1

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 background image. 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.onclick
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
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)
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 (for example a CSS border) then specify an object containing those values to this property. For example:
tooltips: {
    borderLeft: 'gray 2px solid',
    borderBottom: 'gray 2px solid'
}
null
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
The tooltip for the background image. 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: onclick

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

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)

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 (for example a CSS border) 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

Border properties

PropertyDescriptionDefault
borderThis controls whether a border is drawn around the image.false
borderLinewidthThis is the linewidth of the image.1
borderColorThis is the color of the border.black
borderRadiusThis is the severity of the curve used for the corners.0
border
This controls whether a border is drawn around the image.
Default: false

borderLinewidth
This is the linewidth of the image.
Default: 1

borderColor
This is the color of the border.
Default: black

borderRadius
This is the severity of the curve used for the corners.
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
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 image 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 image.
y The Y coordinate of the image.
width The width of the image.
height The height of the image.
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>
    image.canvas.onclick = function (e)
    {
        RGraph.redraw();
    
        var canvas = e.target,
            obj    = canvas.__object__,
            shape  = obj.getShape(e);

        if (shape) {            
            // Highlight the main body of the Image object.
            obj.path(
                'lw 10 b r % % % % s black f rgba(255,0,0,0.25)',
                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:

var obj = new RGraph.Drawing.Image({
    id: 'cvs',
    x: 64,
    y: 64,
    src: '/images/logo.png',
}).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>
    image = new RGraph.Drawing.Image({
        id: 'cvs',
        x: 50,
        y: 50,
        src: '/images/logo.png',
        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>