JfreemCahrt 画柱状图

public static void main(String[] args) {
CategoryDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D("水果", // 图表标题
"水果种类", // 目录轴的显示标签
"数量", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);

// 从这里开始
CategoryPlot plot = chart.getCategoryPlot();// 获取图表区域对象
CategoryAxis domainAxis = plot.getDomainAxis(); // 水平底部列表
domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 14)); // 水平底部标题
domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12)); // 垂直标题
ValueAxis rangeAxis = plot.getRangeAxis();// 获取柱状
rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));// 设置标题字体

iSetBarChart(chart);

try {
// 创建图形显示面板
ChartFrame cf = new ChartFrame("柱状图", chart);
// // 设置图片大小
cf.setSize(800, 800);
// // 设置图形可见
cf.setVisible(true);


// 保存图片到指定文件夹
// ChartUtilities.saveChartAsPNG(new File(path), chart, width,
// height);
System.err.println("成功");
} catch (Exception e) {
System.err.println("创建图形时出错");
}
}


private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "苹果");
dataset.addValue(100, "上海", "苹果");
dataset.addValue(100, "广州", "苹果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "广州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "广州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "广州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "广州", "荔枝");
return dataset;
}

/**
* 设置柱状图的样式

* @param chart
*/
public static void iSetBarChart(JFreeChart chart) {
CategoryPlot categoryplot = chart.getCategoryPlot();// 图本身
ValueAxis rangeAxis = categoryplot.getRangeAxis();
CategoryAxis domainAxis = categoryplot.getDomainAxis();
// 设置Y轴的提示文字样式
rangeAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
// 设置Y轴刻度线的长度
rangeAxis.setTickMarkInsideLength(10f);


// rangeAxis.setTickMarkOutsideLength(10f);
// 设置X轴下的标签文字
domainAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
// 设置X轴上提示文字样式
domainAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
NumberAxis vn = (NumberAxis) categoryplot.getRangeAxis();


// 设置Y轴的数字为百分比样式显示
DecimalFormat df = new DecimalFormat("0.0%");
vn.setNumberFormatOverride(df);
// 使柱状图反过来显示
// vn.setInverted(true);
// vn.setVerticalTickLabels(true);


// 自定义柱状图中柱子的样式
BarRenderer brender = new BarRenderer();
brender.setSeriesPaint(1, Color.decode("#C0504D")); // 给series1 Bar
brender.setSeriesPaint(0, Color.decode("#E46C0A")); // 给series2 Bar
brender.setSeriesPaint(2, Color.decode("#4F81BD")); // 给series3 Bar
brender.setSeriesPaint(3, Color.decode("#00B050")); // 给series4 Bar
brender.setSeriesPaint(4, Color.decode("#7030A0")); // 给series5 Bar
brender.setSeriesPaint(5, Color.decode("#00BF00")); // 给series6 Bar
// 设置柱状图的顶端显示数字
brender.setIncludeBaseInRange(true);
brender.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
// brender.setBaseItemLabelsVisible(isBaseItemLabelsVisible());
// 设置柱子为平面图不是立体的
brender.setBarPainter(new StandardBarPainter());
// 设置柱状图之间的距离0.1代表10%;
brender.setItemMargin(0.1);
// 设置柱子的阴影,false代表没有阴影
brender.setShadowVisible(false);


// 设置图的背景为白色
categoryplot.setBackgroundPaint(Color.WHITE);
// 设置背景虚线的颜色
categoryplot.setRangeGridlinePaint(Color.WHITE);
// categoryplot.setRangeGridlinePaint(Color.decode("#B6A2DE"));

// 去掉柱状图的背景边框,使边框不可见
categoryplot.setOutlineVisible(false);
// 设置标题的字体样式
chart.getTitle().setFont(new Font("微软雅黑", Font.PLAIN, 24));
// 设置图表下方图例上的字体样式
chart.getLegend().setItemFont(new Font("微软雅黑", Font.PLAIN, 12));


categoryplot.setRenderer(brender);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值