View Javadoc

1   package net.sf.magicproject.chart.datasets;
2   
3   import java.util.Collection;
4   
5   import org.jfree.data.statistics.HistogramType;
6   
7   import net.sf.magicproject.chart.ChartFilter;
8   import net.sf.magicproject.chart.IChartKey;
9   import net.sf.magicproject.chart.IDataProvider;
10  import net.sf.magicproject.clickable.targetable.card.CardModel;
11  
12  /***
13   * 
14   */
15  public class HistogramDataset extends
16  		org.jfree.data.statistics.HistogramDataset implements Dataset {
17  
18  	private int[] values = new int[0];
19  
20  	/***
21  	 * Create a new instance of this class.
22  	 * 
23  	 * @param provider
24  	 *          the key provider.
25  	 * @param filter
26  	 *          the filter attached to this dataset.
27  	 */
28  	public HistogramDataset(IDataProvider provider, ChartFilter filter) {
29  		super();
30  		this.provider = provider;
31  		this.filter = filter;
32  		this.setType(HistogramType.FREQUENCY);
33  	}
34  
35  	/***
36  	 * Add cards to all datasets.
37  	 * 
38  	 * @param cardModel
39  	 *          the card to add.
40  	 * @param amount
41  	 *          the amount of card to add.
42  	 */
43  	public void addCard(final CardModel cardModel, final int amount) {
44  		Collection<IChartKey> keys = provider.getKeys(cardModel, filter);
45  		for (IChartKey key : keys) {
46  			if (key.getIntegerKey() + 1 > values.length) {
47  				int[] tmp = new int[key.getIntegerKey() + 1];
48  				System.arraycopy(values, 0, tmp, 0, values.length);
49  				values = tmp;
50  			}
51  			values[key.getIntegerKey()] += amount;
52  		}
53  		list.clear();
54  		int max = 0;
55  		for (int value : values)
56  			if (value > max)
57  				max = value;
58  		double[] amounts = new double[max + 1];
59  		for (int index = max + 1; index-- > 0;)
60  			amounts[values[index]] = index;
61  		try {
62  			addSeries("data1", amounts, amounts.length, 0, amounts.length);
63  		} catch (Exception e) {
64  			e.printStackTrace();
65  		}
66  		fireDatasetChanged();
67  	}
68  
69  	/***
70  	 * Remove cards to all datasets.
71  	 * 
72  	 * @param cardModel
73  	 *          the card to remove.
74  	 * @param amount
75  	 *          the amount of card to remove.
76  	 */
77  	public void removeCard(final CardModel cardModel, final int amount) {
78  		Collection<IChartKey> keys = provider.getKeys(cardModel, filter);
79  		for (IChartKey key : keys) {
80  			values[key.getIntegerKey()] -= amount;
81  			double max = 0;
82  			for (double value : values)
83  				if (value > max)
84  					max = value;
85  			list.clear();
86  			try {
87  				// addSeries("data1", values, 100, 0, max);
88  			} catch (Exception e) {
89  				e.printStackTrace();
90  			}
91  		}
92  		fireDatasetChanged();
93  	}
94  
95  	public void setValue(IChartKey key, Integer value) {
96  		// super.setValue(value, "My Key", key);
97  	}
98  
99  	public void removeAll() {
100 		//
101 	}
102 
103 	private final IDataProvider provider;
104 
105 	private final ChartFilter filter;
106 
107 }