score:0

try this. declare label and data as

var labels = result['labels'].split(","); // return array of strings of labels
var data =   json.parse("[" +  result['data'] + "]");// return array of data

chage

labels: [labels],
data: [data],

to

labels: labels,
    data: data,

because it already in array

score:1

for the first time you can fill the chart with your own value, but when it is coming for dynamically appending data then you should remove the canvas element and regenerate it. so that your data will get appended dynamically on chart.

you can try something like this.

<canvas id="chartbtc" class="canvaschart"></canvas>

<script>

    dynamicdataappendonchart(labels, data); // on page loading you will be having your own data so you can pass them, or if you want that to be happen with ajax when page loading itself you can trigger the click event.

    $('#btnclick').trigger('click');

    // you will be calling the function with click event...
    $('#btnclick').on('click', function () {
        var data = '', labels = '';
        $.ajax({
            url: url,
            type: "post",
            data: data,
            async: isasync,
            success: function (result) {
               result = json.parse(result);
               labels = result['labels'];
               data = result['data'];
            }
        });
        dynamicdataappendonchart(labels, data); // now you can call the function by passing labels and data
    });

    function dynamicdataappendonchart() {
        $('#chartbtc').remove();
        $('#appendthecanvaselement').append('<canvas id="chartbtc" class="canvaschart"><canvas>'); // this would create new canvas element every time and removes the older one.
        var ctx = document.getelementbyid("chartbtc");
        var mychart = new chart(ctx, {
            type: 'line',
            data: {
                labels: [labels],
                datasets: [{
                    label: 'market price (usd)',
                    data: [data],
                    backgroundcolor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    bordercolor: [
                        'rgba(255,99,132,1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderwidth: 1
                }]
            },
            options: {
                responsive: true,
                maintainaspectratio: false,
                scales: {
                    yaxes: [{
                        ticks: {
                            beginatzero: false
                        }
                    }]
                }
            }
        });
    }
</script>

score:2

since you have provided your ajax result, this will not work with the chart generation. in your case result["labels"] is a string and you are just making a list with 1 element which is the whole string(which is not valid for the labels property and will not work). what you should do is split the string, format it correctly and then supply it as a list to the labels property. easiest way would be to use the split() function, and in your case you could split by ", ". although i would recommend to implement a better response if its possible (have it something like so: {"labels": ["13-nov-2017", "14-nov-2017", ...]}).

hope this helps!


Related Query

More Query from same tag