0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rechartsで凡例をカスタマイズする

Last updated at Posted at 2022-08-03

Rechartsを使って凡例のフォーマットを変更する必要があったので調査しました。

    <LineChart
      width={600}
      height={300}
      data={data}
      margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
    >
      <CartesianGrid strokeDasharray="3 3" />
      <XAxis dataKey="name" />
      <YAxis />
      <Tooltip />
      <Legend formatter={renderColorfulLegendText} />
      <Line
        type="monotone"
        dataKey="hoge"
        stroke="#8884d8"
        activeDot={{ r: 8 }}
      />
      <Line type="monotone" dataKey="fuga" stroke="#82ca9d" />
    </LineChart>

Legendformatterで関数を指定すれば凡例を操作できます。

const colorList = ["#FFFFFF", "#00C49F", "#FFBB28", "#FF8042"];
const renderColorfulLegendText = (value: string, entry: any, index: number) => {
  const color  = colorList[index];
  return <span style={{ color }}>{value}</span>;
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?