折线图不适用于类型时间 chart.js

我将 chart.js 与 React 一起使用,但我无法弄清楚为什么折线图不适用于type: 'time',也许我可能会遗漏一些东西:

Chart.js 代码沙箱

import React from "react";

import { Line } from "react-chartjs-2";


const startDate = new Date(2020, 4, 20);


const values = JSON.parse(json).responses[0].rows.map(row => row.values[0]);


const options = {

  legend: {

    display: false

  },

  hover: {

    mode: "index",

    intersect: false,

    animationDuration: 0

  },

  scales: {

    yAxes: [{ position: "right" }],

    xAxes: [

      {

        gridLines: { display: false },

        type: "time",

        time: {

          parser: "MMM D",

          unit: "day",

          unitStepSize: 5,

          displayFormats: {

            day: "MMM D"

          }

        },

        ticks: {

          min: startDate, //May 20

          max: new Date()

        }

      }

    ]

  },

  tooltips: {

    mode: "x-axis"

  }

};


const data = {

  datasets: [

    {

      label: "data",

      fill: false,

      data: values,

      backgroundColor: "pink",

      borderWidth: 2,

      lineTension: 0,

      borderColor: "pink",

      hoverBorderWidth: 2,

      pointBorderColor: "rgba(0, 0, 0, 0)",

      pointBackgroundColor: "rgba(0, 0, 0, 0)",

      pointHoverBackgroundColor: "#fff",

      pointHoverBorderColor: "pink",

      showLine: true

    }

  ]

};


const LineChart = () => {

  return (

    <div style={{ maxWidth: 1024, margin: "32px auto" }}>

      <Line data={data} options={options} />

    </div>

  );

};


export default LineChart;


元芳怎么了
浏览 94回答 1
1回答

叮当猫咪

您应该像这样生成特定的数据集:const values = JSON.parse(json).responses[0].rows.map((row, index) => {&nbsp; let date = new Date(2020, 4, 20);&nbsp; date.setDate(startDate.getDate() + index)&nbsp; return {&nbsp; &nbsp; y: row.values[0],&nbsp; &nbsp; x: date&nbsp; };});例子
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript