legendFilter
上一篇
fisheye
下一篇
legendHighlight
Loading...
legendFilter
是一种交互功能,允许用户通过点击图例项来过滤图表中显示的数据。筛选功能支持两种类型的图例:
通过图例筛选,用户可以动态控制图表中显示的数据项,增强数据探索和分析能力。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',});chart.data(temperatures).encode('x', 'month').encode('y', 'temperature').encode('color', 'city').call((chart) => chart.line()).call((chart) => chart.point());chart.interaction('legendFilter', true);chart.render();
图例筛选功能会在使用图例时默认开启。
({type: 'interval',legend: {color: {},size: {},},});
也可以在 interaction 中手动设置是否开启:
({type: 'interval',legend: {color: {},size: {},},interaction: {legengFilter: true, // 启用图例筛选交互},});
图例筛选交互可以配置在 View 层级:
chart.interaction('legendFilter', true);
当前版本的 LegendFilter 插件无可配置参数,调用时仅需指定 type:
属性 | 描述 | 类型 | 默认值 | 必选 |
---|---|---|---|---|
type | 交互类型标识符 | string | 无 | 是 |
LegendFilter 插件内部根据图例类型自动判断是否为类目图例或连续图例,进行不同的处理:
插件内部通过图例元素中注入的数据和结构信息来自动识别这些信息,用户无需手动指定。
具体文档看图例 legend
chart.on('legend:filter', (e) => {const { nativeEvent, data } = e;if (!nativeEvent) return;console.log(data);});chart.on('legend:reset', (e) => {const { nativeEvent } = e;if (!nativeEvent) return;console.log('end');});
chart.emit('legend:filter', {data: { channel: 'color', values: ['Sports', 'Strategy'] },});chart.emit('legend:reset', {});
下面展示了一个离散型数据的 legendfilter 交互功能。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',});chart.options({type: 'line',autoFit: true,height: 300,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/temperatures1.json',},encode: {x: (d) => new Date(d.date),y: 'value',color: 'condition',},});chart.render();