react-chartjs – 显示所有数据点的折线图,而不仅仅在悬停时显示。

huangapple go评论92阅读模式
英文:

react-chartjs - line chart displaying all datapoints, not just on hover

问题

我正在尝试在React中创建一个基本的折线图。据我了解,Chartjs默认情况下会在悬停时显示数据点,但由于某种原因,图表上显示了每个数据点,这并不理想,因为数据点可能有成千上万个。

据我所知,Chartjs中折线图的默认行为是在悬停时显示数据。我尝试禁用工具提示,代码如下:

options: {
    tooltips: {
         enabled: false
    }
}

我还尝试直接复制粘贴了react-chartjs中的示例代码,并插入了自己的数据,但仍然显示了所有数据点。我有点不知道该尝试什么,提前感谢您的帮助。以下是我的组件代码:

import { 
  CategoryScale, 
  LineElement, 
  LinearScale, 
  PointElement, 
  TimeScale,
  Legend,
  Title,
  Chart,
  Tooltip 
} from 'chart.js';
import './App.css';
import { Line } from 'react-chartjs-2';
import annotationPlugin from 'chartjs-plugin-annotation';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import 'chartjs-adapter-date-fns';
import { useEffect, useState } from 'react';


Chart.register(
  CategoryScale,
  LinearScale,
  PointElement, 
  LineElement,
  Title,
  annotationPlugin,
  ChartDataLabels,
  TimeScale,
  Legend,
  Tooltip
);

function App() {

  const [ temps, setTemps ] = useState([]);
  const [ times, setTimes ] = useState([]);

  useEffect(() => {
    async function fetchData () {
      const dates = [];
      const temps = [];
      const url = 'data/device1/input_device_1_a.json';
      const response = await fetch(url);
      const data = await response.json();
      for (let i = 0; i < data.length; i++) {
        dates.push(data[i].time);
        temps.push(data[i].tmp);
      }
      setTimes(dates);
      setTemps(temps);
    }
  
    fetchData();
  }, []);

  const chartData = {
    labels: times,
    datasets: [
      {
        label: 'Device 1',
        data: temps,
        borderColor: 'rgb(255, 99, 132)',
        backgroundColor: 'rgba(255, 99, 132, 0.5)',
      },
    ],
  }

  const chartOptions = {
    responsive: true,
    plugins: {
      legend: {
        position: 'top',
      },
      title: {
        display: true,
        text: 'Chart.js Line Chart',
      },
    },
  }

  return (
    <Line data={chartData} options={chartOptions} />
  );
}

export default App;

希望对你有所帮助!

英文:

I'm trying to create a basic line chart in React. As I understand it, by default Chartjs will display the datapoint on hover but for some reason every single data point is showing on the chart, which is not ideal since there are going to be thousands of them. chart examle

As far as I can tell, the default behavior of a line chart in Chartjs is to display data on hover. I've tried disabling tooltips like so:

options: {
tooltips: {
enabled: false
}
}

I also just tried straight up copy pasting the example code from react-chartjs and plugging in my own data, but I'm still getting all data points showing. I'm at bit of a loss as to what to try so thanks in advance. Here's my component:

import { 
CategoryScale, 
LineElement, 
LinearScale, 
PointElement, 
TimeScale,
Legend,
Title,
Chart,
Tooltip 
} from &#39;chart.js&#39;;
import &#39;./App.css&#39;;
import { Line } from &#39;react-chartjs-2&#39;;
import annotationPlugin from &#39;chartjs-plugin-annotation&#39;;
import ChartDataLabels from &#39;chartjs-plugin-datalabels&#39;;
import &#39;chartjs-adapter-date-fns&#39;;
import { useEffect, useState } from &#39;react&#39;;
Chart.register(
CategoryScale,
LinearScale,
PointElement, 
LineElement,
Title,
annotationPlugin,
ChartDataLabels,
TimeScale,
Legend,
Tooltip
);
function App() {
const [ temps, setTemps ] = useState([]);
const [ times, setTimes ] = useState([]);
useEffect(() =&gt; {
async function fetchData () {
const dates = [];
const temps = [];
const url = &#39;data/device1/input_device_1_a.json&#39;;
const response = await fetch(url);
const data = await response.json();
for (let i = 0; i &lt; data.length; i++) {
dates.push(data[i].time);
temps.push(data[i].tmp);
}
setTimes(dates);
setTemps(temps);
}
fetchData();
}, []);
const chartData = {
labels: times,
datasets: [
{
label: &#39;Device 1&#39;,
data: temps,
borderColor: &#39;rgb(255, 99, 132)&#39;,
backgroundColor: &#39;rgba(255, 99, 132, 0.5)&#39;,
},
],
}
const chartOptions = {
responsive: true,
plugins: {
legend: {
position: &#39;top&#39;,
},
title: {
display: true,
text: &#39;Chart.js Line Chart&#39;,
},
},
}
return (
&lt;Line data={chartData} options={chartOptions} /&gt;
);
}
export default App;

答案1

得分: 0

在Chart.js的术语中,可能涉及到三个不同的概念:

  • 工具提示,默认情况下,当你在点的附近悬停时,会显示一个带有标签的框
  • 悬停(例如,线条悬停),通常与工具提示一起使用,当悬停在一个点上时,可以改变其样式
  • datalabels插件,默认情况下为每个点添加标签(无论是否悬停)

如果你不特别需要datalabels,最简单的方法是不全局注册它。你仍然可以将它作为插件传递给需要它的特定图表。

如果你只想对某些点或某些数据集使用datalabels,那么你需要查看datalabels的文档-它有很多选项,包括(为了完全灵活)可脚本化选项,可以通过JavaScript回调应用任何你想要的逻辑。

英文:

In Chart.js's terminology, there are maybe three different concepts that are getting involved here:

  • Tooltips, which by default display a box with a label when you hover over or near a point
  • Hover (e.g., line hover), which are commonly used with tooltips to change the styling of a point when it's hovered
  • The datalabels plugin, which by default labels every single point (whether or not it's hovered)

If you don't specifically need datalabels, it's simplest to not globally register it. You can still pass it as a plugin to specific charts that need it.

If you want to use datalabels only for some points or some datasets, then you'll have to check the datalabels documentation - it has quite a few options, including (for full flexibility) scriptable options that can apply whatever logic you want via JavaScript callbacks.

huangapple
  • 本文由 发表于 2023年8月9日 05:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定