|
| 1 | +# ExtensionMark SyncState Plugin |
| 2 | + |
| 3 | +When using `extensionMark` to draw custom graphics on a series, these graphics do not follow the interactive states (hover, select, etc.) of the primary marks (bars, dots, etc.) by default. |
| 4 | + |
| 5 | +The `ExtensionMarkSyncStatePlugin` enables extensionMarks configured with `syncState: true` to automatically synchronize the interactive states of the corresponding primary marks, providing consistent visual feedback during interaction. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +After each render, the plugin pairs extensionMark graphic elements with primary mark graphic elements via `context.key` (the data dimension identifier). When a primary mark's graphic changes state (e.g., highlight, blur, select), the corresponding extensionMark graphic automatically adopts the same state. |
| 10 | + |
| 11 | +> Note: State synchronization only works when the extension mark and the primary mark are bound to the same datum (i.e., they share the same `context.key`). |
| 12 | +
|
| 13 | +## Registration |
| 14 | + |
| 15 | +```js |
| 16 | +import { registerExtensionMarkSyncStatePlugin } from '@visactor/vchart-extension'; |
| 17 | + |
| 18 | +// Register before creating VChart instances |
| 19 | +registerExtensionMarkSyncStatePlugin(); |
| 20 | +``` |
| 21 | + |
| 22 | +When using the CDN global variable `VChartExtension`, call `VChartExtension.registerExtensionMarkSyncStatePlugin()`. |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +Add `syncState: true` to the extensionMark configuration, along with the corresponding `state` styles: |
| 27 | + |
| 28 | +```javascript livedemo |
| 29 | +/** --Add the following code when used in business-- */ |
| 30 | +// When using in a business context, install @visactor/vchart-extension separately (keep version consistent with vchart) |
| 31 | +// import { registerExtensionMarkSyncStatePlugin } from '@visactor/vchart-extension'; |
| 32 | +/** --Add the above code when used in business-- */ |
| 33 | + |
| 34 | +/** --Delete the following code when used in business-- */ |
| 35 | +const { registerExtensionMarkSyncStatePlugin } = VChartExtension; |
| 36 | +/** --Delete the above code when used in business-- */ |
| 37 | + |
| 38 | +registerExtensionMarkSyncStatePlugin(); |
| 39 | + |
| 40 | +const data = [ |
| 41 | + { category: 'A', value: 80, group: 'February' }, |
| 42 | + { category: 'B', value: 120, group: 'February' }, |
| 43 | + { category: 'C', value: 60, group: 'February' }, |
| 44 | + { category: 'D', value: 150, group: 'February' }, |
| 45 | + { category: 'A', value: 90, group: 'March' }, |
| 46 | + { category: 'B', value: 100, group: 'March' }, |
| 47 | + { category: 'C', value: 110, group: 'March' }, |
| 48 | + { category: 'D', value: 70, group: 'March' } |
| 49 | +]; |
| 50 | + |
| 51 | +const spec = { |
| 52 | + type: 'bar', |
| 53 | + data: [{ id: 'barData', values: data }], |
| 54 | + xField: 'category', |
| 55 | + yField: 'value', |
| 56 | + seriesField: 'group', |
| 57 | + bar: { |
| 58 | + state: { |
| 59 | + highlight: { stroke: '#000', lineWidth: 2 }, |
| 60 | + blur: { fillOpacity: 0.2 }, |
| 61 | + selected: { stroke: 'red', lineWidth: 3 } |
| 62 | + } |
| 63 | + }, |
| 64 | + extensionMark: [ |
| 65 | + { |
| 66 | + type: 'symbol', |
| 67 | + dataId: 'barData', |
| 68 | + name: 'topDot', |
| 69 | + syncState: true, |
| 70 | + style: { |
| 71 | + fill: datum => (datum.group === 'February' ? '#1664FF' : '#1AC6FF'), |
| 72 | + symbolType: 'circle', |
| 73 | + size: 12, |
| 74 | + x: (datum, ctx) => |
| 75 | + ctx.valueToX([datum.category]) + ctx.xBandwidth() / 4 + (datum.group === 'March' ? ctx.xBandwidth() / 2 : 0), |
| 76 | + y: (datum, ctx) => ctx.valueToY([datum.value]) - 15 |
| 77 | + }, |
| 78 | + state: { |
| 79 | + highlight: { fill: 'orange', size: 20, stroke: '#000', lineWidth: 2 }, |
| 80 | + blur: { fillOpacity: 0.15, size: 8 }, |
| 81 | + selected: { |
| 82 | + fill: 'red', |
| 83 | + size: 22, |
| 84 | + outerBorder: { distance: 3, lineWidth: 2, stroke: 'red' } |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + type: 'text', |
| 90 | + dataId: 'barData', |
| 91 | + name: 'topLabel', |
| 92 | + syncState: true, |
| 93 | + style: { |
| 94 | + text: datum => `${datum.value}`, |
| 95 | + fontSize: 11, |
| 96 | + fill: '#333', |
| 97 | + textAlign: 'center', |
| 98 | + textBaseline: 'bottom', |
| 99 | + x: (datum, ctx) => |
| 100 | + ctx.valueToX([datum.category]) + ctx.xBandwidth() / 4 + (datum.group === 'March' ? ctx.xBandwidth() / 2 : 0), |
| 101 | + y: (datum, ctx) => ctx.valueToY([datum.value]) - 26 |
| 102 | + }, |
| 103 | + state: { |
| 104 | + highlight: { fill: 'orange', fontSize: 16, fontWeight: 'bold' }, |
| 105 | + blur: { fillOpacity: 0.1 }, |
| 106 | + selected: { fill: 'red', fontSize: 14, fontWeight: 'bold' } |
| 107 | + } |
| 108 | + } |
| 109 | + ], |
| 110 | + interaction: { |
| 111 | + hover: { enable: true }, |
| 112 | + select: { enable: true } |
| 113 | + }, |
| 114 | + legends: { visible: true, orient: 'top' }, |
| 115 | + title: { |
| 116 | + visible: true, |
| 117 | + text: 'extensionMark syncState Plugin Demo', |
| 118 | + subtext: 'Hover / Click bars to see state synchronization' |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +const vchart = new VChart(spec, { dom: CONTAINER_ID }); |
| 123 | +vchart.renderSync(); |
| 124 | + |
| 125 | +// Just for the convenience of console debugging, DO NOT COPY! |
| 126 | +window['vchart'] = vchart; |
| 127 | +``` |
| 128 | + |
| 129 | +## API |
| 130 | + |
| 131 | +### registerExtensionMarkSyncStatePlugin() |
| 132 | + |
| 133 | +Registers the ExtensionMark SyncState plugin. Must be called before creating VChart instances. |
| 134 | + |
| 135 | +### Configuration |
| 136 | + |
| 137 | +Add the following field to each extensionMark configuration: |
| 138 | + |
| 139 | +| Property | Type | Default | Description | |
| 140 | +| ----------- | --------- | ------- | --------------------------------------------------------------- | |
| 141 | +| `syncState` | `boolean` | `false` | Whether to synchronize interactive states from the primary mark | |
| 142 | + |
| 143 | +When using `syncState`, configure the corresponding state styles in `state` (e.g., `highlight`, `blur`, `selected`) to produce visual effects on state changes. |
| 144 | + |
| 145 | +## Important Notes |
| 146 | + |
| 147 | +1. **Data binding**: The extensionMark must have `dataId` (or `dataIndex`) configured to share the same data source with the primary mark, ensuring correct `context.key` pairing |
| 148 | +2. **State styles**: `syncState` only synchronizes the state name — it does not provide default styles. Configure `highlight`, `blur`, `selected`, etc. in the `state` object |
| 149 | +3. **Group type not supported**: `type: 'group'` extensionMarks do not support `syncState` |
0 commit comments