jfreechart barchart 显示设置

/**
*	JFreeChart 柱状图显示设置
	(1)当只有少数几个柱子的时候也可以显示的宽度适中
	(2)设置每个柱子的颜色,柱子顶端显示数据
	(3)消除柱状图输出的图片的字体的锯齿
	(4)显示100%这条突出的线
*
**/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.RenderingHints;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import org.apache.log4j.Logger;
import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
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.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import com.statestr.eta.server.bean.MainReportBean;
public class ExportBar
{
private static Logger logger = Logger.getLogger(ExportBar.class);
private List<MainReportBean> mainReportBeanInfoList;
private DecimalFormat numberformat1 = new DecimalFormat("###%");
private DecimalFormat numberformat2 = new DecimalFormat("###%");
private String fontPath ;
public ExportBar(List<MainReportBean> mainReportBeanInfoList,String path)
{
this.mainReportBeanInfoList = mainReportBeanInfoList;
this.fontPath = path ;
}
// wrap data to DefaultCategoryDataset
public DefaultCategoryDataset copeWithData()
{
DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
int rowLength = mainReportBeanInfoList.size();
for (int i = 0; i < rowLength; i++)
{
MainReportBean mb = mainReportBeanInfoList.get(i);
categoryDataset.setValue(mb.getEarnedValue() / 100, "Earned Value", mb.getProjectName()); // value
// E,A,B
// proName
categoryDataset.setValue(mb.getActualCost(), "Actual Cost", mb.getProjectName());
categoryDataset.setValue(mb.getBudgetedCost(), "Budgeted Cost", mb.getProjectName());
}
return categoryDataset;
}
public void configFont(JFreeChart chart, Double maxData)
{
//chart.setBorderVisible(false) ;
chart.setBorderPaint(ChartColor.RED) ;
CategoryPlot plot = chart.getCategoryPlot();



BarRenderer render = (BarRenderer) plot.getRenderer();

plot.setBackgroundPaint(ChartColor.WHITE) ;
plot.setRangeGridlinePaint(new Color(167,192,222)) ;
//set 100% rangeAxis line
plot.setRangeCrosshairValue(1d) ;
plot.setRangeCrosshairPaint(ChartColor.RED) ;
plot.setRangeCrosshairVisible(true) ;

//create font
Font Titlefont = null ;// new Font("Tahoma",Font.BOLD,13) ;
Font plainFont = null ;// new Font("Tahoma",Font.PLAIN,12) ;
try
{
//create chart title font
FileInputStream fileInTitle = new FileInputStream(fontPath+"\\CALIBRIB.TTF") ;
Font fontTitle = Font.createFont(Font.TRUETYPE_FONT, fileInTitle) ;
Titlefont = fontTitle.deriveFont(16f) ;

//create chart plain font
FileInputStream fileInPlain = new FileInputStream(fontPath+"\\CALIBRI.TTF") ;
Font fontPlain = Font.createFont(Font.TRUETYPE_FONT, fileInPlain) ;
plainFont = fontPlain.deriveFont(12f) ;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (FontFormatException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
//set title style
TextTitle textTitle = chart.getTitle() ;
textTitle.setFont(Titlefont) ; //
textTitle.setMargin(10, 0, 10, 0) ;
//set legend style
LegendTitle legend = chart.getLegend();
// legend.setPosition(RectangleEdge.RIGHT) ;
legend.setBorder(0, 0, 0, 0);
legend.setItemFont(plainFont); //new Font("Calibri", Font.PLAIN, 12)
legend.setMargin(10, 0, 10, 0) ;
//legend.setPosition(RectangleEdge.RIGHT);


//set CategoryAxis style
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelPaint(Color.BLACK);
domainAxis.setTickLabelFont(plainFont) ; //sans-serif
domainAxis.setMaximumCategoryLabelWidthRatio(1.5f) ;

//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
domainAxis.setCategoryMargin(0.15) ;
domainAxis.setTickMarkPaint(Color.red);
domainAxis.setTickMarkInsideLength((float) 0.1) ;
domainAxis.setTickMarkOutsideLength((float)0.2) ;
domainAxis.setTickMarksVisible(true) ;
int length = mainReportBeanInfoList.size() ;
if(length ==1){
domainAxis.setLowerMargin(0.26) ;
domainAxis.setUpperMargin(0.56) ;
}else if(length<6){
double margin = (1.0-length*0.08)/3 ;
domainAxis.setLowerMargin(margin) ;
domainAxis.setUpperMargin(margin) ;
}
//domainAxis.setTickLabelsVisible(true) ;
//domainAxis.setTickLabelPaint(ChartColor.BLUE) ;
// bar margin
render.setItemMargin(0.0);
render.setSeriesPaint(0, new Color(0, 176, 80));
render.setSeriesPaint(1, new Color(255, 192, 0));
render.setSeriesPaint(2, new Color(85, 142, 213));
// set bar column show number
render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", numberformat2));// format
render.setBaseItemLabelsVisible(true);
render.setBaseItemLabelFont(plainFont);
// render.setBasePositiveItemLabelPosition(new
// ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BOTTOM_LEFT,TextAnchor.HALF_ASCENT_LEFT,-0.57D))
// ;
render.setMaximumBarWidth(0.2);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setNumberFormatOverride(numberformat1);
rangeAxis.setTickLabelFont(plainFont) ;
rangeAxis.setTickMarksVisible(false) ;
if (maxData >= 1)
{
rangeAxis.setRange(0, maxData * (1 + 0.1));
}
else
{
rangeAxis.setRange(0, 1);
}
//rangeAxis.setUpperMargin(0.1);
}
// create jfreechart
public JFreeChart create_bar_chart(String title)
{
JFreeChart barChart = ChartFactory.createBarChart(title, null, null, copeWithData(), PlotOrientation.VERTICAL, true, true,
false);
double maxData = getMaxData(mainReportBeanInfoList);
configFont(barChart, maxData);
barChart.setBackgroundPaint(ChartColor.WHITE) ;

barChart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
//barChart.setAntiAlias(true) ;
barChart.setTextAntiAlias(true) ;
return barChart;
}
// get max data from list mainReportBeanInfoList
public double getMaxData(List<MainReportBean> mainReportBeanInfos)
{
double maxActual = 0;
double maxBudge = 0;
double maxEarned = 0;
for (int i = 0, size = mainReportBeanInfos.size(); i < size; i++)
{
maxActual = Math.max(maxActual, mainReportBeanInfos.get(i).getActualCost());
maxBudge = Math.max(maxBudge, mainReportBeanInfos.get(i).getBudgetedCost());
maxEarned = Math.max(maxEarned, (mainReportBeanInfos.get(i).getEarnedValue() / 100));
}
double temp = Math.max(maxEarned, maxBudge);
logger.info("get max data from list is:" + Math.max(temp, maxActual));
return (Math.max(temp, maxActual));
}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值