您的位置:首页 > 其它

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

2017-11-28 15:14 381 查看
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)散点图效果。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu jfreechart