JFreeeChart绘制波形图/折线图

时间:2021-04-18 10:26:57


用JFreeeChart绘制波形图/折线图。

参考:http://javapub.iteye.com/blog/757685

前提:导入需要的2个jar文件,jcommon-版本号.jar,jfreechart-版本号.jar。

import java.awt.Dimension;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.EmptyBlock;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;

public class PaintPlotLine {
	String xName = "";
	String yName = "";
	String plotName = "";
	String xySeriesName = "";
	String dataPath = "";

	/**
	 * 
	 * @param xName
	 *            X轴名称
	 * @param yName
	 *            Y轴名称
	 * @param plotName
	 *            图名
	 * @param xySeriesName
	 *            图示名
	 * @param dataPath
	 *            数据文件位置
	 */
	public PaintPlotLine(String yName, String xName, String plotName,
			String xySeriesName, String dataPath) {
		this.xName = xName;
		this.yName = yName;
		this.plotName = plotName;
		this.xySeriesName = xySeriesName;
		this.dataPath = dataPath;
	}

	/**
	 * 从数据文件中读取数据,文件中数据是由空格分隔的,数据必须是数字
	 * @param fileURL 文件位置
	 * @return 独到的数据构成的数组
	 */
	public double[] getDataFromFile(String fileURL) {

		FileReader reader;
		List<Double> list = new ArrayList<Double>();
		StringBuffer sb = new StringBuffer();
		try {
			reader = new FileReader(fileURL);
			int b;
			while ((b = reader.read()) != -1) {
				sb.append((char) b);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		String dataString = null;

		if (sb != null) {
			dataString = sb.toString().trim();
		}

		//将读到的数据存入字符串数组中
		String dataArray[] = dataString.split(" ");

		//去除数组中的数据前后空格
		for (int i = 0; i < dataArray.length; i++) {
			dataArray[i] = dataArray[i].trim();
		}

		//解析字符串数组,得到对应的数字
		for (int i = 0; i < dataArray.length; i++) {

			//数据不为"" ," ",null才作解析
			if (!"".equals(dataArray[i]) && !" ".equals(dataArray[i])
					&& !dataArray[i].equals(null)) {
				double d = Double.parseDouble(dataArray[i]);
				list.add(new Double(d));
			}
		}

		double dataArrayD[] = new double[list.size()];
		for (int i = 0; i < list.size(); i++) {
			dataArrayD[i] = list.get(i).doubleValue();
		}
		return dataArrayD;

	}

	public XYDataset createDataset1(String xySeriesName, String path) {
		
		XYSeries xyseries = new XYSeries(xySeriesName);
		double[] dataArrayD = getDataFromFile(path);
		int dataLen = dataArrayD.length;

		for (int i = 0; i < dataLen; i++) {

			xyseries.add(i, dataArrayD[i]);
		}
		XYSeriesCollection xyseriescollection = new XYSeriesCollection();
		xyseriescollection.addSeries(xyseries);
		return xyseriescollection;
	}

	public JFreeChart createChart(String xName, String yName, String plotName,
			String xySeriesName, String path) {

		XYDataset xydataset = createDataset1(xySeriesName, path);

		// ///--------------
		// 创建主题样式
		StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 设置标题字体
		standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); // 设置图例的字体
		standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15)); // 设置轴向的字体
		standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15)); // 应用主题样式
		ChartFactory.setChartTheme(standardChartTheme);
		// ///------------------

		JFreeChart jfreechart = ChartFactory.createXYLineChart(plotName, xName,
				yName, xydataset, PlotOrientation.VERTICAL, false, true, false);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();

		NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
		numberaxis.setAutoRangeIncludesZero(false);

		xyplot.mapDatasetToRangeAxis(1, 1);
		XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
				.getRenderer();

		LegendTitle legendtitle = new LegendTitle(xylineandshaperenderer);

		BlockContainer blockcontainer = new BlockContainer(
				new BorderArrangement());
		blockcontainer.add(legendtitle, RectangleEdge.LEFT);

		blockcontainer.add(new EmptyBlock(2000D, 0.0D));

		CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
		compositetitle.setPosition(RectangleEdge.BOTTOM);
		jfreechart.addSubtitle(compositetitle);

		return jfreechart;
	}

	public JPanel createDemoPanel() {
		JFreeChart jfreechart = createChart(xName, yName, plotName,
				xySeriesName, dataPath);
		return new ChartPanel(jfreechart);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		//路径按实际情况改,此处使用的绝对路径,可改成相对路径
		String dataPath = "C:\\Users\\Mao\\Desktop\\chanel0-index0.txt";
		JPanel panel = new PaintPlotLine("waveLength/nm", "时间", "波形图", "波长",
				dataPath).createDemoPanel();
		frame.add(panel);
		frame.setVisible(true);
		frame.setSize(new Dimension(500, 500));
		frame.setDefaultCloseOperation(3);
	}

}


 

jfreechart的版本过高可能会是的汉字出现乱码。其中解决方案如下:

 
	// ///--------------
		// 创建主题样式
		StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 设置标题字体
		standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); // 设置图例的字体
		standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15)); // 设置轴向的字体
		standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15)); // 应用主题样式
		ChartFactory.setChartTheme(standardChartTheme);
		// ///------------------