import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.RectangleEdge;
import com.growatt.oss.system.StaticParamUtils;
import aj.org.objectweb.asm.Type;
public class JFreeChartUtils {
public static String createAnnularChar(PieDataset dataset,
String chartTitle, String[] pieKeys, String charName,int width,int height){
JFreeChart chart = ChartFactory.createRingChart(chartTitle,dataset,true,true, false);
chart.setBorderVisible(false);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.white);
RingPlot ringplot=(RingPlot) chart.getPlot();
ringplot.setOutlineVisible(false);
ringplot.setBackgroundAlpha(0.8f);
ringplot.setNoDataMessagePaint(Color.red);
ringplot.setLabelFont(new Font("宋体", Font.BOLD, 15));
ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));
ringplot.setBackgroundPaint(Color.white);
ringplot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
ChartFrame mChartFrame = new ChartFrame("环形图", chart);
mChartFrame.pack();
LegendTitle legendTitle = chart.getLegend();
legendTitle.setPosition(RectangleEdge.RIGHT);
legendTitle.setBorder(0, 0, 0, 0);
legendTitle.setPadding(0, 0, 0, 50);
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
if (charName != null) {
Font font2 = new Font("宋体", Font.BOLD, 15);
TextTitle title2 = new TextTitle(charName);
title2.setFont(font2);
chart.addSubtitle(title2);
}
FileOutputStream fos_jpg = null;
try{
String name = System.currentTimeMillis()+""+Math.round((Math.random()+1) * 1000)+".png";
String chartName = StaticParamUtils.COMMONFILEPATH + name;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, width, height);
return chartName;
}
catch (Exception e){
e.printStackTrace();
return null;
}
finally{
try{
fos_jpg.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
public static String createWireChar(int num,String type,String chartTitle, String x, String y,
CategoryDataset dataset,int width,int height){
JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,
dataset, PlotOrientation.VERTICAL, false, true, false);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(false);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.setOutlinePaint(Color.WHITE);
ValueAxis valueAxis = categoryplot.getRangeAxis();
valueAxis.setAutoRange(true);
valueAxis.setAxisLineVisible(false);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
lineandshaperenderer.setSeriesPaint(0,Color.blue);
if (type!=null) {
lineandshaperenderer.setBaseShapesVisible(true);
lineandshaperenderer.setBaseLinesVisible(true);
lineandshaperenderer.setBaseItemLabelsVisible(true);
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}%",NumberFormat.getNumberInstance()));
lineandshaperenderer.setUseFillPaint(true);
lineandshaperenderer.setBaseFillPaint(Color.blue);
}
Font labelFont = new Font("宋体",Font.BOLD, 12);
CategoryAxis domainAxis = categoryplot.getDomainAxis();
domainAxis.setLabelFont(labelFont);
domainAxis.setTickLabelFont(labelFont);
if (num == 0) {
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
}else if (num == 1) {
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
}else{
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
}
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
FileOutputStream fos_jpg = null;
try{
String name = System.currentTimeMillis()+""+Math.round((Math.random()+1) * 1000)+".png";
String chartName = StaticParamUtils.COMMONFILEPATH + name;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart,width, height );
return chartName;
}
catch (Exception e){
e.printStackTrace();
return null;
}
finally{
try{
fos_jpg.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static String createPillarChart(int num,String type,CategoryDataset dataset, String x,String y,
String chartTitle,int width,int height){
JFreeChart chart = ChartFactory.createBarChart(chartTitle,
x,
y,
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.gray);
plot.setOutlinePaint(Color.WHITE);
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
vn.setTickUnit(new NumberTickUnit(20));
DecimalFormat df = new DecimalFormat("#0.00");
vn.setNumberFormatOverride(df);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);
domainAxis.setTickLabelFont(labelFont);
if (num == 0) {
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
}else if (num == 1) {
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
}else{
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
}
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);
domainAxis.setLowerMargin(0.1);
domainAxis.setUpperMargin(0.1);
plot.setDomainAxis(domainAxis);
plot.setBackgroundPaint(new Color(255, 255, 255));
BarRenderer renderer = new BarRenderer();
renderer.setSeriesPaint(0, new Color(100 ,149 ,237));
renderer.setMaximumBarWidth(0.03);
renderer.setMinimumBarLength(0.2);
renderer.setDrawBarOutline(false);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
rangeAxis.setUpperMargin(0.15);
rangeAxis.setLowerMargin(0.15);
rangeAxis.setAxisLineVisible(false);
rangeAxis.setAutoRange(true);
if (type != null) {
rangeAxis.setVisible(false);
renderer.setIncludeBaseInRange(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setAutoPopulateSeriesFillPaint(true);
renderer.setItemMargin(0.3);
}
plot.setRangeAxis(rangeAxis);
plot.setRenderer(renderer);
plot.setForegroundAlpha(1.0f);
FileOutputStream fos_jpg = null;
try{
String name = System.currentTimeMillis()+""+Math.round((Math.random()+1) * 1000)+".png";
String chartName = StaticParamUtils.COMMONFILEPATH + name;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, width, height, true, 10);
return chartName;
}
catch (Exception e){
e.printStackTrace();
return null;
}
finally{
try{
fos_jpg.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
public static String createPieChart(PieDataset dataset,String chartTitle,int width,int height){
JFreeChart chart = ChartFactory.createPieChart(chartTitle,
dataset,
true,
true, false);
chart.setBackgroundPaint(Color.WHITE);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setNoDataMessage("无对应的数据,请重新查询。");
plot.setNoDataMessagePaint(Color.red);
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
plot.setLabelGenerator(null);
plot.setOutlinePaint(Color.WHITE);
plot.setShadowPaint(Color.WHITE);
LegendTitle legend = chart.getLegend();
legend.setFrame(new BlockBorder(Color.WHITE));
FileOutputStream fos_jpg = null;
try
{
String name = System.currentTimeMillis()+""+Math.round((Math.random()+1) * 1000)+".png";
String chartName = StaticParamUtils.COMMONFILEPATH + name;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart,width, height);
return chartName;
}
catch (Exception e){
e.printStackTrace();
return null;
}
finally{
try{
fos_jpg.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
public static PieDataset getDataAnnularUtil(double[] data,String[] datadescription){
if (data != null && datadescription != null){
if (data.length == datadescription.length){
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < data.length; i++){
dataset.setValue(datadescription[i],data[i]);
}
return dataset;
}
}
return null;
}
public static PieDataset getDataPieSetByUtil(double[] data,
String[] datadescription){
if (data != null && datadescription != null){
if (data.length == datadescription.length){
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < data.length; i++){
dataset.setValue(datadescription[i], data[i]);
}
return dataset;
}
}
return null;
}
public static CategoryDataset getBarData(double[][] data, String[] rowKeys,String[] columnKeys){
return DatasetUtilities
.createCategoryDataset(rowKeys, columnKeys, data);
}
private static void isChartPathExist(String chartPath){
File file = new File(chartPath);
if (!file.exists()){
file.mkdirs();
}
}
}