如何在Java中逐步绘制图表? [英] How can I incrementally draw a chart in Java?

查看:80
本文介绍了如何在Java中逐步绘制图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everybody,

我必须为ARFF数据集创建图表。这些图只是图像(PNG和SVG),一旦创建,它们就不会改变。此外,它们不需要特别漂亮(我需要轴名称和改变点颜色,大小和形状的能力)。

现在,我一直在使用jFreeChart和Weka来阅读整个数据集。首先,我将数据集读入内存,然后为jFreeChart创建适当的XYDataset。

这会消耗大量内存,因此我决定逐步创建图形。我的问题是,jFreeChart似乎不支持增量图绘制。



我已经有了从我的数据集中检索下一个项目的界面。我试图创建一个实现XYPlot的自定义类,但在渲染函数中,我仍然需要传递jFreeChart XYDataset进行渲染,这是我不想要的。



  public   boolean  render(Graphics2D g2,Rectangle2D dataArea, int  index,
PlotRenderingInfo info,CrosshairState crosshairState){

.. 。
renderer.drawItem(g2,state,dataArea,info, this ,xAxis,
yAxis,dataset, 0 ,itemindex,crosshairState,pass); // 我不希望内存中有数据集
...
}





这是我目前的数据集读者:



< pre lang =java> public class ArffDataset {

private InputStream arffStream;

private int attrXIndex;
private int attrYIndex;
private int attrColorIndex;

private static int READER_CAPACITY = 100;

/ * *
*为ARFF数据集创建新的数据集,可以在ArffPlot中使用。
* @param stream ARFF数据集的源输入流
* @param XName属性名称,包含X轴数据
* @param YName属性名称,包含数据对于Y轴
* @param ZName属性的名称,包含点着色的数据
* /

public ArffDataset(InputStream流,字符串 XName,字符串 YName,字符串 ColorName){
.arffStream = stream;

init();

this .attrXIndex = this.translateAttributeNameToIndex(XName);
this .attrYIndex = this.translateAttributeNameToIndex(YName);
this .attrColorIndex = this.translateAttributeNameToIndex(ColorName);
}

/ * *
*创建新的数据集ARFF数据集,可用于ArffPlot
* @param流ARFF数据集的源输入流
* @param XIndex属性索引,包含X轴数据
* @param YIndex属性索引,包含Y轴的数据
* @param ColorIndex属性索引,包含点着色数据
* /

public ArffDataset(InputStream流, int XIndex, int YIndex , int ColorIndex){
this .arffStream = stream;
this .attrXIndex = XIndex;
this .attrYIndex = YIndex;
this .attrColorIndex = ColorIndex;

init();
}


ArffReader arffReader = null;
Instances data = null;
boolean datasetInitialized = false;

private void init(){
尝试 {
arffReader = new ArffReader(
new BufferedReader(
new InputStreamReader( this .arffStream)
),
READER_CAPACITY);
data = arffReader.getStructure();
data.setClassIndex(data.numAttributes() - 1 );
datasetInitialized = true;
} catch (IOException ioe){
datasetInitialized = false;
}
}

private int translateAttributeNameToIndex( String attrName){
if (data!= null){
return data.attribute(attrName).index();
} 其他 {
返回 - 1 ;
}
}

/ * *
*返回原始数据集的下一个实例的点。要检索正确的X,Y,Z值,请使用
*属性索引。
* @return数据集中的下一个点
* @throws IOException
* /

public ArffXYZPoint getNext() throws IOException {
Instance inst = null;
if (datasetInitialized){
inst = arffReader.readInstance(data);
if (inst!= null){
ArffXYZPoint nextPoint = new ArffXYZPoint(
inst.value( this .attrXIndex),
inst.value( this .attrYIndex),
inst.value( this .attrColorIndex)
);
return nextPoint;
}
} 其他 {
throw new DatasetNotInitializedException();
}
return null;
}
}





我的最后一个问题是,是否可以使用jFreeChart或我应该使用哪个Java库?提前谢谢。

解决方案

Hello Everybody,
I have to create graphs for ARFF datasets. These graphs are just images (PNG and SVG) and once they created, they wouldn't change. Furthermore, they don't need to be especially pretty (I need axis names and ability to change the point colour, size and shape).
For now, I have been using jFreeChart with Weka to read the entire dataset. First, I read dataset to memory and then created the appropriate XYDataset for jFreeChart.
This consumes a large amount of memory, so I decided to create the graph incrementally. My problem is, that jFreeChart doesn't seem to support incremental graph drawing.

I've already have the interface for retrieving the next item from my dataset. I tried to create a custom class implementing XYPlot, but in render function I still need to pass the jFreeChart XYDataset for rendering, which I don't want to have.

public boolean render(Graphics2D g2, Rectangle2D dataArea, int index,
	         PlotRenderingInfo info, CrosshairState crosshairState) {

...
renderer.drawItem(g2, state, dataArea, info, this, xAxis,
				                  yAxis, dataset, 0, itemindex, crosshairState, pass); //I don't want to have dataset in memory
...
}



And this is my current dataset reader:

public class ArffDataset {

	private InputStream arffStream;

	private int attrXIndex;
	private int attrYIndex;
	private int attrColorIndex;

	private static int READER_CAPACITY=100;

	/**
	 * Creates a new Dataset for an ARFF dataset, that can be used in the ArffPlot.
	 * @param stream The source inputstream for the ARFF Dataset
	 * @param XName The name of attribute, that contains data for X axis
	 * @param YName The name of attribute, that contains data for Y axis
	 * @param ZName The name of attribute, that contains data for point coloring
	 */
	public ArffDataset(InputStream stream,String XName,String YName,String ColorName){
		this.arffStream=stream;

		init();

		this.attrXIndex=this.translateAttributeNameToIndex(XName);
		this.attrYIndex=this.translateAttributeNameToIndex(YName);
		this.attrColorIndex=this.translateAttributeNameToIndex(ColorName);
	}

	/**
	 * Creates a new Dataset for an ARFF dataset, that can be used in the ArffPlot
	 * @param stream The source inputstream for the ARFF Dataset
	 * @param XIndex The index of attribute, that contains data for X axis
	 * @param YIndex The index of attribute, that contains data for Y axis
	 * @param ColorIndex The index of attribute, that contains data for point coloring
	 */
	public ArffDataset(InputStream stream,int XIndex,int YIndex,int ColorIndex){
		this.arffStream=stream;
		this.attrXIndex=XIndex;
		this.attrYIndex=YIndex;
		this.attrColorIndex=ColorIndex;

		init();
	}


	ArffReader arffReader=null;
	Instances data=null;
	boolean datasetInitialized=false;

	private void init(){
		try{
			arffReader=new ArffReader(
					new BufferedReader(
							new InputStreamReader(this.arffStream)
							),
							READER_CAPACITY);
			data=arffReader.getStructure();
			data.setClassIndex(data.numAttributes()-1);
			datasetInitialized=true;
		}catch(IOException ioe){
			datasetInitialized=false;
		}
	}

	private int translateAttributeNameToIndex(String attrName){
		if(data!=null){
			return data.attribute(attrName).index();
		}else{
			return -1;
		}
	}

	/**
	 * Returns th epoint for the next instance of the original dataset. To retrieve the correct X,Y,Z values
	 * indexes of attributes are used.
	 * @return the next point from the dataset
	 * @throws IOException
	 */
	public ArffXYZPoint getNext() throws IOException {
		Instance inst=null;
		if(datasetInitialized){
			inst=arffReader.readInstance(data);
			if(inst!=null){
				ArffXYZPoint nextPoint=new ArffXYZPoint(
						inst.value(this.attrXIndex),
						inst.value(this.attrYIndex),
						inst.value(this.attrColorIndex)
						);
				return nextPoint;
			}
		}else{
			throw new DatasetNotInitializedException();
		}
		return null;
	}
}



My final question is if it's possible using jFreeChart or which Java library should I use? Thank you in advance.

解决方案

这篇关于如何在Java中逐步绘制图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆