React 封装AntV F2

1.介绍

最近项目遇到要在移动端使用图表,最后选择了AntV F2来自阿里的开源项目。具体细节可以访问。AntV F2的官网

2.在项目中使用到了条形图和折线图,直接上代码说问题。详细代码地址可以访问完整代码自行下载。

import React, { Component } from "react";
import PropTypes from "prop-types";
import F2 from "@antv/f2";

export default class BarChart extends Component {
  static defaultProps = {
    data: [],
  };

  componentWillReceiveProps(nextProps) {
    if (JSON.stringify(nextProps.data) !== JSON.stringify(this.props.data)) {
      this.chart.clear();
      this.initDraw(nextProps.data);
    }
  }
  componentDidMount() {
    this.initDraw(this.props.data);
  }

  initDraw = (data) => {
    const chart = new F2.Chart({
      id: "container-item",
      pixelRatio: window.devicePixelRatio,
    });
    this.chart = chart;
    chart.source(data, {
      targetValue1: {
        tickCount: 5,
      },
    });
    chart.coord({
      transposed: true,
    });
    chart.tooltip({
      showItemMarker: true,
      triggerOn: ["touchstart", "touchmove"],
      background: {
        radius: 2,
        fill: "#1890FF",
        padding: [6, 10],
      },
      nameStyle: {
        fill: "#fff",
        textAlign: "start",
        textBaseline: "middle",
      },
      onShow: function onShow(ev) {
        const items = ev.items;
        items[0].name = items[0].title.split("\n").join("");
        items[0].value = items[0].value;
      },
    });

    if (data.length) {
      // 柱状图添加文本
      data.forEach(function (obj) {
        chart.guide().text({
          position: [obj.orgName, obj.targetValue1],
          content: obj.targetValue1 || "",
          style: {
            textAlign: "center",
            textBaseline: "bottom",
          },
          offsetY: -10,
          offsetX: -15,
        });
      });
    }

    chart
      .interval()
      .position("orgName*targetValue1")
      .style("type", {
        radius: function radius() {
          return [0, 50, 50, 0];
        },
      });
    chart.render();
  };

  render() {
    return (
      <div>
        <canvas id="container-item" width={window.innerWidth} height="280"></canvas>
      </div>
    );
  }
}

BarChart.propTypes = {
  data: PropTypes.array,
};

3.实际的效果

4.在使用的过程中,遇到的问题。

1.数据的排序问题,需要的是降序排列展示数据。--->>解决办法是拿到数据后,我们把数据从小到大排序。需要自己处理数据。效果上图所示。

2.条形图的圆弧效果。--->>配置

style("type", {

radius: function radius() {

return [0, 50, 50, 0];

},

})

3.柱状图添加文本,还有上图我配置的文案是在柱状图上面而不是右侧居中显示。当时考虑到如果当前的数据和x轴上最大数据一样后,会造成数据超出canvas画布。--->>

配置

chart.guide().text({

position: [obj.orgName, obj.targetValue1],

content: obj.targetValue1 || "",

style: {

textAlign: "center",

textBaseline: "bottom",

},

offsetY: -10,

offsetX: -15,

});

4.有没有同学发现y轴的数据展示问题。数据过多怎么办--->>解决办法是处理y轴数据时候,我是每4(具体数字根据个人来决定)个字符后面加一个\n数据就会自动换行。上图最后一个数据就换行展示了。

5.关键点--敲黑板划重点。

很难发现的问题,当我们把项目发到线上后,真机访问的时候,你上下滑动图表,会发现图表一闪一闪,显示错乱的图表。(如果想在本地看到效果,按住图表上下左右滑动就会出现我描述的问题)

--->>解决办法是,加了一个方法 this.chart.clear();结合上面代码来讲的。想要看到效果可以把这一行代码注释就会出现。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React 可以与 AntV X6 集成,让你可以在 React 应用程序中使用 X6 的图表组件。下面是一个简单的例子,演示如何将 AntV X6 集成到 React 中: 1. 安装依赖 首先,你需要安装以下依赖: ``` npm install @antv/x6 @antv/x6-react-shape @antv/x6-react @antv/x6-react-router ``` 2. 创建一个 React 组件 创建一个新的 React 组件并导入必要的依赖: ```jsx import React from 'react'; import { Graph, Shape } from '@antv/x6'; import { ReactShape, ReactShapeAttrs, ReactShapeComponent } from '@antv/x6-react-shape'; import { X6Router, X6RouterAttrs, X6RouterComponent } from '@antv/x6-react-router'; const MyGraph = () => { const containerRef = React.useRef(null); React.useEffect(() => { const graph = new Graph({ container: containerRef.current, width: 800, height: 600, }); Shape.register('react', ReactShape, true); graph.addNode({ shape: 'react', component: MyComponent, x: 100, y: 100, }); graph.on('node:click', ({ node }) => { console.log('Node clicked:', node); }); return () => { graph.dispose(); }; }, []); return <div ref={containerRef} />; }; const MyComponent = (props: ReactShapeAttrs & X6RouterAttrs) => { const { x, y, width, height, label, onClick } = props; return ( <div style={{ position: 'absolute', left: x, top: y, width, height, border: '1px solid black' }} onClick={onClick} > {label} </div> ); }; export default MyGraph; ``` 在这个例子中,我们创建了一个 `MyGraph` 组件,它包含一个 X6 图表和一个 React 组件 `MyComponent`。在 `useEffect` 钩子中,我们创建了一个新的 Graph 实例,并向其添加一个 `ReactShape` 节点。该节点使用 `MyComponent` 作为组件,具有一些自定义属性(如 `x`, `y`, `width`, `height`, `label`, `onClick`),这些属性都可以在组件中使用。 3. 使用组件 在你的应用程序中,你可以使用 `MyGraph` 组件来渲染你的 X6 图表: ```jsx import React from 'react'; import MyGraph from './MyGraph'; const App = () => { return <MyGraph />; }; export default App; ``` 现在,你已经成功地将 AntV X6 集成到了 React 应用程序中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

见光就死123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值