MENU
.net Powerful JavaScript charts
About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 18 years old) and has a wealth of features making it an ideal choice to use for showing charts on your website.

More »

 

Version 7.20
Version 7.20 (released in June 2026) is the latest version of RGraph and the major change in this version is an update to the default values of properties making for better looking charts without having to set any properties. Read more about this and other changes in the changelog.

Download »

 

Download
Get the latest version of RGraph (version 7.20, 9th June 2026) 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 »

 

Latest forum posts
These are the latest support forum posts that have been posted or updated.

9th June, Richard
New version of RGraph: version 7.20
3rd June, Patrick
Question about installing RGraph
1st June, Ouja
How do I add a click event to a bar in my Bar chart?
8th May, Anthony Kuma
Does the SVG Line chart have outofbounds functionality?


Support forum »

 

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 »

How to use the isPointInStroke function to make your line clickable

The alternative method shown below is a lot less code and makes this process a lot easier
[No canvas support]

Introduction

Currently, you can use the drawing api Poly object to draw a shape around your line and then use the Poly objects tooltip or events to add interactivity. Now though, since the addition of the isPointInStroke function to the standard canvas api, the process is made much easier as the example below shows. It involves:


The original Line chart

Here is the original Line chart before the new path is created.

<script>
    new RGraph.Line({
        id: 'cvs',
        data: [4,8,6,3,5,2,4],
        options: {
            linewidth: 5,
            xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
            textSize: 14
        }
    }).draw();
</script>

The Line chart with the draw event listener

Now a draw event listener is added that draws a line. Since this is drawn after the Line chart it will be the last path that is drawn on the canvas. Normally you would make this a transparent color - but so that you can see it, it's been drawn in semi-opaque black.

<script>
    line = new RGraph.Line({
        id: 'cvs',
        data: [4,8,6,3,5,2,4],
        options: {
            marginInner: 15,
            linewidth: 5,
            xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
            textSize: 14,
            events: {
                draw: function (obj)
                {
                    var coords = obj.coords
            
                    obj.context.beginPath();
                        obj.context.lineWidth = 10;
                        obj.context.lineCap = 'round';
                        obj.context.strokeStyle = 'rgba(0,0,0,0.25)';
                        
                        obj.context.moveTo(coords[0][0], coords[0][1]);
                        
                        
                        for (var i=1; i<coords.length; i+=1) {
                            obj.context.lineTo(coords[i][0], coords[i][1]);
                        }
                    obj.context.stroke();
                }
            }
        }
    }).draw();
</script>

The Line chart with the draw event listener and the click event listener

Now the click event is added to the canvas. Since the line that we drew using the Line chart coordinates was the last path added to the canvas the isPointInStroke function can be used to see if the path (ie the line) was clicked.

<script>
    line = new RGraph.Line({
        id: 'cvs',
        data: [4,8,6,3,5,2,4],
        options: {
            marginInner: 15,
            linewidth: 5,
            xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
            textSize: 14,
            events: {
                draw: function (obj)
                {
                    var coords = obj.coords
            
                    obj.context.beginPath();
                        obj.context.lineWidth   = 10;
                        obj.context.lineCap     = 'round';
                        obj.context.strokeStyle = 'rgba(0,0,0,0.2)';
                        
                        obj.context.moveTo(coords[0][0], coords[0][1]);
                        
                        for (var i=1; i<coords.length; i+=1) {
                            obj.context.lineTo(coords[i][0], coords[i][1]);
                        }
                    obj.context.stroke();
                }
            }
        }
    }).draw();


    //
    // Define the mousemove event that changes the cursor
    //
    line.canvas.onmousemove =  function (e)
    {
        var mouseXY = RGraph.getMouseXY(e);
        var over    = line.context.isPointInStroke(mouseXY[0], mouseXY[1]);

        if (over) {
            e.target.style.cursor = 'pointer';
        } else {
            e.target.style.cursor = 'default';
        }
    };


    //
    // Define the click event listener that handles a click on
    // the line
    //
    line.canvas.onclick = function (e)
    {
        var mouseXY = RGraph.getMouseXY(e);

        if (line.context.isPointInStroke(mouseXY[0], mouseXY[1])) {
            alert('The line was clicked');
        }
    };
</script>

An alternative method using the Drawing API Line object

An alternative method using the drawing API Line object can eliminate a lot of custom code - meaning that you just need to pass the coordinates of the line - which are stored in the Line chart coords array after it's been drawn - to the drawing API Line object. This method looks like this:

[No canvas support]
<script>
    line = new RGraph.Line({
        id: 'cvs2',
        data: [4,5,6,8,5,2,3],
        options: {
            spline: true,
            linewidth: 5,
            xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
            textSize: 14
        }
    }).draw();
    
    new RGraph.Drawing.Line({
        id:'cvs',
        // Get the coordinates from the Line chart object
        coords: line.coordsSpline[0],
        
        options: {
            colorsStroke: '#ff0a',
            linewidth: 10,
            events: {
                click: function (obj, shape)
                {
                    alert('Line has been clicked!')
                },
                mousemove: function (obj, shape)
                {
                    return true;
                }
            }
        }
    }).draw();
</script>