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 show charts on your website.

More »

 

Download
Get the latest version of RGraph (version 6.17) from the download page. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

More »

 

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

More »

Drawing the chart a second time doesn't clear the previous chart


Posted by Roberto at 12:20 on Thursday 22nd April 2021 [link]
Hello,
I should draw the graph and based on the input data updating the graph with the new data, and this works but draw the new bars on top of the old ones without gradually removing the bars of the previous graph, without updating the graph. How can I solve?
Thanks

Posted by Richard at 16:24 on Thursday 22nd April 2021 [link]
You can use the RGraph.clear() function to clear the canvas before drawing a second time. For example:

var canvas = myObject.canvas;

RGraph.clear(canvas);

PS. If you want an animated change then the Bar chart supports this by supplying the new data to the grow() function. There's an example here:

https://codepen.io/rgraph/pen/yLgQmvw

The code of which is this:

bar = new RGraph.Bar({
id:'cvs',
data: [8,4,6,3,5,4,2],
options: {
     yaxis:false,
     xaxis:false,
     backgroundGridVlines: false,
     backgroundGridBorder: false,
     yaxisScaleMax: 20
}
}).grow();

setInterval(function ()
{
    // count,min,max[,decimals]
    newData = RGraph.arrayRandom(7,0,20);

    bar.grow({data: newData});
}, 2000);

Richard



Posted by Roberto at 17:19 on Thursday 22nd April 2021 [link]
I tried to write:

var canvas = document.getElementById ('cvs');
RGraph.clear (canvas);

before redrawing the graph the second time but I don't update the graph.

Posted by Richard at 17:28 on Thursday 22nd April 2021 [link]
You then need to redraw the chart with either an animation function or the draw function like this:

var canvas = document.getElementById ('cvs'); // Or myObject.canvas works too

RGraph.clear (canvas); // Clear the canvas
myObject.draw();     // Redraw the chart

Posted by Roberto at 19:13 on Thursday 22nd April 2021 [link]
I tried but don't work.
I share the whole function so that you can check where the error is:


function mostraNascondiGrafico(arrayValGraficoAsseX, arrayDateLabelAsseX, arrayLimiti) {






    if ($('#divGraph').is(':visible')) {
     $("#divGraph").hide();
     $("#buttMostrNascGrafico").text("MOSTRA GRAFICO");
     }
     else {

     $("#divGraph").show();
     $("#buttMostrNascGrafico").text("NASCONDI GRAFICO");
    


//I entered the suggested lines here, is that correct?
    var canvas =document.getElementById('cvs');
    RGraph.clear(canvas);




    
    bar = new RGraph.Bar({
        id: 'cvs',
    
        data: arrayValGraficoAsseX,


        options: {

            yaxisScaleMax: 80,
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            backgroundGridColor: '#999',
            
            xaxisLabels: arrayDateLabelAsseX,

            marginLeft: 55,
            marginRight: 55,
            marginBottom: 75,

            colors: ['Gradient(#A18AC5:#D1AAF5)'],
            textColor: '#ccc',
            xaxisTickmarksCount: 0,
            xaxis: false,
            yaxis: false,
            shadowColor: 'black',
            shadowOffsetx: 0,
            shadowOffsety: 0,
            shadowBlur: 15,
            colorsStroke: 'rgba(0,0,0,0)',
            combinedEffect: 'wave',
            combinedEffectOptions: '{frames: 30}'

        }
    });


    line = new RGraph.Line({
        id: 'cvs',
        data: arrayLimiti,
        options: {
            yaxisScaleMax: 80,
            xaxis: false,
            yaxis: false,
            backgroundGrid: false,
            colors: ['#d00'],
            yaxisPosition: 'right',
            textColor: '#ccc',
            marginLeft: 45,
            marginRight: 45,
            tickmarksStyle: null,
            spline: true,
            combinedEffect: 'trace',
            textSize: 18
        }
    });

    combo = new RGraph.CombinedChart([bar, line]).draw();

    // Poiche la classe combinata disattiva le etichette del grafico a linee,
    // le riattiva
    line.set({
        yaxisScale: true
    });

    RGraph.redraw();

    // Aggiunge alcune capacita di risposta al grafico a barre
    bar.responsive([


         {maxWidth: null, width: 1400, height: 500, options: {textSize: 18, marginInner: 20}},
        {maxWidth: 1600, width: 1400, height: 500, options: {textSize: 12, marginInner: 7}}



    ]);

    
    line.responsive([
        {maxWidth: null, width: 1400, height: 500, options: {textSize: 18, marginInner: 45, linewidth: 7}},
        {maxWidth: 1600, width: 1400, height: 500, options: {textSize: 12, marginInner: 20, linewidth: 5}}

        
    ]);




    }
}

Posted by Roberto at 20:32 on Thursday 22nd April 2021 [link]
solved, I used the reset () function instead of clear ()

[Replies are now closed]