首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >chartjs-plugin-zoom无法处理我的React项目

chartjs-plugin-zoom无法处理我的React项目
EN

Stack Overflow用户
提问于 2021-06-10 10:48:18
回答 1查看 827关注 0票数 2

我有一个使用' react -chartjs-2‘库的react组件来显示一些数据。带有数据的图表运行良好。我不能做的是让图表与'chartjs- plugin -zoom‘插件一起工作。我不确定配置出了什么问题。我正在使用:

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

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

代码语言:javascript
复制
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文件,以防万一。

代码语言:javascript
复制
.chart-container {
  height: 450px;
  width: 900px;
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-10 15:25:59

有两件事,你必须按照文档中的说明注册缩放插件,无论是全局注册还是内联注册(https://www.chartjs.org/chartjs-plugin-zoom/guide/integration.html#bundlers-webpack-rollup-etc),而且你的配置是错误的,缩放选项没有enabled选项,你将不得不启用所有不同的缩放类型,然后它就会工作(https://www.chartjs.org/chartjs-plugin-zoom/guide/options.html#zoom-options)

代码语言:javascript
复制
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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67913985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档