|
| 1 | +/* This file is part of PuffinPlot, a program for palaeomagnetic |
| 2 | + * data plotting and analysis. Copyright 2012-2019 Pontus Lurcock. |
| 3 | + * |
| 4 | + * PuffinPlot is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * PuffinPlot is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with PuffinPlot. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package net.talvi.puffinplot.plots; |
| 18 | + |
| 19 | +import java.awt.Color; |
| 20 | +import java.awt.Graphics2D; |
| 21 | +import java.awt.geom.Path2D; |
| 22 | +import java.awt.geom.Rectangle2D; |
| 23 | +import java.util.List; |
| 24 | +import java.util.OptionalDouble; |
| 25 | +import net.talvi.puffinplot.data.SampleRpiEstimate; |
| 26 | +import net.talvi.puffinplot.data.Suite; |
| 27 | +import net.talvi.puffinplot.data.SuiteRpiEstimate; |
| 28 | + |
| 29 | +/** |
| 30 | + * A plot which shows the current suite RPI estimate. |
| 31 | + */ |
| 32 | +public class RpiPlot extends Plot { |
| 33 | + |
| 34 | + /** |
| 35 | + * Instantiates a new RPI plot. |
| 36 | + * |
| 37 | + * @param params the plot parameters controlling this plot's content |
| 38 | + */ |
| 39 | + public RpiPlot(PlotParams params) { |
| 40 | + super(params); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getName() { |
| 45 | + return "rpiplot"; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getNiceName() { |
| 50 | + return "RPI plot"; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void draw(Graphics2D graphics) { |
| 55 | + clearPoints(); |
| 56 | + final SuiteRpiEstimate suiteRpi = params.getSuiteRpiEstimate(); |
| 57 | + if (suiteRpi == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + final Suite suite = suiteRpi.getNrmSuite(); |
| 61 | + if (suite == null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + final PlotAxis.AxisParameters xAxisParams = new PlotAxis |
| 66 | + .AxisParameters(suite.getMaxDepth(), Direction.RIGHT) |
| 67 | + .withLabel("Depth") |
| 68 | + .withNumberEachTick(); |
| 69 | + final PlotAxis xAxis = new PlotAxis(xAxisParams, this); |
| 70 | + |
| 71 | + final List<SampleRpiEstimate> sampleRpis = suiteRpi.getRpis(); |
| 72 | + final OptionalDouble optionalMaxRpi = |
| 73 | + sampleRpis.stream().mapToDouble(rpi -> rpi.getEstimate()).max(); |
| 74 | + if (!optionalMaxRpi.isPresent()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + final double maxRpi = optionalMaxRpi.getAsDouble(); |
| 78 | + |
| 79 | + final PlotAxis.AxisParameters upAxisParams = |
| 80 | + new PlotAxis.AxisParameters(maxRpi, Direction.UP). |
| 81 | + withLabel("RPI").withNumberEachTick(); |
| 82 | + final PlotAxis upAxis = new PlotAxis(upAxisParams, this); |
| 83 | + |
| 84 | + final Rectangle2D dim = |
| 85 | + cropRectangle(getDimensions(), 320, 100, 50, 290); |
| 86 | + final double xScale = dim.getWidth() / (xAxis.getLength()); |
| 87 | + final double yScale = |
| 88 | + dim.getHeight() / upAxis.getLength(); |
| 89 | + |
| 90 | + int i = 0; |
| 91 | + final Path2D path = new Path2D.Double(Path2D.WIND_EVEN_ODD, |
| 92 | + sampleRpis.size()); |
| 93 | + for (SampleRpiEstimate sampleRpi : sampleRpis) { |
| 94 | + final double depth = sampleRpi.getNrmSample().getDepth(); |
| 95 | + final double rpi = sampleRpi.getEstimate(); |
| 96 | + final double xPos = dim.getMinX() + depth * xScale; |
| 97 | + final double yPos = dim.getMaxY() - rpi * yScale; |
| 98 | + if (i == 0) { |
| 99 | + path.moveTo(xPos, yPos); |
| 100 | + } else { |
| 101 | + path.lineTo(xPos, yPos); |
| 102 | + } |
| 103 | + i++; |
| 104 | + } |
| 105 | + |
| 106 | + graphics.setColor(Color.BLACK); |
| 107 | + upAxis.draw(graphics, yScale, (int) dim.getMinX(), |
| 108 | + (int) (dim.getMaxY())); |
| 109 | + xAxis.draw(graphics, xScale, (int) dim.getMinX(), |
| 110 | + (int) (dim.getMaxY())); |
| 111 | + graphics.draw(path); |
| 112 | + } |
| 113 | +} |
0 commit comments