File size: 8,513 Bytes
79dc9c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Events</title>
  <style>
    :root {
      color-scheme: light;
    }
    body {
      margin: 0;
      padding: 20px;
      font-family: Segoe UI, Arial, sans-serif;
      background: #f5f7fb;
      color: #1f2a37;
    }
    h1 {
      margin: 0 0 10px 0;
      font-size: 20px;
      font-weight: 600;
    }
    #legend-container {
      margin-bottom: 8px;
    }
    #chart-wrap {
      position: relative;
      width: min(1100px, 100%);
      height: 520px;
      background: #ffffff;
      border: 1px solid #e2e8f0;
      border-radius: 10px;
      padding: 12px;
      box-sizing: border-box;
    }
    #chart {
      width: 100%;
      height: 100%;
    }
  </style>
  <script src="./luxon.min.js"></script>
  <script src="./chart.umd.min.js"></script>
  <script src="./chartjs-adapter-luxon.umd.min.js"></script>
</head>
<body>
  <h1>Events</h1>
  <div id="legend-container"></div>
  <div id="chart-wrap">
    <canvas id="chart"></canvas>
  </div>
  <script>

const components = { Tooltip: Chart.Tooltip };

const Utils = (() => {
  let _seed = 1;

  const valueOrDefault = (value, defaultValue) => (value === undefined ? defaultValue : value);
  const MONTHS = [
    'January', 'February', 'March', 'April', 'May', 'June',
    'July', 'August', 'September', 'October', 'November', 'December'
  ];

  const COLORS = [
    '#4dc9f6', '#f67019', '#f53794', '#537bc4',
    '#acc236', '#166a8f', '#00a950', '#58595b', '#8549ba'
  ];

  const CHART_COLORS = {
    red: 'rgb(255, 99, 132)',
    orange: 'rgb(255, 159, 64)',
    yellow: 'rgb(255, 205, 86)',
    green: 'rgb(75, 192, 192)',
    blue: 'rgb(54, 162, 235)',
    purple: 'rgb(153, 102, 255)',
    grey: 'rgb(201, 203, 207)'
  };

  const NAMED_COLORS = [
    CHART_COLORS.red,
    CHART_COLORS.orange,
    CHART_COLORS.yellow,
    CHART_COLORS.green,
    CHART_COLORS.blue,
    CHART_COLORS.purple,
    CHART_COLORS.grey
  ];

  function srand(seedValue) {
    const normalized = Number(seedValue);
    _seed = Number.isFinite(normalized) ? (normalized >>> 0) : 1;
  }

  function rand(min, max) {
    const low = valueOrDefault(min, 0);
    const high = valueOrDefault(max, 0);
    _seed = (_seed * 9301 + 49297) % 233280;
    return low + (_seed / 233280) * (high - low);
  }

  function numbers(config) {
    const cfg = config || {};
    const min = valueOrDefault(cfg.min, 0);
    const max = valueOrDefault(cfg.max, 100);
    const from = valueOrDefault(cfg.from, []);
    const count = valueOrDefault(cfg.count, 8);
    const decimals = valueOrDefault(cfg.decimals, 8);
    const continuity = valueOrDefault(cfg.continuity, 1);
    const dfactor = Math.pow(10, decimals) || 0;
    const data = [];

    for (let i = 0; i < count; ++i) {
      const value = (from[i] || 0) + rand(min, max);
      if (rand(0, 1) <= continuity) {
        data.push(Math.round(dfactor * value) / dfactor);
      } else {
        data.push(null);
      }
    }

    return data;
  }

  function points(config) {
    const xs = numbers(config);
    const ys = numbers(config);
    return xs.map((x, i) => ({x, y: ys[i]}));
  }

  function bubbles(config) {
    const cfg = config || {};
    return points(cfg).map((pt) => ({...pt, r: rand(cfg.rmin, cfg.rmax)}));
  }

  function labels(config) {
    const cfg = config || {};
    const min = valueOrDefault(cfg.min, 0);
    const max = valueOrDefault(cfg.max, 100);
    const count = valueOrDefault(cfg.count, 8);
    const decimals = valueOrDefault(cfg.decimals, 8);
    const prefix = valueOrDefault(cfg.prefix, '');
    const step = (max - min) / count;
    const dfactor = Math.pow(10, decimals) || 0;
    const values = [];

    for (let i = min; i < max; i += step) {
      values.push(prefix + Math.round(dfactor * i) / dfactor);
    }

    return values;
  }

  function months(config) {
    const cfg = config || {};
    const count = valueOrDefault(cfg.count, 12);
    const section = cfg.section;
    const values = [];
    for (let i = 0; i < count; ++i) {
      const value = MONTHS[Math.ceil(i) % 12];
      values.push(value.substring(0, section));
    }
    return values;
  }

  function color(index) {
    return COLORS[index % COLORS.length];
  }

  function parseColorToRgb(input) {
    const value = String(input || '').trim();

    const hex3 = /^#([0-9a-f]{3})$/i.exec(value);
    if (hex3) {
      const r = parseInt(hex3[1][0] + hex3[1][0], 16);
      const g = parseInt(hex3[1][1] + hex3[1][1], 16);
      const b = parseInt(hex3[1][2] + hex3[1][2], 16);
      return [r, g, b];
    }

    const hex6 = /^#([0-9a-f]{6})$/i.exec(value);
    if (hex6) {
      const intValue = parseInt(hex6[1], 16);
      const r = (intValue >> 16) & 255;
      const g = (intValue >> 8) & 255;
      const b = intValue & 255;
      return [r, g, b];
    }

    const rgb = /^rgba?\(([^)]+)\)$/i.exec(value);
    if (rgb) {
      const parts = rgb[1].split(',').map((part) => Number(part.trim()));
      if (parts.length >= 3) {
        return [parts[0] || 0, parts[1] || 0, parts[2] || 0];
      }
    }

    const helper = document.createElement('canvas').getContext('2d');
    helper.fillStyle = value || '#000000';
    const normalized = helper.fillStyle;
    const fallback = /^rgba?\(([^)]+)\)$/i.exec(normalized);
    if (fallback) {
      const parts = fallback[1].split(',').map((part) => Number(part.trim()));
      if (parts.length >= 3) {
        return [parts[0] || 0, parts[1] || 0, parts[2] || 0];
      }
    }

    return [0, 0, 0];
  }

  function transparentize(value, opacity) {
    const alpha = opacity === undefined ? 0.5 : 1 - opacity;
    const [r, g, b] = parseColorToRgb(value);
    return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')';
  }

  const BASE_DATE_MS = Date.UTC(2021, 0, 1, 0, 0, 0, 0);
  function newDate(days) {
    return new Date(BASE_DATE_MS + (Number(days) || 0) * 24 * 60 * 60 * 1000);
  }

  function newDateString(days) {
    return newDate(days).toISOString();
  }

  function parseISODate(str) {
    const millis = Date.parse(str);
    return {
      toMillis() {
        return millis;
      }
    };
  }

  return {
    srand,
    rand,
    numbers,
    points,
    bubbles,
    labels,
    months,
    color,
    transparentize,
    CHART_COLORS,
    namedColor(index) {
      return NAMED_COLORS[index % NAMED_COLORS.length];
    },
    newDate,
    newDateString,
    parseISODate
  };
})();

window.components = components;
window.Utils = Utils;
Utils.srand(101024);


const data = {
  labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
  datasets: [{
    label: '# of Votes',
    data: [12, 19, 3, 5, 2, 3],
    borderWidth: 1,
    backgroundColor: ['#CB4335', '#1F618D', '#F1C40F', '#27AE60', '#884EA0', '#D35400'],
  }]
};

// Append '4d' to the colors (alpha channel), except for the hovered index
function handleHover(evt, item, legend) {
  legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => {
    colors[index] = index === item.index || color.length === 9 ? color : color + '4D';
  });
  legend.chart.update();
}

// Removes the alpha channel from background colors
function handleLeave(evt, item, legend) {
  legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => {
    colors[index] = color.length === 9 ? color.slice(0, -2) : color;
  });
  legend.chart.update();
}

const config = {
  type: 'pie',
  data: data,
  options: {
    plugins: {
      legend: {
        onHover: handleHover,
        onLeave: handleLeave
      }
    }
  }
};

function forceEnableHoverTooltip(chartConfig) {
  if (!chartConfig || typeof chartConfig !== 'object') {
    return;
  }

  if (!chartConfig.options || typeof chartConfig.options !== 'object') {
    chartConfig.options = {};
  }

  const options = chartConfig.options;
  if (!options.plugins || typeof options.plugins !== 'object') {
    options.plugins = {};
  }

  const plugins = options.plugins;
  if (plugins.tooltip === false) {
    plugins.tooltip = {};
  }

  if (!plugins.tooltip || typeof plugins.tooltip !== 'object') {
    plugins.tooltip = {};
  }

  const hasExternalTooltip = typeof plugins.tooltip.external === 'function';
  if (!hasExternalTooltip) {
    plugins.tooltip.enabled = true;
  }
}

forceEnableHoverTooltip(config);

const sampleCanvas = document.getElementById('chart');
const sampleChart = new Chart(sampleCanvas, config);
  </script>
</body>
</html>