ChartJS 如何将图表背景borderColor渐变添加到Next.js

mefy6pfw  于 2022-11-07  发布在  Chart.js
关注(0)|答案(2)|浏览(259)

我想给图表背景添加渐变。下面是一个例子:

我使用的是Next.js和react-chart.js。
下面是我的代码的一个例子:

  1. import { ChartProps } from "./Chart.props";
  2. import styles from "./Chart.module.css";
  3. import React, { useState, useEffect, useRef } from 'react';
  4. import { Chart as ChartJS, ArcElement, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, ScriptableContext, } from "chart.js";
  5. import { Chart, Line } from "react-chartjs-2";
  6. ChartJS.register(ArcElement, Tooltip, Legend, CategoryScale, LinearScale, PointElement, LineElement);
  7. import { optionsChart } from "./ChartConfig.js";
  8. export const CoinPriceChart = (props) => {
  9. const data = {
  10. labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"],
  11. datasets: [
  12. {
  13. data: [22, 45, 23, 41, 18, 11, 32, 31, 63, 54, 45, 49, 54, 36],
  14. pointRadius: 0,
  15. pointHoverRadius: 2,
  16. borderColor: "rgba(91,56,237,255)",
  17. }
  18. ],
  19. options: {
  20. ...optionsChart
  21. },
  22. };
  23. return (
  24. <div
  25. className={styles.CardList}
  26. {...props}
  27. >
  28. <Line id="myChart" data={data} options={optionsChart}/>
  29. </div>
  30. );
  31. };

有一次我试着这样做:

但是在React中(不是在Node.js中),在Node.js中它对我不起作用。
下面是我的图表(现在)的样子:

等待您的建议。提前感谢!

eiee3dmh

eiee3dmh1#

这是因为在chart.js V3中,折线图在默认情况下是不填充的。您需要导入填充插件,并告诉数据集如下填充:

  1. import {Chart, Filler} from 'chart.js'
  2. import {Chart as ReactChart, Line} from 'react-chartjs-2'
  3. Chart.register(Filler);
  4. const data = {
  5. labels: labels,
  6. datasets: [{
  7. data: data,
  8. fill: true // Set fill to true to enable the backgroundColor under the line
  9. }]
  10. }
cs7cruho

cs7cruho2#

  1. //Importing stuff
  2. import React from 'react';
  3. import { Chart as ChartJS, ArcElement, CategoryScale, LinearScale, PointElement, LineElement, Filler, Tooltip, Legend, ScriptableContext, } from "chart.js";
  4. import { Chart as ReactChart, Line } from "react-chartjs-2";
  5. ChartJS.register(ArcElement, Tooltip, Filler, Legend, CategoryScale, LinearScale, PointElement, LineElement);
  6. // init our Line Chart with gradient of course
  7. export const CoinPriceChart = (props) => {
  8. const data = {
  9. labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"],
  10. datasets: [
  11. {
  12. data: [22, 45, 23, 41, 18, 11, 32, 31, 63, 54, 45, 49, 54, 36],
  13. pointRadius: 0,
  14. pointHoverRadius: 2,
  15. backgroundColor: (context: ScriptableContext<"line">) => {
  16. const ctx = context.chart.ctx;
  17. const gradient = ctx.createLinearGradient(0, 0, 0, 250);
  18. gradient.addColorStop(0, "rgba(91,56,237,0.45)");
  19. gradient.addColorStop(1, "rgba(91,56,237,0.0)");
  20. return gradient;
  21. }, //background gradient color
  22. borderColor: "rgba(91,56,237,255)", //line color
  23. fill: true, // this line allow us to use background gradient
  24. }
  25. ],
  26. };
  27. return (
  28. <div
  29. {...props}
  30. >
  31. <Line id="myChart" data={data} />
  32. </div>
  33. );
  34. };

下面的结果看起来像:

并且不要忘记将此组件导入到主app.js文件中

  1. import { CoinPriceChart } from '../shared/components';//your path of course
  2. <CoinPriceChart data={coinData}></CoinPriceChart>
展开查看全部

相关问题