-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofessional_chart.html
More file actions
632 lines (571 loc) · 22.1 KB
/
Copy pathprofessional_chart.html
File metadata and controls
632 lines (571 loc) · 22.1 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Freight Transport Analysis</title>
<!-- Use Chart.js 3.9.1 for best compatibility with Financial plugin -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@2.0.0/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/chartjs-chart-financial@0.1.1/dist/chartjs-chart-financial.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@1.2.1"></script>
<style>
body {
font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 20px;
/* Allow scrolling, don't force center alignment that might squash if viewport is small */
display: block;
overflow: auto;
}
#controls {
display: none;
margin-bottom: 20px;
text-align: center;
}
button {
background-color: #2962FF;
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
cursor: pointer;
border-radius: 4px;
font-weight: bold;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
button:hover {
background-color: #1565C0;
}
#chart-container {
width: 95%;
max-width: 1900px;
margin: 0 auto;
background-color: white;
padding: 40px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
gap: 15px;
}
@media (max-width: 768px) {
#chart-container {
padding: 15px;
width: 100%;
gap: 10px;
}
.chart-header h1 {
font-size: 1.5rem;
}
#c1-container {
height: 400px;
}
#c3-container {
height: 180px;
}
#c4-container {
height: 180px;
}
}
.chart-header {
text-align: center;
margin-bottom: 20px;
}
.chart-header h1 {
margin: 0 0 5px 0;
font-size: 32px;
/* Slightly larger title */
text-transform: uppercase;
color: #000;
}
.chart-header h2 {
margin: 0;
font-size: 16px;
color: #555;
font-weight: normal;
}
.chart-panel {
position: relative;
width: 100%;
}
/* Adjusted heights for widescreen look */
#c1-container {
height: 700px;
}
#c2-container {
height: 250px;
}
#c3-container {
height: 250px;
}
#c4-container {
height: 250px;
}
#c5-container {
height: 300px;
border-bottom: none;
}
canvas {
width: 100% !important;
height: 100% !important;
}
.source-text {
text-align: right;
font-size: 12px;
color: #666;
margin-top: 10px;
font-weight: 500;
}
</style>
</head>
<body>
<!-- Download button removed for GitHub Pages version -->
<div id="chart-container">
<div class="chart-header">
<h1>MONTHLY CANDLESTICK CHART - GENERAL FREIGHT TRANSPORT</h1>
<!-- Subtitle removed as requested -->
</div>
<div id="c1-container" class="chart-panel">
<canvas id="chart1"></canvas>
</div>
<div id="c2-container" class="chart-panel">
<canvas id="chart2"></canvas>
</div>
<div id="c3-container" class="chart-panel">
<canvas id="chart3"></canvas>
</div>
<div id="c4-container" class="chart-panel">
<canvas id="chart4"></canvas>
</div>
<div id="c5-container" class="chart-panel">
<canvas id="chart5"></canvas>
</div>
<div class="source-text">Source: UTK | Generated by RMN</div>
</div>
<!-- Load Data -->
<script src="embedded_data.js"></script>
<script>
// Check if data loaded
if (typeof rawData === 'undefined') {
alert("Error: Data file 'embedded_data.js' not found or failed to load.");
}
// Register plugins
Chart.register(ChartDataLabels);
// --- Data Processing ---
const plMonths = ["styczeń", "luty", "marzec", "kwiecień", "maj", "czerwiec",
"lipiec", "sierpień", "wrzesień", "październik", "listopad", "grudzień"];
// Arrays for Chart.js
const dates = [];
const massOHLC = [];
const massCloses = [];
const massOpens = [];
const momentum = [];
const workData = [];
const distData = [];
const yearlyDataMap = {};
rawData.forEach((d, idx) => {
// Correct date parsing
const year = d.year;
// "styczeń/ January" -> index 0
const mStr = d.month.split('/')[0].trim().toLowerCase();
const mIdx = plMonths.indexOf(mStr);
const date = new Date(year, mIdx, 15); // Middle of month
dates.push(date.getTime()); // Timestamp for Chart.js time scale
// Data Values (Convert Mass to Millions of tons)
const massMil = d.mass_ton / 1000.0;
// OHLC Simulation
const open = idx === 0 ? massMil : (rawData[idx - 1].mass_ton / 1000.0);
const close = massMil;
const high = Math.max(open, close) * 1.005; // +/- 0.5% shadow
const low = Math.min(open, close) * 0.995;
massOHLC.push({ x: date.getTime(), o: open, h: high, l: low, c: close });
massCloses.push(close);
massOpens.push(open);
// Momentum (MoM %)
let mom = 0;
if (idx > 0) {
const prev = rawData[idx - 1].mass_ton / 1000.0;
mom = ((massMil - prev) / prev) * 100;
}
momentum.push({ x: date.getTime(), y: mom });
// Work (Trend Panel)
workData.push({ x: date.getTime(), y: d.work_mln_tkm });
// Distance
distData.push({ x: date.getTime(), y: d.distance_km });
// Yearly Sums
if (!yearlyDataMap[year]) yearlyDataMap[year] = 0;
yearlyDataMap[year] += massMil;
});
// --- Indicators Calculation ---
function calculateSMA(data, window) {
const result = [];
for (let i = 0; i < data.length; i++) {
if (i < window - 1) {
result.push({ x: dates[i], y: null });
} else {
const sum = data.slice(i - window + 1, i + 1).reduce((a, b) => a + b, 0);
result.push({ x: dates[i], y: sum / window });
}
}
return result;
}
const ma12 = calculateSMA(massCloses, 12);
const ma24 = calculateSMA(massCloses, 24);
const distMA12 = calculateSMA(distData.map(d => d.y), 12);
// Linear Regression Trend for Work Chart
function calculateTrendLine(dataPoints) { // dataPoints: {x, y}
const n = dataPoints.length;
let sumX = 0, sumY = 0, sumXY = 0, sumXX = 0;
// Use index as X for regression calculation to avoid huge timestamp numbers
for (let i = 0; i < n; i++) {
sumX += i;
sumY += dataPoints[i].y;
sumXY += i * dataPoints[i].y;
sumXX += i * i;
}
const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
const intercept = (sumY - slope * sumX) / n;
return dataPoints.map((p, i) => ({
x: p.x,
y: slope * i + intercept
}));
}
const workTrend = calculateTrendLine(workData);
// Yearly Bars
const years = Object.keys(yearlyDataMap).sort();
const yearlyVals = years.map(y => yearlyDataMap[y]);
const yearlyColors = yearlyVals.map((v, i) => {
if (i === 0) return '#66BB6A'; // Green
return v >= yearlyVals[i - 1] ? '#66BB6A' : '#EF5350'; // Green vs Red/Pink
});
// --- Chart Configuration ---
// Helper for independent axis control
function setupIndependentAxes(chart) {
chart.canvas.addEventListener('mousemove', (e) => {
const rect = chart.canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const area = chart.chartArea;
if (!area) return;
let mode = 'xy';
if (y > area.bottom) {
mode = 'x';
chart.canvas.style.cursor = 'ew-resize';
} else if (x < area.left || x > area.right) {
mode = 'y';
chart.canvas.style.cursor = 'ns-resize';
} else {
chart.canvas.style.cursor = 'crosshair';
}
if (chart.options.plugins.zoom.zoom.mode !== mode) {
chart.options.plugins.zoom.zoom.mode = mode;
chart.options.plugins.zoom.pan.mode = mode;
chart.update('none');
}
});
chart.canvas.addEventListener('mouseleave', () => {
chart.options.plugins.zoom.zoom.mode = 'xy';
chart.options.plugins.zoom.pan.mode = 'xy';
chart.canvas.style.cursor = 'default';
chart.update('none');
});
}
const commonOptions = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false },
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
label: function (context) {
let label = context.dataset.label || '';
if (label.includes('MA') || label.includes('Trend')) return null;
let val = context.parsed.y;
if (context.dataset.type === 'candlestick') val = context.raw.c;
if (val === null || val === undefined) return null;
return label + ': ' + val.toLocaleString(undefined, { minimumFractionDigits: 1, maximumFractionDigits: 1 });
},
title: function (context) {
const d = new Date(context[0].parsed.x);
return d.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
}
}
},
zoom: {
pan: {
enabled: true,
mode: 'xy',
threshold: 5
},
zoom: {
wheel: { enabled: true },
pinch: { enabled: true },
mode: 'xy'
}
},
datalabels: { display: false }
},
scales: {
x: {
type: 'time',
time: {
unit: 'year',
displayFormats: {
year: 'yyyy'
}
},
grid: {
display: false, // Grid removed
drawBorder: true,
borderColor: '#000',
tickColor: '#000'
},
ticks: {
color: '#000',
font: { weight: 'bold' },
autoSkip: false,
maxRotation: 0
}
},
y: {
grid: {
display: false, // Grid removed
drawBorder: true,
borderColor: '#000',
tickColor: '#000'
},
ticks: {
color: '#000'
}
}
}
};
const ch1 = new Chart(document.getElementById('chart1'), {
data: {
datasets: [
{
type: 'candlestick',
label: 'Freight Volume',
data: massOHLC,
color: { up: '#00C853', down: '#D50000', unchanged: '#999' },
borderColor: '#000',
borderWidth: 0.5,
order: 2
},
{
type: 'line',
label: 'MA(12) - Annual',
data: ma12,
borderColor: '#FF6D00', // Orange
borderWidth: 2,
pointRadius: 0,
order: 1
},
{
type: 'line',
label: 'MA(24) - Long term',
data: ma24,
borderColor: '#6200EA', // Purple
borderWidth: 2,
pointRadius: 0,
order: 1
}
]
},
options: {
...commonOptions,
plugins: {
...commonOptions.plugins,
legend: {
display: true,
position: 'top',
align: 'start',
labels: {
filter: function (item, chart) {
// Remove 'Freight Volume' label from legend
return !item.text.includes('Freight Volume');
}
}
},
title: { display: false }
},
scales: {
x: {
...commonOptions.scales.x,
display: true,
grid: {
display: false, // Remove grid as requested
drawBorder: true,
borderColor: '#000'
}
},
y: {
...commonOptions.scales.y,
title: { display: true, text: 'Volume (mln ton)' },
min: 14, max: 24,
grid: {
display: false, // Remove grid as requested
drawBorder: true,
borderColor: '#000'
}
}
}
}
});
const ch2 = new Chart(document.getElementById('chart2'), {
type: 'bar',
data: {
datasets: [{
label: 'MoM %',
data: momentum,
backgroundColor: (ctx) => {
const v = ctx.raw ? ctx.raw.y : 0;
return v >= 0 ? '#4CAF50' : '#F44336';
}
}]
},
options: {
...commonOptions,
plugins: {
...commonOptions.plugins,
title: { display: true, text: 'Monthly Momentum - Volatility Over Time' }
},
scales: {
x: { ...commonOptions.scales.x, display: false },
y: { ...commonOptions.scales.y, min: -15, max: 25, title: { display: true, text: 'MoM (%)' } }
}
}
});
const ch3 = new Chart(document.getElementById('chart3'), {
data: {
datasets: [
{
type: 'line',
label: 'Work',
data: workData,
borderColor: '#2196F3',
backgroundColor: 'rgba(33, 150, 243, 0.2)',
fill: true,
pointRadius: 0,
borderWidth: 1.5
},
{
type: 'line',
label: 'Trend',
data: workTrend,
borderColor: '#F44336',
borderDash: [5, 5],
borderWidth: 2,
pointRadius: 0,
fill: false
}
]
},
options: {
...commonOptions,
plugins: {
...commonOptions.plugins,
title: { display: true, text: 'Transport Work - Long Haul Trend' },
legend: { display: true, position: 'top', align: 'end', labels: { boxWidth: 10 } }
},
scales: {
x: { ...commonOptions.scales.x, display: false },
y: { ...commonOptions.scales.y, title: { display: true, text: 'Performance (mln tkm)' } }
}
}
});
const ch4 = new Chart(document.getElementById('chart4'), {
data: {
datasets: [
{
type: 'line',
label: 'Avg Dist',
data: distData,
borderColor: '#D32F2F',
borderWidth: 1.5,
pointRadius: 0
},
{
type: 'line',
label: 'MA(12)',
data: distMA12,
borderColor: '#1976D2',
borderWidth: 2,
pointRadius: 0
}
]
},
options: {
...commonOptions,
plugins: {
...commonOptions.plugins,
title: { display: true, text: 'Average Distance of Transport' },
legend: { display: true, position: 'bottom', align: 'end' }
},
scales: {
x: { ...commonOptions.scales.x, display: false },
y: { ...commonOptions.scales.y, min: 190, max: 280, title: { display: true, text: 'Avg dist (km)' } }
}
}
});
const ch5 = new Chart(document.getElementById('chart5'), {
type: 'bar',
data: {
labels: years,
datasets: [{
label: 'Yearly Volume',
data: yearlyVals,
backgroundColor: yearlyColors,
borderColor: '#000', // Black border as requested
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false },
title: { display: true, text: 'Annual Summary - YoY Comparison' },
datalabels: {
display: true,
anchor: 'end',
align: 'top',
formatter: (val) => Math.round(val) + 'm',
font: { weight: 'bold', size: 11 },
color: '#333'
}
},
layout: { padding: { top: 20 } },
scales: {
x: { grid: { display: false } },
y: {
display: true,
title: { display: true, text: 'Yearly Volume (mln ton)' },
grid: { display: false, drawBorder: true, borderColor: '#000' }
}
}
}
});
[ch1, ch2, ch3, ch4, ch5].forEach(setupIndependentAxes);
// Download Function
function downloadImage() {
const chartDiv = document.getElementById("chart-container");
// Set windowWidth larger than container to ensure full render
html2canvas(chartDiv, { scale: 2, windowWidth: 2000 }).then(canvas => {
const link = document.createElement("a");
link.download = "pkp_freight_professional_chart.png";
link.href = canvas.toDataURL("image/png");
link.click();
});
}
</script>
</body>
</html>