import{ Line }from'@antv/g2plot';fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json').then((res)=> res.json()).then((data)=>{const line =newLine('container',{
data,xField:'year',yField:'value',seriesField:'category',xAxis:{type:'time',},yAxis:{label:{// 数值格式化为千分位formatter:(v)=>`${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g,(s)=>`${s},`),},},});
line.render();});
网格填充
import{ Line }from'@antv/g2plot';fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json').then((res)=> res.json()).then((data)=>{const line =newLine('container',{data: data
.slice(data.length -90, data.length).filter((item)=> item.category ==='Gas fuel'|| item.category ==='Cement production'),xField:'year',yField:'value',seriesField:'category',// X 轴相关配置xAxis:{nice:true,// tickCount: 8,// 文本标签label:{// autoRotate: false,rotate: Math.PI/6,offset:10,style:{fill:'#aaa',fontSize:12,},formatter:(name)=> name,},title:{text:'年份',style:{fontSize:16,},},// 坐标轴线的配置项 null 表示不展示line:{style:{stroke:'#aaa',},},tickLine:{style:{lineWidth:2,stroke:'#aaa',},length:5,},grid:{line:{style:{stroke:'#ddd',lineDash:[4,2],},},alternateColor:'rgba(0,0,0,0.05)',},},// Y 轴相关配置yAxis:{// max: 3000,// 文本标签label:{autoRotate:false,style:{fill:'#aaa',fontSize:12,},// 数值格式化为千分位formatter:(v)=>`${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g,(s)=>`${s},`),},title:{text:'排放量(顿)',style:{fontSize:16,},},// 坐标轴线的配置项 null 表示不展示line:{style:{stroke:'#aaa',},},tickLine:{style:{lineWidth:2,stroke:'#aaa',},length:5,},grid:{line:{style:{stroke:'#ddd',lineDash:[4,2],},},alternateColor:'rgba(0,0,0,0.05)',},},// labellabel:{layout:[{type:'hide-overlap'}],// 隐藏重叠labelstyle:{textAlign:'right',},formatter:(item)=> item.value,},// pointpoint:{size:5,style:{lineWidth:1,fillOpacity:1,},shape:(item)=>{if(item.category ==='Cement production'){return'circle';}return'diamond';},},annotations:[// 辅助线{type:'line',start:['0%','10%'],end:['100%','10%'],top:true,style:{stroke:'l(0) 0:#ffffff 0.5:#7ec2f3 1:#1890ff',lineWidth:2,},},// 辅助区域{type:'region',start:['0%','0%'],end:['20%','10%'],top:true,style:{fill:'#1890ff',fillOpacity:1,opacity:1,},},// 辅助文本{type:'text',position:['10%','5%'],content:'二氧化碳排放量来源',style:{fill:'#fff',fontSize:12,textAlign:'center',textBaseline:'middle',shadowColor:'#fff',shadowOffsetX:12,shadowOffsetY:12,shadowBlur:2,},},// 辅助线{type:'line',start:['min','median'],end:['max','median'],style:{stroke:'Turquoise',lineDash:[4,2],},},],legend:{position:'top-right',itemName:{style:{fill:'#000',},formatter:(name)=> name,},},// 度量相关配置meta:{// year 对应 xField || yFieldyear:{range:[0,1],},},});
line.render();});
import{ Line }from'@antv/g2plot';fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json').then((res)=> res.json()).then((data)=>{const line =newLine('container',{
data,xField:'year',yField:'value',seriesField:'category',yAxis:{label:{// 数值格式化为千分位formatter:(v)=>`${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g,(s)=>`${s},`),},},color:['#1979C9','#D62A0D','#FAA219'],});
line.render();});
通过回调函数指定折线样式
import{ Line }from'@antv/g2plot';fetch('https://gw.alipayobjects.com/os/bmw-prod/c48dbbb1-fccf-4a46-b68f-a3ddb4908b68.json').then((res)=> res.json()).then((data)=>{const line =newLine('container',{
data,xField:'date',yField:'value',yAxis:{label:{// 数值格式化为千分位formatter:(v)=>`${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g,(s)=>`${s},`),},},seriesField:'type',color:({ type })=>{return type ==='register'?'#F4664A': type ==='download'?'#30BF78':'#FAAD14';},lineStyle:({ type })=>{if(type ==='register'){return{lineDash:[4,4],opacity:1,};}return{opacity:0.5,};},});
line.render();});
通过回调函数指定数据点样式
import{ Line }from'@antv/g2plot';import{ uniq, findIndex }from'@antv/util';fetch('https://gw.alipayobjects.com/os/bmw-prod/55424a73-7cb8-4f79-b60d-3ab627ac5698.json').then((res)=> res.json()).then((data)=>{constCOLOR_PLATE_10=['#5B8FF9','#5AD8A6','#5D7092','#F6BD16','#E8684A','#6DC8EC','#9270CA','#FF9D4D','#269A99','#FF99C3',];const container = document.getElementById('container');const line =newLine(container,{
data,xField:'year',yField:'value',seriesField:'category',yAxis:{label:{// 数值格式化为千分位formatter:(v)=>`${v}`.replace(/\d{1,3}(?=(\d{3})+$)/g,(s)=>`${s},`),},},color:COLOR_PLATE_10,point:{shape:({ category })=>{return category ==='Gas fuel'?'square':'circle';},style:({ year })=>{return{r:Number(year)%4?0:3,// 4 个数据示一个点标记};},},});
line.render();});