-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaceted-pointrange.html
160 lines (144 loc) · 5.41 KB
/
faceted-pointrange.html
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
<!DOCTYPE html>
<html>
<head>
<title>Faceted Point Range Plot</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.min.js"></script>
<style>
.chart-container {
display: flex;
flex-direction: column;
gap: 20px;
width: 800px;
margin: 20px auto;
}
.facet {
position: relative;
}
.facet-label {
position: absolute;
left: -40px;
top: 50%;
transform: translateY(-50%) rotate(-90deg);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial;
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class="chart-container">
<div class="facet">
<div class="facet-label">Group 1</div>
<canvas id="chart1"></canvas>
</div>
<div class="facet">
<div class="facet-label">Group 2</div>
<canvas id="chart2"></canvas>
</div>
<div class="facet">
<div class="facet-label">Group 3</div>
<canvas id="chart3"></canvas>
</div>
</div>
<script>
// Register custom plugin for error bars
const errorBarPlugin = {
id: 'errorBar',
afterDatasetsDraw: (chart, args, plugins) => {
const {ctx} = chart;
chart.data.datasets.forEach((dataset, i) => {
if (!dataset.errorBars) return;
const meta = chart.getDatasetMeta(i);
meta.data.forEach((element, index) => {
const {x, y} = element.getCenterPoint();
const data = dataset.data[index];
if (!data.lower || !data.upper) return;
ctx.save();
ctx.beginPath();
ctx.strokeStyle = dataset.borderColor;
ctx.lineWidth = 2;
// Draw vertical line
ctx.moveTo(x, chart.scales.y.getPixelForValue(data.lower));
ctx.lineTo(x, chart.scales.y.getPixelForValue(data.upper));
// Draw horizontal caps
const capWidth = 4;
ctx.moveTo(x - capWidth, chart.scales.y.getPixelForValue(data.lower));
ctx.lineTo(x + capWidth, chart.scales.y.getPixelForValue(data.lower));
ctx.moveTo(x - capWidth, chart.scales.y.getPixelForValue(data.upper));
ctx.lineTo(x + capWidth, chart.scales.y.getPixelForValue(data.upper));
ctx.stroke();
ctx.restore();
});
});
}
};
Chart.register(errorBarPlugin);
// Generate dummy data
function generateData(months, baseline, volatility) {
return months.map(month => {
const y = baseline + Math.random() * volatility;
return {
x: month,
y: y,
lower: y - 0.5,
upper: y + 0.5
};
});
}
const months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const groups = ['Group 1', 'Group 2', 'Group 3'];
const scenarios = ['Scenario A', 'Scenario B'];
const colors = {
'Scenario A': 'rgb(75, 192, 192)',
'Scenario B': 'rgb(255, 99, 132)'
};
function createChart(canvasId, groupData, groupLabel) {
const ctx = document.getElementById(canvasId).getContext('2d');
const datasets = scenarios.map(scenario => ({
label: scenario,
data: groupData[scenario],
borderColor: colors[scenario],
backgroundColor: colors[scenario],
pointRadius: 4,
tension: 0.1,
errorBars: true
}));
return new Chart(ctx, {
type: 'line',
data: {
datasets: datasets
},
options: {
responsive: true,
plugins: {
legend: {
position: 'bottom'
}
},
scales: {
x: {
type: 'linear',
ticks: {
stepSize: 1
},
min: 1,
max: 12
},
y: {
beginAtZero: false
}
}
}
});
}
// Create a chart for each group
groups.forEach((group, index) => {
const groupData = {};
scenarios.forEach(scenario => {
groupData[scenario] = generateData(months, 30 + index * 10, 5);
});
createChart(`chart${index + 1}`, groupData, group);
});
</script>
</body>
</html>