重新绘制自定义点

8 javascript charts jsx reactjs recharts

我有一个StackedAreaChart,在我在图表上显示区域后,我想添加一个自定义点。

目前我的代码看起来是这样,但我想画一个点取决于XAxisYAxis数据。是否存在任何传递xy坐标CustomizedDot类的方法?

import React from 'react';
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, Label, Dot } from 'recharts';

const data = [
    { name: 142, underweight: 36.3, normal: 11.3, overweight: 6.8, obese: 56.7 },
    { name: 196, underweight: 70.3, normal: 22.7, overweight: 15.9, obese: 2.2 },
];

class CustomizedDot extends React.Component {
    render() {
        const { cx, cy } = this.props;

        return (
            <circle cx={160} cy={50} r={25} stroke="black" strokeWidth={3} fill="red" />
        );
    }
};

class BMIGraph extends React.Component {
    render() {
        return (
            <div>
                <h2>Index telesne mase za odrasle</h2>
                <AreaChart width={600} height={400} data={data} margin={{ top: 10, right: 30, left: 0, bottom: 0 }}>
                    <XAxis dataKey="name">
                        <Label value="Višina (cm)" offset={0} position="insideBottom" />
                    </XAxis>
                    <YAxis>
                        <Label value="Teža (kg)" angle={-90} position="insideLeft" textAnchor="middle" />
                    </YAxis>
                    <CartesianGrid strokeDasharray="3 3" />
                    {/* <Tooltip /> */}
                    <CustomizedDot />
                    <Area type='monotone' dataKey='underweight' stackId="1" stroke='#8884d8' fill='#7d7dff' animationDuration={3500} name="Podhranjenost" dot={<CustomizedDot />} />
                    <Area type='monotone' dataKey='normal' stackId="1" stroke='#82ca9d' fill='#7dff7d' animationDuration={3500} name="Normalna teža" />
                    <Area type='monotone' dataKey='overweight' stackId="1" stroke='#ffc658' fill='#ffff7d' animationDuration={3500} name="Prekomerna teža" />
                    <Area type='monotone' dataKey='obese' stackId="1" stroke='#ffc658' fill='#ff7d7d' animationDuration={3500} name="Debelost" />
                </AreaChart>
            </div>
        );
    }
}

export default BMIGraph;
Run Code Online (Sandbox Code Playgroud)

当前代码的结果

Ini*_*han 10

您无需传递 X 和 Y 轴数据。

通过使用

const { cx, cy } = this.props;
Run Code Online (Sandbox Code Playgroud)

cx & cy 持有您需要的 x, y 位置。

在你的CustomizedDot课堂上试试这个

 class CustomizedDot extends React.Component {
    render() {
        const { cx, cy } = this.props;

        return (
            <circle cx={cx - 10} cy={cy - 10} r={25} stroke="black" strokeWidth={3} fill="red" />
        );
    }
  };
Run Code Online (Sandbox Code Playgroud)

  • @pol-albacar-fernandez 您的编辑评论很有用,但应在此处添加为评论,以便按照作者的意图留下答案。编辑评论是:“使用 &lt;circle&gt; 元素,您可以删除 cx={cx-10} 和 cy={cy-10} 中的“-10”。这将使您的圆圈精确地在原始点位置居中。” (2认同)