【问题标题】:Displaying line chart for multiple datasets using chart.js使用 chart.js 显示多个数据集的折线图
【发布时间】:2020-02-24 11:43:29
【问题描述】:

我必须在一个图表中显示包含两个不同数据集的折线图。执行查询后,我得到了以下形式的 plan_plan 和 actual_plan。

plan_plan = 0: {week: "46", planned_del: "20"}
               1: {week: "53", planned_del: "94"}

actual_plan = 0: {week: "8", actual_del: "1"}

javascript部分:

function show_Graph()
        {
            {
                var plandata = <?php echo json_encode($plan_plan); ?>;
                var actualdata = <?php echo json_encode($actual_plan); ?>;

                   console.log(plandata);
                     var labels = [];
                    var plandel = [];
                    var actualdel = [];

      for (var i in plandata) {
                        labels.push(plandata[i].week);
                        plandel.push(plandata[i]);
                      }
    for (var j in actualdata) {
                       labels.push(actualdata[j].week);
                        actualdel.push(actualdata[j]);
                      }

                    var chartdata = {
                        labels: labels,

                        datasets: [

                            {
                                label: "Planned Deliverables",
                                fill: false,
                                borderColor: "rgba(255, 0, 0, 1)",
                                pointHoverBackgroundColor: "rgba(255, 0, 0, 1)",
                                data: plandel
                            },
                            {
                                label: "Actual Deliverables",
                                fill: false,
                                backgroundColor: "rgba(0, 255, 0, 0.75)",
                                borderColor: "rgba(0, 255, 0, 1)",
                                pointHoverBackgroundColor: "rgba(0, 255, 0, 1)",
                                pointHoverBorderColor: "rgba(0, 255, 0, 1)",
                                data: actualdel

                            }


                        ]

                    };

                    var graphTarget = $("#scurve_chart");

                  var barGraph = new Chart(graphTarget, {
                        type: 'line',
                        data: chartdata,
                        options: {
                            elements: {
                    point:{
                        radius: 1,
                    }
                },


        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }],
            xAxes: [{
            barValueSpacing: 2,
            barPercentage: 0.2
        }]
        }
    }
                    });

            }}

我想在 x 轴上获取星期,然后在 y 轴上获取planned_del 和 actual_del 数据,这些数据可以显示为折线图。即使没有要连接的数据点,也应该显示单点条目。

【问题讨论】:

    标签: javascript php chart.js linechart


    【解决方案1】:

    实现此目的的一种方法是将data.labels 定义为从0 到53 的数字array

    labels: [0, 1, 2, ... 53]
    

    两个datasets 中的data 将只包含individual points 的定义。

    data: [{ x: 46, y: 20 }, { x: 53, y: 94 }] // plandata 
    ...
    data: [{ x: 8, y: 1}] // actualdata 
    

    这是说明如何获得所需结果的 JavaScript 部分。

    var plandata = [{ week: "46", planned_del: "20" }, { week: "53", planned_del: "94" }];
    var actualdata = [{ week: "8", actual_del: "1" }];
    
    new Chart("scurve_chart", {
      type: 'line',
      data: {
        labels: Array.from(new Array(54), (x, i) => i),
        datasets: [{
            label: "Planned Deliverables",
            fill: false,
            borderColor: "rgba(255, 0, 0, 1)",
            pointHoverBackgroundColor: "rgba(255, 0, 0, 1)",
            data: plandata.map(o => ({ x: Number(o.week), y: Number(o.planned_del)}))
          },
          {
            label: "Actual Deliverables",
            fill: false,
            backgroundColor: "rgba(0, 255, 0, 0.75)",
            borderColor: "rgba(0, 255, 0, 1)",
            pointHoverBackgroundColor: "rgba(0, 255, 0, 1)",
            pointHoverBorderColor: "rgba(0, 255, 0, 1)",
            data: actualdata.map(o => ({x: Number(o.week), y: Number(o.actual_del)}))
          }
        ]
      },
      options: {    
        tooltips: {
          callbacks: {
            title: (tooltipItem, data) => "Week " + data.datasets[tooltipItem[0].datasetIndex].data[tooltipItem[0].index].x
          }
        },
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }],
          xAxes: [{
            ticks: {          
              min: 0,
              max: 53,
              stepSize: 1
            }
          }]
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
    <canvas id="scurve_chart" height="90"></canvas>

    【讨论】:

    • 我使用了&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"&gt;&lt;/script&gt; ,但我的引导模板不起作用。 &lt;script src="build/js/custom.min.js"&gt;&lt;/script&gt; ,这个标签似乎被禁用了。您能否建议我启用它的任何想法?
    • 对不起,我不知道 Bootstrap,在这里无法为您提供帮助。
    • 当我有数据以以下格式绘制时我会做什么:plan_plan = 0: {label: "Feb-20", planned_del: "20"} 1: {label: "Oct-20", planned_del: "94"} actual_plan = 0: {label: "Mar-20", actual_del: "1"}.我将如何绘制这些数据?
    • @FRECEENA FRANCIS:请为这个新问题发布一个新问题,这样可以更容易地提供清晰的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多