use of org.knime.base.node.viz.aggregation.AggregationMethod in project knime-core by knime.
the class BarDataModel method setElementRectangle.
/**
* @param baseLine the x coordinate of the base line (0) on the screen
* @param barElementColors all element colors which define the order
* the elements should be drawn
*/
private void setElementRectangle(final int baseLine, final List<Color> barElementColors, final HistogramHiliteCalculator calculator) {
final Rectangle2D barRectangle = getShape();
if (barRectangle == null) {
// also reset the element rectangles
final Collection<BarElementDataModel> elements = getElements();
for (final BarElementDataModel element : elements) {
element.setRectangle(null, calculator);
}
return;
}
final AggregationMethod aggrMethod = calculator.getAggrMethod();
final HistogramLayout layout = calculator.getLayout();
final double maxAggrVal = getMaxAggregationValue(aggrMethod, layout);
final double minAggrVal = getMinAggregationValue(aggrMethod, layout);
double valRange;
if (minAggrVal < 0 && maxAggrVal > 0) {
// if the bar contains negative and positive elements
// we have to add the min and max aggregation value
// to get the full range
valRange = maxAggrVal + Math.abs(minAggrVal);
} else {
// if the bar contains either negative or positive elements
// simply take the maximum since one of them is zero
valRange = Math.max(Math.abs(maxAggrVal), Math.abs(minAggrVal));
}
final int barHeight = (int) barRectangle.getHeight();
final int barWidth = (int) barRectangle.getWidth();
final int noOfElements = getNoOfElements();
setPresentable(elementsFitInBar(layout, barElementColors, noOfElements, barWidth, barHeight), calculator);
if (!isPresentable()) {
return;
}
if (HistogramLayout.STACKED.equals(layout)) {
setStackedRectangles(barRectangle, barElementColors, valRange, aggrMethod, calculator);
} else if (HistogramLayout.SIDE_BY_SIDE.equals(layout)) {
setSideBySideRectangles(barRectangle, barElementColors, valRange, aggrMethod, baseLine, calculator);
} else {
throw new IllegalArgumentException("Layout " + layout + " not supported");
}
return;
}
use of org.knime.base.node.viz.aggregation.AggregationMethod in project knime-core by knime.
the class FixedHistogramDataModel method loadFromFile.
/**
* @param directory the directory to write to
* @param exec the {@link ExecutionMonitor} to provide progress messages; must not be <code>null</code>
* @return the histogram data model
* @throws InvalidSettingsException if the x column specification
* wasn't valid
* @throws IOException if a file exception occurs
* @throws CanceledExecutionException if the operation is canceled
*/
public static FixedHistogramDataModel loadFromFile(final File directory, final ExecutionMonitor exec) throws InvalidSettingsException, IOException, CanceledExecutionException {
exec.setProgress(0.0, "Start reading data from file");
final ConfigRO config;
final FileInputStream is;
final GZIPInputStream inData;
try {
final File settingsFile = new File(directory, CFG_DATA_FILE);
is = new FileInputStream(settingsFile);
inData = new GZIPInputStream(is);
config = NodeSettings.loadFromXML(inData);
} catch (final FileNotFoundException e) {
throw e;
} catch (final IOException e) {
LOGGER.error("Unable to load histogram data: " + e.getMessage());
throw new IOException("Please reexecute the histogram node. " + "(For details see log file)");
}
final Config xConfig = config.getConfig(CFG_X_COL_SPEC);
final DataColumnSpec xColSpec = DataColumnSpec.load(xConfig);
final boolean binNominal = config.getBoolean(CFG_NOMINAL);
AggregationMethod aggrMethod = AggregationMethod.getDefaultMethod();
try {
aggrMethod = AggregationMethod.getMethod4Command(config.getString(CFG_AGGR_METHOD));
} catch (final Exception e) {
// Take the default method
}
exec.setProgress(0.1, "Binning column specification loaded");
exec.setProgress("Loading aggregation columns...");
final Config aggrConf = config.getConfig(CFG_AGGR_COLS);
final int aggrColCounter = aggrConf.getInt(CFG_AGGR_COL_COUNTER);
final ArrayList<ColorColumn> aggrCols = new ArrayList<ColorColumn>(aggrColCounter);
for (int i = 0; i < aggrColCounter; i++) {
final Config aggrColConf = aggrConf.getConfig(CFG_COLOR_COL + i);
aggrCols.add(ColorColumn.loadFromFile(aggrColConf, exec));
}
exec.setProgress(0.3, "Loading bins...");
final ConfigRO binsConf = config.getConfig(CFG_BINS);
final int binCounter = binsConf.getInt(CFG_BIN_COUNTER);
final List<BinDataModel> bins = new ArrayList<BinDataModel>(binCounter);
for (int i = 0; i < binCounter; i++) {
final Config binConf = binsConf.getConfig(CFG_BIN + i);
bins.add(BinDataModel.loadFromFile(binConf, exec));
}
final Config missingConfig = binsConf.getConfig(CFG_MISSING_BIN);
final BinDataModel missingBin = BinDataModel.loadFromFile(missingConfig, exec);
BinDataModel invalidBin = null;
if (binsConf.containsKey(CFG_INVALID_BIN)) {
final Config invalidConfig = binsConf.getConfig(CFG_INVALID_BIN);
invalidBin = BinDataModel.loadFromFile(invalidConfig, exec);
}
exec.setProgress(0.9, "Loading element colors...");
final ConfigRO colorColsConf = config.getConfig(CFG_COLOR_COLS);
final int counter = colorColsConf.getInt(CFG_ROW_COLOR_COUNTER);
final SortedSet<Color> rowColors = new TreeSet<Color>(HSBColorComparator.getInstance());
for (int i = 0; i < counter; i++) {
rowColors.add(new Color(colorColsConf.getInt(CFG_ROW_COLOR + i)));
}
exec.setProgress(1.0, "Histogram data model loaded ");
// close the stream
inData.close();
is.close();
return new FixedHistogramDataModel(xColSpec, aggrMethod, aggrCols, binNominal, bins, missingBin, invalidBin, rowColors);
}
use of org.knime.base.node.viz.aggregation.AggregationMethod in project knime-core by knime.
the class AbstractHistogramPlotter method getAggregationColSpec.
/**
* @return the <code>DataColumnSpec</code> of the aggregation column
*/
public DataColumnSpec getAggregationColSpec() {
final AbstractHistogramVizModel vizModel = getHistogramVizModel();
if (vizModel == null) {
LOGGER.debug("VizModel was null");
throw new IllegalStateException("Exception in getAggregationColSpec: " + "Viz model must not be null");
}
double lowerBound = vizModel.getMinAggregationValue();
double upperBound = vizModel.getMaxAggregationValue();
// coordinate
if (lowerBound > 0) {
lowerBound = 0;
} else if (upperBound < 0) {
upperBound = 0;
}
final AggregationMethod aggrMethod = vizModel.getAggregationMethod();
// set the column type depending on the aggregation method and type of
// the aggregation column. If the method is count set it to integer. If
// the aggregation method is summary and the data type of the
// aggregation column is integer the result must be an integer itself
DataType type = DoubleCell.TYPE;
final Collection<? extends ColorColumn> columnNames = vizModel.getAggrColumns();
if (AggregationMethod.COUNT.equals(aggrMethod) || AggregationMethod.VALUE_COUNT.equals(aggrMethod)) {
type = IntCell.TYPE;
}
if (AggregationMethod.SUM.equals(aggrMethod) && columnNames != null) {
// if the aggregation method is summary and ...
boolean allInteger = true;
for (final ColorColumn column : columnNames) {
final DataColumnSpec colSpec = m_tableSpec.getColumnSpec(column.getColumnName());
if (colSpec == null || !colSpec.getType().isCompatible(IntValue.class)) {
allInteger = false;
break;
}
}
// ... all columns of the int type we can set the column type to int
if (allInteger) {
type = IntCell.TYPE;
}
}
final String displayColumnName = createAggregationColumnName(columnNames, aggrMethod);
final DataColumnSpec spec = createColumnSpec(displayColumnName, type, lowerBound, upperBound, null);
return spec;
}
use of org.knime.base.node.viz.aggregation.AggregationMethod in project knime-core by knime.
the class AbstractHistogramPlotter method setHistogramBinRectangle.
/**
* Calculates and sets the drawing rectangle of each bin.
* @param xCoordinates The <code>Coordinate</code> object which contains
* the start position of an bar on the x axis
* @param yCoordinates The <code>Coordinate</code> object which contains
* the start position of an bar on the y axis
*/
private static void setHistogramBinRectangle(final AbstractHistogramVizModel vizModel, final Coordinate xCoordinates, final Coordinate yCoordinates) {
final Dimension drawingSpace = vizModel.getDrawingSpace();
final int binWidth = vizModel.getBinWidth();
final AggregationMethod aggrMethod = vizModel.getAggregationMethod();
final List<Color> barElementColors = vizModel.getRowColors();
final Collection<ColorColumn> aggrColumns = vizModel.getAggrColumns();
final HistogramLayout layout = vizModel.getHistogramLayout();
final HistogramHiliteCalculator calculator = vizModel.getHiliteCalculator();
final double drawingWidth = drawingSpace.getWidth();
final double drawingHeight = drawingSpace.getHeight();
final int baseLine = (int) (drawingHeight - yCoordinates.calculateMappedValue(new DoubleCell(0), drawingHeight));
// this is the minimum size of a bar with an aggregation value > 0
final int minHeight = Math.max((int) HistogramDrawingPane.getBarStrokeWidth(), AbstractHistogramVizModel.MINIMUM_BAR_HEIGHT);
// final int binWidth = getBinWidth();
for (final BinDataModel bin : vizModel.getBins()) {
final DataCell captionCell = bin.getXAxisCaptionCell();
final double labelCoord = xCoordinates.calculateMappedValue(captionCell, drawingWidth);
if (labelCoord < 0) {
// this bin is not on the x axis (because it is empty and the
// empty bins shouldn't be displayed) so we simply set the
// rectangle to null and continue
bin.setBinRectangle(null, baseLine, barElementColors, aggrColumns, calculator);
continue;
}
// if the maximum value is negative use 0 to end at the base line
final double maxAggrVal = Math.max(bin.getMaxAggregationValue(aggrMethod, layout), 0);
// if the minimum value is positive use 0 to start at the base line
final double minAggrVal = Math.min(bin.getMinAggregationValue(aggrMethod, layout), 0);
// subtract half of the bar width from the start position to place
// the middle point of the bar on the mapped coordinate position
final int xCoord = (int) (labelCoord - (binWidth / 2.0));
final int upperY = (int) (drawingHeight - yCoordinates.calculateMappedValue(new DoubleCell(maxAggrVal), drawingHeight));
final int lowerY = (int) (drawingHeight - yCoordinates.calculateMappedValue(new DoubleCell(minAggrVal), drawingHeight));
final Rectangle binRect = calculateBorderRectangle(xCoord, lowerY, upperY, minHeight, binWidth, maxAggrVal, baseLine);
bin.setBinRectangle(binRect, baseLine, barElementColors, aggrColumns, calculator);
}
// end of for loop over the x axis coordinates
}
use of org.knime.base.node.viz.aggregation.AggregationMethod in project knime-core by knime.
the class AbstractHistogramPlotter method registerPropertiesChangeListener.
/**
* Registers all histogram properties listener to the histogram
* properties panel.
*/
private void registerPropertiesChangeListener() {
if (m_histoProps == null) {
throw new IllegalStateException("Properties panel must not be null");
}
m_histoProps.addShowGridChangedListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
setShowGridLines(e.getStateChange() == ItemEvent.SELECTED);
}
});
m_histoProps.addShowBinOutlineChangedListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
final AbstractHistogramVizModel vizModel = getHistogramVizModel();
if (vizModel != null) {
vizModel.setShowBinOutline(e.getStateChange() == ItemEvent.SELECTED);
final HistogramDrawingPane histoDrawingPane = getHistogramDrawingPane();
histoDrawingPane.repaint();
}
}
});
m_histoProps.addShowBarOutlineChangedListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
final AbstractHistogramVizModel vizModel = getHistogramVizModel();
if (vizModel != null) {
vizModel.setShowBarOutline(e.getStateChange() == ItemEvent.SELECTED);
final HistogramDrawingPane histoDrawingPane = getHistogramDrawingPane();
histoDrawingPane.repaint();
}
}
});
m_histoProps.addShowElementOutlineChangedListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
final AbstractHistogramVizModel vizModel = getHistogramVizModel();
if (vizModel != null) {
vizModel.setShowElementOutline(e.getStateChange() == ItemEvent.SELECTED);
final HistogramDrawingPane histoDrawingPane = getHistogramDrawingPane();
histoDrawingPane.repaint();
}
}
});
m_histoProps.addLabelOrientationListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
final AbstractHistogramVizModel vizModel = getHistogramVizModel();
if (vizModel != null) {
final AbstractHistogramProperties histoProps = getHistogramPropertiesPanel();
if (histoProps != null) {
vizModel.setShowLabelVertical(histoProps.isShowLabelVertical());
final HistogramDrawingPane histoDrawingPane = getHistogramDrawingPane();
histoDrawingPane.repaint();
}
}
}
});
m_histoProps.addLabelDisplayListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
final AbstractHistogramVizModel vizModel = getHistogramVizModel();
if (vizModel != null) {
final AbstractHistogramProperties histoProps = getHistogramPropertiesPanel();
if (histoProps != null) {
vizModel.setLabelDisplayPolicy(histoProps.getLabelDisplayPolicy());
final HistogramDrawingPane histoDrawingPane = getHistogramDrawingPane();
histoDrawingPane.repaint();
}
}
}
});
m_histoProps.addLayoutListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
final AbstractHistogramProperties histoProps = getHistogramPropertiesPanel();
if (histoProps != null) {
setHistogramLayout(histoProps.getHistogramLayout());
}
}
});
m_histoProps.addBinWidthChangeListener(new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent e) {
final int binWidth = m_histoProps.getBinWidth();
updateBinWidth(binWidth);
}
});
m_histoProps.addNoOfBinsChangeListener(new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent e) {
final int noOfBins = m_histoProps.getNoOfBins();
if (setNumberOfBins(noOfBins)) {
updatePaintModel();
}
}
});
m_histoProps.addAggrMethodListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
final String methodName = e.getActionCommand();
if (!AggregationMethod.valid(methodName)) {
throw new IllegalArgumentException("No valid aggregation method");
}
final AggregationMethod aggrMethod = AggregationMethod.getMethod4Command(methodName);
if (setAggregationMethod(aggrMethod)) {
updatePaintModel();
}
}
});
m_histoProps.addShowEmptyBinListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
if (setShowEmptyBins(e.getStateChange() == ItemEvent.SELECTED)) {
updatePaintModel();
}
}
});
m_histoProps.addShowMissingValBinListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
if (setShowMissingValBin(e.getStateChange() == ItemEvent.SELECTED)) {
updatePaintModel();
}
}
});
m_histoProps.addShowInvalidValBinListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
if (setShowInvalidValBin(e.getStateChange() == ItemEvent.SELECTED)) {
updatePaintModel();
}
}
});
}
Aggregations