简体   繁体   English

如何在折线图js中显示多个数据集

[英]How to show multiple datasets in line chart js

I'm using line chart.js我正在使用线 chart.js
I want to show my line chart have two line (actually it's will dynamic datasets) base with location and label in bottom will base with month.我想显示我的折线图有两条线(实际上它将是动态数据集)以位置为基础,底部的 label 将以月份为基础。 Now my chart it's show only last data set.现在我的图表只显示最后一个数据集。

My data is look like this我的数据看起来像这样

{
location: "Apartment A", 
color: "#b168ac",
set_data: [{
   month: 1,
   value: 3500
},{
   month: 2,
   value: 2700
},{
   month: 3,
   value: 1500
}]
},
{
location: "Apartment B", 
color: "#b168aa",
set_data: [{
   month: 1,
   value: 1700
},{
   month: 2,
   value: 2800
},{
   month: 3,
   value: 3200
}]
}

I made my code something like this:我让我的代码是这样的:

var obj = JSON.parse(data);
$.each(obj,function(i,item){
    var locate = [];
    var amt = [];
    var color = [];
    var item_set = [];
    locate.push(item.location);

    var m = [];
    var val = [];
    var item_s =  item.set_data;

    $.each(item_s,function(i2,item2){
        val.push(item2.value);
        m.push(item2.month);

        var chartdata = {
            labels: m,
            datasets : [{
                label:locate,
                data: val,
                backgroundColor: item.color,
                borderWidth: 1,
                fill: false,
            }]
        };
        var ctx = document.getElementById("graph_by_usage").getContext('2d');
        var LineChart = new Chart(ctx,{
            type : 'line',
            data : chartdata,
            options: {
                responsive:true
            }
        });
    });
});

The data should be look like this https://codepen.io/k3no/pen/pbYGVa Please advice me.数据应该看起来像这样https://codepen.io/k3no/pen/pbYGVa请给我建议。 Thank you.谢谢你。

Assuming that your data is a JSON array, you could generate labels and datasets as follows:假设您的数据是 JSON 数组,您可以生成标签和数据集,如下所示:

const labels = baseData[0].set_data.map(v => v.month);
const dataSets = [];
baseData.forEach(o => dataSets.push({
    label: o.location,
    data: o.set_data.map(v => v.value),
    borderColor: o.color,
    borderWidth: 1,
    fill: false
  })
);

Please have a look at the runnable code sample below.请查看下面的可运行代码示例。

 const baseData = [{ location: "Apartment A", color: "red", set_data: [ { month: 1, value: 3500 }, { month: 2, value: 2700 }, { month: 3, value: 1500 } ] }, { location: "Apartment B", color: "blue", set_data: [ { month: 1, value: 1700 }, { month: 2, value: 2800 }, { month: 3, value: 3200 } ] } ]; const labels = baseData[0].set_data.map(v => v.month); const dataSets = []; baseData.forEach(o => dataSets.push({ label: o.location, data: o.set_data.map(v => v.value), borderColor: o.color, borderWidth: 1, fill: false }) ); new Chart('graph_by_usage', { type: 'line', data: { labels: labels, datasets: dataSets }, options: { responsive:true } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script> <canvas id="graph_by_usage" height="90"></canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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