我写了一个折线图的工具类,在此记录一下,这个折线工具类可以在折线上面显示数据,防止中文乱码,生成在本地

package com.leniao.highchartsUtils;


import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;


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.labels.CategoryItemLabelGenerator;
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.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.TextAnchor;


/*
 * 折线图
 */
public class LineChartsUtils extends ApplicationFrame {


	private static final long serialVersionUID = 1L;


	public LineChartsUtils(String s) {
		super(s);
		setContentPane(createDemoLine());
	}


	private Container createDemoLine() {
		return null;
	}


	// 生成图表主对象JFreeChart
	public static JFreeChart createChart1(String chartTitle, String chartXdesc, String chartYdesc,
			DefaultCategoryDataset linedataset) {
		// 图表标题
		// chartTitle,
		// chartXdesc, // 横轴的显示标签
		// chartYdesc, // 纵轴的显示标签
		// dataset, // 数据集. 就是CategoryDataset类的实例对象
		// PlotOrientation.VERTICAL, // 图表方向:水平、垂直 || 显示方式
		// true, // 是否显示图例(标题)
		// true, // 是否生成提示工具 tooltips || 是否启动热键
		// false // 是否生成URL链接
		JFreeChart chart = ChartFactory.createLineChart(chartTitle, chartXdesc, chartYdesc, linedataset,
				PlotOrientation.VERTICAL, true, true, false);
		// 设置标题
		// ===============为了防止中文乱码:必须设置字体
		chart.setTitle(new TextTitle(chartTitle, new Font("黑体", Font.ITALIC, 22)));
		LegendTitle legend = chart.getLegend(); // 获取图例
		legend.setItemFont(new Font("宋体", Font.BOLD, 12)); // 设置图例的字体,防止中文乱码
		CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
		// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
		plot.setBackgroundPaint(new Color(255, 255, 204));
		plot.setForegroundAlpha(0.65F); // 设置前景色透明度
		// 设置横虚线可见
		plot.setRangeGridlinesVisible(true);
		// 虚线色彩
		plot.setRangeGridlinePaint(Color.gray);
		CategoryAxis h = plot.getDomainAxis(); // 获取x轴
		h.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示
		h.setLabelFont(new Font("宋体", Font.BOLD, 12));// 设置字体,防止中文乱码
		h.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// 轴数值
		// h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜
		plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 12)); // Y轴设置字体,防止中文乱码


		// 折线上面显示数据
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
		DecimalFormat decimalformat1 = new DecimalFormat("##.##");// 数据点显示数据值的格式
		renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
		// 上面这句是设置数据项标签的生成器
		renderer.setItemLabelsVisible(true);// 设置项标签显示
		renderer.setBaseItemLabelsVisible(true);// 基本项标签显示
		// 上面这几句就决定了数据点按照设定的格式显示数据值
		renderer.setShapesFilled(Boolean.TRUE);// 在数据点显示实心的小图标
		renderer.setShapesVisible(true);// 设置显示小图标


		return chart;
	}


	/**
	 * 在本地生成图片的方法
	 * 
	 * @param destPath
	 * @param chart
	 * @param width
	 * @param heigth
	 * @return
	 */
	private static boolean drawToOutputStream(String destPath, JFreeChart chart, int width, int heigth) {
		FileOutputStream fos = null;
		boolean returnValue = true;
		try {
			fos = new FileOutputStream(destPath);
			ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流
					chart, // 图表对象
					width, // 宽
					heigth, // 高
					null); // ChartRenderingInfo信息
		} catch (IOException e) {
			e.printStackTrace();
			returnValue = false;
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return returnValue;
	}


	/**
	 * 测试曲线图
	 */
	public static void demo() {
		// 初始化数据
		String chartTitle = "访问量统计图形";
		String chartXdesc = "时间";
		String chartYdesc = "访问量";
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		// 各曲线名称
		String series1 = "冰箱";
		String series2 = "彩电";
		String series3 = "洗衣机";		// 横轴名称(列名称)
		String type1 = "1月";
		String type2 = "2月";
		String type3 = "3月";
		linedataset.addValue(0.0, series1, type1);
		linedataset.addValue(4.2, series1, type2);
		linedataset.addValue(3.9, series1, type3);
		linedataset.addValue(1.0, series2, type1);
		linedataset.addValue(5.2, series2, type2);
		linedataset.addValue(7.9, series2, type3);
		linedataset.addValue(2.0, series3, type1);
		linedataset.addValue(9.2, series3, type2);
		linedataset.addValue(8.9, series3, type3);


		// 生成图表
		JFreeChart chart = createChart1(chartTitle, chartXdesc, chartYdesc, linedataset);
		// 生成图片
		drawToOutputStream("C:\\Users\\Administrator\\Desktop\\折线图.JPG", chart, 640, 480);
	}


	public static void main(String[] args) {
		demo();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于poi树状图、饼图、折线图生成,可以使用Apache POI提供的XSSFChart和XDDFChart类进行操作。 首先,我们需要创建一个工作簿对象,然后在工作簿中创建一个工作表对象。接着,我们可以创建一个图表对象,并将其添加到工作表中。然后,我们可以使用XSSFChart和XDDFChart类的方法来定义图表数据和样式。 具体实现步骤如下: 1. 创建一个工作簿对象 ``` XSSFWorkbook workbook = new XSSFWorkbook(); ``` 2. 创建一个工作表对象 ``` XSSFSheet sheet = workbook.createSheet("Chart"); ``` 3. 创建一个图表对象 ``` XSSFChart chart = sheet.createDrawingPatriarch().createChart(anchor); ``` 其中,anchor是图表的位置和大小,可以使用XSSFClientAnchor类来定义。 4. 定义图表数据和样式 对于饼图、折线图和树状图,数据一般是通过一个数据源来提供的。我们可以使用XDDFDataSource类来创建数据源,并将其添加到图表中。样式方面,可以使用XDDFChartLegend和XDDFChartData类来定义图表的标题、图例、颜色等属性。 下面是一个生成饼图的示例代码: ``` // 创建一个工作簿对象 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个工作表对象 XSSFSheet sheet = workbook.createSheet("Chart"); // 创建一个图表对象 XSSFChart chart = sheet.createDrawingPatriarch().createChart(anchor); // 创建一个数据源 XDDFDataSource<String> categoryDataSource = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 4, 0, 0)); XDDFNumericalDataSource<Double> dataDataSource = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 4, 1, 1)); // 创建一个饼图数据对象 XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null); // 添加数据源到饼图数据对象中 data.setCategoryData(categoryDataSource); data.setValues(dataDataSource); // 创建一个饼图样式对象 XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); XDDFPieChartSeries series = (XDDFPieChartSeries) data.getSeries().get(0); series.setTitle("Pie Chart"); XDDFShapeProperties shapeProperties = series.getShapeProperties(); shapeProperties.setFillProperties(new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE))); ``` 这样,就可以在Excel文档中生成一个饼图了。其他类型的图表生成也是类似的,只需要根据需要调用不同的类和方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vegetari

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值