React折线图阴影(Chart.js)

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

React line chart shadows (Chart.js)

问题

你可以修改这段代码以仅将阴影效果应用于PriceGraph组件渲染的折线图,而不影响应用中的其他图表组件。请将你的代码修改如下:

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

const PriceGraph = ({ 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,
        }
      }
    }
  }

  // Custom shadow for chart
  useEffect(() => {
    // Check if the chart with the id 'customShadow' already exists
    if (!Chart.controllers.customShadow) {
      Chart.controllers.customShadow = Chart.controllers.line.extend({
        draw: function() {
          const ctx = this.chart.ctx;

          // Apply shadow effect only to the PriceGraph component's chart
          ctx.save();
          ctx.shadowColor = 'rgba(0, 0, 0, 0.2)';
          ctx.shadowBlur = 10;
          ctx.shadowOffsetX = 4;
          ctx.shadowOffsetY = 4;

          Chart.controllers.line.prototype.draw.apply(this, arguments);

          ctx.restore();
        }
      });
    }
  }, []);

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

  return chartData.labels ? <Line data={data} options={options} plugins={['customShadow']} /> : null;
}

export default PriceGraph;

通过这种方式,你的自定义阴影效果将仅应用于PriceGraph组件的折线图,而不会影响应用中的其他图表组件。希望这对你有所帮助!

英文:

I have multiple charts in my React application created using the react-chartjs-2 library. I am using the Chart.register method to apply a custom shadow effect to the line charts. This however applies the shadow to all of the chart components.

Here is the relevant code:

import React, { useEffect } from &#39;react&#39;;
import { Line } from &#39;react-chartjs-2&#39;;
import { Chart, registerables } from &#39;chart.js&#39;;
Chart.register(...registerables);
const PriceGraph = ({ 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,
}
}
}
}
// Custom shadow for chart
useEffect(() =&gt; {
Chart.register({
id: &#39;customShadow&#39;,
beforeDraw: (chart) =&gt; {
const ctx = chart.ctx;
ctx.save();
const originalLineDraw = ctx.stroke;
ctx.stroke = function () {
ctx.save();
ctx.shadowColor = &#39;rgba(0, 0, 0, 0.2)&#39;;
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 4;
ctx.shadowOffsetY = 4;
originalLineDraw.apply(this, arguments);
ctx.restore();
};
}
});
}, []);
const data = {
labels: chartData.labels,
datasets: [
{
data: chartData.data,
backgroundColor: chartFillColour,
borderColor: chartColour,
fill: true,
}
]
}
return chartData.labels ? &lt;Line data={data} options={options}/&gt; : null;
}
export default PriceGraph;

How can I modify this code to apply a shadow effect only to the line chart rendered by the PriceGraph component, without affecting other chart components in my application?

Any help would be appreciated. Thanks!

答案1

得分: 2

如果您注册了一个插件,它将用于应用程序中所有图表实例。您应该将其设置为一个“内联”插件,放在您希望使用它的图表实例中(希望 react-chartjs-2 语法是正确的):

const myPlugin = {
  id: 'customShadow',
  beforeDraw: (chart) => {
    const ctx = chart.ctx;
    ctx.save();

    const originalLineDraw = ctx.stroke;
    ctx.stroke = function () {
      ctx.save();
      ctx.shadowColor = 'rgba(0, 0, 0, 0.2)';
      ctx.shadowBlur = 10;
      ctx.shadowOffsetX = 4;
      ctx.shadowOffsetY = 4;
      originalLineDraw.apply(this, arguments);
      ctx.restore();
    };
  }
};

return chartData.labels ? <Line data={data} options={options} plugins={[myPlugin]}/> : null;
英文:

If you register a plugin, this is used for all charts instances in the application.
You should set it as an "inline" plugin, in the chart instance where you want to use it (hopefully react-chartjs-2 syntax is correct):

const myPlugin = {
id: &#39;customShadow&#39;,
beforeDraw: (chart) =&gt; {
const ctx = chart.ctx;
ctx.save();
const originalLineDraw = ctx.stroke;
ctx.stroke = function () {
ctx.save();
ctx.shadowColor = &#39;rgba(0, 0, 0, 0.2)&#39;;
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 4;
ctx.shadowOffsetY = 4;
originalLineDraw.apply(this, arguments);
ctx.restore();
};
}
};
return chartData.labels ? &lt;Line data={data} options={options} plugins={[myPlugin]}/&gt; : null;

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

发表评论

匿名网友

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

确定