|
| 1 | +# Custom Marker Functions |
| 2 | + |
| 3 | +This document describes how to use custom SVG marker functions in plotly.js scatter plots. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +You can now pass a custom function directly as the `marker.symbol` value to create custom marker shapes. This provides a simple, flexible way to extend the built-in marker symbols without any registration required. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +### Basic Example |
| 12 | + |
| 13 | +```javascript |
| 14 | +// Define a custom marker function |
| 15 | +function heartMarker(r, angle, standoff) { |
| 16 | + var x = r * 0.6; |
| 17 | + var y = r * 0.8; |
| 18 | + return 'M0,' + (-y/2) + |
| 19 | + 'C' + (-x) + ',' + (-y) + ' ' + (-x*2) + ',' + (-y/3) + ' ' + (-x*2) + ',0' + |
| 20 | + 'C' + (-x*2) + ',' + (y/2) + ' 0,' + (y) + ' 0,' + (y*1.5) + |
| 21 | + 'C0,' + (y) + ' ' + (x*2) + ',' + (y/2) + ' ' + (x*2) + ',0' + |
| 22 | + 'C' + (x*2) + ',' + (-y/3) + ' ' + (x) + ',' + (-y) + ' 0,' + (-y/2) + 'Z'; |
| 23 | +} |
| 24 | + |
| 25 | +// Use it directly in a plot |
| 26 | +Plotly.newPlot('myDiv', [{ |
| 27 | + type: 'scatter', |
| 28 | + x: [1, 2, 3, 4, 5], |
| 29 | + y: [2, 3, 4, 3, 2], |
| 30 | + mode: 'markers', |
| 31 | + marker: { |
| 32 | + symbol: heartMarker, // Pass the function directly! |
| 33 | + size: 15, |
| 34 | + color: 'red' |
| 35 | + } |
| 36 | +}]); |
| 37 | +``` |
| 38 | + |
| 39 | +### Multiple Custom Markers |
| 40 | + |
| 41 | +You can use different custom markers for different points by passing an array: |
| 42 | + |
| 43 | +```javascript |
| 44 | +function heartMarker(r) { |
| 45 | + var x = r * 0.6, y = r * 0.8; |
| 46 | + return 'M0,' + (-y/2) + 'C...Z'; |
| 47 | +} |
| 48 | + |
| 49 | +function starMarker(r) { |
| 50 | + var points = 5; |
| 51 | + var outerRadius = r; |
| 52 | + var innerRadius = r * 0.4; |
| 53 | + var path = 'M'; |
| 54 | + |
| 55 | + for (var i = 0; i < points * 2; i++) { |
| 56 | + var radius = i % 2 === 0 ? outerRadius : innerRadius; |
| 57 | + var ang = (i * Math.PI) / points - Math.PI / 2; |
| 58 | + var x = radius * Math.cos(ang); |
| 59 | + var y = radius * Math.sin(ang); |
| 60 | + path += (i === 0 ? '' : 'L') + x.toFixed(2) + ',' + y.toFixed(2); |
| 61 | + } |
| 62 | + path += 'Z'; |
| 63 | + return path; |
| 64 | +} |
| 65 | + |
| 66 | +Plotly.newPlot('myDiv', [{ |
| 67 | + type: 'scatter', |
| 68 | + x: [1, 2, 3, 4, 5], |
| 69 | + y: [2, 3, 4, 3, 2], |
| 70 | + mode: 'markers', |
| 71 | + marker: { |
| 72 | + symbol: [heartMarker, starMarker, heartMarker, starMarker, heartMarker], |
| 73 | + size: 18, |
| 74 | + color: ['red', 'gold', 'pink', 'orange', 'crimson'] |
| 75 | + } |
| 76 | +}]); |
| 77 | +``` |
| 78 | + |
| 79 | +### Mixing with Built-in Symbols |
| 80 | + |
| 81 | +Custom functions work seamlessly with built-in symbol names: |
| 82 | + |
| 83 | +```javascript |
| 84 | +function customDiamond(r) { |
| 85 | + var rd = r * 1.5; |
| 86 | + return 'M' + rd + ',0L0,' + rd + 'L-' + rd + ',0L0,-' + rd + 'Z'; |
| 87 | +} |
| 88 | + |
| 89 | +Plotly.newPlot('myDiv', [{ |
| 90 | + type: 'scatter', |
| 91 | + x: [1, 2, 3, 4], |
| 92 | + y: [1, 2, 3, 4], |
| 93 | + mode: 'markers', |
| 94 | + marker: { |
| 95 | + symbol: ['circle', customDiamond, 'square', customDiamond], |
| 96 | + size: 15 |
| 97 | + } |
| 98 | +}]); |
| 99 | +``` |
| 100 | + |
| 101 | +## Function Signature |
| 102 | + |
| 103 | +Your custom marker function should have the following signature: |
| 104 | + |
| 105 | +```javascript |
| 106 | +function customMarker(r, angle, standoff) { |
| 107 | + // r: radius/size of the marker |
| 108 | + // angle: rotation angle in degrees (for directional markers) |
| 109 | + // standoff: standoff distance from the point (for advanced use) |
| 110 | + |
| 111 | + // Return an SVG path string |
| 112 | + return 'M...Z'; |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Parameters |
| 117 | + |
| 118 | +- **r** (number): The radius/size of the marker. Your path should scale proportionally with this value. |
| 119 | +- **angle** (number, optional): The rotation angle in degrees. Most simple markers can ignore this. |
| 120 | +- **standoff** (number, optional): The standoff distance. Most markers can ignore this. |
| 121 | + |
| 122 | +### Return Value |
| 123 | + |
| 124 | +The function must return a valid SVG path string. The path should: |
| 125 | +- Be centered at (0, 0) |
| 126 | +- Scale proportionally with the radius `r` |
| 127 | +- Use standard SVG path commands (M, L, C, Q, A, Z, etc.) |
| 128 | + |
| 129 | +## SVG Path Commands |
| 130 | + |
| 131 | +Here are the common SVG path commands you can use: |
| 132 | + |
| 133 | +- `M x,y`: Move to absolute position (x, y) |
| 134 | +- `m dx,dy`: Move to relative position (dx, dy) |
| 135 | +- `L x,y`: Line to absolute position |
| 136 | +- `l dx,dy`: Line to relative position |
| 137 | +- `H x`: Horizontal line to x |
| 138 | +- `h dx`: Horizontal line by dx |
| 139 | +- `V y`: Vertical line to y |
| 140 | +- `v dy`: Vertical line by dy |
| 141 | +- `C x1,y1 x2,y2 x,y`: Cubic Bézier curve |
| 142 | +- `Q x1,y1 x,y`: Quadratic Bézier curve |
| 143 | +- `A rx,ry rotation large-arc sweep x,y`: Elliptical arc |
| 144 | +- `Z`: Close path |
| 145 | + |
| 146 | +## Examples |
| 147 | + |
| 148 | +### Simple Triangle |
| 149 | + |
| 150 | +```javascript |
| 151 | +function triangleMarker(r) { |
| 152 | + var h = r * 1.5; |
| 153 | + return 'M0,-' + h + 'L' + r + ',' + (h/2) + 'L-' + r + ',' + (h/2) + 'Z'; |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### Pentagon |
| 158 | + |
| 159 | +```javascript |
| 160 | +function pentagonMarker(r) { |
| 161 | + var points = 5; |
| 162 | + var path = 'M'; |
| 163 | + for (var i = 0; i < points; i++) { |
| 164 | + var angle = (i * 2 * Math.PI / points) - Math.PI / 2; |
| 165 | + var x = r * Math.cos(angle); |
| 166 | + var y = r * Math.sin(angle); |
| 167 | + path += (i === 0 ? '' : 'L') + x.toFixed(2) + ',' + y.toFixed(2); |
| 168 | + } |
| 169 | + return path + 'Z'; |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +### Arrow |
| 174 | + |
| 175 | +```javascript |
| 176 | +function arrowMarker(r) { |
| 177 | + var headWidth = r; |
| 178 | + var headLength = r * 1.5; |
| 179 | + return 'M0,-' + headLength + |
| 180 | + 'L-' + headWidth + ',0' + |
| 181 | + 'L' + headWidth + ',0Z'; |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +## Notes |
| 186 | + |
| 187 | +- Custom marker functions work with all marker styling options (color, size, line, etc.) |
| 188 | +- The function is called for each point that uses it |
| 189 | +- Functions are passed through as-is and not stored in any registry |
| 190 | +- This approach is simpler than the registration-based API |
| 191 | +- For best performance, define your functions once outside the plot call |
| 192 | + |
| 193 | +## Browser Compatibility |
| 194 | + |
| 195 | +Custom marker functions work in all browsers that support plotly.js and SVG path rendering. |
0 commit comments