Dodaj metodę get_min_roas w klasie Products oraz zaktualizuj szablon product_history.php, aby wyświetlał linię limitu ROAS na wykresie.
This commit is contained in:
@@ -127,62 +127,108 @@
|
||||
});
|
||||
});
|
||||
|
||||
Highcharts.chart('container', {
|
||||
title: {
|
||||
text: ``,
|
||||
},
|
||||
subtitle: {
|
||||
text: ``,
|
||||
},
|
||||
yAxis: {
|
||||
title: {
|
||||
text: ''
|
||||
},
|
||||
},
|
||||
xAxis: {
|
||||
categories: parsedData.dates,
|
||||
labels: {
|
||||
style: {
|
||||
fontSize: '14px'
|
||||
},
|
||||
formatter: function() {
|
||||
var date = new Date(Date.parse(this.value));
|
||||
var day = date.getDate();
|
||||
var month = date.getMonth() + 1;
|
||||
var year = date.getFullYear();
|
||||
const chart = Highcharts.chart('container', {
|
||||
title: { text: `` },
|
||||
subtitle: { text: `` },
|
||||
|
||||
if (day === 1 || this.isLast) {
|
||||
return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
// (możesz zostawić lub usunąć warunkowy blok yAxis z PHP — ten kod zadziała niezależnie)
|
||||
xAxis: {
|
||||
categories: parsedData.dates,
|
||||
labels: {
|
||||
style: { fontSize: '14px' },
|
||||
formatter: function() {
|
||||
var date = new Date(Date.parse(this.value));
|
||||
var day = date.getDate();
|
||||
var month = date.getMonth() + 1;
|
||||
var year = date.getFullYear();
|
||||
if (day === 1 || this.isLast) {
|
||||
return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
plotLines: plotLines
|
||||
},
|
||||
legend: {
|
||||
layout: 'vertical',
|
||||
align: 'right',
|
||||
verticalAlign: 'middle',
|
||||
itemStyle: {
|
||||
fontSize: '14px'
|
||||
}
|
||||
},
|
||||
plotOptions: {
|
||||
series: {
|
||||
label: {
|
||||
connectorAllowed: false
|
||||
},
|
||||
pointStart: 0
|
||||
},
|
||||
},
|
||||
series: parsedData.chart_data,
|
||||
tooltip: {
|
||||
style: {
|
||||
fontSize: '14px'
|
||||
}
|
||||
}
|
||||
plotLines: plotLines
|
||||
},
|
||||
legend: {
|
||||
layout: 'vertical',
|
||||
align: 'right',
|
||||
verticalAlign: 'middle',
|
||||
itemStyle: { fontSize: '14px' }
|
||||
},
|
||||
plotOptions: {
|
||||
series: { label: { connectorAllowed: false }, pointStart: 0 }
|
||||
},
|
||||
series: parsedData.chart_data,
|
||||
tooltip: { style: { fontSize: '14px' } }
|
||||
});
|
||||
|
||||
// >>> DODAJ TO PO UTWORZENIU WYKRESU <<<
|
||||
<?php if ($this->min_roas): ?>
|
||||
(function() {
|
||||
var limitVal = Number(<?= json_encode($this->min_roas) ?>);
|
||||
|
||||
var roasSeries = chart.series.find(function(s) {
|
||||
return (s.name || '').toLowerCase() === 'roas';
|
||||
});
|
||||
|
||||
var targetAxis = roasSeries ? roasSeries.yAxis : chart.yAxis[0];
|
||||
|
||||
// dodanie linii
|
||||
targetAxis.addPlotLine({
|
||||
id: 'min-roas-line',
|
||||
color: 'red',
|
||||
width: 2,
|
||||
value: limitVal,
|
||||
dashStyle: 'Dash',
|
||||
zIndex: 5,
|
||||
label: {
|
||||
text: 'Limit <?= htmlspecialchars((string)$this->min_roas, ENT_QUOTES) ?>',
|
||||
align: 'right',
|
||||
style: { color: 'red', fontSize: '14px' }
|
||||
}
|
||||
});
|
||||
|
||||
function adjustAxisToIncludeValue(axis, val) {
|
||||
var e = axis.getExtremes();
|
||||
if (e.dataMin == null || e.dataMax == null) return;
|
||||
|
||||
var min = Math.min(e.dataMin, val);
|
||||
var max = Math.max(e.dataMax, val);
|
||||
|
||||
var span = (max - min) || 1;
|
||||
var pad = span * 0.05;
|
||||
|
||||
var newMin = min - pad;
|
||||
var newMax = max + pad;
|
||||
|
||||
// uniknij niepotrzebnych setExtremes
|
||||
if (newMin !== e.min || newMax !== e.max) {
|
||||
axis.setExtremes(newMin, newMax); // domyślnie robi redraw
|
||||
}
|
||||
}
|
||||
|
||||
// 1) po załadowaniu wykresu (jeśli już załadowany – od razu)
|
||||
if (chart.hasLoaded) {
|
||||
adjustAxisToIncludeValue(targetAxis, limitVal);
|
||||
} else {
|
||||
Highcharts.addEvent(chart, 'load', function () {
|
||||
adjustAxisToIncludeValue(targetAxis, limitVal);
|
||||
});
|
||||
}
|
||||
|
||||
// 2) przy show/hide serii (klik w legendzie)
|
||||
chart.series.forEach(function (s) {
|
||||
Highcharts.addEvent(s, 'show', function () {
|
||||
adjustAxisToIncludeValue(targetAxis, limitVal);
|
||||
});
|
||||
Highcharts.addEvent(s, 'hide', function () {
|
||||
adjustAxisToIncludeValue(targetAxis, limitVal);
|
||||
});
|
||||
});
|
||||
|
||||
})();
|
||||
<?php endif; ?>
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
console.error('Error AJAX:', textStatus, errorThrown);
|
||||
|
||||
Reference in New Issue
Block a user