This commit is contained in:
root
2025-10-07 16:41:56 +05:00
parent 6268304bd8
commit 31be4ebe1e
2 changed files with 28 additions and 12 deletions
@@ -154,20 +154,24 @@ public class OpenAiChartService {
// learningsAsString + "\n---";
// }
private String buildChartDataPrompt(String learningsAsString) {
return "You are an AI data analyst robot. Carefully analyze the following text. Find all sets of numerical data suitable for simple bar or pie charts. For EACH data set you find, create a JSON object."
return "You are an AI data analyst robot. Carefully analyze the following text. Find all sets of numerical data suitable for simple diagrams. For EACH data set you find, create a JSON object."
+
" Return ONLY a JSON array of these objects. The structure of each object MUST STRICTLY be: " +
"{\"chartType\": \"bar\" or \"pie\", \"title\": \"Chart Title\", \"labels\": [\"Label 1\"], \"data\": [number1, number2]}. "
" Return ONLY a JSON array of these objects." +
" The structure of each object MUST STRICTLY be: " +
"{\"chartType\": \"...\", \"title\": \"Chart Title\", \"labels\": [\"Label 1\"], \"data\": [number1]}."
+
"CRITICAL RULES:\n" +
"1. The 'data' array MUST contain ONLY NUMBERS. Do not include text, units, or placeholders like 'trillion' or 'placeholder'. Convert all values to simple numbers (e.g., '1.08 trillion' becomes 1.08).\n"
"\n\nCRITICAL RULES:\n" +
"1. For 'chartType', use 'pie' if the data represents parts of a whole (like percentages or market shares). In all other cases, use 'bar'.\n"
+
"2. If a numerical value is not available for a label, you MUST use the number 0 as a placeholder.\n" +
"3. DO NOT omit any keys.\n\n" +
"2. The 'data' array MUST contain ONLY NUMBERS. Do not include text, units, or placeholders. Convert all values to simple numbers.\n"
+
"3. If a numerical value is not available for a label, you MUST use the number 0 as a placeholder.\n" +
"4. DO NOT omit any keys.\n\n" +
"Example of a correct response:\n" +
"[{\"chartType\":\"bar\",\"title\":\"Growth by Year\",\"labels\":[\"2023\",\"2024\"],\"data\":[100,150]}]"
"[{\"chartType\":\"pie\",\"title\":\"Market Share\",\"labels\":[\"Company A\",\"Company B\"],\"data\":[60,40]}]"
+
"\n\n" +
"If no suitable data is found, return an empty array [].\n\n" +
@@ -202,19 +202,31 @@ public class ResearchPdfService {
if (chartImages == null || chartImages.isEmpty()) {
return;
}
document.newPage();
Paragraph title = new Paragraph("Визуализация данных", headingFont);
Chapter chapter = new Chapter(title, 3);
chapter.setNumberDepth(0);
for (byte[] imageData : chartImages) {
try {
Image chartImage = Image.getInstance(imageData);
float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin())
/ chartImage.getWidth()) * 80;
chartImage.scalePercent(scaler);
// --- УЛУЧШЕННАЯ ЛОГИКА МАСШТАБИРОВАНИЯ ---
float availableWidth = document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin();
float availableHeight = document.getPageSize().getHeight() - document.topMargin()
- document.bottomMargin() - 50; // -50 для доп. отступа
// Масштабируем пропорционально, чтобы вписать и по ширине, и по высоте
chartImage.scaleToFit(availableWidth, availableHeight);
// --- КОНЕЦ УЛУЧШЕНОЙ ЛОГИКИ ---
chartImage.setAlignment(Element.ALIGN_CENTER);
chapter.add(new Paragraph(" "));
chapter.add(new Paragraph(" ")); // Отступ
chapter.add(chartImage);
} catch (Exception e) {
System.err.println("Не удалось добавить изображение диаграммы в PDF: " + e.getMessage());
}