代码之家  ›  专栏  ›  技术社区  ›  Alwaysblue

在React中创建图形

  •  2
  • Alwaysblue  · 技术社区  · 6 年前

    我很难理解如何在React中创建图形(这是我第一次使用它)

    这就是我的数据集的样子。

    (3) [{…}, {…}, {…}]
    0:{id: "SAMPLE_#SPMJXVC_1_2", x: Array(963), y: Array(963)}
    1: {id: "SAMPLE_#SPMJXVC_1_3", x: Array(964), y: Array(964)}
    2: {id: "SAMPLE_#SPMJXVC_1_1", x: Array(954), y: Array(954)}
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Badrush    6 年前

    有一个包装器使Chart.js在React中易于使用: https://github.com/jerairrest/react-chartjs-2

    https://www.overloop.io/blog/2018/6/19/top-5-react-chart-libraries

    如果你决定用这个 react-chartjs-2 data 对象,然后简单地渲染 <Scatter data={data} />

    import React from 'react';
    import {Scatter} from 'react-chartjs-2';
    
    const data = {
      labels: ['Scatter'],
      datasets: [
        {
          label: 'My First dataset',
          fill: false,
          backgroundColor: 'rgba(75,192,192,0.4)',
          pointBorderColor: 'rgba(75,192,192,1)',
          pointBackgroundColor: '#fff',
          pointBorderWidth: 1,
          pointHoverRadius: 5,
          pointHoverBackgroundColor: 'rgba(75,192,192,1)',
          pointHoverBorderColor: 'rgba(220,220,220,1)',
          pointHoverBorderWidth: 2,
          pointRadius: 1,
          pointHitRadius: 10,
          data: [
            { x: 65, y: 75 },
            { x: 59, y: 49 },
            { x: 80, y: 90 },
            { x: 81, y: 29 },
            { x: 56, y: 36 },
            { x: 55, y: 25 },
            { x: 40, y: 18 },
          ]
        }
      ]
    };
    
    export default React.createClass({
      displayName: 'ScatterExample',
    
      render() {
        return (
          <div>
            <h2>Scatter Example</h2>
            <Scatter data={data} />
          </div>
        );
      }
    });