package timeflow.app.ui.filter; import javax.swing.*; import timeflow.data.time.Interval; import timeflow.model.Display; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.text.*; public class BabyHistogram extends JPanel { private int[] buckets; private double[] x; private double min, max; private int numDefined; private int maxBucket; private double value; private static final DecimalFormat df=new DecimalFormat("###,###,###,###.##"); Point mouseHit=new Point(); Point mouse=new Point(-1,0); enum Modify {START, END, POSITION, NONE}; Modify change=Modify.NONE; Rectangle startRect=new Rectangle(-1,-1,0,0); Rectangle endRect=new Rectangle(-1,-1,0,0); Rectangle positionRect=new Rectangle(-1,-1,0,0); Color sidePlain=Color.orange; Color sideMouse=new Color(230,100,0); double relLow=0, relHigh=1, originalLow, originalHigh; public void setRelRange(double relLow, double relHigh) { this.relLow=Math.max(0,relLow); this.relHigh=Math.min(1,relHigh); repaint(); } public void setTrueRange(double low, double high) { double span=max-min; if (span<=0) // nothing much to do... return; setRelRange((low-min)/span, (high-min)/span); } public boolean isEverything() { return relLow==0 && relHigh==1; } public double getLow() { return abs(relLow); } public double getHigh() { return abs(relHigh); } private double abs(double x) { return x*max+(1-x)*min; } public BabyHistogram(final Runnable changeAction) { addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int mx=e.getX(); int my=e.getY(); int lx=lowX(), hx=highX(); int ox=0; if (Math.abs(mx-lx)0; if (Double.isNaN(min)) { min=m; max=m; value=m; } else { min=Math.min(m, min); max=Math.max(m, max); } } } if (numDefined==0) return; if (min==max) { buckets=new int[1]; buckets[0]=numDefined; maxBucket=numDefined; return; } int numBuckets=(int)Math.min(50, 2*Math.sqrt(numDefined)); buckets=new int[numBuckets]; maxBucket=0; for (int i=0; i