简体   繁体   中英

Convert object to Chartis.js structure

Can anybody help me with converting the following structure to a Chartist.js structure? I use chartis.js to show line chart. I need convert this JSON below to object structure.

<code>
data = {
    "el1": [{
        "date": "2017.01",
        "data1": {
            "series_1": {
                "a": 10,
                "b": 20,
                "c": 50,
                "d": 15,
                "e": 8
            },
            "Series_2": {
                "yes": 5,
                "no": 3
            },
            "Series_3": {
                "s": 2,
                "n": 9
            }
        },
        "text": [{
            "t": "header",
            "c": "text"
        }, {
            "t": "header2",
            "c": "text2"
        }]
    }, {
        "date": "2017.02",
        "data1": {
            "series_1": {
                "a": 56,
                "b": 23,
                "c": 45,
                "d": 69,
                "e": 14
            },
            "Series_2": {
                "yes": 2,
                "no": 1
            },
            "Series_3": {
                "s": 6,
                "n": 4
            }
        },
        "text": [{
            "t": "header",
            "c": "text"
        }, {
            "t": "header2",
            "c": "text2"
        }]
    }, {
        "date": "2017.03",
        "data1": {
            "series_1": {
                "a": 15,
                "b": 12,
                "c": 10,
                "d": 54,
                "e": 4
            },
            "Series_2": {
                "yes": 20,
                "no": 16
            },
            "Series_3": {
                "s": 9,
                "n": 7
            }
        },
        "text": [{
            "t": "header",
            "c": "text"
        }, {
            "t": "header2",
            "c": "text2"
        }]
    }
    ]
};
</code>

And I Need Object like this:

<code>
var example = [ 
{ 
labels: ['2017.01', '2017.02', '2017.03'] , series: [10,56,15], [20,23, 12], [50,45,10], [15, 69, 54], [8,14,4]}, 
labels: ['2017.01', '2017.02', '2017.03'] , series: [5,2,20], [3,1, 16]}, 
labels: ['2017.01', '2017.02', '2017.03'] , series: [2,6,9], [9,4, 7]}, 
] ; 
</code>

Please check the digit's and tray to comapre JSON and Output to better understand how to should look's like. Sorry for my English !
Thanks for the help! Best regards!

You can just iterate the array and can manipulate it to the result you are looking for...

 function getFormatted(data) { var _newData = {}; var allSeries = []; data.el1.forEach(function(el){ _newData[el.date] = el.data1; if(allSeries.length==0) allSeries = Object.keys(el.data1); }); return allSeries.map(function(el) { var obj = { labels: [], series: [] }; obj.labels = Object.keys(_newData); Object.keys(_newData).forEach(function(_el) { obj.series.push(Object.keys(_newData[_el][el])); }); var _newSeries = []; obj.series[0].forEach(function(el, i){ _newSeries.push([el, obj.series[1][i]]); }); obj.series = _newSeries; return obj; }); } var data = { "el1": [{ "date": "2017.01", "data1": { "series_1": { "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }, "Series_2": { "yes": 1, "no": 2 }, "Series_3": { "s": 1, "n": 2 } }, "text": [{ "t": "header", "c": "text" }, { "t": "header2", "c": "text2" }] }, { "date": "2017.02", "data1": { "series_1": { "a2": 1, "b2": 2, "c2": 3, "d2": 4, "e2": 5 }, "Series_2": { "yes2": 1, "no2": 2 }, "Series_3": { "s2": 1, "n2": 2 } }, "text": [{ "t": "header", "c": "text" }, { "t": "header2", "c": "text2" }] }] }; console.log(getFormatted(data)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM