| 1 | /* Graphics_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012-2022 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 20120727 latest modification |
| 21 | */ |
| 22 | |
| 23 | #include "NUM2.h" |
| 24 | #include "Permutation.h" |
| 25 | #include "Graphics_extensions.h" |
| 26 | |
| 27 | #include "enums_getText.h" |
| 28 | #include "Graphics_extensions_enums.h" |
| 29 | #include "enums_getValue.h" |
| 30 | #include "Graphics_extensions_enums.h" |
| 31 | |
| 32 | double Graphics_getFontSizeInsideBox (Graphics g, double widthWC, double heightWCx, double maxNumberOfCharacters_line, double maxNumberOfLines) { |
| 33 | const double pointsPerMillimetre = 72.0 / (10.0 * 2.54); |
| 34 | const double boxWidth_points = Graphics_dxWCtoMM (g, widthWC) * pointsPerMillimetre; |
| 35 | const double boxHeight_points = Graphics_dyWCtoMM (g, heightWCx) * pointsPerMillimetre; |
| 36 | return std::min (boxHeight_points / maxNumberOfLines , 2.0 * boxWidth_points / maxNumberOfCharacters_line); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | Draw a box plot of x [1..x.size]. The vertical center line of the plot |
| 41 | is at position 'x'. The rectangle box is 2*w wide, the whisker 2*r. |
| 42 | All drawing outside [ymin, ymax] is clipped. |
| 43 | */ |
| 44 | |
| 45 | void Graphics_boxAndWhiskerPlot (Graphics g, constVEC data, double x, double r, double w, double ymin, double ymax) { |
| 46 | const int lineType = Graphics_inqLineType (g); |
| 47 | |
| 48 | Melder_assert (r > 0.0 && w > 0.0); |
| 49 | if (data.size < 3) |
| 50 | return; |
| 51 | /* |
| 52 | Sort the data (ascending: x [1] <= ... <= x [ndata]). |
| 53 | Get the median (q50) and the upper and lower quartile points |
| 54 | (q25 and q75). |
| 55 | Now q25 and q75 are the lower and upper hinges, respectively. |
| 56 | The fences can be calculated from q25 and q75. |
| 57 | The spread is defined as the interquartile range or midrange |
| 58 | |q75 - q25|. |
| 59 | The fences are defined as: |
| 60 | (lower/upper) innerfence = (lower/upper) hinge +/- 1.5 hspread |
| 61 | (lower/upper) outerfence = (lower/upper) hinge +/- 3.0 hspread |
| 62 | */ |
| 63 | |
| 64 | autoVEC sorted = sort_e_VEC (data); |
| 65 | |
| 66 | if (ymax <= ymin) { |
| 67 | ymin = sorted [1]; |
| 68 | ymax = sorted [sorted.size]; |
| 69 | } |
| 70 | if (sorted [1] > ymax || sorted [sorted.size] < ymin) |
| 71 | return; |
| 72 | |
| 73 | const double mean = NUMmean (sorted.get()); |
| 74 | const double q25 = NUMquantile (sorted.get(), 0.25); |
| 75 | const double q50 = NUMquantile (sorted.get(), 0.5); |
| 76 | const double q75 = NUMquantile (sorted.get(), 0.75); |
| 77 | |
| 78 | const double hspread = fabs (q75 - q25); |
| 79 | const double lowerOuterFence = q25 - 3.0 * hspread; |
| 80 | const double lowerInnerFence = q25 - 1.5 * hspread; |
| 81 | const double upperInnerFence = q75 + 1.5 * hspread; |
| 82 | const double upperOuterFence = q75 + 3.0 * hspread; |
| 83 | /* |
| 84 | Decide whether there are outliers that have to be drawn. |
| 85 | First process sorted from below. |
| 86 | */ |
| 87 | integer i = 1; |
| 88 | while (i <= sorted.size && sorted [i] < ymin) |
| 89 | i ++; |
| 90 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 91 | while (i <= sorted.size && sorted [i] < lowerOuterFence) |
| 92 | Graphics_text (g, x, sorted [i ++], U"o"); |
| 93 | |
| 94 | while (i <= sorted.size && sorted [i] < lowerInnerFence) |
| 95 | Graphics_text (g, x, sorted [i ++], U"*"); |
| 96 | |
| 97 | const double lowerWhisker = sorted [i] < q25 ? sorted [i] : lowerInnerFence; |
| 98 | if (lowerWhisker > ymax) |
| 99 | return; |
| 100 | /* |
| 101 | Next process data from above. |
| 102 | */ |
| 103 | const integer ilow = i; |
| 104 | i = sorted.size; |
| 105 | while (i >= ilow && sorted [i] > ymax) |
| 106 | i --; |
| 107 | while (i >= ilow && sorted [i] > upperOuterFence) |
| 108 | Graphics_text (g, x, sorted [i --], U"o"); |
| 109 | |
| 110 | while (i >= ilow && sorted [i] > upperInnerFence) |
| 111 | Graphics_text (g, x, sorted [i --], U"*"); |
| 112 | |
| 113 | const double upperWhisker = sorted [i] > q75 ? sorted [i] : upperInnerFence; |
| 114 | if (upperWhisker < ymin) |
| 115 | return; |
| 116 | /* |
| 117 | Determine what parts of the "box" have to be drawn within the |
| 118 | range [ymin, ymax]. |
| 119 | Horizontal lines first. |
| 120 | */ |
| 121 | double y1 = lowerWhisker; |
| 122 | if (ymax > y1 && ymin < y1) |
| 123 | Graphics_line (g, x - r, y1, x + r, y1); |
| 124 | y1 = q25; |
| 125 | if (ymax > y1 && ymin < y1) |
| 126 | Graphics_line (g, x - w, y1, x + w, y1); |
| 127 | y1 = q50; |
| 128 | if (ymax > y1 && ymin < y1) |
| 129 | Graphics_line (g, x - w, y1, x + w, y1); |
| 130 | y1 = q75; |
| 131 | if (ymax > y1 && ymin < y1) |
| 132 | Graphics_line (g, x - w, y1, x + w, y1); |
| 133 | y1 = upperWhisker; |
| 134 | if (ymax > y1 && ymin < y1) |
| 135 | Graphics_line (g, x - r, y1, x + r, y1); |
| 136 | /* |
| 137 | Extension: draw the mean too. |
| 138 | */ |
| 139 | y1 = mean; |
| 140 | if (ymax > y1 && ymin < y1) { |
| 141 | Graphics_setLineType (g, Graphics_DOTTED); |
| 142 | Graphics_line (g, x - w, y1, x + w, y1); |
| 143 | Graphics_setLineType (g, lineType); |
| 144 | } |
| 145 | /* |
| 146 | Vertical lines. |
| 147 | */ |
| 148 | y1 = lowerWhisker; |
| 149 | double y2 = q25; |
| 150 | if (ymax > y1 && ymin < y2) { |
| 151 | y1 = y1 > ymin ? y1 : ymin; |
| 152 | y2 = y2 < ymax ? y2 : ymax; |
| 153 | Graphics_line (g, x, y1, x, y2); |
| 154 | } |
| 155 | y1 = q25; |
| 156 | y2 = q75; |
| 157 | if (ymax > y1 && ymin < y2) { |
| 158 | y1 = y1 > ymin ? y1 : ymin; |
| 159 | y2 = y2 < ymax ? y2 : ymax; |
| 160 | Graphics_line (g, x - w, y1, x - w, y2); |
| 161 | Graphics_line (g, x + w, y1, x + w, y2); |
| 162 | } |
| 163 | y1 = q75; |
| 164 | y2 = upperWhisker; |
| 165 | if (ymax > y1 && ymin < y2) { |
| 166 | y1 = y1 > ymin ? y1 : ymin; |
| 167 | y2 = y2 < ymax ? y2 : ymax; |
| 168 | Graphics_line (g, x, y1, x, y2); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void Graphics_quantileQuantilePlot (Graphics g, integer numberOfQuantiles, constVEC x, constVEC y, |
| 173 | double xmin, double xmax, double ymin, double ymax, double labelSize, conststring32 plotLabel) { |
| 174 | const double fontSize = Graphics_inqFontSize (g); |
| 175 | |
| 176 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 177 | Graphics_setFontSize (g, labelSize); |
| 178 | autoVEC xsorted = sort_e_VEC (x); |
| 179 | autoVEC ysorted = sort_e_VEC (y); |
| 180 | |
| 181 | const integer numberOfData = std::min (x.size, y.size); |
| 182 | numberOfQuantiles = std::min (numberOfData, numberOfQuantiles); |
| 183 | const double un = pow (0.5, 1.0 / numberOfQuantiles); |
| 184 | const double u1 = 1.0 - un; |
| 185 | if (xmin == xmax) { |
| 186 | xmin = NUMquantile (xsorted.get(), u1); |
| 187 | xmax = NUMquantile (xsorted.get(), un); |
| 188 | } |
| 189 | if (ymin == ymax) { |
| 190 | ymin = NUMquantile (ysorted.get(), u1); |
| 191 | ymax = NUMquantile (ysorted.get(), un); |
| 192 | } |
| 193 | for (integer i = 1; i <= numberOfQuantiles; i++) { |
| 194 | const double ui = i == 1 ? u1 : (i == numberOfQuantiles ? un : (i - 0.3175) / (numberOfQuantiles + 0.365)); |
| 195 | const double qx = NUMquantile (xsorted.get(), ui); |
| 196 | const double qy = NUMquantile (ysorted.get(), ui); |
| 197 | if (qx < xmin || qx > xmax || qy < ymin || qy > ymax) |
| 198 | continue; // outside area |
| 199 | Graphics_text (g, qx, qy, plotLabel); |
| 200 | } |
| 201 | Graphics_setLineType (g, Graphics_DOTTED); |
| 202 | Graphics_line (g, xmin, ymin, xmax, ymax); |
| 203 | Graphics_setLineType (g, Graphics_DRAWN); |
| 204 | Graphics_setFontSize (g, fontSize); |
| 205 | } |
| 206 | |
| 207 | void Graphics_lagPlot (Graphics g, constVEC data, double xmin, double xmax, integer lag, double labelSize, conststring32 plotLabel) |
| 208 | { |
| 209 | if (lag < 0 || lag >= data.size) |
| 210 | return; |
| 211 | const double fontSize = Graphics_inqFontSize (g); |
| 212 | Graphics_setFontSize (g, labelSize); |
| 213 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 214 | /* |
| 215 | Plot x [i] vertically and x [i-lag] horizontally |
| 216 | */ |
| 217 | for (integer i = 1; i <= data.size - lag; i ++) { |
| 218 | const double x = data [i + lag], y = data [i]; |
| 219 | if (x >= xmin && x <= xmax && y >= xmin && y <= xmax) |
| 220 | Graphics_text (g, x, y, plotLabel); |
| 221 | } |
| 222 | Graphics_setLineType (g, Graphics_DRAWN); |
| 223 | Graphics_setFontSize (g, fontSize); |
| 224 | } |
| 225 | |
| 226 | void getGridLayout (integer numberOfItems, integer *out_numberOfRows, integer *out_numberOfColumns) { |
| 227 | integer numberOfColumns = sqrt (numberOfItems); |
| 228 | integer numberOfRows = 1 + Melder_roundDown ((numberOfItems - 1) / numberOfColumns); |
| 229 | if (numberOfColumns > numberOfRows) |
| 230 | std::swap (numberOfRows, numberOfColumns); |
| 231 | if (out_numberOfRows) |
| 232 | *out_numberOfRows = numberOfRows; |
| 233 | if (out_numberOfColumns) |
| 234 | *out_numberOfColumns = numberOfColumns; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | Implicit: |
| 239 | Rectangular grid. |
| 240 | Origin (0,0) is at left-bottom, top-right is at (1, 1) |
| 241 | */ |
| 242 | integer getGridCellIndex (double x, double y, integer numberOfRows, integer numberOfColumns) { |
| 243 | const integer icol = 1 + (int) (x * numberOfColumns); |
| 244 | if (icol < 1 || icol > numberOfColumns) |
| 245 | return 0; |
| 246 | const integer irow = 1 + (int) ((1.0 - y) * numberOfRows); |
| 247 | if (irow < 1 || irow > numberOfRows) |
| 248 | return 0; |
| 249 | return (irow - 1) * numberOfColumns + icol; // left-to-right, top-to-bottom |
| 250 | } |
| 251 | |
| 252 | /* End of file Graphics_extensions.cpp */ |