MENU
.net Powerful JavaScript charts
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 use for showing charts on your website.

More »

 

Download
Get the latest version of RGraph (version 6.19, 28th September 2024) from the download page. You can read the changelog here. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

Download »

 

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

More »

HOWTO: Add custom text to your chart

[No canvas support]

The RGraph.text function is a rewrite of the old version of the RGraph.text function which is more efficient, faster and easier to use than its predecessor. Instead of a standard list of arguments, it only takes one argument - an object map of the desired arguments (one of which is the chart object). This means that you no longer have to remember the correct order of the arguments - of which there are many.

One significant improvement of the new text function is that for horizontal and vertical text the coordinates of the texts' bounding box are retained. This means that you can test the coordinates for clicks to see if the user has clicked the text. This is what the drawing API text object does for you though - so if you want clickable text it's much easier to use that.

The drawing API text object

In this particular example, it would be easier for you to use the drawing api Text object as it has tooltips capability built-in so you wouldn't need to use the Rect object at all.

<script>
    new RGraph.Bar({
        id: 'cvs',
        data: [4,8,6,3,5,2,4],
        options: {
            xaxisLabels: ['Harry','Lou','Pete','Jim','Neil','Burt','Kim'],
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            textSize: 16,
            yaxis: false
        }
    }).on('draw', function (obj)
    {
        // The first argument is the object and the second is a map/object literal of options.
        // Available options are listed below.
        var coords = RGraph.text({
            object:      obj,
            font:       'Arial',
            size:       26,
            x:          200,
            y:          100,
            text:       'Sample!',
            halign:     'center',
            valign:     'center',
            bounding:   true,
            marker:     false,
            angle:      0,
            accessible: false
        });

        // Now make a (transparent) rectangle around the text
        var rect = new RGraph.Drawing.Rect({
            id: 'cvs',
            x: coords.x,
            y: coords.y,
            width: coords.width,
            height: coords.height,
            options: {
                tooltips: ['The rectangle tooltip!'],
                tooltipsCss: {
                    fontSize: '16pt'
                },
                colorsFill: 'rgba(0,0,0,0)',
                highlightFill: 'rgba(0,0,0,0)',
                highlightStroke: 'rgba(0,0,0,0)'
            }
        }).draw();

    }).draw();
</script>

Available properties

All of the available options that you can use are:

The defaults object

If you have a lot of bits of text to add to your canvas then you may want to use the defaults object. As it sounds, this object allows you to set the default values that are used for the text function so you can set the values once instead of on every call to the function. For example:

bar = new RGraph.Bar({
    id: 'cvs',
    data: [3,4,8,4,1,4,9],
    options: {
    }
}).draw();

RGraph.text.defaults = {
    font: 'Comic sans MS',
    size: 20,
    bold: true,
    color: 'blue',
    accessible: false
};

RGraph.text({
    object: bar,
    x: 50,
    y: 50,
    text: 'Some example text!'
});

RGraph.text({
    object: bar,
    x: 50,
    y: 100,
    italic: true,
    bold: false,
    text: 'Some more example text but in italic instead of bold!'
});