实用技巧(2):Ubuntu 14.04下JFreeChart的安装和使用

来源:互联网 发布:call死你软件 编辑:程序博客网 时间:2024/06/04 21:46

1.JFreeChart的安装
(1)访问链接http://www.jfree.org/jfreechart/download/并下载JFreeChart的最新版本,博主使用的是jfreechart-1.0.1.tar.gz。
这里写图片描述
(2)解压jfreechart-1.0.1.tar.gz后,将解压目录jfreechart-1.0.1/lib中的jcommon-1.0.0.jar和jfreechart-1.0.1.jar两个文件拷贝到$JAVA_HOME/lib中。
这里写图片描述
(3)启动IDEA,依次点击File->Project Structure->Modules,点击右侧的“+”号,将jcommon-1.0.0.jar和jfreechart-1.0.1.jar添加到自己的项目之中,这时我们已经能够使用JFreeChart了。

2.JFreeChart的使用
(1)博主写了一个使用JFreeChart绘制散点图的小程序,仅供参考。

package com.spark;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.plot.PlotOrientation;import org.jfree.data.xy.XYSeries;import org.jfree.data.xy.XYSeriesCollection;import org.jfree.ui.RefineryUtilities;import java.util.ArrayList;/** * Created by hadoop on 17-11-27. */public class JFChart {    public static void displayData(String title,ArrayList<ArrayList<float[]>> dataArray)    {        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();        for(int i=0;i<dataArray.size();i++)        {            XYSeries xySeries = new XYSeries(i+1);            for (int j=0;j<dataArray.get(i).size();j++ ) {                xySeries.add(Double.parseDouble("" + dataArray.get(i).get(j)[0] + ""), Double.parseDouble("" + dataArray.get(i).get(j)[1] + ""));                xySeriesCollection.addSeries(xySeries);            }        }        final JFreeChart chart =ChartFactory.createScatterPlot(title,"","",xySeriesCollection,PlotOrientation.VERTICAL,false,false,false);        ChartFrame frame = new ChartFrame(title,chart);        frame.pack();//确定frame的最佳大小        RefineryUtilities.centerFrameOnScreen(frame);        frame.setVisible(true);    }    public static void main(String[] args)    {        float[] floatOne = {1.0f,2.0f};        float[] floatTwo= {3.0f,3.0f};        float[] floatThree= {2.0f,4.0f};        ArrayList<float[]> arrayList = new ArrayList<float[]>();        arrayList.add(floatOne);        arrayList.add(floatTwo);        arrayList.add(floatThree);        ArrayList<ArrayList<float[]>> dataArray = new ArrayList<ArrayList<float[]>>();        dataArray.add(arrayList);        JFChart.displayData("散点图",dataArray);    }}

(2)散点图效果。

原创粉丝点击