jFreeChart柱形图

更新时间:2023-11-02 11:58:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

jFreeChart柱形图

JFreeChart 柱状图

以下是源代码: /*

* jfreechart * * */

package cn.com.terje.jfreechart;

import java.awt.Font; import java.io.File; import java.io.IOException;

import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis;

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.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.TextAnchor; /** * *

* @author terje liu */

public class DomeOne {

public static void main(String[] args) throws IOException {

JFreeChart chart = ChartFactory.createBarChart(\

桂林甜点供应(个)\城区\

\供应量\PlotOrientation.HORIZONTAL, true, true, false);

// ---------显示中文------------\\\\

chart.getTitle().setFont(new Font(\宋体\Font.PLAIN, 20));

CategoryPlot plot = chart.getCategoryPlot();

// 设置分类 中文,就是图片里面的那个\城区\和\区\

CategoryAxis cgAxis = plot.getDomainAxis(); cgAxis.setTickLabelFont(new Font(\宋体\Font.PLAIN, 11));

cgAxis.setLabelFont(new Font(\宋体\15));

// 设置数值 中文

ValueAxis vAxis = plot.getRangeAxis(); vAxis.setTickLabelFont(new Font(\宋体\Font.PLAIN, 11));

vAxis.setLabelFont(new Font(\宋体\15));

// 显示说明 中文

chart.getLegend().setItemFont(new Font(\宋体\Font.PLAIN, 16));

// -------微调整-------\\\\

// 设置边距,右边的(防止最大值,跑到图片外面去,所以要设置一下)

vAxis.setUpperMargin(0.08);

// 设置最小值(有个最小值好一些,容易突出重点嘛)

vAxis.setLowerBound(200);

// ------将每个柱子显示的数值 显示出来---------\\\\ BarRenderer barRender = new BarRenderer(); barRender

.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

barRender.setBaseItemLabelFont(new Font(\宋体

\

barRender.setBaseItemLabelsVisible(true); // 避免因为数值过小,显示不明显,或则看不到 barRender.setMinimumBarLength(10); plot.setRenderer(barRender);

// 调节柱子显示的数值的位置

// 通过设置ItemLabelPosition里面的2个静态参数(奇怪了,ItemLabelAnchor的含义文档好像没有怎么说) barRender.setBasePositiveItemLabelPosition(new ItemLabelPosition(

ItemLabelAnchor.OUTSIDE2, TextAnchor.TOP_LEFT));

// ------将结果以图片形式输出------\\\\ File f = new File(\

ChartUtilities.saveChartAsJPEG(f, chart, 800, 640); } /**

本文来源:https://www.bwwdw.com/article/uwt2.html

Top