vue vxe ui vxe-table 分享如何在单元格中渲染高性能的柱状图、饼图等

382 阅读6分钟

官网文档:vxetable.cn

最近有个需求,产品说要在表格中显示一个饼图,用来显示项目类型的比例。找了一下常用图表 echarts 等,发现画出来的图表太庞大了,完全不适合放在表格中,每个单元格都渲染图缩小后也非常卡。看到官方有个轻量级单元格图表插件,使用也很合适,轻量级够用。复杂的图表使用弹窗的方式渲染 echarts 就可以。

评分

{C88033E4-7D25-425C-9C3F-062188270C39}.png

<template>
  <div>
    <vxe-grid v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
const gridOptions = reactive({
  border: true,
  showOverflow: true,
  height: 500,
  columnConfig: {
    resizable: true
  },
  columns: [
    { type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    { field: 'num41', title: '评分', width: 180, cellRender: { name: 'rate' } },
    {
      field: 'num42',
      title: '评分 - 自定义颜色',
      width: 180,
      cellRender: {
        name: 'rate',
        props: {
          colors: ['#91C7AE', '#D48265']
        }
      }
    }
  ],
  data: [
    { id: 101, name: 'test1', num41: 1, num42: 4 },
    { id: 102, name: 'test2', num41: 3, num42: 0 },
    { id: 103, name: 'test3', num41: 1, num42: 1 },
    { id: 104, name: 'test4', num41: 3, num42: 4 },
    { id: 105, name: 'test5', num41: 1, num42: 3 },
    { id: 106, name: 'test6', num41: 3, num42: 3 },
    { id: 107, name: 'test7', num41: 2, num42: 1 },
    { id: 108, name: 'test8', num41: 0, num42: 4 },
    { id: 109, name: 'test9', num41: 1, num42: 2 },
    { id: 1010, name: 'test10', num41: 4, num42: 2 }
  ]
})

</script>

柱状图

{6D9B7F8E-A5E1-4089-BD61-5382FB92F880}.png

<template>
  <div>
    <vxe-grid v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
const gridOptions = reactive({
  border: true,
  showOverflow: true,
  height: 500,
  columnConfig: {
    resizable: true
  },
  columns: [
    { type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    {
      field: 'num10',
      title: '柱状图',
      width: 200,
      cellRender: {
        name: 'bar',
        props: {
          bar: {
            max: 100
          },
          label: {
            formatter: '{value}%'
          }
        }
      }
    },
    {
      field: 'num11',
      title: '柱状图 - 显示值',
      width: 200,
      cellRender: {
        name: 'bar',
        props: {
          label: {
            formatter: '{value}'
          }
        }
      }
    },
    {
      field: 'num12',
      title: '柱状图 - 最大值',
      width: 200,
      cellRender: {
        name: 'bar',
        props: {
          bar: {
            max: 140
          },
          colors: ['#FFDB5C', '#91C7AE', '#D48265'],
          tooltip: {
            formatter: '值:{value}%'
          },
          label: {
            formatter: '{value}%'
          }
        }
      }
    }
  ],
  data: [
    { id: 101, name: 'test1', num10: [60], num11: [60, 111], num12: [60, 134, 76] },
    { id: 102, name: 'test2', num10: [85], num11: [33, 25], num12: [42, 73, 22] },
    { id: 103, name: 'test3', num10: [45], num11: [60, 104], num12: [6, 64, 44] },
    { id: 104, name: 'test4', num10: [88], num11: [76, 99], num12: [41, 81, 77] },
    { id: 105, name: 'test5', num10: [72], num11: [27, 157], num12: [48, 101, 76] },
    { id: 106, name: 'test6', num10: [50], num11: [8, 111], num12: [60, 5, 19] },
    { id: 107, name: 'test7', num10: [16], num11: [60, 6], num12: [9, 57, 34] },
    { id: 108, name: 'test8', num10: [24], num11: [23, 68], num12: [35, 111, 80] },
    { id: 109, name: 'test9', num10: [100], num11: [14, 66], num12: [27, 34, 98] },
    { id: 1010, name: 'test10', num10: [98], num11: [44, 98], num12: [29, 107, 127] }
  ]
})

</script>

饼图

{947F9C56-1B3A-404A-B6E6-E5C297FDC175}.png

<template>
  <div>
    <vxe-grid v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
const gridOptions = reactive({
  border: true,
  showOverflow: true,
  height: 500,
  columnConfig: {
    resizable: true
  },
  columns: [
    { type: 'seq', width: 70 },
    { field: 'name', title: 'Name' },
    {
      field: 'num20',
      title: '饼图 - 环形',
      width: 100,
      cellRender: {
        name: 'pie',
        props: {
          ring: {
            diameter: '60%'
          }
        }
      }
    },
    {
      field: 'num21',
      title: '饼图 - 自定义环形',
      width: 140,
      cellRender: {
        name: 'pie',
        props: {
          colors: ['#FFDB5C', '#F0F0F0'],
          ring: {
            diameter: '60%'
          },
          label: {
            formatter: '{value[0]}%'
          }
        }
      }
    },
    {
      field: 'num23',
      title: '饼图 - 实心',
      width: 100,
      cellRender: {
        name: 'pie'
      }
    },
    {
      field: 'num30',
      title: '饼图 - 自定义实心',
      width: 140,
      cellRender: {
        name: 'pie',
        props: {
          colors: ['#FFDB5C', '#91C7AE', '#D48265'],
          ring: {
            diameter: '60%',
            color: '#2F4554'
          },
          label: {
            color: '#ffffff',
            formatter: '概况'
          }
        }
      }
    },
    {
      field: 'num31',
      title: '多种混合',
      width: 200,
      cellRender: {
        name: 'pies',
        children: [
          {
            props: {
              tooltip: {
                formatter: '值:{value}'
              },
              colors: ['#FFDB5C', '#D48265'],
              ring: { diameter: '60%' }
            }
          },
          {
            props: {
              tooltip: {
                formatter: '值:{value}'
              }
            }
          },
          {
            props: {
              tooltip: {
                formatter: '值:{value}'
              }
            }
          }
        ]
      }
    }
  ],
  data: [
    { id: 101, name: 'test1', num20: [30, 70], num21: [30, 70], num30: [47, 67, 67], num31: [[60, 28, 26], [77, 100], [77, 100, 8, 55, 100, 77, 142]], num23: [20, 30, 50] },
    { id: 102, name: 'test2', num20: [50, 50], num21: [60, 40], num30: [60, 83, 33], num31: [[28, 60, 32], [88, 40], [22, 100, 9, 55, 111, 86, 100]], num23: [200, 400, 500] },
    { id: 103, name: 'test3', num20: [33, 67], num21: [27, 78], num30: [84, 11, 14], num31: [[100, 17, 39], [100, 220], [7, 100, 11, 77, 105, 77, 100]], num23: [10, 80, 10] },
    { id: 104, name: 'test4', num20: [11, 89], num21: [50, 50], num30: [25, 71, 67], num31: [[19, 76, 99], [77, 702], [33, 100, 77, 66, 174, 69, 100]], num23: [80, 10, 10] },
    { id: 105, name: 'test5', num20: [19, 81], num21: [67, 33], num30: [100, 29, 55], num31: [[35, 63, 100], [53, 8], [77, 100, 66, 33, 100, 97, 100]], num23: [18, 30, 26] },
    { id: 106, name: 'test6', num20: [16, 84], num21: [17, 83], num30: [66, 100, 67], num31: [[13, 57, 60], [330, 546], [11, 100, 58, 72, 100, 178, 100]], num23: [14, 33, 26] },
    { id: 107, name: 'test7', num20: [98, 2], num21: [86, 14], num30: [60, 9, 5], num31: [[60, 40, 0], [120, 100], [22, 100, 77, 44, 10, 77, 198]], num23: [20, 5, 15] },
    { id: 108, name: 'test8', num20: [63, 37], num21: [99, 1], num30: [55, 76, 67], num31: [[46, 71, 83], [77, 100], [77, 100, 65, 97, 9, 59, 100]], num23: [10, 20, 30, 40] },
    { id: 109, name: 'test9', num20: [48, 52], num21: [8, 92], num30: [97, 60, 41], num31: [[55, 33, 53], [747, 52], [73, 100, 78, 81, 100, 77, 174]], num23: [14, 20, 36] },
    { id: 1010, name: 'test10', num20: [72, 28], num21: [22, 78], num30: [17, 10, 31], num31: [[77, 100, 58], [75, 85], [14, 250, 59, 44, 16, 50, 100]], num23: [200, 400, 500] }
  ]
})

</script>

复杂的图表使用弹窗,渲染 echarts

{E1392569-66B7-49A7-9200-546646BAC5C1}.png

{9CC3D93B-292F-40A1-BE6C-C80385A4AFD0}.png

{22D88389-13DA-4D02-8CC5-21D9BCD38DEE}.png

github github.com/x-extends/v…
gitee gitee.com/xuliangzhan…