Search in sources :

Example 1 with BinDataModel

use of org.knime.base.node.viz.histogram.datamodel.BinDataModel in project knime-core by knime.

the class AbstractHistogramPlotter method updateBinWidth.

/**
 * Updates ONLY the width of the bins.
 * @param binWidth the new bin width
 */
protected void updateBinWidth(final int binWidth) {
    final AbstractHistogramVizModel vizModel = getHistogramVizModel();
    if (vizModel == null) {
        LOGGER.debug("VizModel was null");
        return;
    }
    if (!vizModel.setBinWidth(binWidth)) {
        return;
    }
    final Dimension drawingSpace = vizModel.getDrawingSpace();
    if (drawingSpace == null) {
        throw new IllegalStateException("Drawing space must not be null");
    }
    final double drawingWidth = drawingSpace.getWidth();
    final double drawingHeight = drawingSpace.getHeight();
    final Coordinate xCoordinates = getXCoordinate();
    final Coordinate aggrCoordinate = getAggregationCoordinate();
    final int baseLine = (int) (drawingHeight - aggrCoordinate.calculateMappedValue(new DoubleCell(0), drawingHeight));
    final HistogramDrawingPane drawingPane = getHistogramDrawingPane();
    final int newBinWidth = vizModel.getBinWidth();
    final List<Color> barElementColors = vizModel.getRowColors();
    final HistogramHiliteCalculator calculator = vizModel.getHiliteCalculator();
    final Collection<ColorColumn> aggrColumns = vizModel.getAggrColumns();
    for (final BinDataModel bin : vizModel.getBins()) {
        final DataCell captionCell = bin.getXAxisCaptionCell();
        final double labelCoord = xCoordinates.calculateMappedValue(captionCell, drawingWidth);
        // 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 - (newBinWidth / 2.0));
        bin.updateBinWidth(xCoord, newBinWidth, barElementColors, aggrColumns, baseLine, calculator);
    }
    // if only the bar width changes we don't need to update the properties
    // since the bar width change is triggered by the property component
    // itself
    drawingPane.setHistogramVizModel(vizModel, false);
}
Also used : AbstractHistogramVizModel(org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel) DoubleCell(org.knime.core.data.def.DoubleCell) Color(java.awt.Color) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) BinDataModel(org.knime.base.node.viz.histogram.datamodel.BinDataModel) Dimension(java.awt.Dimension) Point(java.awt.Point) HistogramHiliteCalculator(org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel.HistogramHiliteCalculator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataCell(org.knime.core.data.DataCell)

Example 2 with BinDataModel

use of org.knime.base.node.viz.histogram.datamodel.BinDataModel 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
}
Also used : AggregationMethod(org.knime.base.node.viz.aggregation.AggregationMethod) DoubleCell(org.knime.core.data.def.DoubleCell) Color(java.awt.Color) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) Rectangle(java.awt.Rectangle) BinDataModel(org.knime.base.node.viz.histogram.datamodel.BinDataModel) Dimension(java.awt.Dimension) Point(java.awt.Point) HistogramHiliteCalculator(org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel.HistogramHiliteCalculator) DataCell(org.knime.core.data.DataCell) HistogramLayout(org.knime.base.node.viz.histogram.HistogramLayout)

Example 3 with BinDataModel

use of org.knime.base.node.viz.histogram.datamodel.BinDataModel in project knime-core by knime.

the class HistogramDrawingPane method paintContent.

// **********************************************
/*--------- the drawing methods ----------------*/
// **********************************************
/**
 * {@inheritDoc}
 */
@Override
public void paintContent(final Graphics g) {
    final Graphics2D g2 = (Graphics2D) g;
    final Rectangle2D bounds = getBounds();
    String msg = m_infoMsg;
    final AbstractHistogramVizModel vizModel = m_vizModel;
    if (vizModel == null || vizModel.getBins() == null) {
        // if we have no bins and no info message display a no bars info
        if (msg == null) {
            msg = "No bins to display";
        }
    }
    // check if we have to display an information message
    if (msg != null) {
        DrawingUtils.drawMessage(g2, INFO_MSG_FONT, msg, bounds);
        return;
    }
    if (m_updatePropertiesPanel && m_properties != null) {
        m_properties.updateHistogramSettings(vizModel);
        m_updatePropertiesPanel = false;
    }
    // check if we have to draw the grid lines
    if (vizModel.isShowGridLines() && m_gridLines != null) {
        for (final int gridLine : m_gridLines) {
            DrawingUtils.paintHorizontalLine(g2, 0, gridLine, (int) bounds.getWidth(), GRID_LINE_COLOR, GRID_LINE_STROKE);
        }
    }
    // get all variables which are needed multiple times
    final AggregationMethod aggrMethod = vizModel.getAggregationMethod();
    // final Collection<ColorColumn> aggrColumns =
    // vizModel.getAggrColumns();
    final HistogramLayout layout = vizModel.getHistogramLayout();
    // if the user has selected more then one aggregation column we have to
    // draw the bar outline to how him which bar belongs to which aggregation
    // column
    final boolean drawBinOutline = vizModel.isShowBinOutline();
    final boolean drawBarOutline = vizModel.isShowBarOutline();
    // (aggrColumns != null
    // && aggrColumns.size() > 1)
    // || HistogramLayout.SIDE_BY_SIDE.equals(
    // m_vizModel.getHistogramLayout());
    final boolean showElementOutline = vizModel.isShowElementOutline();
    final LabelDisplayPolicy labelDisplayPolicy = vizModel.getLabelDisplayPolicy();
    final boolean showLabelVertical = vizModel.isShowLabelVertical();
    final float barOutlineAlpha;
    if (HistogramLayout.SIDE_BY_SIDE.equals(vizModel.getHistogramLayout())) {
        barOutlineAlpha = BAR_SIDE_BY_SIDE_SURROUNDING_ALPHA;
    } else {
        barOutlineAlpha = BAR_STACKED_SURROUNDING_ALPHA;
    }
    // loop over all bins and paint them
    for (final BinDataModel bin : vizModel.getBins()) {
        if (drawBinOutline) {
            DrawingUtils.drawRectangle(g2, bin.getSurroundingRectangle(), BIN_SURROUNDING_COLOR, BIN_SURROUNDING_STROKE);
        }
        if (!bin.isPresentable()) {
            // the bars doen't fit in this bin so we have to
            // fill the complete bin in black to show it to the user
            DrawingUtils.drawBlock(g2, bin.getBinRectangle(), OVERLOADED_ELEMENT_FILLING, OVERLOADED_ELEMENT_ALPHA);
            if (bin.isSelected()) {
                DrawingUtils.drawRectangle(g2, bin.getBinRectangle(), ELEMENT_SELECTED_OUTLINE_COLOR, ELEMENT_SELECTED_OUTLINE_STROKE);
            }
            if (bin instanceof InteractiveBinDataModel) {
                final InteractiveBinDataModel interactiveBin = (InteractiveBinDataModel) bin;
                drawHiliteRect(g2, interactiveBin.getHiliteRectangle());
            }
            continue;
        }
        final Collection<BarDataModel> bars = bin.getBars();
        for (final BarDataModel bar : bars) {
            if (drawBarOutline) {
                // draw the outline of the bar if we have multiple
                // aggregation columns
                DrawingUtils.drawBlock(g2, bar.getSurroundingRectangle(), bar.getColor(), barOutlineAlpha);
            }
            if (bar.isPresentable()) {
                drawElements(g2, bar.getElements(), showElementOutline);
            } else {
                // the elements doen't fit in this bar so we have to
                // fill the complete bar to show it to the user
                final Rectangle2D barRectangle = bar.getShape();
                DrawingUtils.drawBlock(g2, barRectangle, OVERLOADED_ELEMENT_FILLING, OVERLOADED_ELEMENT_ALPHA);
                if (bar.isSelected()) {
                    DrawingUtils.drawRectangle(g2, barRectangle, ELEMENT_SELECTED_OUTLINE_COLOR, ELEMENT_SELECTED_OUTLINE_STROKE);
                }
                if (bar instanceof InteractiveBarDataModel) {
                    final InteractiveBarDataModel interactiveBar = (InteractiveBarDataModel) bar;
                    drawHiliteRect(g2, interactiveBar.getHiliteShape());
                }
            }
            // draw the bar label at last to have them on top
            drawLabels(g2, bar, aggrMethod, layout, bounds, labelDisplayPolicy, showLabelVertical);
        }
    // end of bar loop
    }
    // check if we have to draw the base line
    if (m_baseLine != null) {
        DrawingUtils.paintHorizontalLine(g2, 0, m_baseLine.intValue(), (int) bounds.getWidth(), BASE_LINE_COLOR, BASE_LINE_STROKE);
    }
    return;
}
Also used : AggregationMethod(org.knime.base.node.viz.aggregation.AggregationMethod) LabelDisplayPolicy(org.knime.base.node.viz.aggregation.util.LabelDisplayPolicy) AbstractHistogramVizModel(org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel) Rectangle2D(java.awt.geom.Rectangle2D) BinDataModel(org.knime.base.node.viz.histogram.datamodel.BinDataModel) InteractiveBinDataModel(org.knime.base.node.viz.histogram.datamodel.InteractiveBinDataModel) InteractiveBinDataModel(org.knime.base.node.viz.histogram.datamodel.InteractiveBinDataModel) TexturePaint(java.awt.TexturePaint) Paint(java.awt.Paint) Graphics2D(java.awt.Graphics2D) InteractiveBarDataModel(org.knime.base.node.viz.histogram.datamodel.InteractiveBarDataModel) BarDataModel(org.knime.base.node.viz.histogram.datamodel.BarDataModel) InteractiveBarDataModel(org.knime.base.node.viz.histogram.datamodel.InteractiveBarDataModel) HistogramLayout(org.knime.base.node.viz.histogram.HistogramLayout)

Example 4 with BinDataModel

use of org.knime.base.node.viz.histogram.datamodel.BinDataModel in project knime-core by knime.

the class BinningUtil method createNominalBins.

/**
 * Creates interactive nominal bins for the given column specification.
 * @param colSpec the column specification
 * @return the interactive bins
 */
public static List<BinDataModel> createNominalBins(final DataColumnSpec colSpec) {
    final Set<DataCell> values = getBinDataCells(colSpec);
    final List<BinDataModel> bins = new ArrayList<BinDataModel>(values.size());
    for (final DataCell value : values) {
        bins.add(new BinDataModel(value.toString(), 0, 0));
    }
    // sort the bins by their caption
    Collections.sort(bins, BinningUtil.BIN_CAPTION_COMPARATOR);
    return bins;
}
Also used : ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) BinDataModel(org.knime.base.node.viz.histogram.datamodel.BinDataModel) InteractiveBinDataModel(org.knime.base.node.viz.histogram.datamodel.InteractiveBinDataModel)

Example 5 with BinDataModel

use of org.knime.base.node.viz.histogram.datamodel.BinDataModel in project knime-core by knime.

the class BinningUtil method addDataRow2IntervalBin.

/**
 * Adds the given row to a none nominal bin by checking if the given
 * x value is in the range of the lower and upper bound of a bin.
 * @param bins
 * @param startBin the index of the bin to start with
 * @param xVal the x value of this row
 * @param color the color of this row
 * @param id the id of this row
 * @param aggrColumns the selected aggregation columns
 * @param aggrVals the aggregation values
 * @return the index of the bin this row was added
 */
private static int addDataRow2IntervalBin(final List<? extends BinDataModel> bins, final int startBin, final BinDataModel invalidValueBin, final DoubleValue xVal, final Color color, final RowKey id, final Collection<ColorColumn> aggrColumns, final DataCell... aggrVals) {
    final double value = xVal.getDoubleValue();
    if (Double.isNaN(value) || Double.isInfinite(value)) {
        if (invalidValueBin == null) {
            throw new RuntimeException("Unexpected value found. Please reset and execute the node again.");
        }
        invalidValueBin.addDataRow(id, color, aggrColumns, aggrVals);
        return startBin;
    }
    for (int binIdx = startBin, length = bins.size(); binIdx < length; binIdx++) {
        final BinDataModel bin = bins.get(binIdx);
        final Double lowerBoundObj = bin.getLowerBound();
        final Double upperBoundObj = bin.getUpperBound();
        if (lowerBoundObj == null || upperBoundObj == null) {
            continue;
        }
        final double lowerBound = lowerBoundObj.doubleValue();
        final double upperBound = upperBoundObj.doubleValue();
        boolean add2Bin = false;
        if (binIdx == 0) {
            add2Bin = (value >= lowerBound && value <= upperBound);
        } else if (lowerBound == upperBound && upperBound == value) {
            add2Bin = true;
        } else {
            add2Bin = (value > lowerBound && value <= upperBound);
        }
        if (add2Bin) {
            bin.addDataRow(id, color, aggrColumns, aggrVals);
            return binIdx;
        }
    }
    throw new IllegalArgumentException("No bin found for x value:" + xVal.toString());
}
Also used : BinDataModel(org.knime.base.node.viz.histogram.datamodel.BinDataModel) InteractiveBinDataModel(org.knime.base.node.viz.histogram.datamodel.InteractiveBinDataModel)

Aggregations

BinDataModel (org.knime.base.node.viz.histogram.datamodel.BinDataModel)5 InteractiveBinDataModel (org.knime.base.node.viz.histogram.datamodel.InteractiveBinDataModel)3 DataCell (org.knime.core.data.DataCell)3 Color (java.awt.Color)2 Dimension (java.awt.Dimension)2 Point (java.awt.Point)2 AggregationMethod (org.knime.base.node.viz.aggregation.AggregationMethod)2 HistogramLayout (org.knime.base.node.viz.histogram.HistogramLayout)2 AbstractHistogramVizModel (org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel)2 HistogramHiliteCalculator (org.knime.base.node.viz.histogram.datamodel.AbstractHistogramVizModel.HistogramHiliteCalculator)2 ColorColumn (org.knime.base.node.viz.histogram.util.ColorColumn)2 DoubleCell (org.knime.core.data.def.DoubleCell)2 Graphics2D (java.awt.Graphics2D)1 Paint (java.awt.Paint)1 Rectangle (java.awt.Rectangle)1 TexturePaint (java.awt.TexturePaint)1 Rectangle2D (java.awt.geom.Rectangle2D)1 ArrayList (java.util.ArrayList)1 LabelDisplayPolicy (org.knime.base.node.viz.aggregation.util.LabelDisplayPolicy)1 BarDataModel (org.knime.base.node.viz.histogram.datamodel.BarDataModel)1