我正在使用Reaction-chartjs-2库呈现一个单线图,其中的红色和绿色线段表示不同的情况.我已经按照提供的文档和代码示例创建了图表,但我面临的问题是在红色和绿色部分之间有一个意外的差距.尽管try 了各种配置,但差距依然存在.

这是我的代码:

import React from "react";
import { Line } from "react-chartjs-2";
import { Chart as ChartJS, registerables } from "chart.js";
import "chartjs-adapter-moment";
ChartJS.register(...registerables);

const data = [
  {
    t: 1692691200000,
    o: 209.9,
    h: 210.5,
    c: 210.1,
    v: 24849.905,
    l: 209.7,
    i: 0,
    _id: "64e592b6ed350eac66f1279e"
  },
  {
    t: 1692694800000,
    o: 210.1,
    h: 210.9,
    c: 209.8,
    v: 17912.358,
    l: 209.7,
    i: 0,
    _id: "64e592b6ed350eac66f1279f"
  },
  {
    t: 1692698400000,
    o: 209.8,
    h: 210.1,
    c: 209.7,
    v: 15451.882,
    l: 209.3,
    i: 0,
    _id: "64e592b6ed350eac66f127a0"
  },
  {
    t: 1692702000000,
    o: 209.8,
    h: 209.8,
    c: 209.1,
    v: 14630.712,
    l: 208.8,
    i: 0,
    _id: "64e592b6ed350eac66f127a1"
  },
  {
    t: 1692705600000,
    o: 209.1,
    h: 209.9,
    c: 209.2,
    v: 14840.568,
    l: 209,
    i: 0,
    _id: "64e592b6ed350eac66f127a2"
  },
  {
    t: 1692709200000,
    o: 209.2,
    h: 209.4,
    c: 206.9,
    v: 76291.989,
    l: 205.9,
    i: 0,
    _id: "64e592b6ed350eac66f127a3"
  },
  {
    t: 1692712800000,
    o: 207,
    h: 207.8,
    c: 206.9,
    v: 55066.85,
    l: 205.9,
    i: 0,
    _id: "64e592b6ed350eac66f127a4"
  },
  {
    t: 1692716400000,
    o: 206.9,
    h: 208.1,
    c: 205.6,
    v: 70269.986,
    l: 204.3,
    i: 0,
    _id: "64e592b6ed350eac66f127a5"
  },
  {
    t: 1692720000000,
    o: 205.5,
    h: 207.3,
    c: 207.2,
    v: 107602.133,
    l: 203.4,
    i: 0,
    _id: "64e592b6ed350eac66f127a6"
  },
  {
    t: 1692723600000,
    o: 207.1,
    h: 207.2,
    c: 205.9,
    v: 59274.354,
    l: 204.9,
    i: 0,
    _id: "64e592b6ed350eac66f127a7"
  },
  {
    t: 1692727200000,
    o: 206,
    h: 210.4,
    c: 208.8,
    v: 81805.016,
    l: 204.9,
    i: 0,
    _id: "64e592b6ed350eac66f127a8"
  },
  {
    t: 1692730800000,
    o: 208.8,
    h: 209.9,
    c: 208.4,
    v: 29905.367,
    l: 208,
    i: 0,
    _id: "64e592b6ed350eac66f127a9"
  },
  {
    t: 1692734400000,
    o: 208.3,
    h: 209.4,
    c: 209.3,
    v: 18693.145,
    l: 208.3,
    i: 0,
    _id: "64e592b6ed350eac66f127aa"
  },
  {
    t: 1692738000000,
    o: 209.3,
    h: 209.5,
    c: 206.8,
    v: 60334.243,
    l: 204.7,
    i: 0,
    _id: "64e592b6ed350eac66f127ab"
  },
  {
    t: 1692741600000,
    o: 206.8,
    h: 209.4,
    c: 208.7,
    v: 32667.587,
    l: 206.4,
    i: 0,
    _id: "64e592b6ed350eac66f127ac"
  },
  {
    t: 1692745200000,
    o: 208.8,
    h: 213,
    c: 210.9,
    v: 62314.118,
    l: 208.7,
    i: 0,
    _id: "64e592b6ed350eac66f127ad"
  },
  {
    t: 1692748800000,
    o: 210.9,
    h: 215.6,
    c: 213.7,
    v: 98698.808,
    l: 210.8,
    i: 0,
    _id: "64e592b6ed350eac66f127ae"
  },
  {
    t: 1692752400000,
    o: 213.8,
    h: 214.5,
    c: 214.1,
    v: 19970.85,
    l: 213.5,
    i: 0,
    _id: "64e592b6ed350eac66f127af"
  },
  {
    t: 1692756000000,
    o: 214.1,
    h: 214.2,
    c: 213.4,
    v: 18173.768,
    l: 213.1,
    i: 0,
    _id: "64e592b6ed350eac66f127b0"
  },
  {
    t: 1692763200000,
    o: 212.6,
    h: 213.7,
    c: 213.6,
    v: 10753.479,
    l: 212.3,
    i: 0,
    _id: "64e592b6ed350eac66f127b1"
  },
  {
    t: 1692766800000,
    o: 213.5,
    h: 213.7,
    c: 213.6,
    v: 169.645,
    l: 213.5,
    i: 0,
    _id: "64e592b6ed350eac66f127b2"
  },
  {
    t: 1692770400000,
    o: 213.9,
    h: 214,
    c: 214,
    v: 1.285,
    l: 213.9,
    i: 0,
    _id: "64e5a0c81a0fcedd5b1b6393"
  },
  {
    t: 1692774000000,
    o: 214.8,
    h: 215.3,
    c: 215.2,
    v: 4903.512,
    l: 214.5,
    i: 0,
    _id: "64e5b1561a0fcedd5b312de7"
  },
  {
    t: 1692777600000,
    o: 216.4,
    h: 216.4,
    c: 216.4,
    v: 0,
    l: 216.4,
    i: 0,
    _id: "64e5bce91a0fcedd5b40f590"
  },
  {
    t: 1692781200000,
    o: 215.3,
    h: 215.3,
    c: 215.3,
    v: 51.575,
    l: 215.3,
    i: 0,
    _id: "64e5caf71a0fcedd5b5460b9"
  }
];

const App = () => {
  // Extracting timestamps and values from the data
  const timestamps = data.map((entry) => entry.t);
  const values = data.map((entry) => entry.c); // Using 'c' for closing prices, change it if needed
  console.log("? ~ file: index.tsx:264 ~ ChartComponent ~ values:", values);

  // Defining green and red colors for the chart
  const greenColor = "rgba(0, 255, 0, 0.6)";
  const redColor = "rgba(255, 0, 0, 0.6)";

  // Creating datasets for green and red segments
  const datasets = [
    {
      label: "Close",
      data: values.map((value) => (value >= values[0] ? value : null)),
      borderColor: greenColor,
      backgroundColor: greenColor,
      fill: false, // Filling area above the line
      tension: 0.3,
      pointRadius: 0
    },
    {
      label: "Close",
      data: values.map((value) => (value < values[0] ? value : null)),
      borderColor: redColor,
      backgroundColor: redColor,
      fill: false, // Filling area below the line
      pointRadius: 0,
      tension: 0.3
    }
  ];

  const chartData = {
    labels: timestamps,
    datasets: datasets
  };

  const options = {
    scales: {
      x: {
        display: false // Hide the x-axis
      },
      y: {
        display: false
      }
    },
    plugins: {
      legend: {
        display: false // Hide the legend
      }
    }
  };
  return (
    <div>
      <Line data={chartData} options={options} />
    </div>
  );
};

export default App;

SandBox Demo

enter image description here

推荐答案

通过将数据拆分成两个数据集,您只需在both个端值小于value[0]时将线段定义为红色,如果both个端值大于或等于value[0]则定义为绿色.如果一端较小,另一端较大,您不会说 colored颜色 应该是什么--这些混合线段并不存在,因此出现了间隙.

要做到这一点,最简单的方法是将所有数据保存在一个数据集中(这样您也可以拥有混合的段),并使用segment个可编写脚本的属性.

使用

  const options = {
    segment: {
      borderColor: function ({ p0DataIndex, p1DataIndex }) {
        return values[p0DataIndex] >= values[0] || values[p1DataIndex] >= values[0] ? 
            greenColor : redColor;
      },
    },
    // ....... the other options
 }

如果至少一端的值大于value[0],则将线段设置为绿色,如果两端的值都小于value[0],则将线段设置为红色.因此,混合末端线段将是绿色的.当然,可以实现其他逻辑,并且可以类似地定义其他视觉属性,如backgroundColor.

这是实施后的a fork of the sandbox个.

另一种可能有用的技术是使用渐变精确地以value[0]的级别分割混合分段:

datesets:[{
   // .....
   borderColor: function (context) {
      const chart = context.chart;
      const { ctx, chartArea } = chart;
      if (!chartArea) {
        // This case happens on initial chart load
        return;
      }
      const y0Px = chart.scales.y.getPixelForValue(values[0]),
        y0Fraction = (chartArea.bottom - y0Px) / (chartArea.bottom - chartArea.top);
      const gradient = ctx.createLinearGradient(
          0,
          chartArea.bottom,
          0,
          chartArea.top
      );
      gradient.addColorStop(0, redColor);
      gradient.addColorStop(y0Fraction, redColor);
      gradient.addColorStop(y0Fraction, greenColor);
      gradient.addColorStop(1, greenColor);

      return gradient;
    },
]

这条线从图表的底部到value[0]的水平是红色的,在该水平以上是绿色的.Here's the codesandbox fork和那段代码.

Reactjs相关问答推荐

eslint -无法解析模块的路径@web3modal/ethers/react导入/no-unresolved

全局上下文挂钩-替代不起作用

如何在关闭和打开此 Select 输入时应用旋转效果?

替换谷歌 map api默认信息窗口与自定义

为什么登录错误和密码在创建Reaction-Hook-Form后立即被删除?

面对动画警告:未指定`useNativeDriver`.这是必需的选项,并且必须在REACT本机中显式设置为`true`或`False`

如何使用useState响应快速/顺序状态更新

安装使用环境的BIT组件的依赖项

尽管所有页面在 Reactjs 中都是公开的,但始终导航到特定页面

Reactjs:MUI5:MobileDateTimePicker API:如何自定义日历中的文本

如何在使用 React Router v6 将当前页面的状态传递给上一页的同时导航到上一页?

从服务器获取数据时未定义,错误非法调用

页面不显示

我如何在所有组件中使用 Chakra UI toast?

如何避免在 react useEffect 上滚动时出现小的延迟?

useState 在生命周期中的位置

从 API 中提取映射数组后出现重复元素

getAttribute 函数并不总是检索属性值

如何将本地视频文件上传到 Google Cloud

useEffect 仅在跟随链接后有效,不适用于直接链接