亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

折線圖不適用于類型時間 chart.js

折線圖不適用于類型時間 chart.js

元芳怎么了 2022-12-09 19:47:24
我將 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;
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您應該像這樣生成特定的數據集:


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

  let date = new Date(2020, 4, 20);

  date.setDate(startDate.getDate() + index)

  return {

    y: row.values[0],

    x: date

  };

});

例子


查看完整回答
反對 回復 2022-12-09
  • 1 回答
  • 0 關注
  • 77 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號