React折线图背景填充

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

React line chart background fill

问题

我在我的React应用中使用Chart.js库创建了一条折线图。该折线图应用了背景填充,但我遇到了一个问题,即填充覆盖了0和第一个数据点之间的y轴边框。我希望边框在背景颜色之上绘制,以便它可见。

import React from 'react';
import { Line } from 'react-chartjs-2';
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);

const Graph = ({ chartData, chartColour, chartFillColour }) => {

  const options = {
    plugins: {
      legend: {
        display: false,
      }
    },
    scales: {
      x: {
        grid: {
          display: true,
          drawBorder: true,
          drawOnChartArea: false,
        },
        ticks: {
          display: true,
        }
      },
      y: {
        grid: {
          display: false,
          drawBorder: true,
          drawOnChartArea: false,
        },
        ticks: {
          display: true,
        }
      }
    }
  }

  const data = {
    labels: chartData.labels,
    datasets: [
      {
        data: chartData.data,
        backgroundColor: chartFillColour,
        borderColor: chartColour,
        pointRadius: 0,
        fill: true,
      }
    ]
  }

  return chartData.labels ? <Line data={data} options={options}/> : null;
}

export default Graph;

有办法确保边框在背景颜色之上绘制吗?任何帮助或建议将不胜感激。谢谢!

英文:

I have a line chart in my React application created using Chart.js library. The line chart has a background fill applied to it, but I'm facing an issue where the fill covers the y-axis border between 0 and the first data point. I want the border to be drawn over the background color so that it's visible.

import React from &#39;react&#39;;
import { Line } from &#39;react-chartjs-2&#39;;
import { Chart, registerables } from &#39;chart.js&#39;;
Chart.register(...registerables);

const Graph = ({ chartData, chartColour, chartFillColour }) =&gt; {

  const options = {
    plugins: {
      legend: {
        display: false,
      }
    },
    scales: {
      x: {
        grid: {
          display: true,
          drawBorder: true,
          drawOnChartArea: false,
        },
        ticks: {
          display: true,
        }
      },
      y: {
        grid: {
          display: false,
          drawBorder: true,
          drawOnChartArea: false,
        },
        ticks: {
          display: true,
        }
      }
    }
  }

  const data = {
    labels: chartData.labels,
    datasets: [
      {
        data: chartData.data,
        backgroundColor: chartFillColour,
        borderColor: chartColour,
        pointRadius: 0,
        fill: true,
      }
    ]
  }

  return chartData.labels ? &lt;Line data={data} options={options}/&gt; : null;
}

export default Graph;

Is there a way to ensure that the border is drawn over the background color? Any help or suggestions would be greatly appreciated. Thank you!

答案1

得分: 1

你可以将fillerPlugin的drawTime设置为beforeDraw,这样它会在chart.js绘制其自身组件之前绘制填充。

链接:https://www.chartjs.org/docs/4.3.0/charts/area.html#configuration

new Chart(ctx, {
    data: {
        datasets: []
    },
    options: {
        plugins: {
            filler: {
                drawTime: 'beforeDraw'
            }
        }
    }
});
英文:

You can set the drawTime of the fillerPlugin to beforeDraw so it draws the fill before chart.js draws any of its own components.

https://www.chartjs.org/docs/4.3.0/charts/area.html#configuration

new Chart(ctx, {
    data: {
        datasets: []
    },
    options: {
        plugins: {
            filler: {
                drawTime: &#39;beforeDraw&#39;
            }
        }
    }
});

huangapple
  • 本文由 发表于 2023年7月3日 06:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601080.html
匿名

发表评论

匿名网友

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

确定