G2Plot Line 线性 x 轴头尾两头不留空白(或指定留白范围)

G2Plot 的功能是可以的,但是文档是稀烂,压根就没写清楚什么属性支持什么地方,对应的属性也不写清楚可使用什么子属性,稀烂!

image.png

  • 留白配置主要是 range 属性,这个属性支持很多地方,但是没写清楚哪些地方可以用

    // x轴配置
    xAxis: {
      // 轴头尾留空白,0 - 1 取值,0 是最左边,1 是最右边
      range: [0, 1]
    }
    
  • 下面是案例代码

    <template>
      <!-- 图表 -->
      <div id="Line1"></div>
    </template>
    
    <script>
    // 导入
    import { Line } from '@antv/g2plot'
    export default {
      mounted () {
        // 数据源
        const dataSource = [
          {
            title: '1998年5月',
            team: '三水集团',
            time: '1998',
            num: 500
          },
          {
            title: '1999年5月',
            team: '三水集团',
            time: '1999',
            num: 400
          },
          {
            title: '2021年5月',
            team: '三水集团',
            time: '2021',
            num: 1000
          },
          {
            title: '1998年6月',
            team: '笑笑集团啊',
            time: '1998',
            num: 1000
          },
          {
            title: '1999年6月',
            team: '笑笑集团啊',
            time: '1999',
            num: 100
          },
          {
            title: '2021年6月',
            team: '笑笑集团啊',
            time: '2021',
            num: 500
          }
        ]
        // 创建
        const chart = new Line('Line1', {
          // 数据源
          data: dataSource,
          // x轴对应key,横向线
          xField: 'time',
          // y轴对应key,竖向线
          yField: 'num',
          // 分组对应 key,多组数据
          seriesField: 'team',
          // 线条是否为弧度
          smooth: true,
          // x轴配置
          xAxis: {
            // 轴头尾留空白
            range: [0, 1]
          },
          // 提示
          tooltip: {
            // 标题,设置的值,如果为字段key,则会从数据源中取值显示,如果数据源没有该key,则直接显示
            title: 'title',
            // 允许鼠标滑入提示框
            // enterable: true,
            // 自定义
            containerTpl: `
              <div class="g2-tooltip">
                <div class="g2-tooltip-title"></div>
                <ul class="g2-tooltip-list"></ul>
                <li class="g2-tooltip-list-item">
                  <span class="g2-tooltip-marker"></span>
                  <span class="g2-tooltip-name">总计</span>:
                  <span class="g2-tooltip-value">100000</span>
                </li>
              </div>
            `,
            // 自定义 item 模板
            itemTpl: `
              <li class="g2-tooltip-list-item">
                <span class="g2-tooltip-marker" style="background-color: {color};"></span>
                <span class="g2-tooltip-name">{name}</span>:
                <span class="g2-tooltip-value">{value}</span>
              </li>
            `,
            // 是否显示 hover 辅助线
            showCrosshairs: true,
            // 辅助线配置
            crosshairs: {
              // x 表示 x 轴上的辅助线,y 表示 y 轴上的辅助线,xy 表示两条辅助线
              type: 'x',
              // 辅助线是否跟随鼠标移动
              follow: false,
              // 辅助线配置
              line: {
                // 样式配置
                style: {
                  // 线宽
                  lineWidth: 0.5
                }
              }
            }
          },
          // 图例列表
          legend: {
            // y轴偏移
            offsetY: 15,
            // 摆放位置
            position: 'bottom',
            // 图例高度
            itemHeight: 28,
            // 配置图例
            marker: {
              // 图例样式
              style: (style) => {
                // 重组样式
                return {
                  // 半径
                  r: 6,
                  // 样式类型
                  symbol: 'square',
                  // 填充
                  fill: style.fill || style.stroke,
                  // 边框线宽
                  lineWidth: 1,
                  // 边框填充颜色
                  stroke: style.fill || style.stroke,
                  // 边框透明度
                  strokeOpacity: 1,
                  // 线圆角
                  lineCap: 'round',
                  lineJoin: 'round'
                }
              }
            }
          },
          // 主题配置
          theme: {
            // 分类个数小于 10 时使用
            colors10: ['#A685FF', '#FBD86D']
            // 分类个数大于 10 时使用
            // colors20: ['#A685FF', '#FBD86D']
          },
          // 动画配置
          // https://g2plot.antv.vision/zh/docs/api/options/animation
          animation: {
            // 配置图表第一次加载时的入场动画
            appear: {
              // 动画效果
              animation: 'wave-in',
              // 动画执行时间
              duration: 2000
            }
          }
        })
        // 绘制
        chart.render()
        // 更新配置
        // chart.update({ xField: 'num' })
        // 更新数据
        // chart.changeData(dataSource)
        // 销毁
        // chart.destroy()
      }
    }
    </script>
    
    <style>
    
    </style>
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡尔特斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值