将对象转换为 Chartis.js 结构
Convert object to Chartis.js structure
任何人都可以帮我将以下结构转换为 Chartist.js 结构吗?我使用 chartis.js 来显示折线图。我需要将下面的 JSON 转换为对象结构。
<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>
我需要这样的对象:
<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>
请检查数字和托盘以比较 JSON 和输出以更好地了解应该是什么样子。对不起我的英语!
谢谢您的帮助!此致!
您可以迭代数组并对其进行操作以获得您要查找的结果...
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));
任何人都可以帮我将以下结构转换为 Chartist.js 结构吗?我使用 chartis.js 来显示折线图。我需要将下面的 JSON 转换为对象结构。
<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>
我需要这样的对象:
<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>
请检查数字和托盘以比较 JSON 和输出以更好地了解应该是什么样子。对不起我的英语!
谢谢您的帮助!此致!
您可以迭代数组并对其进行操作以获得您要查找的结果...
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));