The drawing API text object

Summary: The drawing API text object which can be added to your charts to give extra text that is (optionally) clickable, can have tooltips, events and is automatically redrawn by the RGraph ObjectRegistry.

[No canvas support]

The drawing API text object makes it easy to add text to your charts and being a drawing API object - you can also easily add event listeners or tooltips to it too. The chart to the right shows an example chart where the title is drawn using the drawing API text object.


Usage example

<script>
    line = new RGraph.Line({
        id: 'cvs',
        data: [4,8,6,5,3,2,2,5],
        options: {
            marginInner: 5,
            xaxisLabels: ['Lyra','Simon','Jeff','Pete','Lola','Luis','Fred','John'],
            colors: ['red'],
            linewidth: 3,
            spline: true,
            yaxisColor: 'pink',
            axes: false,
            backgroundGridBorder: false,
            backgroundGridVlines: false,
            shadow: false,
            tickmarksSize: 5
        }
    }).draw();
    
    x = line.canvas.width / 2;
    y = line.get('marginTop') + 5;

    text = new RGraph.Drawing.Text({
        id: 'cvs',
        x: x,
        y: y,
        text: 'An example of the \r\ndrawing API text object',
        options: {
            valign: 'top',
            halign: 'center',
            marker: false,
            size: 14,
            tooltips: ['The drawing API text object'],
            tooltipsHighlight: false,
            tooltipsEvent: 'mousemove',
            bounding: false,
            shadow: true,
            shadowColor: '#ddd',
            shadowOffsetx: 2,
            shadowOffsety: 2,
            shadowBlur: 2
        }
    }).on('beforedraw', function (obj)
    {
        RGraph.setShadow(obj,'#aaa',2,2,5);
    }).on('draw', function (obj)
    {
        RGraph.noShadow(obj);
    }).draw();
</script>

Properties

Configuration properties

PropertyDescriptionDefault
textSizeThe size of the text (in points).10
textFontThe font that the text is rendered in.Arial
textBoldWhether the text is bold or not.false
textItalicWhether the text is italic or not.false
angleThe angle that the text is shown at. If angles other than 0/90/180/270 are used then neither events nor tooltips will function.0
markerWhether a marker is shown that illustrates the coordinates given as the position of the text.false
halignThe horizontal alignment of the text. This can be left, center or right.left
valignThe vertical alignment of the text. This can be bottom, center or top.bottom
textSize
The size of the text (in points).
Default: 10

textFont
The font that the text is rendered in.
Default: Arial

textBold
Whether the text is bold or not.
Default: false

textItalic
Whether the text is italic or not.
Default: false

angle
The angle that the text is shown at. If angles other than 0/90/180/270 are used then neither events nor tooltips will function.
Default: 0

marker
Whether a marker is shown that illustrates the coordinates given as the position of the text.
Default: false

halign
The horizontal alignment of the text. This can be left, center or right.
Default: left

valign
The vertical alignment of the text. This can be bottom, center or top.
Default: bottom

Color properties

PropertyDescriptionDefault
colorsAn array of colors for the text. Even though it's an array only the first color is used.['black']
colors
An array of colors for the text. Even though it's an array only the first color is used.
Default: ['black']

Bounding box properties

PropertyDescriptionDefault
boundingWhether the text is shown with a bounding box or not.false
boundingFillThe color used as the fill of the bounding box.rgba(255,255,255,0.7)
boundingStrokeThe color used as the stroke of the bounding box.#777
boundingShadowWhether the bounding box has a shadow or not.false
boundingShadowColorThe color of the shadow.#ccc
boundingShadowOffsetxThe X offset of the shadow.3
boundingShadowOffsetyThe Y offset of the shadow.3
boundingShadowBlurThe severity of the shadow blurring effect.3
bounding
Whether the text is shown with a bounding box or not.
Default: false

boundingFill
The color used as the fill of the bounding box.
Default: rgba(255,255,255,0.7)

boundingStroke
The color used as the stroke of the bounding box.
Default: #777

boundingShadow
Whether the bounding box has a shadow or not.
Default: false

boundingShadowColor
The color of the shadow.
Default: #ccc

boundingShadowOffsetx
The X offset of the shadow.
Default: 3

boundingShadowOffsety
The Y offset of the shadow.
Default: 3

boundingShadowBlur
The severity of the shadow blurring effect.
Default: 3

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
tooltipsCssClassThis is the name of the CSS class the chart uses.RGraph_tooltip
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

tooltipsCssClass
This is the name of the CSS class the chart uses.
Default: RGraph_tooltip

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

Link properties

PropertyDescriptionDefault
linkIf you want the text object to be a link you can specify a URL here. Remember to include the RGraph.common.dynamic.js file as well or it won't function. Remember that you can also use links in tooltips - which are HTML DIV tags at their core - so no special syntax is needed.null
linkTargetThis is used as the target window for the link (the name of the window to show the link in). By default this will open in the same window (_self) - but if you want it to open in a new window you can use _blank._self
linkOptionsOptions that will be passed to the window.open call. These are the same as the third argument to window.open - eg width=400,height=400.nothing
link
If you want the text object to be a link you can specify a URL here. Remember to include the RGraph.common.dynamic.js file as well or it won't function. Remember that you can also use links in tooltips - which are HTML DIV tags at their core - so no special syntax is needed.
Default: null

linkTarget
This is used as the target window for the link (the name of the window to show the link in). By default this will open in the same window (_self) - but if you want it to open in a new window you can use _blank.
Default: _self

linkOptions
Options that will be passed to the window.open call. These are the same as the third argument to window.open - eg width=400,height=400.
Default: nothing

Shadow properties

PropertyDescriptionDefault
shadowWhether a drop shadow is applied.false
shadowColorThe color of the shadow.#ccc
shadowOffsetxThe X offset of the shadow.2
shadowOffsetyThe Y offset of the shadow.2
shadowBlurThe severity of the shadow blurring effect.3
shadow
Whether a drop shadow is applied.
Default: false

shadowColor
The color of the shadow.
Default: #ccc

shadowOffsetx
The X offset of the shadow.
Default: 2

shadowOffsety
The Y offset of the shadow.
Default: 2

shadowBlur
The severity of the shadow blurring effect.
Default: 3

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.#ccc
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: #ccc

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 Text object 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 text.
y The Y coordinate of the text.
width The width of the text.
height The height of the text.
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 Text 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>
    text.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 Text 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:

obj = new RGraph.Drawing.Text({
    
    id: 'cvs',
    x: 100,
    y: 100,
    text: 'Some sample text!',

}).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>
    text = new RGraph.Drawing.Text({
        id: 'cvs',
        x: 50,
        y: 50,
        text: 'Some example text!',
        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>