JfreeChart 的一些简单使用 柱状图,饼状图,点线图

DefaultPieDataset dataset = new DefaultPieDataset();

dataset.setValue("Level1", 100);
dataset.setValue("Level6", 500);
dataset.setValue("Level7", 400);

 



JFreeChart chart = ChartFactory.createPieChart3D("IT Product Code", dataset, true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot();
 


pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat
.getNumberInstance(), new DecimalFormat("0.00%")));

pieplot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));



 
pieplot.setNoDataMessage("Not Inforaction ");
pieplot.setCircular(false);
pieplot.setLabelGap(0.02D);
String filename = ServletUtilities.saveChartAsPNG(chart, 350, 250,getRequest().getSession());

String graphURL = getServletContext().getContextPath() + "/DisplayChart?filename=" + filename;

getRequest().setAttribute("pie",graphURL);*/






/* double[][] data = new double[][] { { 100, 99.3, 89.6, 98, 96 }, { 87, 98, 89.1, 91, 93 },
{ 92, 96, 93, 90.4, 95 } };
String[] rowKeys = { "指标A", "指标B", "指标C" };
String[] columnKeys = { "员工A", "员工B", "员工C", "员工D", "员工F" };
String chartTitle = "点线图";
String xName = "人名";
String yName = "分数";
int width = 500;
int height = 400;
*//*******************************//*

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

JFreeChart chart = ChartFactory.createLineChart(chartTitle, // 图表标题
xName, // 目录轴的显示标签
yName, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);

chart.setTextAntiAlias(false);

TextTitle title = new TextTitle(chartTitle);
title.setFont(new Font("隶书", Font.BOLD, 25));
chart.setTitle(title);// 设置图标题的字体重新设置title

chart.getLegend().setItemFont(new Font("SansSerif", Font.BOLD, 12));// 图例显示格式

chart.setBackgroundPaint(Color.WHITE);//设置背景颜色
chart.setBorderVisible(true);//设置背景边框

CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();//获取图表

categoryplot.setDomainGridlinesVisible(true);// x轴网格是否可见

categoryplot.setRangeGridlinesVisible(true); // y轴网格是否可见

categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩

categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩

categoryplot.setBackgroundPaint(Color.lightGray);//设定图表数据显示部分背景色

categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); // 设置轴和面板之间的距离 参数1:上距 参数2:左距 参数3:下距 参数4:右距

CategoryAxis domainAxis = categoryplot.getDomainAxis();//横轴
domainAxis.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));// 轴标题
domainAxis.setTickLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));// 轴数值
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的Lable 45度倾斜
domainAxis.setLowerMargin(0.0);// 设置距离图片左端距离
domainAxis.setUpperMargin(0.0);// 设置距离图片右端距离

ValueAxis rangeAxis = categoryplot.getRangeAxis(); //纵轴
rangeAxis.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));// 轴标题
rangeAxis.setTickLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));// 轴数值
rangeAxis.setRange(85, 100.5);//纵轴显示范围
rangeAxis.setAutoTickUnitSelection(true);

rangeAxis.setLowerMargin(5);// 设置距离图片下端距离
rangeAxis.setUpperMargin(0.5);// 设置距离图片上端距离

NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setUpperMargin(1.15); // 设置最高的一个 Item 与图片顶端的距离
numberaxis.setLowerMargin(0.15);// 设置最低的一个 Item 与图片底端的距离
DecimalFormat df = new DecimalFormat("#0.00");
numberaxis.setNumberFormatOverride(df); //设置纵轴精度

categoryplot.setRangeAxis(numberaxis);

LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();// 获得renderer

lineandshaperenderer.setBaseShapesVisible(true); // 点(即数据点)可见

lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineandshaperenderer.setBaseItemLabelsVisible(true);// 显示折点数据
lineandshaperenderer.setBaseItemLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));// 折点数据字体
lineandshaperenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE10,
TextAnchor.BASELINE_CENTER));//这点数据显示位置

lineandshaperenderer.setBaseLinesVisible(true); // 点(即数据点)间有连线可见

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

String filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, getSession());

String graphURL = getServletContext().getContextPath() + "/DisplayChart?filename=" + filename;*/









double[][] data = new double[][] {{672, 766, 223, 540, 126},{325, 521, 210, 340, 106},{332, 256, 523, 240, 526}};
String[] rowKeys = {"Apple","LIzi","PuTao"};
String[] columnKeys = {"北京","上海","广州","成都","深圳"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计",
null,
null,
dataset,
PlotOrientation.VERTICAL,
true,false,false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();

CategoryAxis domainAxis = plot.getDomainAxis();
//domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);

ValueAxis rangeAxis = plot.getRangeAxis();
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);

BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色
renderer.setWallPaint(Color.gray);
//设置每种水果代表的柱的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
renderer.setSeriesPaint(1, new Color(0, 100, 255));
renderer.setSeriesPaint(2, Color.GREEN);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
//显示每个柱的数值,并修改该数值的字体属性
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);

//设置柱的透明度
plot.setForegroundAlpha(0.6f);
//设置地区、销量的显示位置
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, getSession());
String graphURL = getRequest().getContextPath() + "/DisplayChart?filename=" + filename; 


getRequest().setAttribute("pie",graphURL);

转载于:https://www.cnblogs.com/taoxinwan/archive/2012/03/02/2377829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值