MENU
.net Powerful JavaScript charts
About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 16 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.20, 1st December 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 »

The measureText function

The measureText function allows you to measure the horizontal width of your text. It returns an object - the only property of which is the width setting. Unfortunately, it does not provide the height - however, there are ways around this. The way that RGraph uses is to add the text to a span tag which is positioned off-screen. The offsetHeight is then read. The text must of course use the same font, size, bold and italic settings as the text that is to be drawn on the canvas.

Adding elements to the dom like this is a slow process and can slow down animations - so RGraph employs caching so that it's only necessary to do it once.

Arguments to the function

An example

<script>
    window.onload = function ()
    {
        var canvas  = document.getElementById("cvs");
        var context = canvas.getContext('2d');

        // Note how the function returns an object of which the width is a property
        width = context.measureText('Some text to measure').width;
    }
</script>