图表中的散点图 - 如何使用 4 个象限?

如何解决图表中的散点图 - 如何使用 4 个象限?

我正在使用 Recharts 在 react 中绘制数据。

我希望能够在绘图过程中看到所有四个笛卡尔象限。我目前正在使用此代码

import React,{ PureComponent } from 'react';
import { ScatterChart,Scatter,XAxis,YAxis,Cartesiangrid,Tooltip,Cell,ResponsiveContainer } from 'recharts';
import { scaleOrdinal } from 'd3-scale';
import { schemeCategory10 } from 'd3-scale-chromatic';

const colors = scaleOrdinal(schemeCategory10).range();

const data = [
  { x: -100,y: 200,z: 200 },{ x: -120,y: 100,z: 260 },{ x: -170,y: 300,z: 400 },{ x: 140,y: 250,z: 280 },{ x: 150,y: 400,z: 500 },{ x: 110,y: 280,];

export default class Baseline extends PureComponent {
  static demoUrl = 'https://codesandBox.io/s/scatter-chart-with-cells-2sk2o';

  render() {
    return (
      <ResponsiveContainer width="100%" height="100%">
        <ScatterChart
          width={400}
          height={400}
          margin={{
            top: 20,right: 20,bottom: 20,left: 20,}}
        >
          <Cartesiangrid />
          <XAxis type="number" dataKey="x" name="stature" unit="cm" />
          <YAxis type="number" dataKey="y" name="weight" unit="kg" />
          <Tooltip cursor={{ strokeDasharray: '3 3' }} />
          <Scatter name="A school" data={data} fill="#8884d8">
            {data.map((entry,index) => (
              <Cell key={`cell-${index}`} fill={colors[index % colors.length]} />
            ))}
          </Scatter>
        </ScatterChart>
      </ResponsiveContainer>
    );
  }
}

我的目标是近似于此,

enter image description here

recharts 是最好的主意还是可以用它?喜欢任何例子:)

解决方法

您只需添加 ReferenceLine 即可将图表分成 4 个象限。

工作Codesandbox

import "./styles.css";
import React from "react";
import {
  ScatterChart,Scatter,XAxis,YAxis,CartesianGrid,Tooltip,ReferenceLine,Cell
} from "recharts";

const data = [
  { x: -100,y: -200,z: 200 },{ x: -100,y: 200,{ x: 120,y: 100,z: 260 },y: -100,{ x: 170,y: 300,z: 400 },{ x: -140,y: -250,z: 280 },y: 250,{ x: 150,y: 400,z: 500 },{ x: 110,y: 280,y: -280,z: 200 }
];

const COLORS = ["#0088FE","#00C49F","#FFBB28","#FF8042","red","pink"];

export default function App() {
  return (
    <ScatterChart
      width={500}
      height={400}
      margin={{
        top: 20,right: 20,bottom: 20,left: 20
      }}
    >
      <CartesianGrid />
      <XAxis type="number" dataKey="x" name="stature" unit="cm" />
      <YAxis type="number" dataKey="y" name="weight" unit="kg" />
      <Tooltip cursor={{ strokeDasharray: "3 3" }} />
      <Scatter name="A school" data={data} fill="#8884d8">
        {data.map((entry,index) => (
          <Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
        ))}
      </Scatter>
      <Tooltip cursor={{ strokeDasharray: "3 3" }} />
      <ReferenceLine y={0} stroke="#000000" />
      <ReferenceLine x={0} stroke="#000000" />
      <ReferenceLine
        segment={[
          {
            x: 0,y: 0
          },{
            x: 0,y: 0
          }
        ]}
        label={{
          value: "(0,0)",position: "bottom"
        }}
      />
    </ScatterChart>
  );
}

enter image description here

编辑:

您可以使用 segment 将标签放在不同的象限中。

 <ReferenceLine
      segment={[
          {
            x: 85,y: 200
          },{
            x: 85,y: 200
          }
        ]}
        label={{
          value: "high speed high accuracy",position: "insideBottom",stroke:"#DE3163"
        }}/>
        <ReferenceLine
      segment={[
          {
            x: -85,{
            x: -85,y: 200
          }
        ]}
        label={{
          value: "low speed high accuracy",stroke:"#DE3163"
        }}/>
        <ReferenceLine
      segment={[
          {
            x: 85,y: -200
          },y: -200
          }
        ]}
        label={{
          value: "high speed low accuracy",y: -200
          }
        ]}
        label={{
          value: "low speed low accuracy",stroke:"#DE3163"
        }}/>

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?