antv/g2中的柱状图一些配置

g2图表有些api的属性不全,文档看着有些费劲,所以整理了一些项目中用到的柱状图的简单配置项

图标初始化

const chart = new Chart({
        container: this.rootRef.current, //图标容器使用react的createRef()指定
        autoFit: true, //图标的宽度自适应
        height: isYesNoLabel ? 500 : chartH,
      });
      chart.data(data);  //导入数据data:[{type:srting,value:number},{},{}]
      const labelWidth = (chart.width/data.length)
      //g2的x轴labe超长时自适应会有些问题,所以自己写了个判断x轴坐标label超长旋转方法
      const isRotateOrNot = data.some(item=>getPixLength(item.type)>labelWidth)
      //根据是否旋转label来设置图表的padding值,如果设为auto的话,xTitle会被挤出canvas画布,chart.padding设置可行,但是chart.height或者.width都不行
      chart.padding = isRotateOrNot ? [20, 50, 150, 100]: [30, 50, 50, 100]

柱体颜色、宽度

chart.legend(false);关闭图例
      const isSettingSizeInstance = chart
        .interval()  //设置柱状图
        .position('type*value') //指定展示的数据
        .color('value', color); //指定柱子颜色
      if (isYesNoLabel && !isCrowd) {
        isSettingSizeInstance.size(60);  //根据条件设置柱子宽度
      }
      // chart.interaction('element-active');//在鼠标hover时出发交互
      chart.interaction('active-region');  //鼠标交互方式,在柱子上方时触发交互
      chart.render();

X轴Y轴的Tittle以及刻度分布

      // Y轴的刻度标题配置
      chart.scale('value', {
        alias: valueTitle, // 定义y title
        ticks: ticksMap, // 设置Y轴的刻度值分布 根据需求formatter后的(10份)
      });
      chart.scale('type', {
        alias: typeTitle,   //定义x title
      })

X轴坐标相关配置

chart.axis('type', {
        tickLine: null, // 不显示刻度线
        title:{     //title:null 不显示title
          style: {
            fontSize: 14,
            fontFamily: 'Roboto-Bold',
            textAlign: 'center',
            fill: '#333', // 文本颜色
          },
          offset:isRotateOrNot	//xTitle的垂直方向偏移设置
          ?maxTypeLength>142
            ?130:maxTypeLength/1.2
            :40
        },
  
        label: {
          autoHide: false,   //关闭label默认的自动隐藏
          autoEllipsis: false, //关闭lable自动省略
          autoRotate:false, //关闭默认的label超长自动旋转 默认时右下45度旋转
          style: {
            fill: '#666',
            textBaseline:'top',  //自定义label旋转时要设置的否者会以文本中心旋转会与柱状图重叠
            textAlign:isRotateOrNot?'right':'center'  //不旋转与不旋转时的文本与柱子对齐方式
          },                
           rotate:isRotateOrNot?Math.PI * -0.25:null, //旋转角度以3.14表示360度 
          offset: 6,
          formatter:(val,index)=>{  //超长label时做的截断
            if(getPixLength(val)>142){
              let PixLength  = 0
              let index = val.split('').findIndex((item)=>{
                PixLength += getPixLength(item)
                return PixLength>142
              })
              return `${val.slice(0,index)}...`
            }else return val
          },
        },
        // X轴线设置
        line: {
          style: {
            stroke: '#ccc',
            width: 1
          }
        }
      });

文本标注信息

data.forEach((item) => {
        const instance = chart.annotation();
        if(false){ //可以通过条件判断来设置是否将数据显示在柱体中
          instance = instance.text({
            position: [item.type, item.value/2 ],  //value在柱子里上下居中
            content:maxValue>=10*10000
            ? !isYesNoLabel?`${(item.value/10000).toFixed(1)}万`:item.value
            :item.value,
            style: {
              // opacity: showNumInInterval ? 1 : 0, //是否在柱子中间显示数据  //定义instance前,用的css隐藏来达到目的
              textAlign: 'center',
              fontSize: 12,
              fill:'#fff',
              stroke:'transparent' //通过设置transparent来消除柱中展示数字的描边
              // fillOpacity:0.1
            },
            // offsetY:-20,
             // offsetX:10,
            // formatter:maxValue>=100*10000?(val)=>{
            //   return (val/10000).toFixed(1)
            // }:null
           
          });
        }; 
        instance.text({ // 百分比文本
          position: [item.type, item.value],
          content: `${(item.value / sum * 100).toFixed(2)}%`, 
          style: {
            textAlign: 'center',
            fontSize: isCrowd ? 10 : 14,
            // fill: '#666',
            linwHeight: 20
          },
          offsetY: -7,
        });
      });
 **顺便提一下g2没有设置图表的title的api ,可以通过char.annotation().text加定位来自己设置或者用外部div来做,g2plot和bizcharts都有**
  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AntV G2 是一款基于图形语法的可视化图表库,支持绘制多种类型的图表,包括柱状图、折线图等。要绘制分组柱状图和折线图混合的图表,可以使用 G2 的 ComboChart 组件。 首先,需要定义数据源,并指定数据字段的类型(例如 x 轴、y 轴等)。接着,可以使用 ComboChart 组件来绘制混合的图表。在 ComboChart ,可以分别指定柱状图和折线图的样式、颜色等属性,如下所示: ```javascript import { Chart } from '@antv/g2'; const data = [ { year: '2010', sales: 450, profit: 200 }, { year: '2011', sales: 560, profit: 230 }, { year: '2012', sales: 620, profit: 250 }, { year: '2013', sales: 750, profit: 300 }, { year: '2014', sales: 800, profit: 350 }, ]; const chart = new Chart({ container: 'container', autoFit: true, height: 400, }); chart.data(data); chart.scale({ sales: { min: 0, }, profit: { min: 0, }, }); chart.axis('year', { label: { style: { fill: '#aaaaaa', }, }, }); chart.line().position('year*profit').color('#fdae6b'); chart.interval().position('year*sales').color('#2c7bb6'); chart.render(); ``` 在上面的代码,我们定义了一个数据源,并指定了 x 轴和 y 轴的数据字段。接着,创建一个 Chart 实例,并指定容器,然后设置数据源和坐标轴的样式。最后,使用 line() 和 interval() 方法分别绘制折线图和柱状图,并指定它们的位置和颜色。 需要注意的是,ComboChart 支持多种混合图表类型,包括折线图、柱状图、面积图等,可以根据自己的需求选择适合的图表类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值