import javax.swing.JPanel;
+import de.republib.util.DoublePair;
import de.republib.util.IPairData;
+import de.republib.util.PairData;
/**
* A diagram sheet.
* the pair data for one line
* @return the instance (for chaining)
*/
- Diagram addData(IPairData data) {
+ public Diagram addData(IPairData data) {
this.data.add(data);
return this;
}
final double y0 = this.realY0;
final double realHeight = this.realHeight;
for (final IPairData data : this.data) {
+ final DoublePair minMax = PairData.minMaxY(data, null);
this.realX0 = data.getX(0);
this.realWidth = data.getX(data.getSteps() - 1) - this.realX0;
+ this.realY0 = minMax.getValue1();
+ final int steps = data.getSteps();
+ final double maxY = minMax.getValue2();
+ this.realHeight = maxY + (maxY - this.realY0) / steps;
int lastX = 0, lastY = 0;
for (int ix = 0; ix < data.getSteps(); ix++) {
final int x = transformX(data.getX(ix));
*/
protected void paintScale(Graphics graphics) {
graphics.setColor(this.colorScale);
- graphics.drawLine(this.hScaleDistance, this.vScaleDistance, this.getWidth() - this.hScaleDistance,
- this.vScaleDistance);
- graphics.drawLine(this.hScaleDistance, this.vScaleDistance, this.hScaleDistance,
- this.getHeight() - this.vScaleDistance);
+ final int y0 = this.getHeight() - this.vScaleDistance;
+ final int y1 = this.vScaleDistance;
+ final int x0 = this.hScaleDistance;
+ final int x1 = this.getWidth() - this.hScaleDistance;
+ // vertical scale:
+ graphics.drawLine(x0, y0, x0, y1);
+ // horizontal scale:
+ graphics.drawLine(x0, y0, x1, y0);
}
final double yPixel = (y - this.realY0) * (getHeight() - 2 * this.vScaleDistance);
int rc;
if (yPixel < 0) {
- rc = -1;
- } else if (yPixel > getHeight()) {
rc = getHeight();
+ } else if (yPixel > getHeight()) {
+ rc = -1;
} else {
- rc = (int) Math.round(yPixel);
+ rc = getHeight() - (int) Math.round(yPixel);
}
return rc;
}
import javax.swing.JTextField;
import de.republib.gui.Diagram;
+import de.republib.util.FunctionPairData;
import de.republib.util.I18N;
+import de.republib.util.MathFunction;
/**
* Created by hm on 03.07.16.
private JPanel panelOutput;
private JPanel panelLog;
private JLabel labelStatusLine;
- private JPanel panelData;
+ private Diagram panelData;
private JPanel panelHead;
private JTextField textFieldServer;
private JTextField textFieldPort;
this.panelCenter.add(this.tabbedPane = new JTabbedPane());
this.tabbedPane.addTab(I18N.tr("Output"), this.panelOutput = new JPanel());
this.tabbedPane.addTab(I18N.tr("Diagram"), this.panelData = new Diagram());
+ FunctionPairData function = new FunctionPairData(MathFunction.X, 0.0, 10.0, 790);
+ function.setFactor(9.0);
+ function.setOffset(1.0);
+ this.panelData.addData(function);
+ function = new FunctionPairData(MathFunction.SIN, 0.0, 10.0, 790);
+ function.setFactor(4.0);
+ function.setOffset(5.0);
+ this.panelData.addData(function);
this.tabbedPane.addTab(I18N.tr("Log"), this.panelLog = new JPanel());
}
--- /dev/null
+/**
+ *
+ */
+package de.republib.util;
+
+/**
+ * Stores two double values.
+ *
+ * @author hm
+ *
+ */
+public class DoublePair {
+ /**
+ * first value.
+ */
+ public double value1;
+ /**
+ * second value.
+ */
+ public double value2;
+
+ /**
+ * Constructor.
+ *
+ * @param value1
+ * @param value2
+ */
+ DoublePair(double value1, double value2) {
+ this.value1 = value1;
+ this.value2 = value2;
+ }
+
+ /**
+ * @return the value1
+ */
+ public double getValue1() {
+ return this.value1;
+ }
+
+ /**
+ * @return the value2
+ */
+ public double getValue2() {
+ return this.value2;
+ }
+
+ /**
+ * @param value1
+ * the value1 to set
+ */
+ public void setValue1(double value1) {
+ this.value1 = value1;
+ }
+
+ /**
+ * @param value2
+ * the value2 to set
+ */
+ public void setValue2(double value2) {
+ this.value2 = value2;
+ }
+}
*
*/
public class PairData {
+ /**
+ * Returns the maximal y value of all pairs.
+ *
+ * @param data
+ * data to inspect
+ * @return the maximum of the y values
+ */
public static double maxY(IPairData data) {
double rc = data.getY(0);
for (int ix = data.getSteps() - 1; ix > 0; ix--) {
return rc;
}
+ /**
+ * Returns the minimal y value of all pairs.
+ *
+ * @param data
+ * data to inspect
+ * @return the minimal of the y values
+ */
+ public static DoublePair minMaxY(IPairData data, DoublePair minMax) {
+ double min = data.getY(0);
+ double max = min;
+ for (int ix = data.getSteps() - 1; ix > 0; ix--) {
+ final double current = data.getY(ix);
+ if (current < min) {
+ min = current;
+ } else if (current > max) {
+ max = current;
+ }
+ }
+ if (minMax == null) {
+ minMax = new DoublePair(min, max);
+ } else {
+ minMax.value1 = min;
+ minMax.value2 = max;
+ }
+ return minMax;
+ }
+
+ /**
+ * Returns the minimal y value of all pairs.
+ *
+ * @param data
+ * data to inspect
+ * @return the minimal of the y values
+ */
public static double minY(IPairData data) {
double rc = data.getY(0);
for (int ix = data.getSteps() - 1; ix > 0; ix--) {
final double current = data.getY(ix);
- if (current > rc) {
+ if (current < rc) {
rc = current;
}
}
--- /dev/null
+package de.republib.util;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class PairDataTest {
+
+ @Test
+ public void maxY() {
+ final BytePairData data = new BytePairData(4, 4, -2.0, 2.0, 1);
+ for (byte value = -4; value < 0; value++) {
+ data.append(value);
+ }
+ Assert.assertEquals(PairData.maxY(data), -1.0);
+ Assert.assertEquals(PairData.minY(data), -4.0);
+ final DoublePair pair = PairData.minMaxY(data, new DoublePair(0.0, 0.0));
+ Assert.assertEquals(pair.getValue2(), -1.0);
+ Assert.assertEquals(pair.getValue1(), -4.0);
+ }
+
+ @Test
+ public void minMaxY() {
+ final BytePairData data = new BytePairData(4, 4, -2.0, 2.0, 8);
+ final long value = 0x12345678abcdefL;
+ final double value2 = value;
+ data.appendLittleEndian(value, 8);
+ Assert.assertEquals(PairData.maxY(data), value2);
+ Assert.assertEquals(PairData.minY(data), value2);
+ final DoublePair pair = PairData.minMaxY(data, new DoublePair(0.0, 0.0));
+ Assert.assertEquals(pair.getValue2(), value2);
+ Assert.assertEquals(pair.getValue1(), value2);
+ }
+
+ @Test
+ public void minY() {
+ final BytePairData data = new BytePairData(4, 4, -2.0, 2.0, 4);
+ for (int value = 4; value < 8; value++) {
+ data.appendLittleEndian(value, 4);
+ }
+ Assert.assertEquals(PairData.maxY(data), 7.0);
+ Assert.assertEquals(PairData.minY(data), 4.0);
+ final DoublePair pair = PairData.minMaxY(data, new DoublePair(0.0, 0.0));
+ Assert.assertEquals(pair.getValue2(), 7.0);
+ Assert.assertEquals(pair.getValue1(), 4.0);
+ }
+}