G2Plot icon indicating copy to clipboard operation
G2Plot copied to clipboard

如何在react项目中使用G2Plot?

Open xiaodu527 opened this issue 7 years ago • 21 comments

hello, 如题,请问如何在react项目中使用g2plot?

xiaodu527 avatar Nov 30 '19 04:11 xiaodu527

我要想问这个问题。。

xgj1988 avatar Nov 30 '19 16:11 xgj1988

G2Plot的react封装版本会以Ant Design Chart的方式呈现,敬请期待~

paleface001 avatar Dec 01 '19 02:12 paleface001

同问?

kaoding avatar Dec 01 '19 02:12 kaoding

@xiaodu527 @xgj1988 @kaoding G2Plot 项目本身是不感知用户使用 React 还是 Vue 技术栈的,在 React 环境中封装一层 G2Plot 包装,应该难不倒大家。

欢迎大家开源各种 G2Plot 的 react 周边项目,可以参考 echarts-for-react,当然也敬请期待 在 Ant Design 体系下的官方呈现 ^_^

hustcc avatar Dec 02 '19 01:12 hustcc

最简单的方式:

import React, { useRef, useEffect } from 'react';
import { Bar } from '@antv/g2plot';

const App = () => {
  const data = [
    { year: '1951 年', sales: 38 },
    { year: '1952 年', sales: 52 },
    { year: '1956 年', sales: 61 },
    { year: '1957 年', sales: 145 },
    { year: '1958 年', sales: 48 },
  ];

  const container = useRef(null);

  useEffect(() => {
    if (!container.current) {
      return;
    }
    const bar = new Bar(container.current, {
      data,
      xField: 'sales',
      yField: 'year',
      colorField: 'year',
    });
    bar.render();
  }, []);

  return (
    <div>
      <div ref={container} />
    </div>
  ); 
};

afc163 avatar Dec 02 '19 02:12 afc163

@hustcc 难是不难,关键是我npm 装不上。yarn 也不行。 。项目重新删了。重新 npm install 和 yarn 都不行

xgj1988 avatar Dec 02 '19 12:12 xgj1988

@hustcc 难是不难,关键是我npm 装不上。yarn 也不行。 。项目重新删了。重新 npm install 和 yarn 都不行

有报什么错误码?来来点具体的信息~

hustcc avatar Dec 02 '19 12:12 hustcc

#325 @hustcc 看看这个

xgj1988 avatar Dec 02 '19 12:12 xgj1988

没有index.js,暂时只能这样import { Ring } from '@antv/g2plot/esm'

image

zlab avatar Dec 02 '19 12:12 zlab

@zlab 谢谢。

xgj1988 avatar Dec 02 '19 12:12 xgj1988

和bizcharts有什么区别?

chart.tooltip({
  useHtml: true,
  htmlContent: function(title, items) {
    return '<div><ul><li>.....</li></ul></div>';
  },
});

htmlContent不支持ReactNode,太恶心了,回到了拼接html的时代

itemTpl: '<li data-index={index}>' +
  '<span style="background-color:{color};width:8px;height:8px;border-radius:50%;display:inline-block;margin-right:8px;"></span>' +
  '{name}<span class="g2-tooltip-value">{value}</span>' +
  '</li>';

看的头疼

zlab avatar Dec 02 '19 12:12 zlab

请问下如何刷新图表数据呢,暂时没有找到刷新数据的接口呢

ldd27 avatar Dec 07 '19 07:12 ldd27

最简单的方式:

import React, { useRef, useEffect } from 'react';
import { Bar } from '@antv/g2plot';

const App = () => {
  const data = [
    { year: '1951 年', sales: 38 },
    { year: '1952 年', sales: 52 },
    { year: '1956 年', sales: 61 },
    { year: '1957 年', sales: 145 },
    { year: '1958 年', sales: 48 },
  ];

  const container = useRef(null);

  useEffect(() => {
    if (!container.current) {
      return;
    }
    const bar = new Bar(container.current, {
      data,
      xField: 'sales',
      yField: 'year',
      colorField: 'year',
    });
    bar.render();
  }, []);

  return (
    <div>
      <div ref={container} />
    </div>
  ); 
};

@afc163 请问一下

  if (!container.current) {     
     return;
 }

这段代码是否是多余的(因为我删除了一下试了试好像也能用,这个useEffect 第二个参数为空数组不是只会执行一次吗?) 再请问一下useEffect第一次执行是否对应class组件生命周期中的componentDidMount?

GiorgioPeng avatar Dec 08 '19 05:12 GiorgioPeng

请问下如何刷新图表数据呢,暂时没有找到刷新数据的接口呢

chart.updateConfig({ data: newData });

hustcc avatar Dec 09 '19 02:12 hustcc

请问下如何刷新图表数据呢,暂时没有找到刷新数据的接口呢

chart.updateConfig({ data: newData });

好的,我试下,谢谢

ldd27 avatar Dec 09 '19 02:12 ldd27

请问下如何刷新图表数据呢,暂时没有找到刷新数据的接口呢

chart.updateConfig({ data: newData });

好的,我试下,谢谢

image

devtorich avatar Dec 23 '19 07:12 devtorich

当成js就可以在vue,react,或者其他语言环境中使用 image

JasonWong-ZS avatar Dec 26 '19 02:12 JasonWong-ZS

简单的封装了一下

open-data-plan/g2plot-react

kagawagao avatar Jan 16 '20 02:01 kagawagao

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Mar 01 '20 00:03 github-actions[bot]

Ant Design Charts 已经发布了:https://charts.ant.design/

Jackeriss avatar May 27 '20 02:05 Jackeriss