如何在React-Chartsjs-2中滚动X轴

发布于 2025-02-07 22:07:25 字数 917 浏览 0 评论 0原文

我已经在react.js中实现了一个条形图,我想弄清楚当值越来越多的时候如何滚动x轴。在下图中,我有一些用户一天内进行的搜索值。到目前为止,我有100天以上的时间,我认为我的问题会更大,直到我们到达年底,X轴的价值更高。

我的图表代码和选项在下面。

  const options = {
    responsive: true,
    plugins: {
      title: {
        display: true,
      },
    },
  };
  const dataForSearchBins = {
    labels,
    datasets: [
      {
        label: "Number of searches",
        data: searchBins?.map((item) => item.value),
        backgroundColor: "lightBlue",
      },
    ],
  };
<Stack>
  <Text style={{ textAlign: "center" }}>{`Number of clicks per ${binSize.toLowerCase()}`}</Text>
  <Bar options={options} data={data} />
  <Box mb={6} />
</Stack>

I have implemented a bar chart in react.js and I want to figure out how I can scroll in x-axis when the values are more and more. In below chart I have some search values that user do in a day. Until now I have 100+ days and I think my problem will be bigger until we reach the end of the year when I will have more values in x-axis.

enter image description here

My code for the chart and the options are below.

  const options = {
    responsive: true,
    plugins: {
      title: {
        display: true,
      },
    },
  };
  const dataForSearchBins = {
    labels,
    datasets: [
      {
        label: "Number of searches",
        data: searchBins?.map((item) => item.value),
        backgroundColor: "lightBlue",
      },
    ],
  };
<Stack>
  <Text style={{ textAlign: "center" }}>{`Number of clicks per ${binSize.toLowerCase()}`}</Text>
  <Bar options={options} data={data} />
  <Box mb={6} />
</Stack>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人│生佛魔见 2025-02-14 22:07:25

您可以使用 chartjs-plugin-zoom 为此。

import zoomPlugin from 'chartjs-plugin-zoom';
import { Chart as ChartJS} from 'chart.js';
ChartJS.register(zoomPlugin);

注册Zoom插件后,修改Options对象。

  const options = {
    responsive: true,
    plugins: {
      title: {
        display: true,
      },
      zoom: {
        pan: {
            enabled: true,
            mode: 'x'
        },
        zoom: {
            pinch: {
                enabled: true       // Enable pinch zooming
            },
            wheel: {
                enabled: true       // Enable wheel zooming
            },
            mode: 'x',
        }
    }
    },
  };

You can use chartjs-plugin-zoom for this.

import zoomPlugin from 'chartjs-plugin-zoom';
import { Chart as ChartJS} from 'chart.js';
ChartJS.register(zoomPlugin);

After registering zoom plugin, modify the options object.

  const options = {
    responsive: true,
    plugins: {
      title: {
        display: true,
      },
      zoom: {
        pan: {
            enabled: true,
            mode: 'x'
        },
        zoom: {
            pinch: {
                enabled: true       // Enable pinch zooming
            },
            wheel: {
                enabled: true       // Enable wheel zooming
            },
            mode: 'x',
        }
    }
    },
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文