viser图表的使用

10 篇文章 1 订阅

最近项目里要用到图表,如图。经理找的模板里自带viser,我找了一下文档,写的真的不咋地,底下一片骂声。

 

找了半天,大概是这么写(数据我瞎写的):

<template>
  <div :style="{ padding: '0 0 32px 32px' }">
    <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
    <v-chart :data="data" :height="124" :force-fit="true" :onClick="handleClick">
      <v-tooltip/>
      <v-axis/>
      <v-legend/>
      <v-bar position="x*y" :adjust="adjust"/>
    </v-chart>
  </div>
</template>
export default {
    data(){
        return{
            title: '统计图',
            dataSource: [
              { type: '已完成', '套餐1': 18, '套餐2': 28, '套餐3': 39, '套餐4': 81},
              { type: '已取消', '套餐1': 18, '套餐2': 28, '套餐3': 39, '套餐4': 81}
        ],
           fields: ['套餐1', '套餐2', '套餐3', '套餐4'],
            adjust: [{
              type: 'dodge',
              marginRatio: 1 / 32
            }],
            aliases: []
       }
    },
    computed: {
      data() {
        const dv = new DataSet.View().source(this.dataSource)
        dv.transform({
          type: 'fold',
          fields: this.fields,
          key: 'x',
          value: 'y'
        })

        // bar 使用不了 - 和 / 所以替换下
        let rows = dv.rows.map(row => {
          if (typeof row.x === 'string') {
            row.color = row.type == 'Jeecg' ? '#ffff00' : '#000000'
            row.x = row.x.replace(/[-/]/g, '_')
          }
          return row
        })
        // 替换别名
        rows.forEach(row => {
          for (let item of this.aliases) {
            if (item.field === row.type) {
              row.type = item.alias;
              break
            }
          }
        })
        return rows
      }
    },
}

问题来了,出来的俩柱状图的颜色就是默认颜色,怎么改呢,文档上也没写。

我百度了半天。有的说写colors,有的说写color,反正我怎么写都不对,只能去找找源码看,你们看看这个数据格式有多变态。

得这么写才行:

<v-bar position="x*y" :color="['type',['#7196C1','#E52A1C']]" :adjust="adjust"/>

贴一下源码里的方法:

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值