Skip to content

Commit 343dd6c

Browse files
Copilotgatopeich
andcommitted
Add demo file for custom marker feature
Co-authored-by: gatopeich <[email protected]>
1 parent 8310635 commit 343dd6c

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

devtools/custom_marker_demo.html

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Custom Marker Demo - Plotly.js</title>
6+
<!-- After building, use the local build instead of CDN -->
7+
<script src="dist/plotly.js"></script>
8+
<style>
9+
body {
10+
font-family: Arial, sans-serif;
11+
margin: 20px;
12+
background-color: #f5f5f5;
13+
}
14+
.container {
15+
max-width: 1200px;
16+
margin: 0 auto;
17+
background-color: white;
18+
padding: 20px;
19+
border-radius: 8px;
20+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
21+
}
22+
h1 {
23+
color: #333;
24+
border-bottom: 2px solid #3b82f6;
25+
padding-bottom: 10px;
26+
}
27+
.plot {
28+
width: 100%;
29+
height: 500px;
30+
margin: 20px 0;
31+
}
32+
.code-block {
33+
background-color: #f8f9fa;
34+
border: 1px solid #dee2e6;
35+
border-radius: 4px;
36+
padding: 15px;
37+
margin: 15px 0;
38+
overflow-x: auto;
39+
}
40+
pre {
41+
margin: 0;
42+
font-family: 'Courier New', monospace;
43+
font-size: 14px;
44+
}
45+
.info {
46+
background-color: #e7f3ff;
47+
border-left: 4px solid #3b82f6;
48+
padding: 15px;
49+
margin: 15px 0;
50+
}
51+
</style>
52+
</head>
53+
<body>
54+
<div class="container">
55+
<h1>Custom SVG Markers in Plotly.js</h1>
56+
57+
<div class="info">
58+
<strong>New Feature:</strong> You can now add custom SVG marker symbols to scatter plots using
59+
<code>Plotly.Drawing.addCustomMarker(name, drawFunc, opts)</code>
60+
</div>
61+
62+
<h2>Example: Custom Markers</h2>
63+
<div id="plot1" class="plot"></div>
64+
65+
<h3>Code:</h3>
66+
<div class="code-block">
67+
<pre>// Define a custom heart-shaped marker
68+
function heartMarker(r, angle, standoff) {
69+
var x = r * 0.6;
70+
var y = r * 0.8;
71+
return 'M0,' + (-y/2) +
72+
'C' + (-x) + ',' + (-y) + ' ' + (-x*2) + ',' + (-y/3) + ' ' + (-x*2) + ',0' +
73+
'C' + (-x*2) + ',' + (y/2) + ' 0,' + (y) + ' 0,' + (y*1.5) +
74+
'C0,' + (y) + ' ' + (x*2) + ',' + (y/2) + ' ' + (x*2) + ',0' +
75+
'C' + (x*2) + ',' + (-y/3) + ' ' + (x) + ',' + (-y) + ' 0,' + (-y/2) + 'Z';
76+
}
77+
78+
// Define a custom star marker
79+
function star5Marker(r, angle, standoff) {
80+
var points = 5;
81+
var outerRadius = r;
82+
var innerRadius = r * 0.4;
83+
var path = 'M';
84+
85+
for (var i = 0; i < points * 2; i++) {
86+
var radius = i % 2 === 0 ? outerRadius : innerRadius;
87+
var ang = (i * Math.PI) / points - Math.PI / 2;
88+
var x = radius * Math.cos(ang);
89+
var y = radius * Math.sin(ang);
90+
path += (i === 0 ? '' : 'L') + x.toFixed(2) + ',' + y.toFixed(2);
91+
}
92+
path += 'Z';
93+
return path;
94+
}
95+
96+
// Register the custom markers
97+
Plotly.Drawing.addCustomMarker('heart', heartMarker);
98+
Plotly.Drawing.addCustomMarker('star5', star5Marker);
99+
100+
// Use them in a plot
101+
Plotly.newPlot('plot1', [{
102+
x: [1, 2, 3, 4, 5],
103+
y: [2, 3, 4, 3, 2],
104+
mode: 'markers',
105+
marker: {
106+
symbol: ['heart', 'star5', 'heart-open', 'star5-open', 'heart-dot'],
107+
size: 20,
108+
color: ['red', 'gold', 'pink', 'orange', 'crimson']
109+
}
110+
}]);</pre>
111+
</div>
112+
113+
<h2>API Reference</h2>
114+
<div class="code-block">
115+
<pre><strong>Plotly.Drawing.addCustomMarker(name, drawFunc, opts)</strong>
116+
117+
<strong>Parameters:</strong>
118+
- name (string): The name of the new marker symbol
119+
- drawFunc (function): A function(r, angle, standoff) that returns an SVG path string
120+
- opts (object, optional): Configuration options
121+
- backoff (number): Backoff distance for this symbol (default: 0)
122+
- needLine (boolean): Whether this symbol needs a line (default: false)
123+
- noDot (boolean): Whether to skip creating -dot variants (default: false)
124+
- noFill (boolean): Whether this symbol should not be filled (default: false)
125+
126+
<strong>Returns:</strong>
127+
- (number): The symbol number assigned to the new marker
128+
129+
<strong>Marker Variants:</strong>
130+
Automatically creates these variants (unless noDot is true):
131+
- 'name' : Base marker (filled)
132+
- 'name-open' : Open marker (outline only)
133+
- 'name-dot' : Marker with dot in center
134+
- 'name-open-dot' : Open marker with dot in center</pre>
135+
</div>
136+
</div>
137+
138+
<script>
139+
// Check if the API is available
140+
if (typeof Plotly.Drawing === 'undefined' || typeof Plotly.Drawing.addCustomMarker !== 'function') {
141+
document.getElementById('plot1').innerHTML =
142+
'<div style="padding: 20px; text-align: center; color: #d32f2f;">' +
143+
'<h3>⚠️ Custom Marker API Not Available</h3>' +
144+
'<p>This page requires a build of plotly.js with the custom marker feature.</p>' +
145+
'<p>Run <code>npm run bundle</code> to build the library with this feature.</p>' +
146+
'</div>';
147+
} else {
148+
// Define a custom heart-shaped marker
149+
function heartMarker(r, angle, standoff) {
150+
var x = r * 0.6;
151+
var y = r * 0.8;
152+
return 'M0,' + (-y/2) +
153+
'C' + (-x) + ',' + (-y) + ' ' + (-x*2) + ',' + (-y/3) + ' ' + (-x*2) + ',0' +
154+
'C' + (-x*2) + ',' + (y/2) + ' 0,' + (y) + ' 0,' + (y*1.5) +
155+
'C0,' + (y) + ' ' + (x*2) + ',' + (y/2) + ' ' + (x*2) + ',0' +
156+
'C' + (x*2) + ',' + (-y/3) + ' ' + (x) + ',' + (-y) + ' 0,' + (-y/2) + 'Z';
157+
}
158+
159+
// Define a custom 5-point star marker
160+
function star5Marker(r, angle, standoff) {
161+
var points = 5;
162+
var outerRadius = r;
163+
var innerRadius = r * 0.4;
164+
var path = 'M';
165+
166+
for (var i = 0; i < points * 2; i++) {
167+
var radius = i % 2 === 0 ? outerRadius : innerRadius;
168+
var ang = (i * Math.PI) / points - Math.PI / 2;
169+
var x = radius * Math.cos(ang);
170+
var y = radius * Math.sin(ang);
171+
path += (i === 0 ? '' : 'L') + x.toFixed(2) + ',' + y.toFixed(2);
172+
}
173+
path += 'Z';
174+
return path;
175+
}
176+
177+
// Register the custom markers
178+
console.log('Registering custom markers...');
179+
var heartNum = Plotly.Drawing.addCustomMarker('heart', heartMarker);
180+
var star5Num = Plotly.Drawing.addCustomMarker('star5', star5Marker);
181+
console.log('Heart marker number:', heartNum);
182+
console.log('Star5 marker number:', star5Num);
183+
184+
// Create the plot
185+
var trace = {
186+
x: [1, 2, 3, 4, 5],
187+
y: [2, 3, 4, 3, 2],
188+
mode: 'markers+lines',
189+
name: 'Custom Markers',
190+
marker: {
191+
symbol: ['heart', 'star5', 'heart-open', 'star5-open', 'heart-dot'],
192+
size: 20,
193+
color: ['#e74c3c', '#f39c12', '#e91e63', '#ff9800', '#c0392b'],
194+
line: {
195+
color: '#34495e',
196+
width: 2
197+
}
198+
},
199+
line: {
200+
color: '#95a5a6',
201+
width: 2,
202+
dash: 'dot'
203+
}
204+
};
205+
206+
var layout = {
207+
title: 'Custom SVG Markers Demo',
208+
xaxis: {
209+
title: 'X Axis',
210+
gridcolor: '#ecf0f1'
211+
},
212+
yaxis: {
213+
title: 'Y Axis',
214+
gridcolor: '#ecf0f1'
215+
},
216+
plot_bgcolor: '#fafafa',
217+
showlegend: true
218+
};
219+
220+
Plotly.newPlot('plot1', [trace], layout);
221+
console.log('Plot created successfully!');
222+
}
223+
</script>
224+
</body>
225+
</html>

0 commit comments

Comments
 (0)