-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcustom_marker_demo.html
More file actions
172 lines (161 loc) · 5.19 KB
/
custom_marker_demo.html
File metadata and controls
172 lines (161 loc) · 5.19 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Custom Marker Functions Demo</title>
<script src="dist/plotly.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #333;
border-bottom: 2px solid #3b82f6;
padding-bottom: 10px;
}
.plot {
width: 100%;
height: 500px;
margin: 20px 0;
}
.code-block {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 4px;
padding: 15px;
margin: 15px 0;
overflow-x: auto;
}
pre {
margin: 0;
font-family: 'Courier New', monospace;
font-size: 14px;
}
.info {
background-color: #e7f3ff;
border-left: 4px solid #3b82f6;
padding: 15px;
margin: 15px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>Custom Marker Functions Demo</h1>
<div class="info">
<strong>New Feature:</strong> You can now pass custom functions directly as
<code>marker.symbol</code> values to create custom marker shapes!
</div>
<h2>Example: Custom Marker Functions</h2>
<div id="plot1" class="plot"></div>
<h3>Code:</h3>
<div class="code-block">
<pre>// Define custom marker functions
function heartMarker(r) {
var x = r * 0.6, y = r * 0.8;
return 'M0,' + (-y/2) +
'C' + (-x) + ',' + (-y) + ' ' + (-x*2) + ',' + (-y/3) + ' ' + (-x*2) + ',0' +
'C' + (-x*2) + ',' + (y/2) + ' 0,' + (y) + ' 0,' + (y*1.5) +
'C0,' + (y) + ' ' + (x*2) + ',' + (y/2) + ' ' + (x*2) + ',0' +
'C' + (x*2) + ',' + (-y/3) + ' ' + (x) + ',' + (-y) + ' 0,' + (-y/2) + 'Z';
}
function star5Marker(r) {
var points = 5, path = 'M';
for (var i = 0; i < points * 2; i++) {
var radius = i % 2 === 0 ? r : r * 0.4;
var ang = (i * Math.PI) / points - Math.PI / 2;
path += (i === 0 ? '' : 'L') +
(radius * Math.cos(ang)).toFixed(2) + ',' +
(radius * Math.sin(ang)).toFixed(2);
}
return path + 'Z';
}
// Use them directly in a plot
Plotly.newPlot('plot1', [{
x: [1, 2, 3, 4, 5],
y: [2, 3, 4, 3, 2],
mode: 'markers+lines',
marker: {
symbol: [heartMarker, star5Marker, 'circle', star5Marker, heartMarker],
size: 20,
color: ['red', 'gold', 'blue', 'orange', 'crimson']
}
}]);</pre>
</div>
</div>
<script>
// Define custom heart marker
function heartMarker(r) {
var x = r * 0.6;
var y = r * 0.8;
return 'M0,' + (-y/2) +
'C' + (-x) + ',' + (-y) + ' ' + (-x*2) + ',' + (-y/3) + ' ' + (-x*2) + ',0' +
'C' + (-x*2) + ',' + (y/2) + ' 0,' + (y) + ' 0,' + (y*1.5) +
'C0,' + (y) + ' ' + (x*2) + ',' + (y/2) + ' ' + (x*2) + ',0' +
'C' + (x*2) + ',' + (-y/3) + ' ' + (x) + ',' + (-y) + ' 0,' + (-y/2) + 'Z';
}
// Define custom 5-point star marker
function star5Marker(r) {
var points = 5;
var outerRadius = r;
var innerRadius = r * 0.4;
var path = 'M';
for (var i = 0; i < points * 2; i++) {
var radius = i % 2 === 0 ? outerRadius : innerRadius;
var ang = (i * Math.PI) / points - Math.PI / 2;
var x = radius * Math.cos(ang);
var y = radius * Math.sin(ang);
path += (i === 0 ? '' : 'L') + x.toFixed(2) + ',' + y.toFixed(2);
}
path += 'Z';
return path;
}
// Create the plot
var trace = {
x: [1, 2, 3, 4, 5],
y: [2, 3, 4, 3, 2],
mode: 'markers+lines',
name: 'Custom Markers',
marker: {
symbol: [heartMarker, star5Marker, 'circle', star5Marker, heartMarker],
size: 20,
color: ['#e74c3c', '#f39c12', '#3498db', '#ff9800', '#c0392b'],
line: {
color: '#34495e',
width: 2
}
},
line: {
color: '#95a5a6',
width: 2,
dash: 'dot'
}
};
var layout = {
title: 'Custom Marker Functions Demo',
xaxis: {
title: 'X Axis',
gridcolor: '#ecf0f1'
},
yaxis: {
title: 'Y Axis',
gridcolor: '#ecf0f1'
},
plot_bgcolor: '#fafafa',
showlegend: true
};
Plotly.newPlot('plot1', [trace], layout);
console.log('Plot created successfully!');
</script>
</body>
</html>