透视与组合

指标分组

通过 measures 中的 parentId 属性,可以将指标进行分组展示。

Loading...

行列透视

通过配置维度的 encoding 属性,可以控制维度在透视表中的行列位置。默认维度在行上,设置 encoding: 'column' 可将其放置在列上。

import { useRef, useEffect, memo } from 'react'
import VChart from '@visactor/vchart'
import { register, PivotChart, PivotChartConstructorOptions } from '@visactor/vtable'
import { registerAll, VSeed, Builder, ColorIdEncoding } from '@visactor/vseed'

register.chartModule('vchart', VChart)
registerAll()

const Demo = () => {
  const ref = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (!ref.current) {
      return
    }

    const vseed: VSeed = {
      chartType: 'column',
      dataset: [
        { date: '2019', category: 'food', profit: 10, sales: 88 },
        { date: '2019', category: 'book', profit: 33, sales: 210 },
        { date: '2019', category: 'tools', profit: 27, sales: 150 },

        { date: '2020', category: 'food', profit: 10, sales: 100 },
        { date: '2020', category: 'book', profit: 11, sales: 122 },
        { date: '2020', category: 'tools', profit: 23, sales: 150 },

        { date: '2021', category: 'food', profit: 10, sales: 100 },
        { date: '2021', category: 'book', profit: 30, sales: 190 },
        { date: '2021', category: 'tools', profit: 15, sales: 50 },
      ],
      dimensions: [
        { id: 'date', alias: '日期' },
        { id: 'category', alias: '类型', encoding: 'column' },
      ],
      measures: [
        { id: 'profit', alias: '利润', parentId: 'profit-group' },
        { id: 'sales', alias: '销售额', parentId: 'sales-group' },
      ],
    }

    const builder = Builder.from(vseed)
    const spec = builder.build()

    const tableInstance = new PivotChart(ref.current, spec)

    tableInstance.on('legend_item_click', (args) => {
      console.log('LEGEND_ITEM_CLICK', args)
      tableInstance.updateFilterRules([
        {
          filterKey: ColorIdEncoding,
          filteredValues: args.value,
        },
      ])
    })

    return () => tableInstance.release()
  })

  return <div ref={ref} style={{ height: 260, width: '100%' }}></div>
}

export default Demo