admin管理员组文章数量:1289540
I have a react ponent using the 'react-chartjs-2' library to show some data. The chart with the data works fine. What I cannot do is make the chart work with the 'chartjs-plugin-zoom' plugin. I am not sure what is wrong with the config. I am using:
"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
This is the css file, just in case.
.chart-container {
height: 450px;
width: 900px;
}
Any ideas?
I have a react ponent using the 'react-chartjs-2' library to show some data. The chart with the data works fine. What I cannot do is make the chart work with the 'chartjs-plugin-zoom' plugin. I am not sure what is wrong with the config. I am using:
"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
This is the css file, just in case.
.chart-container {
height: 450px;
width: 900px;
}
Any ideas?
Share Improve this question asked Jun 10, 2021 at 2:48 matiasdiezcansecomatiasdiezcanseco 411 silver badge4 bronze badges1 Answer
Reset to default 112 things, you will have to register the zoomplugin as stated in the documentation by registering it either globally or inline (https://www.chartjs/chartjs-plugin-zoom/guide/integration.html#bundlers-webpack-rollup-etc), also your config was incorrect, the zoom option does not have an enabled
option, you will have to enable all the different zoom types appart, then it will work (https://www.chartjs/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 link: https://codesandbox.io/s/react-chart-js-forked-cn6wh?file=/src/ponents/Chart.js
本文标签: javascriptchartjspluginzoom not working with my React projectStack Overflow
版权声明:本文标题:javascript - chartjs-plugin-zoom not working with my React project - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741391105a2376107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论