11import ReactDOM from 'react-dom' ;
2- import React from 'react' ;
3- import { Line } from '@antv/g2plot' ;
4-
2+ import React , { useState , useEffect } from 'react' ;
3+ import { Line , Column } from '@antv/g2plot' ;
54import ReactG2Plot from '../src' ;
65
7- const data = [
6+ const Data = [
87 { year : '1991' , value : 3 } ,
98 { year : '1992' , value : 4 } ,
109 { year : '1993' , value : 3.5 } ,
@@ -16,8 +15,7 @@ const data = [
1615 { year : '1999' , value : 13 } ,
1716] ;
1817
19- // line chart config
20- const config = {
18+ const LineConfig = {
2119 title : {
2220 visible : true ,
2321 text : '曲线折线图' ,
@@ -28,17 +26,55 @@ const config = {
2826 } ,
2927 padding : 'auto' ,
3028 forceFit : true ,
31- data,
29+ data : Data ,
3230 xField : 'year' ,
3331 yField : 'value' ,
3432 smooth : true ,
3533} ;
3634
37- function ref ( plot ) {
38- console . log ( 'G2Plot instance Ref' , plot ) ;
35+ const ColumnConfig = {
36+ title : {
37+ visible : true ,
38+ text : '柱形图' ,
39+ } ,
40+ description : {
41+ visible : true ,
42+ text : '使用柱形图展示每一年的对比情况。' ,
43+ } ,
44+ padding : 'auto' ,
45+ forceFit : true ,
46+ data : Data ,
47+ xField : 'year' ,
48+ yField : 'value' ,
49+ } ;
50+
51+ function Entry ( ) {
52+ const [ isLine , setIsLine ] = useState < boolean > ( true ) ;
53+
54+ function ref ( plot ) {
55+ console . log ( 'G2Plot instance Ref' , plot ) ;
56+ }
57+
58+ function getCtor ( ) {
59+ return isLine ? Line : Column ;
60+ }
61+
62+ function getConfig ( ) {
63+ return isLine ? LineConfig : ColumnConfig ;
64+ }
65+
66+ useEffect ( ( ) => {
67+ const id = setInterval ( ( ) => {
68+ // 切换
69+ setIsLine ( ! isLine ) ;
70+ } , 3000 ) ;
71+
72+ return ( ) => {
73+ clearInterval ( id ) ;
74+ } ;
75+ } , [ isLine ] ) ;
76+
77+ return < ReactG2Plot className = "g2plot-for-react" Ctor = { getCtor ( ) } config = { getConfig ( ) } ref = { ref } /> ;
3978}
4079
41- ReactDOM . render (
42- < ReactG2Plot className = "g2plot-for-react" Ctor = { Line } config = { config } ref = { ref } /> ,
43- document . getElementById ( 'root-container' ) ,
44- ) ;
80+ ReactDOM . render ( < Entry /> , document . getElementById ( 'root-container' ) ) ;
0 commit comments