Files
html/api/ambre-pdf-enh.php

80 lines
3.3 KiB
PHP

<?php
header("Content-Type: application/json");
$path = "/var/www/html/api/ambre-tool-pdf-premium.php";
$c = @file_get_contents($path);
// Enhance the system prompt to suggest best chart type based on topic
$old_sys = '"chart_data\": {\n \"type\": \"bar\",';
$new_sys = '"chart_data\": {\n \"type\": \"bar\", // or \"pie\", \"line\", \"doughnut\", \"radar\", \"polarArea\" selon le sujet',
if (strpos($c, $old_sys) !== false) {
$c = str_replace($old_sys, $new_sys, $c);
}
// Enhance the rendered Chart.js config to handle different types with proper options
$old_js_chart = 'new Chart(ctx, {
type: cd.type || "$chart_type",
data: {
labels: cd.labels || [],
datasets: [{
label: cd.title || "Données",
data: cd.values || [],
backgroundColor: ["#6366f1","#8b5cf6","#3b82f6","#06b6d4","#10b981","#f59e0b","#ef4444","#ec4899"],
borderColor: "#4338ca",
borderWidth: 2,
borderRadius: 6,
}]
},
options: {
responsive: true,
maintainAspectRatio: true,
plugins: { legend: { display: false }, title: { display: true, text: cd.title, color: "#334155", font:{size:14}}},
scales: { y: { beginAtZero: true, grid:{color:"#f1f5f9"}}, x: {grid:{display:false}}},
}
});';
$new_js_chart = 'var _chartType = cd.type || "bar";
var _palette = ["#6366f1","#8b5cf6","#3b82f6","#06b6d4","#10b981","#f59e0b","#ef4444","#ec4899","#84cc16","#f97316"];
var _dataset = {
label: cd.title || "Données",
data: cd.values || [],
backgroundColor: (["pie","doughnut","polarArea"].indexOf(_chartType) >= 0) ? _palette : _palette.slice(0, (cd.values||[]).length),
borderColor: (["line","radar"].indexOf(_chartType) >= 0) ? "#6366f1" : "#4338ca",
borderWidth: (["pie","doughnut","polarArea"].indexOf(_chartType) >= 0) ? 2 : (["line","radar"].indexOf(_chartType) >= 0 ? 3 : 2),
borderRadius: (_chartType === "bar") ? 6 : 0,
tension: (_chartType === "line") ? 0.35 : 0,
fill: (_chartType === "line") ? false : true,
pointBackgroundColor: "#6366f1",
pointRadius: (["line","radar"].indexOf(_chartType) >= 0) ? 5 : 0,
};
var _showLegend = (["pie","doughnut","polarArea","radar"].indexOf(_chartType) >= 0);
var _showScales = (["pie","doughnut","polarArea","radar"].indexOf(_chartType) < 0);
new Chart(ctx, {
type: _chartType,
data: { labels: cd.labels || [], datasets: [_dataset] },
options: {
responsive: true,
maintainAspectRatio: true,
plugins: {
legend: { display: _showLegend, position: "bottom", labels: {boxWidth: 12, font: {size: 11}}},
title: { display: true, text: cd.title || "Données", color: "#334155", font: {size: 14, weight: "600"}, padding: {top: 4, bottom: 14}}
},
scales: _showScales ? { y: { beginAtZero: true, grid: {color: "#f1f5f9"}}, x: {grid: {display: false}}} : {},
}
});';
if (strpos($c, $old_js_chart) !== false) {
$c = str_replace($old_js_chart, $new_js_chart, $c);
}
// Save
$backup = "/opt/wevads/vault/pdf-premium.GOLD-" . date("Ymd-His") . "-chart-types";
@copy($path, $backup);
$wrote = @file_put_contents($path, $c);
echo json_encode([
"wrote" => $wrote,
"size" => strlen($c),
"backup" => basename($backup),
]);