【问题标题】:Displaying line chart for multiple months using chart.js使用 chart.js 显示多个月的折线图
【发布时间】:2020-02-27 09:05:41
【问题描述】:

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

plan_plan = 0: {label: "Feb-20", plan_hrs: "20"} 
            1: {label: "Oct-20", plan_hrs: "94"} 
actual_plan = 0: {label: "Mar-20", actual_hrs: "1"}

javacript 代码:

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

                 var labels = [];
                  for (var i in plandata) {
                        labels.push(plandata[i].label);
                  }
                  for (var j in actualdata) {
                        labels.push(actualdata[j].label);
                  }
                new Chart("scurve_chart", {
  type: 'line',
  data: {
    labels: Array.from(labels),
    datasets: [{
        label: "Planned Hours",
        fill: false,
        borderColor: "rgba(255, 0, 0, 1)",
        pointHoverBackgroundColor: "rgba(255, 0, 0, 1)",
        data: plandata.map(o => ({ x: Number(o.label), y: Number(o.plan_hrs)}))
      },
      {
        label: "Actual Hours",
        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.label), y: Number(o.actual_hrs)}))
      }
    ]
  },
  options: {    
    tooltips: {
      callbacks: {
        title: (tooltipItem, data) => "Month " + data.datasets[tooltipItem[0].datasetIndex].data[tooltipItem[0].index].x
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        },
       scaleLabel: {
        display: true,
        labelString: 'Hours'
      }
      }],
      xAxes: [{
        ticks: {          
          min: 0,
          max: 53,
          stepSize: 1
        },
        scaleLabel: {
        display: true,
        labelString: 'Month'
      }
      }]
    }
  }
});
 }}

我想在 x 轴上获取格式为(1 月 20 日)的月份,然后在 y 轴上获取 plan_hrs 和 actual_hrs 数据,可以显示为折线图。即使没有要连接的数据点,也应该显示单点条目。我已经使用了脚本标签:

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>

但是当我包含以下引导脚本时,它没有显示图表:

<script src="build/js/custom.min.js">

【问题讨论】:

    标签: javascript php chart.js linechart


    【解决方案1】:

    我可以帮助解析和显示 xAxis 上的日期,但不幸的是我不知道 Bootstrap。

    如下定义您的xAxis:

    xAxes: [{
      type: 'time',
      time: {
        parser: 'MMM-YY',
        unit: 'month',
        displayFormats: {
          month: 'MMM-YY'
        }
      }
    }]
    

    Chart.js 内部使用 Moment.js,因此您可以使用以下date/time 格式来解析和显示日期。在你的情况下,这是'MMM-YYD'。

    const plan_plan = [{label: "Feb-20", plan_hrs: "20"}, {label: "Oct-20", plan_hrs: "94"}]; 
    const actual_plan = [{label: "Mar-20", actual_hrs: "1"}];
    
    new Chart("scurve_chart", {
      type: 'line',
      data: {
        datasets: [{
            label: "Planned Hours",
            fill: false,
            borderColor: "rgba(255, 0, 0, 1)",
            data: plan_plan.map(o => ({ x: o.label, y: Number(o.plan_hrs)}))
          },
          {
            label: "Actual Hours",
            fill: false,
            borderColor: "rgba(0, 255, 0, 1)",
            data: actual_plan.map(o => ({x: o.label, y: Number(o.actual_hrs)}))
          }
        ]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }],
          xAxes: [{
            type: 'time',
            time: {
              parser: 'MMM-YY',
              unit: 'month',
              displayFormats: {
                 month: 'MMM-YY'
              }
            }
          }]
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
    <canvas id="scurve_chart" height="90"></canvas>

    【讨论】:

    • 我已经尝试过了,但我想要格式(MMM-YY)而不是格式(MMM-DD)。我已将解析器的代码更改为MMM-YY,但它四次显示为 2 月 20 日,接下来的几个月四次未显示 x 轴上的所有月份。
    • 抱歉,我误解了您提供的日期格式,现在已使用使用日期格式“MMM-YY”的解决方案更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多