gpt4 book ai didi

javascript - chartjs-plugin-zoom 不适用于我的 React 项目

转载 作者:行者123 更新时间:2023-12-05 08:47:16 24 4
gpt4 key购买 nike

我有一个使用“react-chartjs-2”库来显示一些数据的 React 组件。带有数据的图表工作正常。我不能做的是让图表与“chartjs-plugin-zoom”插件一起工作。我不确定配置有什么问题。我正在使用:

"react-chartjs-2": "^3.0.3"

"chartjs-plugin-zoom": "^1.0.1"

import { Line } from 'react-chartjs-2'
import * as zoom from 'chartjs-plugin-zoom'

import classes from './Chart.module.css'

interface Chart {
title: string
labels: string[]
datasets: any
}

const Chart: React.FC<Chart> = props => {
const data = {
title: props.title,
labels: props.labels,
datasets: props.datasets,
}

const options = {
maintainAspectRatio: false,
responsive: true,
elements: {
point: {
radius: 0,
},
line: {
borderWidth: 1.5,
},
},
scales: {
x: {
ticks: {
color: 'rgba( 0, 0, 1)',
},
grid: {
color: 'rgba(0, 0, 0, 1)',
},
},
y: {
min: 1,
max: 200000,
type: 'logarithmic',
ticks: {
color: 'rgba(0, 0, 0, 1)',
},
grid: {
color: 'rgba(0, 0, 0, 1)',
},
},
},
plugins: {
zoom: {
zoom: {
enabled: true,
mode: 'xy',
speed: 100,
},
pan: {
enabled: true,
mode: 'xy',
speed: 100,
},
},
},
}

return (
<div className={classes.chartContainer}>
<Line
type='line'
title={props.title}
data={data}
options={options}
width={900}
height={450}
/>
</div>
)
}

export default Chart

这是 css 文件,以防万一。

.chart-container {
height: 450px;
width: 900px;
}

有什么想法吗?

最佳答案

2 件事,您必须按照文档中的说明注册 zoomplugin,方法是全局或内联注册 (https://www.chartjs.org/chartjs-plugin-zoom/guide/integration.html#bundlers-webpack-rollup-etc),而且您的配置不正确,缩放选项没有启用 选项,你必须启用所有不同的缩放类型 appart,然后它才会工作 ( https://www.chartjs.org/chartjs-plugin-zoom/guide/options.html#zoom-options )

import { Line, Chart } from "react-chartjs-2";
import React from "react";
import zoomPlugin from "chartjs-plugin-zoom";

Chart.register(zoomPlugin); // REGISTER PLUGIN

const Chart2 = (props) => {
const data = {
title: props.title,
labels: props.labels,
datasets: props.datasets
};

const options = {
maintainAspectRatio: false,
responsive: true,
elements: {
point: {
radius: 0
},
line: {
borderWidth: 1.5
}
},
scales: {
x: {
ticks: {
color: "rgba( 0, 0, 1)"
},
grid: {
color: "rgba(0, 0, 0, 1)"
}
},
y: {
min: 1,
max: 200000,
type: "logarithmic",
ticks: {
color: "rgba(0, 0, 0, 1)"
},
grid: {
color: "rgba(0, 0, 0, 1)"
}
}
},
plugins: {
zoom: {
zoom: {
wheel: {
enabled: true // SET SCROOL ZOOM TO TRUE
},
mode: "xy",
speed: 100
},
pan: {
enabled: true,
mode: "xy",
speed: 100
}
}
}
};

return (
<div>
<Line
type="line"
data={data}
options={options}
width={900}
height={450}
/>
</div>
);
};

export default Chart2;

Codesandbox 链接:https://codesandbox.io/s/react-chart-js-forked-cn6wh?file=/src/components/Chart.js

关于javascript - chartjs-plugin-zoom 不适用于我的 React 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67913985/

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