-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathridgeline.js
More file actions
167 lines (140 loc) · 4.61 KB
/
ridgeline.js
File metadata and controls
167 lines (140 loc) · 4.61 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
import * as d3 from 'd3';
export function createRidgelinePlot() {
// Update margins for better mobile display
const margin = {
top: 20,
right: 10,
bottom: 10,
left: 140,
};
const container = document.getElementById('ridgeline');
if (!container) return;
// Clear any existing content
container.innerHTML = '';
const width = container.clientWidth - margin.left - margin.right;
const height = container.clientHeight - margin.top - margin.bottom;
// Business metrics
const metrics = [
"Daily Active Users",
"Order Volume",
"Basket Size",
"New Customers",
"Repeat Orders",
"Revenue per User",
"Cart Conversions",
"Payment Success"
];
// Create SVG
const svg = d3.select('#ridgeline')
.append('svg')
.attr('width', '100%')
.attr('height', '100%')
.attr('viewBox', `0 0 ${width + margin.left + margin.right} ${height + margin.top + margin.bottom}`)
.attr('preserveAspectRatio', 'xMidYMid meet')
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Scales
const x = d3.scaleLinear()
.domain([0, 100])
.range([0, width]);
const y = d3.scalePoint()
.domain(metrics)
.range([0, height])
.padding(0.5);
// Add y-axis labels
svg.append('g')
.attr('class', 'y-axis')
.call(d3.axisLeft(y)
.tickSize(0))
.call(g => g.select('.domain').remove())
.selectAll('.tick text')
.attr('class', 'metric-label');
// Generator factory function with improved smoothing
function createGenerator(volatility, trend, spikeProb, spikeMagnitude) {
return function*() {
let value = 50;
let momentum = 0;
const momentumDecay = 0.95;
while (true) {
const spike = Math.random() < spikeProb ? (Math.random() * spikeMagnitude + 1.2) : 1;
momentum = momentum * momentumDecay + (Math.random() - 0.5) * volatility * 10 * spike;
value += momentum + trend;
value = Math.max(35, Math.min(65, value));
yield value;
}
};
}
// Updated patterns with refined parameters for each metric
const patterns = {
"Daily Active Users": createGenerator(0.2, 0.05, 0.05, 1.5),
"Order Volume": createGenerator(0.25, 0.06, 0.08, 1.8),
"Basket Size": createGenerator(0.15, 0.02, 0.04, 1.2),
"New Customers": createGenerator(0.3, 0.08, 0.1, 2.0),
"Repeat Orders": createGenerator(0.2, 0.04, 0.06, 1.5),
"Revenue per User": createGenerator(0.18, 0.03, 0.05, 1.4),
"Cart Conversions": createGenerator(0.15, -0.02, 0.04, 1.3),
"Payment Success": createGenerator(0.1, 0.01, 0.02, 1.1)
};
// Initialize generators
const generators = {};
metrics.forEach(metric => {
generators[metric] = patterns[metric]();
});
// Initial data setup
const pointsPerMetric = 50;
const metricData = new Map(metrics.map(metric => [
metric,
Array.from({ length: pointsPerMetric }, () => generators[metric].next().value)
]));
// Line and area generators with smoother curve
const line = d3.line()
.curve(d3.curveBasis)
.x((d, i) => x(i * (100 / (pointsPerMetric - 1))))
.y(d => d);
const area = d3.area()
.curve(d3.curveBasisClosed)
.x((d, i) => x(i * (100 / (pointsPerMetric - 1))))
.y0(d => y(d.metric))
.y1(d => y(d.metric) + (d.value - 50) * (18 / 50));
// Create initial paths
metrics.forEach(metric => {
const data = metricData.get(metric);
// Add area
svg.append('path')
.attr('class', `metric-area ${metric.replace(/\s+/g, '-')}`)
.datum(data.map(value => ({ metric, value })))
.attr('d', area);
// Add line
svg.append('path')
.attr('class', `metric-line ${metric.replace(/\s+/g, '-')}`)
.datum(data.map(value => y(metric) + (value - 50) * (20 / 50)))
.attr('d', line);
});
let frameCount = 0;
const updateFrequency = 5;
function animate() {
frameCount++;
if (frameCount % updateFrequency === 0) {
metrics.forEach(metric => {
const data = metricData.get(metric);
// Shift existing data left
data.shift();
// Add new value
data.push(generators[metric].next().value);
// Update visualizations
svg.select(`.metric-area.${metric.replace(/\s+/g, '-')}`)
.datum(data.map(value => ({ metric, value })))
.attr('d', area);
svg.select(`.metric-line.${metric.replace(/\s+/g, '-')}`)
.datum(data.map(value => y(metric) + (value - 50) * (20 / 50)))
.attr('d', line);
});
}
requestAnimationFrame(animate);
}
animate();
// Return a cleanup function
return () => {
container.innerHTML = '';
};
}