package com.ots.project.tool;
|
import com.ots.common.enums.LangTypeEnum;
|
import com.ots.framework.config.EssConfig;
|
import com.ots.project.exam.domain.TLibraryCode;
|
import com.ots.project.tool.report.MAQ.base.BaseCondition;
|
import lombok.extern.slf4j.Slf4j;
|
import org.jfree.chart.ChartFactory;
|
import org.jfree.chart.ChartUtils;
|
import org.jfree.chart.JFreeChart;
|
import org.jfree.chart.axis.CategoryAxis;
|
import org.jfree.chart.axis.ValueAxis;
|
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
|
import org.jfree.chart.plot.CategoryPlot;
|
import org.jfree.chart.plot.PlotOrientation;
|
import org.jfree.chart.renderer.category.BarRenderer;
|
import org.jfree.data.category.CategoryDataset;
|
import org.jfree.data.category.DefaultCategoryDataset;
|
import org.springframework.stereotype.Component;
|
import java.awt.*;
|
import java.io.FileOutputStream;
|
import java.util.Map;
|
import java.util.UUID;
|
|
@Component
|
@Slf4j
|
public class BarChart extends BaseCondition {
|
String percentile_score = "Percentile Score";
|
String rowKey1 = "Task/Results Orientation";
|
String rowKey2 = "People Orientation";
|
JFreeChart chart;
|
private BarChart() {
|
}
|
|
public String generateHistogram(Map<String, String> map, LangTypeEnum langTypeEnum) {
|
TLibraryCode library1 = getMaqReportLibrary(map, "LIBMAQ31003", langTypeEnum);
|
TLibraryCode library2 = getMaqReportLibrary(map, "LIBMAQ31005", langTypeEnum);
|
TLibraryCode library3 = getMaqReportLibrary(map, "LIBMAQ32005", langTypeEnum);
|
percentile_score = library1.getLangTypeContext(langTypeEnum);
|
rowKey1 = library2.getLangTypeContext(langTypeEnum);
|
rowKey2 = library3.getLangTypeContext(langTypeEnum);
|
DefaultCategoryDataset data = getDefaultCategoryDataset(map);
|
chart = getjFreeChart(data);
|
FileOutputStream fos_jpg = null;
|
String CHART_PATH = EssConfig.getUploadPath();
|
String chartName = CHART_PATH + "/" + UUID.randomUUID() + ".png";
|
try {
|
fos_jpg = new FileOutputStream(chartName);
|
ChartUtils.writeChartAsPNG(fos_jpg, chart, 1000, 200, true, 10);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
fos_jpg.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return chartName;
|
}
|
private JFreeChart getjFreeChart(DefaultCategoryDataset data) {
|
JFreeChart chart = ChartFactory.createBarChart(
|
"",
|
"",
|
percentile_score,
|
data,
|
PlotOrientation.HORIZONTAL,
|
false,
|
false,
|
false);
|
|
|
CategoryPlot plot = chart.getCategoryPlot();
|
|
|
|
CategoryAxis domain = plot.getDomainAxis();
|
|
domain.setTickLabelFont(new Font("黑体", Font.BOLD, 16));
|
|
domain.setLabelFont(new Font("黑体", Font.BOLD, 20));
|
|
ValueAxis rangeAxis = plot.getRangeAxis();
|
rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 16));
|
|
|
chart.getTitle().setFont(new Font("黑体", Font.BOLD, 16));
|
BarRenderer renderer = new BarRenderer();
|
|
|
|
|
|
renderer.setDefaultOutlinePaint(Color.BLACK);
|
|
renderer.setDrawBarOutline(true);
|
|
renderer.setSeriesPaint(0, new Color(132, 140, 221));
|
renderer.setSeriesPaint(1, new Color(45, 193, 112));
|
|
renderer.setItemMargin(0.0);
|
|
renderer.setIncludeBaseInRange(true);
|
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
|
renderer.setDefaultItemLabelsVisible(true);
|
plot.setRenderer(renderer);
|
|
plot.setForegroundAlpha(1.0f);
|
return chart;
|
}
|
private DefaultCategoryDataset getDefaultCategoryDataset(Map<String, String> map) {
|
int taskResultsOrientation;
|
int peopleOrientation;
|
try {
|
taskResultsOrientation = Integer.valueOf(map.get("P_Task31"));
|
peopleOrientation = Integer.valueOf(map.get("People32"));
|
} catch (Exception ex) {
|
taskResultsOrientation = 0;
|
peopleOrientation = 0;
|
log.error("柱状图参数为空,{}" + ex.getMessage(), ex);
|
}
|
return (DefaultCategoryDataset) getDataSet(taskResultsOrientation, peopleOrientation);
|
}
|
|
public CategoryDataset getDataSet(int taskResultsOrientation, int peopleOrientation) {
|
DefaultCategoryDataset data = new DefaultCategoryDataset();
|
|
data.setValue(0, rowKey1, rowKey1);
|
data.setValue(taskResultsOrientation, rowKey2, rowKey1);
|
data.setValue(peopleOrientation, rowKey1, rowKey2);
|
data.setValue(0, rowKey2, rowKey2);
|
return data;
|
}
|
@Override
|
public String getName() {
|
return null;
|
}
|
@Override
|
public TLibraryCode calculate(Map<String, String> map, LangTypeEnum langType) {
|
return null;
|
}
|
@Override
|
public Map<String, String> getMAQwaterDropsImages() {
|
return null;
|
}
|
}
|