Javascript 将线栏添加到关联阵列栏
这是我的chart.js关联数组数据 我尝试在条形图中添加折线图。 但我不知道如何在其中添加新图表 因为我的数据集是数组Javascript 将线栏添加到关联阵列栏,javascript,arrays,laravel,chart.js,associative-array,Javascript,Arrays,Laravel,Chart.js,Associative Array,这是我的chart.js关联数组数据 我尝试在条形图中添加折线图。 但我不知道如何在其中添加新图表 因为我的数据集是数组 const tmp_orders = [ { "type": "import", "year": 2020, "total": 1 }, { "type": "import", "year": 2019,
const tmp_orders = [
{
"type": "import",
"year": 2020,
"total": 1
},
{
"type": "import",
"year": 2019,
"total": 0
},
{
"type": "import",
"year": 2018,
"total": 0
},
{
"type": "export",
"year": 2020,
"total": 0
},
{
"type": "export",
"year": 2019,
"total": 0
},
{
"type": "export",
"year": 2018,
"total": 0
}
],
const res = [];
const result = tmp_orders[0].map(function(obj) {
var tmp = []
Object.entries(obj).map(function([key, value]) {
tmp[key] = value
})
res.push(tmp)
});
console.log(res)
// get order type and sort
const types =[...new Set(res.map(v => v['type']))].sort();
// get order year and sort
const years =[...new Set(res.map(v =>v['year']))].sort();
// set order color and sort
const colors = ['#ff0000', '#6383ff'];
// data for target order chart
const target_data = types.map(type => ({
label: type,
data: years.map(year => {
const value = res.find(v => v['type'] == type && v['year'] == year );
return value['total'];
}),
backgroundColor: function(){
if(type === 'import'){
return '#ff0000'
}else if(type === 'export'){
return '#6383ff'
}
}
}))
const targetOrderCtx = $('#chart_target_order'),
optionOrder = {
maintainAspectRatio: false,
responsive: true,
title: {
display: true,
text: 'Target Order',
fontSize: 18,
fontColor: '#6383ff'
},
legend: {
display: true,
position: 'bottom'
},
animation: {
animateScale: true,
animateRotate: true
}
}
// create Target Order Chart
var chart_target_order = new Chart(targetOrderCtx, {
type: 'bar',
data: {
labels: years,
datasets: target_data
},
options: optionOrder
});
如果你看看我的数据集
你会发现这是一个数组。
大多数情况下,我会找到一些解决方案,比如在insie数据集中添加折线图,但对我来说这似乎是不可能的
编辑1
这是我想要的结果
请添加一个工作代码段。很难像这样回答+而且最好添加一个你想要的结果的屏幕截图。@EzraSiton这是我的代码,我遵循这个链接解决方案@埃兹拉西顿,请帮帮我