.
This commit is contained in:
@@ -154,20 +154,24 @@ public class OpenAiChartService {
|
|||||||
// learningsAsString + "\n---";
|
// learningsAsString + "\n---";
|
||||||
// }
|
// }
|
||||||
private String buildChartDataPrompt(String learningsAsString) {
|
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: " +
|
" Return ONLY a JSON array of these objects." +
|
||||||
"{\"chartType\": \"bar\" or \"pie\", \"title\": \"Chart Title\", \"labels\": [\"Label 1\"], \"data\": [number1, number2]}. "
|
|
||||||
|
" The structure of each object MUST STRICTLY be: " +
|
||||||
|
"{\"chartType\": \"...\", \"title\": \"Chart Title\", \"labels\": [\"Label 1\"], \"data\": [number1]}."
|
||||||
+
|
+
|
||||||
|
|
||||||
"CRITICAL RULES:\n" +
|
"\n\nCRITICAL 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"
|
"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" +
|
"2. The 'data' array MUST contain ONLY NUMBERS. Do not include text, units, or placeholders. Convert all values to simple numbers.\n"
|
||||||
"3. DO NOT omit any keys.\n\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" +
|
"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" +
|
"\n\n" +
|
||||||
"If no suitable data is found, return an empty array [].\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()) {
|
if (chartImages == null || chartImages.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.newPage();
|
document.newPage();
|
||||||
Paragraph title = new Paragraph("Визуализация данных", headingFont);
|
Paragraph title = new Paragraph("Визуализация данных", headingFont);
|
||||||
Chapter chapter = new Chapter(title, 3);
|
Chapter chapter = new Chapter(title, 3);
|
||||||
chapter.setNumberDepth(0);
|
chapter.setNumberDepth(0);
|
||||||
|
|
||||||
for (byte[] imageData : chartImages) {
|
for (byte[] imageData : chartImages) {
|
||||||
try {
|
try {
|
||||||
Image chartImage = Image.getInstance(imageData);
|
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);
|
chartImage.setAlignment(Element.ALIGN_CENTER);
|
||||||
chapter.add(new Paragraph(" "));
|
|
||||||
|
chapter.add(new Paragraph(" ")); // Отступ
|
||||||
chapter.add(chartImage);
|
chapter.add(chartImage);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Не удалось добавить изображение диаграммы в PDF: " + e.getMessage());
|
System.err.println("Не удалось добавить изображение диаграммы в PDF: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user