Search code examples
javascriptlinechartmorris.js

Morris.js line chart multiple ykeys from json


I am wokring on morris.js line chart. My json is

[
{"uma":"34","time":"2017-05-11 12:30","mahes":"23","karan":"56"},
{"uma":"45","time":"2017-05-11 12:35","mahes":"45","karan":"56"},
{"uma":"34","time":"2017-05-11 12:38","mahes":"54","karan":"56"}
]

from the above json, I am sure about xkey and ykeys. so I can implement to the below code.

var stringify=JSON.stringify(abovejson);

        var data =stringify,
                  config = {
                    data: JSON.parse(data),
                    xkey: 'time',
                    ykeys: ['uma','mahes','karan'],
                    labels: ['temperature'],
                    fillOpacity: 0.6,
                    hideHover: 'auto',
                    behaveLikeLine: true,
                    resize: true,
                    pointFillColors:['#ffffff'],
                    pointStrokeColors: ['black'],
                    lineColors:['green']
                };
              config.element = 'morris-area-chart-exm';
              Morris.Line(config);`

If my json may have more than 2 ykeys which will be based on the data comes from database. How will i plot it to ykeys?? Please advice.

Sample JSON will be

[
{"uma":"34","time":"2017-05-11 12:30","mahes":"23","karan":"56","janu":"23",....},
{"uma":"45","time":"2017-05-11 12:35","mahes":"45","karan":"56","janu":"56",....},
{"uma":"34","time":"2017-05-11 12:38","mahes":"54","karan":"56","janu":"67",....}
]

Solution

  • I have made a fiddle for you question

    http://jsfiddle.net/athulnair/edwsgj8g/

    In this, I passed the ykeys

    as

    var yvalues = Object.keys(jsonData[0]).filter(i=>i!='time');
    

    So it will fetch all the y key skipping time