JFreeChart深入介绍

由于业务数据方面以及表图显示视觉方面考虑,Jfreechart中需要2个或者更多的坐标,在添加坐标后会出现几个问题
1、两个坐标的数据会以前后方式显示,当两个坐标的数据均以柱状图显示时会遮挡,而且可能两个坐标的数据量不一致导致柱状大小不一。
解决方案:分别扩充两坐标的数据集,添加空数据,使得两坐标的数据相同,而空的数据无法遮挡其他数据。

2、当采用1的方案后会出现图例多出好几个的问题
解决方案:重载plot,重新定义getLegendItems。自定义图例。

3、当采用2的方案后,图例只能显示在一个地方,当需求需要把图例分别显示的时候
解决方案:添加多一个plot,重新定义plot跟plot1的getLegendItems。

4、当采用3的方案后,图例的颜色会重复,系统默认均为品红、蓝、绿、blablabla的顺序赋予颜色
解决方案:在getLegendItems中定义图例同时改变其颜色。

以下是参考代码:
public static String cateoryCdataset(int width, int height,String sqlexeproc1,String sqlexeproc2,String categoryname1,String categoryname2,String categoryvalue1,String categoryvalue2,String title,String leftword,String rightword,String buttonword) throws InstantiationException, Exception, Throwable
{
String filename = null;
CategoryDataset dataset1=getcategoryDataSet(sqlexeproc1,categoryname1,categoryname2,categoryvalue1);
CategoryDataset dataset2=getcategoryDataSet(sqlexeproc2,categoryname1,categoryname2,categoryvalue2);
/*int dsc1 = dataset1.getRowCount();
int dsc2 = dataset2.getRowCount();*/
//JFreeChart chart = ChartFactory.createBarChart3D("hello", leftword, buttonword,dataset, PlotOrientation.VERTICAL, true, true, false);
//JFreeChart chart = ChartFactory.createBarChart("hello", buttonword, leftword,dataset1, PlotOrientation.VERTICAL, true, true, false);
//CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxis = new CategoryAxis(buttonword);
NumberAxis rangeAxis = new NumberAxis(leftword);
BarRenderer renderer1 = new BarRenderer();
CategoryPlot plot1 = new CategoryPlot(dataset1,domainAxis,rangeAxis,renderer1){
public LegendItemCollection getLegendItems() {
/*CategoryDataset data = getDataset();
if (data != null) {
CategoryItemRenderer r = getRenderer();
if (r != null) {
for(int x1=0;x1<data.getRowCount();x1++)
if(data.getValue(x1,0)!=null)
{
LegendItem item = r.getLegendItem(0, x1);

}
}
}*/
LegendItemCollection result = new LegendItemCollection();
CategoryDataset data1 = getDataset(1);
if (data1 != null) {
CategoryItemRenderer r2 = getRenderer(1);
if (r2 != null) {
for(int x1=0;x1<data1.getRowCount();x1++)
if(data1.getValue(x1,0)!=null)
{
LegendItem item = r2.getLegendItem(1, x1);
result.add(item);
}
}
}
return result;
}
};

CategoryPlot plot = new CategoryPlot(dataset1, domainAxis, rangeAxis, renderer1) {

public LegendItemCollection getLegendItems() {

LegendItemCollection result = new LegendItemCollection();

CategoryDataset data = getDataset();
if (data != null) {
CategoryItemRenderer r = getRenderer();
if (r != null) {
for(int x1=0;x1<data.getRowCount();x1++)
if(data.getValue(x1,0)!=null)
{
r.setSeriesPaint(x1,new Color(20*x1, 50*x1, 130*x1%255));
LegendItem item = r.getLegendItem(0, x1);
result.add(item);

}
}
}

// the JDK 1.2.2 compiler complained about the name of this
// variable
/*
CategoryDataset data1 = getDataset(1);
if (data1 != null) {
CategoryItemRenderer r2 = getRenderer(1);
if (r2 != null) {
for(int x1=0;x1<data1.getRowCount();x1++)
if(data1.getValue(x1,0)!=null)
{

LegendItem item = r2.getLegendItem(1, x1);

}
}
}*/

return result;

}

};

JFreeChart chart = new JFreeChart(title, plot);

chart.getLegend(0).visible=false;
Font font=new Font("宋体", Font.PLAIN, 12);
chart.setTitle(new TextTitle(title, new Font("宋体", Font.BOLD + Font.ITALIC, 20)));
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(font);
categoryAxis.setTickLabelFont(font);
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setLabelFont(font);
numberaxis.setUpperMargin(0.2);
numberaxis.setTickLabelFont(font);
ValueAxis axis2 = new NumberAxis(rightword);
axis2.setUpperMargin(0.2);
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setDataset(1, dataset2);
plot.mapDatasetToRangeAxis(1, new Integer(1));
plot.setRangeAxis(1, axis2);
plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
plot1.setDataset(1, dataset2);
plot1.mapDatasetToRangeAxis(1, new Integer(1));
BarRenderer renderer2 = new BarRenderer();
plot.setRenderer(1, renderer2);
plot1.setRenderer(1, renderer2);
LegendTitle l2 = new LegendTitle(plot1);
LegendTitle l1 = new LegendTitle(chart.getPlot());
BlockContainer blockcontainer1 = l1.getItemContainer();
BlockContainer blockcontainer2 = l2.getItemContainer();
blockcontainer1.setPadding(2D, 10D, 5D, 2D);
blockcontainer2.setPadding(2D, 10D, 5D, 2D);
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
BlockContainer blockcontainer3 = new BlockContainer(new BorderArrangement());
//blockcontainer.add(blockcontainer1,RectangleEdge.LEFT);
blockcontainer.add(blockcontainer1);
blockcontainer3.add(blockcontainer2);
//l1.setWrapper(blockcontainer);
l1.setPosition(RectangleEdge.LEFT);
l2.setPosition(RectangleEdge.RIGHT);
//l1.setHorizontalAlignment(HorizontalAlignment.LEFT);
//legendtitle.setWrapper(blockcontainer);
//blockcontainer.add(new EmptyBlock(20D, 0.0D));
chart.addSubtitle(l1);
chart.addSubtitle(l2);
CategoryDataset dataset3=setcategoryFDataSet(dataset1,dataset2);
plot.setDataset(0,dataset3);
plot1.setDataset(0,dataset3);
CategoryDataset dataset4=setcategoryBDataSet(dataset1,dataset2);
int s1 = dataset1.getRowCount();
int s2 = plot.getDatasetCount();
//setitemlablevisible(plot,2,4);
//设置图上显示数字
setitemlablevisible(plot,s2,s1);
plot.setDataset(1,dataset4);
plot1.setDataset(1,dataset4);
//chart.getLegend().setItemFont(font);
//chart.getLegend().getItemContainer().clear();

//chart.getLegend().
plot.setNoDataMessage("无返回数据ʾ");
//将Jfreechart转换为String
filename=getChartfilename(chart,width,height);
return filename;
}

JFreeChart深入介绍

同时注意当图例内字太长的时候,如果把图例放下方两边会出现遮挡现象,故将其放在图的左右两边。

当柱状图跟折线图混合时,如果需要折线图不在同一x坐标上显示(即折线起始跟结束的x坐标要区分)时,普通的LineChart不能满足要求,网上资料对这方面提得很有限,翻查1天后才终于在demo的源文件里查出需要用到StatisticalLineAndShapeRenderer

参考代码:

public static String cateoryBdataset(int width,int height,String sqlexeproc1,String sqlexeproc2,String categoryname1,String categoryname2,String categoryvalue1,String categoryname3,String categoryname4,String categoryvalue2,String title,String leftword,String rightword,String buttonword)throws InstantiationException, Exception, Throwable
{

String filename=null;
CategoryDataset dataset1=getcategoryDataSet(sqlexeproc1,categoryname1,categoryname2,categoryvalue1);
CategoryDataset dataset2=getcategoryDataSet(sqlexeproc2,categoryname3,categoryname4,categoryvalue2);
JFreeChart chart = ChartFactory.createBarChart(title, leftword, buttonword, dataset1, PlotOrientation.VERTICAL, false, true, false);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot)chart.getPlot();
Font font=new Font("宋体", Font.PLAIN, 12);
chart.setTitle(new TextTitle(title, new Font("宋体", Font.BOLD + Font.ITALIC, 20)));
//chart.getLegend().setItemFont(font);
CategoryAxis categoryAxis = plot.getDomainAxis();
//x轴的说明字
categoryAxis.setLabelFont(font);

//x轴上的字
categoryAxis.setTickLabelFont(font);
//设置纵坐标字体
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
//y轴的说明字
numberaxis.setLabelFont(font);
numberaxis.setUpperMargin(0.2);
//y轴上的字
numberaxis.setTickLabelFont(font);
plot.setBackgroundPaint(new Color(238, 238, 255));
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setDataset(0, dataset2);
plot.setDataset(1,dataset1);
plot.mapDatasetToRangeAxis(1, 1);

CategoryAxis categoryaxis = plot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
NumberAxis numberaxis1 = new NumberAxis(rightword);
numberaxis1.setUpperMargin(0.2);
plot.setRangeAxis(1, numberaxis1);
//2D效果

CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
CategoryItemRenderer renderer = new BarRenderer();

plot.setRenderer(0, renderer2);
plot.setRenderer(1,renderer);

StatisticalLineAndShapeRenderer localStatisticalLineAndShapeRenderer = new StatisticalLineAndShapeRenderer();
localStatisticalLineAndShapeRenderer.setUseSeriesOffset(true);
plot.setRenderer(localStatisticalLineAndShapeRenderer);

LegendTitle legendtitle = new LegendTitle(plot.getRenderer());
legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
legendtitle.setBorder(1,1, 1, 1);

LegendTitle legendtitle1 = new LegendTitle(plot.getRenderer(1));
legendtitle1.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
legendtitle1.setBorder(1, 1, 1, 1);
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());

blockcontainer.add(legendtitle, RectangleEdge.LEFT);
blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
blockcontainer.add(new EmptyBlock(2000D, 0.0D));
CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
compositetitle.setPosition(RectangleEdge.BOTTOM);
chart.addSubtitle(compositetitle);

//3D效果
//CategoryItemRenderer renderer2 = new LineRenderer3D();
//renderer2.setToolTipGenerator(new StandardCategoryToolTipGenerator());

/*plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
LegendTitle legendtitle = new LegendTitle(plot.getRenderer(0));
legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));*/
int s1 = dataset1.getRowCount();

int s2 = plot.getDatasetCount();
//setitemlablevisible(plot,2,4);
setitemlablevisible(plot,s2,s1);
filename=getChartfilename(chart,width,height);
return filename;
}

JFreeChart深入介绍