Skip to content

Commit

Permalink
ttools: add optional histogram weighting coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
mbtaylor authored and mmpcn committed Nov 27, 2014
1 parent 88c25ab commit 4243068
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
12 changes: 7 additions & 5 deletions topcat/src/main/uk/ac/starlink/topcat/plot2/CoordPanel.java
Expand Up @@ -164,11 +164,13 @@ public void setTable( TopcatModel tcModel ) {
public void autoPopulate() {
int is = 1;
for ( int ic = 0; ic < coords_.length; ic++ ) {
JComboBox[] colsels = colSelectors_[ ic ];
for ( int iu = 0; iu < colsels.length; iu++ ) {
JComboBox cs = colsels[ iu ];
if ( is < cs.getItemCount() ) {
cs.setSelectedIndex( is++ );
if ( coords_[ ic ].isRequired() ) {
JComboBox[] colsels = colSelectors_[ ic ];
for ( int iu = 0; iu < colsels.length; iu++ ) {
JComboBox cs = colsels[ iu ];
if ( is < cs.getItemCount() ) {
cs.setSelectedIndex( is++ );
}
}
}
}
Expand Down
Expand Up @@ -83,7 +83,7 @@ public Plotter[] getPlotters() {
new LinePlotter(),
new LabelPlotter(),
new ContourPlotter(),
new HistogramPlotter( PlaneDataGeom.X_COORD ),
new HistogramPlotter( PlaneDataGeom.X_COORD, true ),
FunctionPlotter.PLANE,
} ) );
return list.toArray( new Plotter[ 0 ] );
Expand Down
Expand Up @@ -6,8 +6,10 @@
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.Icon;
import uk.ac.starlink.ttools.gui.ResourceIcon;
Expand Down Expand Up @@ -49,7 +51,10 @@ public class HistogramPlotter
implements Plotter<HistogramPlotter.HistoStyle> {

private final FloatingCoord xCoord_;
private final FloatingCoord weightCoord_;
private final SliceDataGeom histoDataGeom_;
private final int icX_;
private final int icWeight_;

public static final ConfigKey<Integer> THICK_KEY =
StyleKeys.createThicknessKey( 2 );
Expand All @@ -65,11 +70,20 @@ public class HistogramPlotter
* Constructor.
*
* @param xCoord X axis coordinate
* @param hasWeight true to permit histogram weighting
*/
public HistogramPlotter( FloatingCoord xCoord ) {
public HistogramPlotter( FloatingCoord xCoord, boolean hasWeight ) {
xCoord_ = xCoord;
weightCoord_ =
hasWeight
? FloatingCoord.createCoord( "Weight", "Weighting of data points"
+ ", if not unity", false )
: null;
histoDataGeom_ =
new SliceDataGeom( new FloatingCoord[] { xCoord_, null }, "X" );
int icol = 0;
icX_ = icol++;
icWeight_ = hasWeight ? icol++ : -1;
}

public String getPlotterName() {
Expand All @@ -88,9 +102,12 @@ public int getPositionCount() {
}

public Coord[] getExtraCoords() {
return new Coord[] {
xCoord_,
};
List<Coord> list = new ArrayList<Coord>();
list.add( xCoord_ );
if ( weightCoord_ != null ) {
list.add( weightCoord_ );
}
return list.toArray( new Coord[ 0 ] );
}

public ConfigKey[] getStyleKeys() {
Expand Down Expand Up @@ -259,9 +276,19 @@ private BinBag readBins( boolean xlog, double binWidth, double binPhase,
DataStore dataStore ) {
BinBag binBag = new BinBag( xlog, binWidth, binPhase, point );
TupleSequence tseq = dataStore.getTupleSequence( dataSpec );
while ( tseq.next() ) {
double x = xCoord_.readDoubleCoord( tseq, 0 );
binBag.addToBin( x, 1 );
if ( weightCoord_ == null ) {
while ( tseq.next() ) {
double x = xCoord_.readDoubleCoord( tseq, icX_ );
binBag.addToBin( x, 1 );
}
}
else {
while ( tseq.next() ) {
double x = xCoord_.readDoubleCoord( tseq, icX_ );
double w = weightCoord_.readDoubleCoord( tseq, icWeight_ );
double weight = Double.isNaN( w ) ? 1 : w;
binBag.addToBin( x, weight );
}
}
return binBag;
}
Expand Down

0 comments on commit 4243068

Please sign in to comment.