forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_marker_demos.html
More file actions
185 lines (172 loc) · 7.3 KB
/
custom_marker_demos.html
File metadata and controls
185 lines (172 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Custom Markers - SVG path strings (New API)</title>
<script src="../../dist/plotly.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 20px; max-width: 900px; margin: 0 auto; padding: 20px; }
h1 { border-bottom: 2px solid #3b82f6; padding-bottom: 10px; }
h2 { color: #333; margin-top: 40px; }
.plot { width: 100%; height: 400px; margin: 20px 0; }
.info { background: #e7f3ff; padding: 10px; border-left: 4px solid #3b82f6; margin: 10px 0; }
pre { background: #f5f5f5; padding: 15px; overflow-x: auto; font-size: 13px; }
code { background: #f0f0f0; padding: 2px 6px; border-radius: 3px; }
</style>
</head>
<body>
<h1>Custom Markers — SVG path strings</h1>
<div class="info">
Pass SVG path strings as <code>marker.symbol</code> to create custom shapes.<br>
Paths are precomputed at <strong>r=20</strong>; Plotly scales them by <code>size/20</code>.<br>
Use an <strong>array</strong> for per-point shapes. Rotation uses <code>marker.angle</code>
(applied as <code>transform="rotate()"</code> via SVG <code><use></code>).
</div>
<!-- Demo 1: Basic custom markers -->
<h2>1. Basic Custom Markers</h2>
<p>Precomputed SVG paths at r=20. Mix with built-in symbol names.</p>
<div id="plot1" class="plot"></div>
<pre>
// Heart shape at r=20
var HEART = 'M0,-8C-12,-16 -24,-5.33 -24,0C-24,8 0,16 0,24C0,16 24,8 24,0C24,-5.33 12,-16 0,-8Z';
// 5-point star at r=20
var STAR = 'M0,-20L4.70,-6.47L19.02,-6.18L7.61,2.47L11.76,16.18' +
'L0,8L-11.76,16.18L-7.61,2.47L-19.02,-6.18L-4.70,-6.47Z';
Plotly.newPlot('plot1', [{
x: [1, 2, 3, 4, 5],
y: [2, 3, 4, 3, 2],
mode: 'markers+lines',
marker: {
// mix path strings and built-in symbol names
symbol: [HEART, STAR, 'circle', STAR, HEART],
size: 25,
color: ['red', 'gold', 'blue', 'gold', 'red']
}
}]);</pre>
<!-- Demo 2: Per-point shapes driven by data -->
<h2>2. Per-Point Shapes Driven by Data</h2>
<p>Build a per-point symbol array from your data instead of using a function.</p>
<div id="plot2" class="plot"></div>
<pre>
var DIAMOND = 'M20,0L0,20L-20,0L0,-20Z';
var BIG_DIAMOND = 'M28,0L0,28L-28,0L0,-28Z'; // 1.4× size
var STAR = '...'; // (same as Demo 1)
var types = ['normal', 'big', 'star', 'normal'];
var symbols = types.map(function(t) {
if (t === 'big') return BIG_DIAMOND;
if (t === 'star') return STAR;
return DIAMOND;
});
Plotly.newPlot('plot2', [{
x: [1, 2, 3, 4],
y: [1, 1, 1, 1],
mode: 'markers',
marker: { symbol: symbols, size: 25, color: '#10b981' }
}]);</pre>
<!-- Demo 3: Weather map -->
<h2>3. Weather Map with Rotation</h2>
<p>Per-point symbols + <code>marker.angle</code> for wind direction rotation via SVG.</p>
<div id="plot3" class="plot"></div>
<pre>
// Paths at r=20 for each weather type
var SUN_PATH = 'M10,0A10,10 0 1,1 0,-10A10,10 0 0,1 10,0Z' + /* rays... */;
var CLOUD_PATH = 'M-12,4A7,7 0 1,1 -2,-4A8,8 0 1,1 10,-2A6,6 0 1,1 14,4L-12,4Z';
var WIND_PATHS = {
1: 'M0,24L0,-24M0,-24L12,-18',
2: 'M0,24L0,-24M0,-24L12,-18M0,-18L12,-12',
3: 'M0,24L0,-24M0,-24L12,-18M0,-18L12,-12M0,-12L12,-6'
};
var symbols = locations.map(function(l) {
if (l.weather.type === 'sunny') return SUN_PATH;
if (l.weather.type === 'cloudy') return CLOUD_PATH;
return WIND_PATHS[l.weather.speed];
});
Plotly.newPlot('plot3', [{
mode: 'markers+text',
marker: {
symbol: symbols,
angle: locations.map(l => l.weather.direction || 0), // SVG rotate()
size: 30 // scale = 30/20 = 1.5×
}
}]);</pre>
<script>
// Precomputed path strings at r=20
var HEART = 'M0,-8C-12,-16 -24,-5.33 -24,0C-24,8 0,16 0,24C0,16 24,8 24,0C24,-5.33 12,-16 0,-8Z';
var STAR = 'M0.00,-20.00L4.70,-6.47L19.02,-6.18L7.61,2.47L11.76,16.18L0.00,8.00L-11.76,16.18L-7.61,2.47L-19.02,-6.18L-4.70,-6.47Z';
var DIAMOND = 'M20,0L0,20L-20,0L0,-20Z';
var BIG_DIAMOND = 'M28,0L0,28L-28,0L0,-28Z';
var SUN_PATH = 'M10,0A10,10 0 1,1 0,-10A10,10 0 0,1 10,0Z' +
'M12,0L18,0M8.49,8.49L12.73,12.73' +
'M0,12L0,18M-8.49,8.49L-12.73,12.73' +
'M-12,0L-18,0M-8.49,-8.49L-12.73,-12.73' +
'M0,-12L0,-18M8.49,-8.49L12.73,-12.73';
var CLOUD_PATH = 'M-12,4A7,7 0 1,1 -2,-4A8,8 0 1,1 10,-2A6,6 0 1,1 14,4L-12,4Z';
var WIND_PATHS = {
1: 'M0,24L0,-24M0,-24L12,-18',
2: 'M0,24L0,-24M0,-24L12,-18M0,-18L12,-12',
3: 'M0,24L0,-24M0,-24L12,-18M0,-18L12,-12M0,-12L12,-6'
};
// === Demo 1: Basic markers ===
Plotly.newPlot('plot1', [{
x: [1, 2, 3, 4, 5],
y: [2, 3, 4, 3, 2],
mode: 'markers+lines',
marker: {
symbol: [HEART, STAR, 'circle', STAR, HEART],
size: 25,
color: ['red', 'gold', 'blue', 'gold', 'red']
}
}], { title: 'Mix custom path strings with built-in symbols' });
// === Demo 2: Per-point shapes from data ===
var types = ['normal', 'big', 'star', 'normal'];
var symbols2 = types.map(function(t) {
if (t === 'big') return BIG_DIAMOND;
if (t === 'star') return STAR;
return DIAMOND;
});
Plotly.newPlot('plot2', [{
x: [1, 2, 3, 4],
y: [1, 1, 1, 1],
mode: 'markers',
marker: { symbol: symbols2, size: 25, color: '#10b981' }
}], { title: 'Per-point shapes mapped from data', xaxis: { range: [0, 5] } });
// === Demo 3: Weather map ===
var locations = [
{ name: 'Seattle', lon: -122, lat: 47, weather: { type: 'cloudy' } },
{ name: 'SF', lon: -122, lat: 38, weather: { type: 'sunny' } },
{ name: 'Denver', lon: -105, lat: 40, weather: { type: 'sunny' } },
{ name: 'Chicago', lon: -88, lat: 42, weather: { type: 'cloudy' } },
{ name: 'NYC', lon: -74, lat: 41, weather: { type: 'cloudy' } },
{ name: 'Miami', lon: -80, lat: 26, weather: { type: 'sunny' } },
{ lon: -115, lat: 46, weather: { type: 'wind', direction: 100, speed: 3 } },
{ lon: -100, lat: 42, weather: { type: 'wind', direction: 120, speed: 2 } },
{ lon: -85, lat: 36, weather: { type: 'wind', direction: 150, speed: 1 } }
];
var symbols3 = locations.map(function(l) {
if (l.weather.type === 'sunny') return SUN_PATH;
if (l.weather.type === 'cloudy') return CLOUD_PATH;
return WIND_PATHS[Math.min(l.weather.speed || 1, 3)];
});
Plotly.newPlot('plot3', [{
x: locations.map(function(l) { return l.lon; }),
y: locations.map(function(l) { return l.lat; }),
text: locations.map(function(l) { return l.name || ''; }),
mode: 'markers+text',
textposition: 'bottom center',
marker: {
symbol: symbols3,
size: 30,
color: locations.map(function(l) {
return { sunny: '#FFD700', cloudy: '#708090', wind: '#4169E1' }[l.weather.type];
}),
angle: locations.map(function(l) { return l.weather.direction || 0; }),
line: { width: 1.5, color: '#333' }
}
}], {
title: 'Weather icons with wind direction via marker.angle (SVG rotate)',
xaxis: { range: [-130, -70] },
yaxis: { range: [20, 52], scaleanchor: 'x' }
});
</script>
</body>
</html>