我正在尝试使用演示模型构建图表程序.对于像字符串或数字这样的简单类型,使用JGoodies进行数据绑定相对容易.但我无法弄清楚如何在hashmap上使用它.
我将尝试解释图表的工作原理以及我的问题是什么:
图表由DataSeries组成,DataSeries由DataPoints组成.我想拥有一个数据模型,并能够在同一模型上使用不同的视图(例如条形图,饼图,……).它们每个都包含三个类.
例如:
DataPointModel:保存数据模型(值,标签,类别)
DataPointViewModel:扩展JGoodies PresentationModel.包装DataPointModel并保存视图属性,如字体和颜色.
DataPoint:抽象类,扩展JComponent.不同的视图必须子类化并实现自己的ui.
绑定和创建数据模型很简单,但我不知道如何绑定我的数据系列模型.
package at.onscreen.chart;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
public class DataSeriesModel {
public static String PROPERTY_DATAPOINT = "dataPoint";
public static String PROPERTY_DATAPOINTS = "dataPoints";
public static String PROPERTY_LABEL = "label";
public static String PROPERTY_MAXVALUE = "maxValue";
/**
* holds the data points
*/
private HashMap dataPoints;
/**
* the label for the data series
*/
private String label;
/**
* the maximum data point value
*/
private Double maxValue;
/**
* the model supports property change notification
*/
private PropertyChangeSupport propertyChangeSupport;
/**
* default constructor
*/
public DataSeriesModel() {
this.maxValue = Double.valueOf(0);
this.dataPoints = new HashMap();
this.propertyChangeSupport = new PropertyChangeSupport(this);
}
/**
* constructor
* @param label - the series label
*/
public DataSeriesModel(String label) {
this.dataPoints = new HashMap();
this.maxValue = Double.valueOf(0);
this.label = label;
this.propertyChangeSupport = new PropertyChangeSupport(this);
}
/**
* full constructor
* @param label - the series label
* @param dataPoints - an array of data points
*/
public DataSeriesModel(String label, DataPoint[] dataPoints) {
this.dataPoints = new HashMap();
this.propertyChangeSupport = new PropertyChangeSupport(this);
this.maxValue = Double.valueOf(0);
this.label = label;
for (int i = 0; i < dataPoints.length; i++) {
this.addDataPoint(dataPoints[i]);
}
}
/**
* full constructor
* @param label - the series label
* @param dataPoints - a collection of data points
*/
public DataSeriesModel(String label, Collection dataPoints) {
this.dataPoints = new HashMap();
this.propertyChangeSupport = new PropertyChangeSupport(this);
this.maxValue = Double.valueOf(0);
this.label = label;
for (Iterator it = dataPoints.iterator(); it.hasNext();) {
this.addDataPoint(it.next());
}
}
/**
* adds a new data point to the series. if the series contains a data point with same id, it will be replaced by the new one.
* @param dataPoint - the data point
*/
public void addDataPoint(DataPoint dataPoint) {
String category = dataPoint.getCategory();
DataPoint oldDataPoint = this.getDataPoint(category);
this.dataPoints.put(category, dataPoint);
this.setMaxValue(Math.max(this.maxValue, dataPoint.getValue()));
this.propertyChangeSupport.firePropertyChange(PROPERTY_DATAPOINT, oldDataPoint, dataPoint);
}
/**
* returns the data point with given id or null if not found
* @param uid - the id of the data point
* @return the data point or null if there is no such point in the table
*/
public DataPoint getDataPoint(String category) {
return this.dataPoints.get(category);
}
/**