Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add dynamic data to morris bar chart

I want to add data to Morris bar-chart via ajax. Following is the json which i get in ajax success

[{"x":"2014-10-02","y":"1"},{"x":"2014-10-19","y":"1"},{"x":"2014-10-20","y":"1"},{"x":"2014-11-13","y":"1"}]

following is the js code

var chart = Morris.Bar({
        element : 'normal-bar-graph',
        data : [{
            "x" : null,
            "y" : null
        }],
        xkey : 'x',
        ykeys : ['y'],
        labels : ['Added']
    });
$.ajax({
            type: "POST",
            url:  "some_url",
            data: {'user_report':[k,v]},

            success: function(html)                    
            {   
                if(html == "error")
                {
                alert('error');
                }
                else
                {
                    chart.setData(html);

                }
                hide_loading();
            }
        });

am using /morris/raphael.2.1.0.min.js and /morris/morris.min.js. The chart.setData(html); functionality does not work as said in the documentation of morris.

thank in advance. please point me out if any error made.

have also made a jsbin if that can help: morris

like image 339
dhpratik Avatar asked Sep 27 '22 17:09

dhpratik


1 Answers

Remove the quotes. setData expects an array. When you use quotes, it converts that into a string instead of array.

Like this : chart.setData([{ "y": "2006", "a": 100, "b": 90 },{ "y": "2006", "a": 100, "b": 90 }]);

like image 136
afrin216 Avatar answered Oct 05 '22 23:10

afrin216