|
| 1 | +/* |
| 2 | + * This file is part of "linear-regression-visualizer", licensed under MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) 2025 neziw |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package ovh.neziw.visualizer.serialization; |
| 25 | + |
| 26 | +import java.awt.Color; |
| 27 | +import java.awt.Shape; |
| 28 | +import ovh.neziw.visualizer.gui.ChartSettings; |
| 29 | + |
| 30 | +public final class ChartSettingsConverter { |
| 31 | + |
| 32 | + public static ChartSettingsData toData(final ChartSettings settings) { |
| 33 | + return new ChartSettingsData( |
| 34 | + settings.getPaddingPercent(), |
| 35 | + settings.getDataPointColor().getRGB(), |
| 36 | + settings.getRegressionLineColor().getRGB(), |
| 37 | + shapeToString(settings.getDataPointShape()), |
| 38 | + settings.isDashedLine() |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public static void applyToSettings(final ChartSettingsData data, final ChartSettings settings) { |
| 43 | + settings.setPaddingPercent(data.getPaddingPercent()); |
| 44 | + settings.setDataPointColor(new Color(data.getDataPointColorRgb(), true)); |
| 45 | + settings.setRegressionLineColor(new Color(data.getRegressionLineColorRgb(), true)); |
| 46 | + settings.setDataPointShape(stringToShape(data.getPointShapeType())); |
| 47 | + settings.setDashedLine(data.isDashedLine()); |
| 48 | + } |
| 49 | + |
| 50 | + private static String shapeToString(final Shape shape) { |
| 51 | + if (shape == null) { |
| 52 | + return "CIRCLE"; |
| 53 | + } |
| 54 | + final String className = shape.getClass().getSimpleName(); |
| 55 | + switch (className) { |
| 56 | + case "Ellipse2D$Double": |
| 57 | + return "CIRCLE"; |
| 58 | + case "Rectangle2D$Double": |
| 59 | + final java.awt.geom.Rectangle2D rect = (java.awt.geom.Rectangle2D) shape; |
| 60 | + if (rect.getWidth() > 6.5) { |
| 61 | + return "SQUARE_LARGE"; |
| 62 | + } |
| 63 | + return "SQUARE"; |
| 64 | + case "Path2D$Double": |
| 65 | + if (isDiamond(shape)) { |
| 66 | + return "DIAMOND"; |
| 67 | + } else if (isTriangle(shape)) { |
| 68 | + return "TRIANGLE"; |
| 69 | + } else if (isStar(shape)) { |
| 70 | + return "STAR"; |
| 71 | + } |
| 72 | + break; |
| 73 | + } |
| 74 | + return "CIRCLE"; |
| 75 | + } |
| 76 | + |
| 77 | + private static Shape stringToShape(final String shapeType) { |
| 78 | + if (shapeType == null) { |
| 79 | + return createCircle(); |
| 80 | + } |
| 81 | + switch (shapeType) { |
| 82 | + case "SQUARE": |
| 83 | + return new java.awt.geom.Rectangle2D.Double(-3, -3, 6, 6); |
| 84 | + case "SQUARE_LARGE": |
| 85 | + return new java.awt.geom.Rectangle2D.Double(-4, -4, 8, 8); |
| 86 | + case "DIAMOND": |
| 87 | + return createDiamond(); |
| 88 | + case "TRIANGLE": |
| 89 | + return createTriangle(); |
| 90 | + case "STAR": |
| 91 | + return createStar(); |
| 92 | + default: |
| 93 | + return createCircle(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static Shape createCircle() { |
| 98 | + return new java.awt.geom.Ellipse2D.Double(-3, -3, 6, 6); |
| 99 | + } |
| 100 | + |
| 101 | + private static Shape createDiamond() { |
| 102 | + final java.awt.geom.Path2D.Double diamond = new java.awt.geom.Path2D.Double(); |
| 103 | + diamond.moveTo(0, -4); |
| 104 | + diamond.lineTo(4, 0); |
| 105 | + diamond.lineTo(0, 4); |
| 106 | + diamond.lineTo(-4, 0); |
| 107 | + diamond.closePath(); |
| 108 | + return diamond; |
| 109 | + } |
| 110 | + |
| 111 | + private static Shape createTriangle() { |
| 112 | + final java.awt.geom.Path2D.Double triangle = new java.awt.geom.Path2D.Double(); |
| 113 | + triangle.moveTo(0, -4); |
| 114 | + triangle.lineTo(-4, 4); |
| 115 | + triangle.lineTo(4, 4); |
| 116 | + triangle.closePath(); |
| 117 | + return triangle; |
| 118 | + } |
| 119 | + |
| 120 | + private static Shape createStar() { |
| 121 | + final java.awt.geom.Path2D.Double star = new java.awt.geom.Path2D.Double(); |
| 122 | + final int outerRadius = 4; |
| 123 | + final int innerRadius = 2; |
| 124 | + final int points = 5; |
| 125 | + for (int i = 0; i < points * 2; i++) { |
| 126 | + final double angle = Math.PI * i / points - Math.PI / 2; |
| 127 | + final int radius = i % 2 == 0 ? outerRadius : innerRadius; |
| 128 | + final double x = Math.cos(angle) * radius; |
| 129 | + final double y = Math.sin(angle) * radius; |
| 130 | + if (i == 0) { |
| 131 | + star.moveTo(x, y); |
| 132 | + } else { |
| 133 | + star.lineTo(x, y); |
| 134 | + } |
| 135 | + } |
| 136 | + star.closePath(); |
| 137 | + return star; |
| 138 | + } |
| 139 | + |
| 140 | + private static boolean isDiamond(final Shape shape) { |
| 141 | + if (!(shape instanceof java.awt.geom.Path2D.Double)) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + final java.awt.geom.Path2D.Double path = (java.awt.geom.Path2D.Double) shape; |
| 145 | + final java.awt.geom.PathIterator it = path.getPathIterator(null); |
| 146 | + final double[] coords = new double[6]; |
| 147 | + int pointCount = 0; |
| 148 | + while (!it.isDone()) { |
| 149 | + final int type = it.currentSegment(coords); |
| 150 | + if (type == java.awt.geom.PathIterator.SEG_MOVETO || type == java.awt.geom.PathIterator.SEG_LINETO) { |
| 151 | + pointCount++; |
| 152 | + } |
| 153 | + it.next(); |
| 154 | + } |
| 155 | + return pointCount == 4; |
| 156 | + } |
| 157 | + |
| 158 | + private static boolean isTriangle(final Shape shape) { |
| 159 | + if (!(shape instanceof java.awt.geom.Path2D.Double)) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + final java.awt.geom.Path2D.Double path = (java.awt.geom.Path2D.Double) shape; |
| 163 | + final java.awt.geom.PathIterator it = path.getPathIterator(null); |
| 164 | + final double[] coords = new double[6]; |
| 165 | + int pointCount = 0; |
| 166 | + while (!it.isDone()) { |
| 167 | + final int type = it.currentSegment(coords); |
| 168 | + if (type == java.awt.geom.PathIterator.SEG_MOVETO || type == java.awt.geom.PathIterator.SEG_LINETO) { |
| 169 | + pointCount++; |
| 170 | + } |
| 171 | + it.next(); |
| 172 | + } |
| 173 | + return pointCount == 3; |
| 174 | + } |
| 175 | + |
| 176 | + private static boolean isStar(final Shape shape) { |
| 177 | + if (!(shape instanceof java.awt.geom.Path2D.Double)) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + final java.awt.geom.Path2D.Double path = (java.awt.geom.Path2D.Double) shape; |
| 181 | + final java.awt.geom.PathIterator it = path.getPathIterator(null); |
| 182 | + final double[] coords = new double[6]; |
| 183 | + int pointCount = 0; |
| 184 | + while (!it.isDone()) { |
| 185 | + final int type = it.currentSegment(coords); |
| 186 | + if (type == java.awt.geom.PathIterator.SEG_MOVETO || type == java.awt.geom.PathIterator.SEG_LINETO) { |
| 187 | + pointCount++; |
| 188 | + } |
| 189 | + it.next(); |
| 190 | + } |
| 191 | + return pointCount == 10; |
| 192 | + } |
| 193 | +} |
0 commit comments