Chartjs-Dynamically created linechart holds previous data value

0๐Ÿ‘

โœ…

Try this hope it will work

$('#lineChart1').empty();
  var data = {
    labels: month,
    datasets: [
    {
        label: "Sanitation",
        fillColor: "rgba(255,0,0,0.2)",
        strokeColor: "rgba(255,0,0,1)",
        pointColor: "rgba(255,0,0,1)",
        pointStrokeColor: "#e74c3c",
        pointHighlightFill: "#e74c3c",
        pointHighlightStroke: "rgba(255,0,0,4)",
        data: sanitationvalue
    },
    {
        label: "School Environment",
        fillColor: "rgba(255,255,0,0.2)",
        strokeColor: "rgba(255,255,0,1)",
        pointColor: "rgba(255,255,0,1)",
        pointStrokeColor: " #fcf3cf",
        pointHighlightFill: " #fcf3cf",
        pointHighlightStroke: "rgba(255,255,0,4)",
        data: environmentvalue
    },
    {
        label: "Sports and Recreation",
        fillColor: "rgba(0,0,255,0.2)",
        strokeColor: "rgba(0,0,255,1)",
        pointColor: "rgba(0,0,255,1)",
        pointStrokeColor: " #85c1e9",
        pointHighlightFill: " #85c1e9",
        pointHighlightStroke: "rgba(0,0,255,4)",
        data: recreationvalue
    },
    {
        label: "Water",
        fillColor: "rgba(0,255,0,0.2)",
        strokeColor: "rgba(0,255,0,1)",
        pointColor: "rgba(0,255,0,1)",
        pointStrokeColor: " #abebc6",
        pointHighlightFill: " #abebc6",
        pointHighlightStroke: "rgba(0,255,0,1)",
        data: watervalue
    }
    ]
};
var ctx = null;
$("#lineChart1").html("");
                //ctx = document.getElementById("lineChart1").innerHTML("");
                ctx = document.getElementById("lineChart1").getContext("2d");
                var options = { };
                var lineChart = null;
                lineChart = new Chart(ctx).Line(data, options);
                lineChart.update();

0๐Ÿ‘

I have solved it by added two line:

$('#lineDiv').html('');
 $('#lineDiv').html('<canvas id="lineChart1" height="450" width="800"></canvas>');

Here is the complete code:

 var data = {
                        labels: month,
                        datasets: [
                            {
                                label: "Sanitation",
                                fillColor: "rgba(255,0,0,0.2)",
                                strokeColor: "rgba(255,0,0,1)",
                                pointColor: "rgba(255,0,0,1)",
                                pointStrokeColor: "#e74c3c",
                                pointHighlightFill: "#e74c3c",
                                pointHighlightStroke: "rgba(255,0,0,4)",
                                data: sanitationvalue
                            },
                            {
                                label: "School Environment",
                                fillColor: "rgba(255,255,0,0.2)",
                                strokeColor: "rgba(255,255,0,1)",
                                pointColor: "rgba(255,255,0,1)",
                                pointStrokeColor: " #fcf3cf",
                                pointHighlightFill: " #fcf3cf",
                                pointHighlightStroke: "rgba(255,255,0,4)",
                                data: environmentvalue
                            },
                            {
                                label: "Sports and Recreation",
                                fillColor: "rgba(0,0,255,0.2)",
                                strokeColor: "rgba(0,0,255,1)",
                                pointColor: "rgba(0,0,255,1)",
                                pointStrokeColor: " #85c1e9",
                                pointHighlightFill: " #85c1e9",
                                pointHighlightStroke: "rgba(0,0,255,4)",
                                data: recreationvalue
                            },
                            {
                                label: "Water",
                                fillColor: "rgba(0,255,0,0.2)",
                                strokeColor: "rgba(0,255,0,1)",
                                pointColor: "rgba(0,255,0,1)",
                                pointStrokeColor: " #abebc6",
                                pointHighlightFill: " #abebc6",
                                pointHighlightStroke: "rgba(0,255,0,1)",
                                data: watervalue
                            }
                        ]
                    };
                    var ctx = null;

                    $('#lineDiv').html('');
                    $('#lineDiv').html('<canvas id="lineChart1" height="450" width="800"></canvas>');
                    ctx = document.getElementById("lineChart1").getContext("2d");
                    var options = { };
                    var lineChart = null;
                    lineChart = new Chart(ctx).Line(data, options);
                    lineChart.update();

Html is:

<div class="col-md-6 pull-left" id="lineDiv">
                <canvas id="lineChart1" height="450" width="800"></canvas>
            </div>

Leave a comment