inflor.core.utils.PlotUtils.java Source code

Java tutorial

Introduction

Here is the source code for inflor.core.utils.PlotUtils.java

Source

/*
 * ------------------------------------------------------------------------ Copyright 2016 by Aaron
 * Hart Email: Aaron.Hart@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License, Version 3, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if
 * not, see <http://www.gnu.org/licenses>.
 * ---------------------------------------------------------------------
 *
 * Created on December 14, 2016 by Aaron Hart
 */
package inflor.core.utils;

import java.awt.Color;
import java.awt.Paint;
import java.util.logging.Logger;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.renderer.LookupPaintScale;
import org.jfree.data.Range;

import inflor.core.fcs.DimensionTypes;
import inflor.core.logging.LogFactory;
import inflor.core.plots.AbstractFCChart;
import inflor.core.plots.ChartSpec;
import inflor.core.plots.ColorSchemes;
import inflor.core.plots.DensityPlot;
import inflor.core.plots.HistogramPlot;
import inflor.core.plots.LogicleNumberAxis;
import inflor.core.plots.PaintModel;
import inflor.core.plots.PlotTypes;
import inflor.core.plots.ScatterPlot;
import inflor.core.transforms.AbstractTransform;
import inflor.core.transforms.BoundDisplayTransform;
import inflor.core.transforms.LogicleTransform;
import inflor.core.transforms.LogrithmicTransform;
import inflor.core.transforms.TransformType;

public class PlotUtils {

    private PlotUtils() {
    }

    public static ValueAxis createAxis(String name, AbstractTransform transform) {
        if (transform instanceof BoundDisplayTransform) {
            NumberAxis axis = new NumberAxis(name);
            BoundDisplayTransform bdt = (BoundDisplayTransform) transform;
            axis.setRange(new Range(bdt.getMinTranformedValue(), bdt.getMaxValue()));
            return axis;
        } else if (transform instanceof LogicleTransform) {
            LogicleTransform llt = (LogicleTransform) transform;
            return new LogicleNumberAxis(name, llt);
        } else if (transform instanceof LogrithmicTransform) {
            NumberAxis axis = new NumberAxis(name);
            LogrithmicTransform logTransform = (LogrithmicTransform) transform;
            axis.setRange(new Range(logTransform.getMin(), logTransform.getMax()));
            return axis;
        } else {
            throw new IllegalArgumentException("Transformation type not supported. Yet.");
        }
    }

    public static AbstractFCChart createPlot(ChartSpec plotSpec) {
        PlotTypes type = plotSpec.getPlotType();
        AbstractFCChart newPlot = null;
        if (type.equals(PlotTypes.SCATTER)) {
            newPlot = new ScatterPlot(plotSpec);
        } else if (type.equals(PlotTypes.HISTOGRAM)) {
            newPlot = new HistogramPlot(plotSpec);
        } else if (type.equals(PlotTypes.DENSITY)) {
            newPlot = new DensityPlot(plotSpec);
        } else {
            Logger logger = LogFactory.createLogger(PlotUtils.class.getName());
            logger.fine("PlotType not supported: " + type.name());
        }
        return newPlot;
    }

    public static AbstractTransform createDefaultTransform(TransformType selectedType) {

        AbstractTransform newTransform = null;

        if (selectedType == TransformType.LINEAR || selectedType == TransformType.BOUNDARY) {
            newTransform = new BoundDisplayTransform(Double.MAX_VALUE, Double.MAX_VALUE);
        } else if (selectedType == TransformType.LOGARITHMIC) {
            newTransform = new LogrithmicTransform(1, 1000000);
        } else if (selectedType == TransformType.LOGICLE) {
            newTransform = new LogicleTransform();
        } else {
            // noop
        }
        return newTransform;
    }

    public static LookupPaintScale createPaintScale(double zMax, ColorSchemes colorScheme) {
        PaintModel pm = new PaintModel(colorScheme, zMax);
        Paint[] paints = pm.getPaints();
        double[] levels = pm.getLevels();
        LookupPaintScale paintScale = new LookupPaintScale(0, pm.getThreshold(), Color.GRAY);
        for (int i = 0; i < levels.length; i++) {
            paintScale.add(levels[i], paints[i]);
        }
        return paintScale;
    }

    // TODO: move to FCSUtils?
    public static AbstractTransform createDefaultTransform(String parameterName) {
        if (DimensionTypes.DNA.matches(parameterName) || DimensionTypes.FORWARD_SCATTER.matches(parameterName)
                || DimensionTypes.SIDE_SCATTER.matches(parameterName) || DimensionTypes.TIME.matches(parameterName)
                || FCSUtilities.MERGE_DIMENSION_NAME.equals(parameterName)) {
            return new BoundDisplayTransform(0, 262144);

        } else if (DimensionTypes.PULSE_WIDTH.matches(parameterName)) {
            return new BoundDisplayTransform(0, 100);
        } else {
            return new LogicleTransform();
        }
    }
}