public class FinancePlot extends Plot implements Cloneable, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/** The default grid line stroke. */
public static final Stroke DEFAULT_GRIDLINE_STROKE = new BasicStroke(0.5f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.0f, new float[]
{2.0f, 2.0f}, 0.0f);
/** The default grid line paint. */
private Color DEFAULT_GRIDLINE_PAINT = Color.lightGray;
private Color DEFAULT_GREENLINE_PAINT = new Color(213,249,254);
/** The default value label font. */
private Font DEFAULT_VALUE_LABEL_FONT = new Font("SansSerif", Font.PLAIN, 18);
private Color DEFAULT_VALUE_LABEL_COLOR = Color.BLACK;
/** The default font for titles. */
public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);
public static final int DEFAULT_MARGIN = 5;
/**每根柱子的高度*/
private static final int DEFAULT_BAR_HEIGHT = 20;
/**每根柱子的寬度*/
private static final int DEFAULT_BAR_WIDTH = 320;
/**標籤寬度*/
private static final int DEFAULT_LABEL_WIDTH = 100;
/**標籤高度*/
private static final int DEFAULT_LABEL_HEIGHT = 70;
/**圖型數據集合*/
private List<Finance> dataset;
/**地區*/
private String district_name;
/**柱子標籤標題*/
private String labelTitle = null;
/**被除數標題*/
private String dividendTitle = null;
/**除數標題*/
private String divisorTitle = null;
/**
* 財務報表圖
* @param dataset 當前財務數據
* @param title1 柱子標籤標題
* @param title2 被除數標題
* @param title3 除數標題
*/
public FinancePlot(List<Finance> dataset,String title1,String title2,String title3){
setDataset(dataset);
setDistrict_name(district_name);
setLabelTitle(title1);
setDividendTitle(title2);
setDivisorTitle(title3);
}
public void draw(Graphics2D g2, Rectangle2D area,
Point2D anchor,
PlotState parentState,
PlotRenderingInfo state) {
// don't let anyone draw outside the data area
Shape savedClip = g2.getClip();
g2.setStroke(new BasicStroke(1.0f));
g2.clip(area);
g2.setFont(DEFAULT_VALUE_LABEL_FONT);
//畫白色背景
g2.fillRect((int)area.getX(), (int)area.getY(), (int)area.getWidth(), (int)area.getHeight());
//畫圖例
int x = getDEFAULT_MARGIN();
int y = getDEFAULT_MARGIN();
if(getLegendColor()!=null){
createLegend(g2);
x = getDEFAULT_MARGIN();
y = 45;
}
//畫三個標題{指標,指標排名,企業總數}
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
if(getLabelTitle()!=null){
g2.drawString(getLabelTitle(), x, y);
}
if(getDividendTitle()!=null){
x = 100+(340-calcStringWidth(g2,DEFAULT_VALUE_LABEL_FONT,getDividendTitle()))/2;
g2.drawString(getDividendTitle(), x, y);
}
if(getDivisorTitle()!=null){
x = 520+((50-calcStringWidth(g2,DEFAULT_VALUE_LABEL_FONT,getDivisorTitle()))/2);
g2.drawString(getDivisorTitle(), x, y);
}
//畫指標排名矩形框
x = 70;
y = y + 30;
// g2.setColor(DEFAULT_GREENLINE_PAINT);
// g2.drawString("", x, 620);
//畫指企業總數矩形框
x = 625;
// g2.setColor(DEFAULT_GREENLINE_PAINT);
// g2.drawString("", x, 85);
//畫後四項的標尺刻度
for(int i = 1; i < dataset.size(); i++){
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
//畫標尺的橫線
g2.drawLine(70, (y+95*i)+10, 400, (y+95*i)+10);
//畫標尺的刻度線,小豎線高度5
g2.drawLine(70, (y+95*i)+10, 70, (y+95*i)+5);
g2.drawString("100%", 70, (y+95*i));
for(int n = 2; n <= 4; n++){
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
g2.drawString((100-25*(n-1))+"%", 95+80*(n-1), (y+95*i));
g2.drawLine(95+80*(n-1), (y+95*i)+10, 95+80*(n-1), (y+95*i)+5);
}
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
g2.drawString("0%", 400, (y+95*i));
g2.drawLine(400, (y+95*i)+10, 400, (y+95*i)+5);
}
//畫柱子標籤、柱子及柱子值
Map<String,Color> legendColor = getLegendColor();
x = 70;
g2.setStroke(new BasicStroke(1));
for(int i = 0;i < dataset.size(); i++){
Finance finance = dataset.get(i);
int j =0;
//循環得到一個小柱形圖
for(String key : legendColor.keySet()){
//標籤
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
TextUtils.drawMultiLineText(finance.getLabel(), g2, this.DEFAULT_VALUE_LABEL_FONT, 65, new Point2D.Double(getDEFAULT_MARGIN(), (y+95*i)+25));
//計算柱形長度
int w = 360;
if(finance.getDivsior(key)!=null && finance.getDivsior(key).intValue()!=0 && finance.getDividend(key)!=null){
w = new BigDecimal(1).subtract(finance.getDividend(key).divide(finance.getDivsior(key),2, RoundingMode.HALF_UP)).multiply(new BigDecimal(320)).intValue();
}
//畫柱子
g2.setColor(Color.WHITE);
g2.drawRect(x, (y+95*i)+20*j+15, w, getDEFAULT_BAR_HEIGHT());
//將矩形填充爲顏色
g2.setColor(finance.getLegend_color().get(key));
g2.fillRect(x, (y+95*i)+20*j+15, w, getDEFAULT_BAR_HEIGHT());
//畫間隔
g2.setColor(Color.WHITE);
g2.drawRect(x, (y+95*i)+20*j+15, w, 2);
//將矩形填充爲顏色
g2.setColor(Color.WHITE);
g2.fillRect(x, (y+95*i)+20*j+15, w, 2);
// //畫柱子值
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
if(finance.getDividend(key)!=null){
g2.drawString(finance.getDividend(key).intValue()+"", x+w+2, (y+95*i)+20*(j+1)+10);
}
//畫柱子除數值
if(finance.getDivsior(key)!=null){
g2.drawString(finance.getDivsior(key).intValue()+"", 530, (y+95*i)+20*(j+1)+10);
}
j++;
}
}
//畫最上面的刻度尺
g2.drawLine(70, y+10, 400, y+10);
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
g2.drawLine(x, y+10, x, y+5);
g2.drawString("100%", x, y-5);
for(int i = 2; i <= 4; i++){
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
g2.drawString((100-25*(i-1))+"%", 70+80*(i-1), y-5);
g2.drawLine(70+80*(i-1), y+10, 70+80*(i-1), y+5);
}
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
g2.drawLine(400, y+10, 400, y+5);
g2.drawString("0%", 400, y-5);
g2.setClip(savedClip);
}
private void createLegend(Graphics2D g2){
int x = getDEFAULT_MARGIN();
int y = getDEFAULT_MARGIN();
Map<String,Color> legendColor = getLegendColor();
for(String key : legendColor.keySet()){
//畫一個矩形
g2.setColor(legendColor.get(key));
g2.drawRect(x, y, 20, 10);
//將矩形填充顏色
g2.fillRect(x, y, 20, 10);
x = x + 20;
//畫圖例標籤
g2.setColor(DEFAULT_VALUE_LABEL_COLOR);
g2.drawString(key, x+10, y+10);
//計算圖例標籤的寬度+圖例間隔30
x = x + calcStringWidth(g2,DEFAULT_VALUE_LABEL_FONT,key) + 30;
}
}
private int calcStringWidth(Graphics2D g2,Font ft,String title){
FontMetrics fm = g2.getFontMetrics(ft);
Rectangle2D textBound = TextUtilities.getTextBounds(title,g2,fm);
return (int)textBound.getWidth();
}
@Override
public String getPlotType() {
return "Finace Plot";
}
public static int getDEFAULT_BAR_HEIGHT() {
return DEFAULT_BAR_HEIGHT;
}
public static int getDEFAULT_BAR_WIDTH() {
return DEFAULT_BAR_WIDTH;
}
public static Stroke getDEFAULT_GRIDLINE_STROKE() {
return DEFAULT_GRIDLINE_STROKE;
}
public static int getDEFAULT_LABEL_HEIGHT() {
return DEFAULT_LABEL_HEIGHT;
}
public static int getDEFAULT_LABEL_WIDTH() {
return DEFAULT_LABEL_WIDTH;
}
public static int getDEFAULT_MARGIN() {
return DEFAULT_MARGIN;
}
public static Font getDEFAULT_TITLE_FONT() {
return DEFAULT_TITLE_FONT;
}
public void setDataset(List<Finance> dataset) {
this.dataset = dataset;
}
public void setDistrict_name(String district_name) {
this.district_name = district_name;
}
public void setDividendTitle(String dividendTitle) {
this.dividendTitle = dividendTitle;
}
public void setDivisorTitle(String divisorTitle) {
this.divisorTitle = divisorTitle;
}
public void setLabelTitle(String labelTitle) {
this.labelTitle = labelTitle;
}
public String getDividendTitle() {
return dividendTitle;
}
public String getDivisorTitle() {
return divisorTitle;
}
public String getLabelTitle() {
return labelTitle;
}
private Map<String,Color> getLegendColor(){
Map<String,Color> legendColor = null;
if(dataset!=null){
legendColor = new LinkedHashMap<String,Color>();
for(int i=0; i<dataset.size(); i++){
Finance finance = dataset.get(i);
if(finance==null || finance.getLegend_color()==null) continue;
legendColor.putAll(finance.getLegend_color());
}
}
return legendColor;
}
public void setDEFAULT_GRIDLINE_PAINT(Color default_gridline_paint) {
DEFAULT_GRIDLINE_PAINT = default_gridline_paint;
}
public void setDEFAULT_VALUE_LABEL_FONT(Font default_value_label_font) {
DEFAULT_VALUE_LABEL_FONT = default_value_label_font;
}
public void setDEFAULT_VALUE_LABEL_COLOR(Color default_value_label_color) {
DEFAULT_VALUE_LABEL_COLOR = default_value_label_color;
}
}
JfreeChar 畫橫向柱狀圖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.