1. 程式人生 > 其它 >JFreeChart 中各屬性值設定

JFreeChart 中各屬性值設定

package com.gllic.workweixin.utils;

import com.gllic.workweixin.mqdto.Renderer;
import com.sun.javafx.charts.Legend;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarPainter;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;

import java.awt.*;
import java.io.*;
import java.util.UUID;

/**
* @program: workweixin-mobile-api
* @ClassName JFreeChartUtil
* @description:
* @author: Marlo
* @create: 2021-06-23 17:23
* @Version 1.0
**/
public class JFreeChartUtil {
private static final String CHART_PATH = "C:/myselefzxq/image/img/";


/**
* 生成柱狀圖
*/
public static String makeBarChart1() {
double[][] data = new double[][]{{5.00, 4.67, 3.65, 3.11, 2.87, 2.58, 2.58, 2.1, 1.89, 1.65}};
String[] rowKeys = {"當日預收標保:萬元"};
String[] columnKeys = {"無錫濱湖-董瑞芳", "無錫新城-趙彩霞", "無錫濱湖-張邦倫", "無錫濱湖-李馨", "無錫濱湖-李鶯霞", "鹽城-丁立桂", "徐州-鄭某", "無錫濱湖-朱玉珍", "泰州-王巧鳳", "無錫城區-秦志芳"};
//初始化柱體顏色
String[] colorValues = {"#FE5F63", "#4AD07C", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54"};
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
String imageName = createBarChart(dataset, "", "", "個人業績十強排名(2021/6/23)", colorValues);
return imageName;
}
public static String makeBarChart2() {
double[][] data = new double[][]{{9.52, 5.00, 3.21, 3.00, 2.78, 2.60, 2.41, 2.20, 1.89, 1.62}};
String[] rowKeys = {"當日預收標保:萬元"};
String[] columnKeys = {"無錫濱湖-董瑞芳", "無錫新城-趙彩霞", "無錫濱湖-張邦倫", "無錫濱湖-李馨", "無錫濱湖-李鶯霞", "鹽城-丁立桂", "徐州-鄭某", "無錫濱湖-朱玉珍", "泰州-王巧鳳", "無錫城區-秦志芳"};
//初始化柱體顏色
String[] colorValues = {"#2197FF", "#7CCC48", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA"};
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
String imageName = createBarChart(dataset, "", "", "營業組十強排名(2021/6/23)", colorValues);
return imageName;
}

/**
* 柱狀圖
*
* @param dataset 資料集
* @param xName x軸的說明(如種類,時間等)
* @param yName y軸的說明(如速度,時間等)
* @param chartTitle 圖示題
* @return
*/
public static String createBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String[] colorValues) {
String chartName = UUID.randomUUID().toString();
JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 圖表標題
xName, // 目錄軸的顯示標籤
yName, // 數值軸的顯示標籤
dataset, // 資料集
PlotOrientation.HORIZONTAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對於簡單的柱狀圖必須是false),(true時,x軸legend下柱子的標題)
false, // 是否生成工具
false // 是否生成URL連結
);
//y軸資料樣式
Font yfont = new Font("宋體", Font.PLAIN, 24);
//圖例資料樣式
Font legendFont = new Font("宋體", Font.PLAIN, 24);
//Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
Font labelFont = new Font("宋體", Font.BOLD, 28);
//設定整個圖片的標題字型
chart.getTitle().setFont(labelFont);
//設定x軸上代表柱子的標題,與上面legend:true 配合使用
chart.getLegend().setItemFont(legendFont);
//設定圖例邊框顏色
chart.getLegend().setFrame(new BlockBorder(Color.white));
/*
* VALUE_TEXT_ANTIALIAS_OFF表示將文字的抗鋸齒關閉,
* 使用的關閉抗鋸齒後,字型儘量選擇12到14號的宋體字,這樣文字最清晰好看
*/
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setTextAntiAlias(false);
//設定邊框顏色
chart.setBackgroundPaint(Color.white);
// create plot
CategoryPlot plot = chart.getCategoryPlot();
plot.setOutlinePaint(null);
// 設定橫虛線可見
plot.setRangeGridlinesVisible(false);
// 虛線色彩
plot.setRangeGridlinePaint(Color.gray);

// 資料軸精度
/* NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0.00");
vn.setNumberFormatOverride(df); // 資料軸資料標籤的顯示格式*/
// y軸設定
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);// 軸標題
domainAxis.setTickLabelFont(labelFont);// 軸數值
//設定Y軸資料樣式
domainAxis.setTickLabelFont(yfont);
// Lable(Math.PI/3.0)度傾斜
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions
// .createUpRotationLabelPositions(Math.PI / 3.0));

domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 橫軸上的 Lable 是否完整顯示

// 設定距離圖片上端距離
domainAxis.setLowerMargin(0.01);
// 設定距離圖片下端距離
domainAxis.setUpperMargin(0.01);
// 設定 columnKey 是否間隔顯示
// domainAxis.setSkipCategoryLabelsToFit(true);

plot.setDomainAxis(domainAxis);
// 設定柱圖背景色(注意,系統取色的時候要使用16位的模式來檢視顏色編碼,這樣比較準確)
//plot.setBackgroundPaint(new Color(255, 255, 204));
plot.setBackgroundPaint(Color.white);

// x軸設定
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// 設定最高的一個 Item 與圖片頂端的距離
rangeAxis.setUpperMargin(0.15);
// 設定最低的一個 Item 與圖片底端的距離
rangeAxis.setLowerMargin(0.15);
//隱藏X軸
rangeAxis.setVisible(false);

plot.setRangeAxis(rangeAxis);

BarRenderer renderer = new Renderer(colorValues);
// 設定柱子寬度
renderer.setMaximumBarWidth(0.05);
// 設定柱子高度
renderer.setMinimumBarLength(0.2);
// 設定柱子邊框顏色
renderer.setBaseOutlinePaint(Color.BLACK);
// 設定柱子邊框可見
renderer.setDrawBarOutline(false);

// // 設定柱的顏色
renderer.setSeriesPaint(0, new Color(255, 255, 255));

// 設定每個地區所包含的平行柱的之間距離
renderer.setItemMargin(0.0);

// 顯示每個柱的數值,並修改該數值的字型屬性
renderer.setIncludeBaseInRange(true);

renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_RIGHT));
//去除柱體陰影效果
renderer.setShadowVisible(false);
//設定柱體的顏色為純色
renderer.setBarPainter(new StandardBarPainter());
renderer.setItemLabelAnchorOffset(-5D);

plot.setRenderer(renderer);
// 設定柱的透明度
plot.setForegroundAlpha(1.0f);

FileOutputStream fos_jpg = null;
try {
isChartPathExist(CHART_PATH);
String chartPath = CHART_PATH + chartName + ".png";
fos_jpg = new FileOutputStream(chartPath);
//ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,chart,600, 1200,null);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 600, 1000, true, 10);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 判斷資料夾是否存在,如果不存在則新建
*
* @param chartPath
*/
private static void isChartPathExist(String chartPath) {
File file = new File(chartPath);
if (!file.exists()) {
file.mkdirs();
// log.info("CHART_PATH="+CHART_PATH+"create.");
}
}

// 柱狀圖,折線圖 資料集
public static CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys) {
return DatasetUtilities
.createCategoryDataset(rowKeys, columnKeys, data);

}
}