001 package EVolve.util.painters; 002 003 import EVolve.visualization.*; 004 import EVolve.util.HelperFuncs; 005 006 import java.util.*; 007 import java.awt.*; 008 009 /** 010 * User: Wei Wang 011 * Date: Jun 12, 2003 012 * Time: 9:41:56 AM 013 */ 014 public class StatisticPainter extends RandomPainter{ 015 private HashMap intervalData; 016 private ArrayList intervals, totals; 017 private long current_interval; 018 private float total; 019 private long last_y; 020 021 public StatisticPainter() { 022 current_interval = 0; 023 intervalData = new HashMap(); 024 intervals = new ArrayList(); 025 intervals.add(intervalData); 026 totals = new ArrayList(); 027 total = 0; 028 } 029 030 //x: time, y:entity id, z: amount 031 public void paint(AutoImage image, long x, long y, long z) { 032 if (y < 0) return; 033 034 while (x>current_interval) { 035 totals.add(new Float(total)); 036 processData((int)current_interval); 037 intervalData = new HashMap(); 038 intervals.add(intervalData); 039 current_interval++; 040 total = 0; 041 } 042 043 Color key = colorCache.getNextColor(y); 044 total += (float)z; 045 if (!intervalData.containsKey(key)) { 046 intervalData.put(key, new Float(z)); 047 } else { 048 Float old = (Float)intervalData.get(key); 049 intervalData.put(key, new Float(old.floatValue()+z)); 050 } 051 last_y = y; 052 } 053 054 public ArrayList getStatisticData() { 055 return intervals; 056 } 057 058 public void postProcess() { 059 totals.add(new Float(total)); 060 processData(intervals.size()-1); 061 } 062 063 private void processData(int index) { 064 HashMap data = (HashMap)intervals.get(index); 065 float total = ((Float)totals.get(index)).floatValue(); 066 if (data.size() == 0) { 067 total = 100; 068 data.put(colorCache.getNextColor(last_y),new Float(100)); 069 } 070 071 Iterator it = data.keySet().iterator(); 072 while (it.hasNext()) { 073 Object key = it.next(); 074 075 Float Value = (Float)data.get(key); 076 data.put(key,new Float(100F*Value.floatValue()/total)); 077 } 078 } 079 080 public Object clone() { 081 StatisticPainter o = (StatisticPainter)super.clone(); 082 083 o.intervals = new ArrayList(); 084 for (int i=0; i<intervals.size(); i++) { 085 o.intervals.add(HelperFuncs.cloneHashMap((HashMap)intervals.get(i))); 086 } 087 o.totals = HelperFuncs.cloneArrayList(totals); 088 089 return o; 090 } 091 }