gpt4 book ai didi

javascript - Chart.js 中未显示折线图

转载 作者:行者123 更新时间:2023-12-04 11:39:34 29 4
gpt4 key购买 nike

问题很前卫,我看不到图形的线条,当我按下任何按钮时。 X 轴的时间应该会根据按下的按钮而相应地改变 我一直在查看文档很长一段时间,但仍然无法弄清楚。
图表数据

import React, { useRef, useEffect, useState } from "react";
import { historyOptions } from '../chartConfig/chartConfig';
import 'chartjs-adapter-moment';
import annotationPlugin from 'chartjs-plugin-annotation';
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
Chart.register(annotationPlugin);



const determineTimeFormat = (
timeFormat: string,
day: any,
week: any,
year: any
) => {
switch (timeFormat) {
case "24h":
return day;
case "7d":
return week;
case "1y":
return year;
default:
return day;
}
};

interface Props {
data: any
}

const ChartData: React.FC<Props> = ({ data }) => {
const chartCanvasRef = useRef<HTMLCanvasElement | null>(null);
const { day, week, year, detail } = data;
const [timeFormat, setTimeFormat] = useState("24h");
const [isRebuildingCanvas, setIsRebuildingCanvas] = useState(false);



useEffect(() => {
setIsRebuildingCanvas(true);
}, [timeFormat]);

useEffect(() => {
if (isRebuildingCanvas) {
setIsRebuildingCanvas(false);
}
}, [isRebuildingCanvas]);

useEffect(() => {
if (chartCanvasRef && chartCanvasRef.current && detail) {
const chartCanvas = chartCanvasRef.current
if (isRebuildingCanvas || !chartCanvas) {
return;
}

const chartInstance = new Chart(chartCanvasRef.current, {
type: "line",
data: {
datasets: [
{
label: `${detail.name} price`,
data: determineTimeFormat(timeFormat, day, week, year),
backgroundColor: "rgba(134,159,152, 1)",
borderColor: "rgba(174, 305, 194, 0.4",

},
],
},

选项
  options: {     
plugins: {
annotation: {
annotations: {

}
}
},

animations: {
tension: {
duration: 1000,
easing: 'linear',
from: 1,
to: 0,
loop: true
}
},
maintainAspectRatio: false,
responsive: true,
scales: {
x:
{
type: 'time',
},
},
}
});
return () => {
chartInstance.destroy();
}
}}, [day, isRebuildingCanvas,timeFormat, week, year, detail]);
其余的组件代码

return (
<div className='chart__container'>
{renderPrice()}
{isRebuildingCanvas ? undefined : (
<canvas ref={chartCanvasRef} id='myChart' width={250} height={250}></canvas>
)}
<button className='time__format' onClick={() => setTimeFormat("24h")}>24h</button>
<button className='time__format' onClick={() => setTimeFormat("7d")}>7d</button>
<button className='time__format' onClick={() => setTimeFormat("1y")}>1y</button>
</div>
);
};

export default ChartData;

最佳答案

看起来像您的 isRebuildingCanvas逻辑可能不一致,或者我不只是理解它。
无论如何,从 Chart.js 的 Angular 来看,您需要更改数据并调用 chartInstance.update()按下更改数据的按钮时。
部分示例:

  const canvas = useRef(null);
const [chart, setChart] = useState();
const [timeFormat, setTimeFormat] = useState("24h");

useEffect(() => {
if (chart || !canvas.current) return;
const ctx = canvas.current.getContext("2d");
if (!ctx) return;

const config = {/*...*/};

setChart(new Chart(ctx, config));
}, [chart, canvas]);

useEffect(() => {
if (!chart) return;

chart.config.data.datasets[0].data = determineTimeFormat(timeFormat, day, week, year);
chart.update();
}, [chart, timeFormat]);
还有一个完整的、非常相似的例子:
https://codesandbox.io/s/blissful-faraday-hzcq0

关于javascript - Chart.js 中未显示折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67689672/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com