gpt4 book ai didi

javascript - 重新绘制散点图,为特定值设置颜色?

转载 作者:行者123 更新时间:2023-12-04 14:52:00 27 4
gpt4 key购买 nike

我正在尝试为进入 recharts 散点图中的值设置特定颜色。

我尝试这样做的方式如下,

<Scatter name="A school" data={data} fill="#8884d8">
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={colorType} />
))}
</Scatter>

进入这个的数据集看起来像这样,

const data = [{x:10,y:50,colourType:"#FFBB28"},..]

目前,它采用其中一种颜色类型并将其应用于每个值。关于如何使其仅适用于单点的任何想法?

谢谢。

##更新

不幸的是,当我应用下面的日志时,颜色仍然总是一样。

这是总的组件。

 <ScatterChart
width={500}
height={400}
margin={{
top: 20,
right: 20,
bottom: 20,
left: 20,
}}
>
<CartesianGrid />
<XAxis
type="number"
domain={[0, 60]}
dataKey="x"
name="Speed"
unit=" Seconds"
reversed
/>
<YAxis
type="number"
domain={[0, 100]}
dataKey="y"
name="Accuracy"
unit="%"
/>
<Tooltip cursor={{ strokeDasharray: "3 3" }} />
<Scatter name="A school" data={data}>
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.colourType ?? "#8884d8"} />
))}
</Scatter>
...(removed reference lines)
</ScatterChart>

感谢并乐于了解我在这里哪里出了问题

最佳答案

我不知道你的 colorType 变量是从哪里来的,但我认为你可以这样做:

<Scatter data={data} name="A school">
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.colourType} />
))}
</Scatter>

如果 colourType 不是每个条目的属性,那么试试这个:

<Scatter data={data} name="A school">
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.colourType ?? "#8884d8"} />
))}
</Scatter>

新鲜 React 应用程序的完整工作代码

注意:这不包括任何进一步的更改。我只是想更明确地演示数据集。

import {
ScatterChart,
CartesianGrid,
XAxis,
YAxis,
Tooltip,
Scatter,
Cell,
} from "recharts";

const data = [
{ x: 10, y: 50, colourType: "#FFBB28" },
{ x: 20, y: 50 },
{ x: 30, y: 40 },
];

function App() {
return (
<ScatterChart
width={500}
height={400}
margin={{
top: 20,
right: 20,
bottom: 20,
left: 20,
}}
>
<CartesianGrid />
<XAxis
type="number"
domain={[0, 60]}
dataKey="x"
name="Speed"
unit=" Seconds"
reversed
/>
<YAxis
type="number"
domain={[0, 100]}
dataKey="y"
name="Accuracy"
unit="%"
/>
<Tooltip cursor={{ strokeDasharray: "3 3" }} />
<Scatter name="A school" data={data}>
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.colourType ?? "#8884d8"} />
))}
</Scatter>
</ScatterChart>
);
}

export default App;

输出:

Scatterplot that renders from the above code example

关于javascript - 重新绘制散点图,为特定值设置颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68919993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com