|
| 1 | +--- |
| 2 | +title: 放大镜 - Magnifier |
| 3 | +type: 组件 |
| 4 | +order: 11 |
| 5 | +--- |
| 6 | + |
| 7 | +放大镜组件用于在图表上提供局部放大功能,帮助用户更清晰地查看数据细节。 |
| 8 | + |
| 9 | +## 何时使用 |
| 10 | + |
| 11 | +- 数据点过于密集,需要查看局部细节 |
| 12 | +- 需要放大特定区域进行详细分析 |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```jsx |
| 17 | +import { jsx, Canvas, Chart, Line, Magnifier } from '@antv/f2'; |
| 18 | + |
| 19 | +const context = document.getElementById('container').getContext('2d'); |
| 20 | +const data = [ |
| 21 | + { date: '2024-01-01', value: 10 }, |
| 22 | + { date: '2024-01-02', value: 15 }, |
| 23 | + { date: '2024-01-03', value: 8 }, |
| 24 | + { date: '2024-01-04', value: 25 }, |
| 25 | + { date: '2024-01-05', value: 30 }, |
| 26 | + { date: '2024-01-06', value: 28 }, |
| 27 | + { date: '2024-01-07', value: 35 }, |
| 28 | +]; |
| 29 | + |
| 30 | +const { props } = ( |
| 31 | + <Canvas context={context} pixelRatio={window.devicePixelRatio}> |
| 32 | + <Chart data={data}> |
| 33 | + <Line x="date" y="value" /> |
| 34 | + <Magnifier show={true} x={200} y={150} width={100} height={80} scale={2} /> |
| 35 | + </Chart> |
| 36 | + </Canvas> |
| 37 | +); |
| 38 | + |
| 39 | +const chart = new Canvas(props); |
| 40 | +chart.render(); |
| 41 | +``` |
| 42 | + |
| 43 | +## Props |
| 44 | + |
| 45 | +部分属性可参考 scale 图表度量,度量详细介绍可见:[度量](/tutorial/scale.zh.md) |
| 46 | + |
| 47 | +### show: boolean |
| 48 | + |
| 49 | +是否显示放大镜,默认为 `false` |
| 50 | + |
| 51 | +### x: number |
| 52 | + |
| 53 | +放大镜中心点的 x 坐标,默认为 `0` |
| 54 | + |
| 55 | +### y: number |
| 56 | + |
| 57 | +放大镜中心点的 y 坐标,默认为 `0` |
| 58 | + |
| 59 | +### width: number |
| 60 | + |
| 61 | +放大镜的宽度,默认为 `100` |
| 62 | + |
| 63 | +### height: number |
| 64 | + |
| 65 | +放大镜的高度,默认为 `80` |
| 66 | + |
| 67 | +### scale: number |
| 68 | + |
| 69 | +放大倍数,默认为 `2` |
| 70 | + |
| 71 | +### radius: number |
| 72 | + |
| 73 | +放大镜圆角半径,默认为 `10` |
| 74 | + |
| 75 | +### borderWidth: number |
| 76 | + |
| 77 | +边框宽度,默认为 `1` |
| 78 | + |
| 79 | +### borderColor: string |
| 80 | + |
| 81 | +边框颜色,默认为 `#e8e8e8` |
| 82 | + |
| 83 | +### backgroundColor: string |
| 84 | + |
| 85 | +背景颜色,默认为 `rgba(255, 255, 255, 0.9)` |
| 86 | + |
| 87 | +### shadowBlur: number |
| 88 | + |
| 89 | +阴影模糊程度,默认为 `10` |
| 90 | + |
| 91 | +### shadowColor: string |
| 92 | + |
| 93 | +阴影颜色,默认为 `rgba(0, 0, 0, 0.3)` |
| 94 | + |
| 95 | +### visible: boolean |
| 96 | + |
| 97 | +是否显示,默认为 `true` |
| 98 | + |
| 99 | +### field: string |
| 100 | + |
| 101 | +数据字段名 |
| 102 | + |
| 103 | +## 方法 |
| 104 | + |
| 105 | +可通过获取 ref 调用 |
| 106 | + |
| 107 | +### show(x: number, y: number) |
| 108 | + |
| 109 | +在指定坐标显示放大镜 |
| 110 | + |
| 111 | +### hide() |
| 112 | + |
| 113 | +隐藏放大镜 |
| 114 | + |
| 115 | +### updatePosition(x: number, y: number) |
| 116 | + |
| 117 | +更新放大镜位置 |
| 118 | + |
| 119 | +### updateScale(scale: number) |
| 120 | + |
| 121 | +更新放大倍数 |
| 122 | + |
| 123 | +## 使用场景示例 |
| 124 | + |
| 125 | +### 基础使用 |
| 126 | + |
| 127 | +```jsx |
| 128 | +<Magnifier show={true} x={200} y={150} /> |
| 129 | +``` |
| 130 | + |
| 131 | +### 自定义样式 |
| 132 | + |
| 133 | +```jsx |
| 134 | +<Magnifier |
| 135 | + show={true} |
| 136 | + x={200} |
| 137 | + y={150} |
| 138 | + width={120} |
| 139 | + height={100} |
| 140 | + scale={3} |
| 141 | + borderColor="#1890ff" |
| 142 | + backgroundColor="rgba(255, 255, 255, 0.95)" |
| 143 | + radius={20} |
| 144 | +/> |
| 145 | +``` |
| 146 | + |
| 147 | +### 动态控制 |
| 148 | + |
| 149 | +```jsx |
| 150 | +const [showMagnifier, setShowMagnifier] = useState(false); |
| 151 | +const [position, setPosition] = useState({ x: 0, y: 0 }); |
| 152 | + |
| 153 | +// 在图表点击时显示放大镜 |
| 154 | +const handleChartClick = (ev) => { |
| 155 | + setPosition({ x: ev.x, y: ev.y }); |
| 156 | + setShowMagnifier(true); |
| 157 | +}; |
| 158 | + |
| 159 | +<Magnifier show={showMagnifier} x={position.x} y={position.y} scale={2.5} />; |
| 160 | +``` |
| 161 | + |
| 162 | +## 常见问题 |
| 163 | + |
| 164 | +### 1. 放大镜不显示 |
| 165 | + |
| 166 | +- 检查 `show` 属性是否为 `true` |
| 167 | +- 确认 `x` 和 `y` 坐标是否在图表范围内 |
| 168 | +- 检查 `visible` 属性是否为 `true` |
0 commit comments