解决JfreeChart柱状图数值显示在柱子顶端的问题

http://topic.csdn.net/u/20080610/16/8b83d303-239d-42fb-99e6-8bdf43c43cfa.html

 

package com.trustel.gd12580.datamanagement.service;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
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.entity.StandardEntityCollection;
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.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
/**
* @author Liucl
*
*/
public class KpiCityCompanyChart {
/**
* 图片长度, 默认为650
*/
public static int IMAGE_WIDTH = 760;

/**
* 图片高度, 默认为350
*/
public static int IMAGE_HEIGHT = 350;

/**
* 设置图片显示大小,默认为450*350
*/
public static void setImageRange(int width, int height) {
IMAGE_WIDTH = width;
IMAGE_HEIGHT = height;
}
/**
* 用于生成柱状图
*
* @param title
*            图形标题
* @param domain
*            横坐标名称
* @param range
*            纵坐标名称
* @param session
*            图片生命周期
* @param pw
*            printWriter
* @param dataset
*            数据源
* @param length
*            分类数
* @return
*/
public static String createBarChartChart(String title, String domain,
String range, HttpSession session, PrintWriter pw,
DefaultCategoryDataset dataset) {
String filename = null;
try {
JFreeChart chart = createChart(title, domain, range, dataset);
// 把生成的图片放到临时目录
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
// 设置图片名称前缀
ServletUtilities.setTempFilePrefix("chart-");
// 生成图片
filename = ServletUtilities.saveChartAsPNG(chart, IMAGE_WIDTH,
IMAGE_HEIGHT, info, session);
// ChartUtilities.writeImageMap(pw, filename, info, false);
pw.flush();
} catch (Exception e) {
e.printStackTrace(System.out);
filename = "error_warning.gif";
}
return filename;
}
/**
* Creates a sample chart.
*
* @param title
*            图形标题
* @param domain
*            横坐标名称
* @param range
*            纵坐标名称
* @param dataset
*            数据源
*
* @return The chart.
*/
private static JFreeChart createChart(String title, String domain,
String range, CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart3D(title, // title
domain, // domain axis label
range, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// set the textTitle of the chart
TextTitle textTitle = chart.getTitle();
//设置标题的字体
String fontA = "华文细黑";
String fontB = "黑体";
textTitle.setFont(new Font(fontA,Font.PLAIN,13));
textTitle.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.decode("#EEF7FF"),
250F, 0.0F, Color.white, true));
textTitle.setExpandToFitSpace(true);
chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.decode("#EEF7FF"),
250F, 0.0F, Color.white, true));

        CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
        BarRenderer3D customBarRenderer = (BarRenderer3D) plot.getRenderer();
        //CustomBarRenderer3D customBarRenderer = new CustomBarRenderer3D();
        //BarRenderer3D customBarRenderer = new BarRenderer3D();

plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.black);
plot.setBackgroundPaint(Color.decode("#F9E9D2"));
// 设置是否有横线
plot.setRangeGridlinesVisible(false);
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//设置纵坐标名称的字体
numberaxis.setLabelFont(new Font(fontA,Font.PLAIN,16));
//设置纵坐标上显示的数字字体
numberaxis.setTickLabelFont(new Font("Fixedsys",Font.PLAIN,13));
//设置横坐标名称的字体
CategoryAxis categoryaxis = plot.getDomainAxis();
categoryaxis.setLabelFont(new Font(fontA,Font.PLAIN,16));
//设置横坐标上显示各个业务子项的字体
categoryaxis.setTickLabelFont(new Font(fontA,Font.PLAIN,12));
categoryaxis.setMaximumCategoryLabelLines(100);
categoryaxis.setMaximumCategoryLabelWidthRatio(100);
//横坐标数据倾斜45度
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
// 顶端设置
numberaxis.setUpperMargin(0.14999999999999999D);
// 设置颜色
Paint apaint[] = createPaint();

customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值
customBarRenderer.setBaseItemLabelsVisible(true);
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
customBarRenderer.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字偏离值
customBarRenderer.setItemLabelsVisible(true);

//设定柱子上面的颜色
customBarRenderer.setSeriesPaint(0, Color.decode("#24F4DB")); // 给series1 Bar
customBarRenderer.setSeriesPaint(1, Color.decode("#7979FF")); // 给series2 Bar
customBarRenderer.setSeriesPaint(2, Color.decode("#FF5555")); // 给series3 Bar
customBarRenderer.setSeriesPaint(3, Color.decode("#F8D661")); // 给series4 Bar
customBarRenderer.setSeriesPaint(4, Color.decode("#F284DC")); // 给series5 Bar
customBarRenderer.setSeriesPaint(5, Color.decode("#00BF00")); // 给series6 Bar
customBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);//边框为黑色
customBarRenderer.setSeriesOutlinePaint(1,Color.BLACK);//边框为黑色
customBarRenderer.setSeriesOutlinePaint(2,Color.BLACK); //边框为黑色
customBarRenderer.setSeriesOutlinePaint(3,Color.BLACK);//边框为黑色
customBarRenderer.setSeriesOutlinePaint(4,Color.BLACK);//边框为黑色
customBarRenderer.setSeriesOutlinePaint(5,Color.BLACK); //边框为黑色
//设置柱子的最大宽度
customBarRenderer.setMaximumBarWidth(0.04);
customBarRenderer.setItemMargin(0.000000005);
plot.setRenderer(customBarRenderer);
return chart;
}
static class CustomBarRenderer3D extends BarRenderer3D {

public Paint getItemPaint(int i, int j) {
return colors[j % colors.length];
}

private Paint colors[];

public CustomBarRenderer3D(Paint apaint[]) {
colors = apaint;
}

public CustomBarRenderer3D() {
// TODO Auto-generated constructor stub
}
}
private static Paint[] createPaint() {
Paint apaint[] = new Paint[5];
apaint[0] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F,
Color.decode("#FF2020"));
apaint[1] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F,
Color.decode("#FF2020"));
apaint[2] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F,
Color.decode("#FF2020"));
apaint[3] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F,
Color.decode("#FF2020"));
apaint[4] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F,
Color.decode("#FF2020"));
return apaint;
}
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值