Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The lightweight React component for @antv/g2.

License

NotificationsYou must be signed in to change notification settings

pearmini/g2-react

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The lightweightReact component for@antv/g2 5.0, which is based on theSpec API.

Installing

$ npm install @berryv/g2-react
importReactfrom"react";import{Chart}from"@berryv/g2-react";exportfunctionDemo(){return(<Chartoptions={{type:"interval",width:640,height:480,data:[{genre:"Sports",sold:275},{genre:"Strategy",sold:115},{genre:"Action",sold:120},{genre:"Shooter",sold:350},{genre:"Other",sold:150},],encode:{x:"genre",y:"sold"},}}/>);}

API Reference

PropertyDescriptionTypeDefault
optionstheoptions for the visualization, saychart.options(options)G2options |null-
renderertherenderer of @antv/g canvasChartOptions-
stylethe style of the containerCSSProperties-
onInitthe callback called after the chart instantiatingFunction-
refthe ref for thechart instanceChartRef-

Examples

Creating Chart

For more examples forprops.options, see eachG2 example and click the "Spec Tab".

importReactfrom"react";import{Chart}from"@berryv/g2-react";import{Renderer}from"@antv/g-svg";exportfunctionDemo(){constrenderer=useMemo(()=>newRenderer(),[]);return(<Chart// Set renderer to SVG, optional.renderer={renderer}options={{type:"interval",autoFit:true,// Fit the container.data:[{genre:"Sports",sold:275},{genre:"Strategy",sold:115},{genre:"Action",sold:120},{genre:"Shooter",sold:350},{genre:"Other",sold:150},],encode:{x:"genre",y:"sold"},}}/>);}

Updating Data

UsinguseMemo to define a computed options with data as a decency, this allows rerendering chart after the data updating.

importReact,{useState,useMemo}from"react";import{Chart}from"@berryv/g2-react";exportfunctionDemo(){const[data,setData]=useState(null);// The options will update after data updating.constoptions=useMemo(()=>({type:"interval",      data,encode:{x:"genre",y:"sold"},}),[data]);useEffect(()=>{// Mock the fetch delay.setTimeout(()=>{// Update the data.setData([{genre:"Sports",sold:275},{genre:"Strategy",sold:115},{genre:"Action",sold:120},{genre:"Shooter",sold:350},{genre:"Other",sold:150},]);},1000);});return<>{data===null ?<p>Loading...</p> :<Chartoptions={options}/>}</>;}

Handling Events

<Chart/> exposes the ref for the G2chart instance, which can be used to handle events or get some instances, such as scale, coordinate, etc,.

importReact,{useRef,useEffect}from"react";import{ChartEvent}from"@antv/g2";import{Chart}from"@berryv/g2-react";exportfunctionDemo(){constchartRef=useRef();functiononInit(){constchart=chartRef.current;// Listen input events.chart.on("plot:mouseover",()=>{});// Listen lifecycle events.chart.on(ChartEvent.AFTER_RENDER,()=>{// Emit to init the state of highlight interaction.chart.emit("element:highlight",{data:{data:{genre:"Sports"},},});});}return<Chartref={chartRef}onInit={onInit}/>;}

Customizing Component

With theregister API of G2, you can customize visual component and using it in options, such as customizing a triangle shape for bar chart:

importReact,{useRef,useEffect}from"react";import{register}from"@antv/g2";// Register a triangle shape for interval globally.register("shape.interval.triangle",(style,context)=>{const{ document}=context;return(P,value,defaults)=>{const{color:defaultColor}=defaults;const[p0,p1,p2,p3]=P;constpm=[(p0[0]+p1[0])/2,p0[1]];const{ color=defaultColor}=value;constgroup=document.createElement("g");constpolygon=document.createElement("polygon",{style:{        ...style,fill:color,points:[pm,p2,p3],},});group.appendChild(polygon);returngroup;};});exportfunctionDemo(){return(<Chartoptions={{type:"interval",data:[{genre:"Sports",sold:275},{genre:"Strategy",sold:115},{genre:"Action",sold:120},{genre:"Shooter",sold:350},{genre:"Other",sold:150},],encode:{x:"genre",y:"sold",shape:"triangle",// Use the custom shape.},}}/>);}

Styling Container

Define the css styles of the container:

importReactfrom"react";import{Chart}from"@berryv/g2-react";exportfunctionDemo(){// ...return(<Chartoptions={{autoFit:true}}style={{width:800,height:600,background:"#eee",padding:"1em",borderRadius:"0.5em",}}/>);}

About

The lightweight React component for @antv/g2.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp