Loading...
Fisheye coordinate system (Fisheye) is a special coordinate system transform that applies Cartesian fisheye effects to input dimensions, magnifying focus areas while compressing regions far from the focus. This transform is similar to the visual effect of a fisheye lens, capable of highlighting local details while maintaining a global view.
Fisheye coordinate system transforms are mainly used in the following scenarios:
In G2, fisheye coordinate systems can be implemented through coordinate system transforms or applied dynamically through interaction components.
The basic principles of fisheye coordinate systems are:
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',});chart.options({coordinate: {transform: [{ type: 'fisheye', focusX: 0.5, focusY: 0.5 }],},type: 'point',data: {type: 'fetch',value: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/bubble.json',},encode: {x: 'GDP',y: 'LifeExpectancy',size: 'Population',color: 'continent',shape: 'point',},scale: {size: {type: 'log',range: [4, 20],},},style: {fillOpacity: 0.3,lineWidth: 1,},});chart.render();
Fisheye coordinate systems are particularly suitable for the following scenarios:
The configuration options for fisheye coordinate systems are as follows:
Property | Description | Type | Default | Required |
---|---|---|---|---|
focusX | X-direction position of fisheye transform center | number | 0 | |
focusY | Y-direction position of fisheye transform center | number | 0 | |
distortionX | X-direction distortion magnitude of fisheye transform | number | 2 | |
distortionY | Y-direction distortion magnitude of fisheye transform | number | 2 | |
visual | Whether focusX and focusY values are visual coordinate points | boolean | false |
visual=false
, the value range is [0, 1], representing normalized coordinates; when visual=true
, it represents actual visual coordinate points.The simplest usage is to set a fixed fisheye focus, suitable for scenarios that need to highlight specific areas.
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',});chart.options({coordinate: {transform: [{type: 'fisheye',focusX: 0.7,focusY: 0.3,distortionX: 3,distortionY: 3,},],},type: 'point',data: [{ x: 1, y: 1, category: 'A' },{ x: 2, y: 2, category: 'B' },{ x: 3, y: 3, category: 'C' },{ x: 4, y: 4, category: 'D' },{ x: 5, y: 5, category: 'E' },{ x: 6, y: 6, category: 'F' },{ x: 7, y: 7, category: 'G' },{ x: 8, y: 8, category: 'H' },{ x: 9, y: 9, category: 'I' },],encode: {x: 'x',y: 'y',color: 'category',shape: 'point',},style: {r: 6,lineWidth: 1,},});chart.render();
By adding interaction components, you can implement dynamic fisheye effects where the focus changes with mouse movement.
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',});chart.options({type: 'point',data: {type: 'fetch',value: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/bubble.json',},encode: {x: 'GDP',y: 'LifeExpectancy',size: 'Population',color: 'continent',shape: 'point',},scale: {size: {type: 'log',range: [4, 20],},},style: {fillOpacity: 0.3,lineWidth: 1,},interaction: {fisheye: true, // Enable fisheye interaction},});chart.render();
Here's a complete example that combines fisheye coordinate systems with scatter plots, showing how to use fisheye effects to analyze multi-dimensional data:
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',autoFit: true,});chart.options({coordinate: {transform: [{type: 'fisheye',focusX: 0.4,focusY: 0.6,distortionX: 2.5,distortionY: 2.5,},],},type: 'point',data: {type: 'fetch',value: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/bubble.json',},encode: {x: 'GDP',y: 'LifeExpectancy',size: 'Population',color: 'continent',shape: 'point',},scale: {size: {type: 'log',range: [6, 30],},color: {palette: 'category10',},},style: {fillOpacity: 0.6,stroke: 'white',lineWidth: 1,},axis: {x: {title: { text: 'GDP per Capita' },labelFormatter: '~s',},y: {title: { text: 'Life Expectancy' },},},legend: {color: {position: 'right',title: { text: 'Continent' },},size: {position: 'bottom',title: { text: 'Population' },},},tooltip: {items: [{ channel: 'x', name: 'GDP', valueFormatter: '~s' },{ channel: 'y', name: 'Life Expectancy' },{ channel: 'size', name: 'Population', valueFormatter: '~s' },{ channel: 'color', name: 'Continent' },],},});chart.render();
This example demonstrates how to use fisheye coordinate systems to analyze multi-dimensional bubble chart data, highlighting specific areas through fisheye effects while maintaining overall data perception. The fisheye effect magnifies data points around the focus (GDP: ~$20,000, Life Expectancy: ~75 years), making it easier to observe detailed patterns in this region.
Fisheye coordinate system transformations can be combined with other coordinate system transformations, such as combining with transpose transformation:
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',});chart.options({coordinate: {transform: [{ type: 'transpose' },{type: 'fisheye',focusX: 0.5,focusY: 0.5,distortionX: 2,distortionY: 2,},],},type: 'interval',data: [{ genre: 'Sports', sold: 275 },{ genre: 'Strategy', sold: 115 },{ genre: 'Action', sold: 120 },{ genre: 'Shooter', sold: 350 },{ genre: 'Other', sold: 150 },],encode: {x: 'genre',y: 'sold',color: 'genre',},});chart.render();
The fisheye coordinate system is a powerful visualization tool, particularly suitable for handling dense data and scenarios that require attention to local details. By properly setting focus position and distortion levels, you can highlight key areas while maintaining a global view. Combined with interactive features, fisheye effects can provide more flexible and intuitive data exploration experiences.
In practical applications, it's recommended to adjust fisheye parameters based on data distribution and user needs, avoiding excessive distortion that leads to visual distortion. Additionally, consider combining with other coordinate system transformations and interactive components to create richer and more effective visualization effects.