| 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"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 24 | #include "Permutation.h"cxx . . $ `<file>/dwsys/Permutation.h`/ |
| 25 | #include "Graphics_extensions.h"cxx . . $ `<file>/dwsys/Graphics_extensions.h`/ |
| 26 | |
| 27 | #include "enums_getText.h"cxx . . $ `<file>/melder/enums_getText.h`/ |
| 28 | #include "Graphics_extensions_enums.h"cxx . . $ `<file>/dwsys/Graphics_extensions_enums.h`/ |
| 29 | #include "enums_getValue.h"cxx . . $ `<file>/melder/enums_getValue.h`/ |
| 30 | #include "Graphics_extensions_enums.h"cxx . . $ `<file>/dwsys/Graphics_extensions_enums.h`/ |
| 31 | |
| 32 | double Graphics_getFontSizeInsideBoxcxx . . $ Graphics_getFontSizeInsideBox(58355cd5a6d131f). (Graphicscxx . . $ 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_dxWCtoMMcxx . . $ Graphics_dxWCtoMM(f042c24bcd87f07e). (g, widthWC) * pointsPerMillimetre; |
| 35 | const double boxHeight_points = Graphics_dyWCtoMMcxx . . $ Graphics_dyWCtoMM(f042c24bcd87f07e). (g, heightWCx) * pointsPerMillimetre; |
| 36 | return stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (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_boxAndWhiskerPlotcxx . . $ Graphics_boxAndWhiskerPlot(fda2f93778a3ff3e). (Graphicscxx . . $ Graphics# g, constVECcxx . . $ constVEC# data, double x, double r, double w, double ymin, double ymax) { |
| 46 | const int lineType = Graphics_inqLineTypecxx . . $ Graphics_inqLineType(d4bbe1b9daaaf11b). (g); |
| 47 | |
| 48 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (r > 0.0 && w > 0.0); |
| 49 | if (data.sizecxx . . $ constvector#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 | autoVECcxx . . $ autoVEC# sorted = sort_e_VECcxx . . $ sort_e_VEC(d4415e68b25283a8). (datacxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).); |
| 65 | |
| 66 | if (ymax <= ymin) { |
| 67 | ymin = sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 68 | ymax = sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).sorted.sizecxx . . $ autovector#size.]; |
| 69 | } |
| 70 | if (sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] > ymax || sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).sorted.sizecxx . . $ autovector#size.] < ymin) |
| 71 | return; |
| 72 | |
| 73 | const double mean = NUMmeancxx . . $ NUMmean(4bb9c3e3a8dd5442). (sortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 74 | const double q25 = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (sortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 0.25); |
| 75 | const double q50 = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (sortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 0.5); |
| 76 | const double q75 = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (sortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 0.75); |
| 77 | |
| 78 | const double hspread = fabscxx . . $ fabs(fee4f1d22bc8e617). (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 | integercxx . . $ integer# i = 1; |
| 88 | while (i <= sorted.sizecxx . . $ autovector#size. && sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] < ymin) |
| 89 | i ++; |
| 90 | Graphics_setTextAlignmentcxx . . $ Graphics_setTextAlignment(a697afee548943ae). (g, kGraphics_horizontalAlignmentcxx . . $ kGraphics_horizontalAlignment#::CENTREcxx . . $ kGraphics_horizontalAlignment#CENTRE., Graphics_HALFcxx . . $ `sys/Graphics.h:253:9`!); |
| 91 | while (i <= sorted.sizecxx . . $ autovector#size. && sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] < lowerOuterFence) |
| 92 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, x, sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i ++], U"o"); |
| 93 | |
| 94 | while (i <= sorted.sizecxx . . $ autovector#size. && sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] < lowerInnerFence) |
| 95 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, x, sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i ++], U"*"); |
| 96 | |
| 97 | const double lowerWhisker = sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] < q25 ? sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] : lowerInnerFence; |
| 98 | if (lowerWhisker > ymax) |
| 99 | return; |
| 100 | /* |
| 101 | Next process data from above. |
| 102 | */ |
| 103 | const integercxx . . $ integer# ilow = i; |
| 104 | i = sorted.sizecxx . . $ autovector#size.; |
| 105 | while (i >= ilow && sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] > ymax) |
| 106 | i --; |
| 107 | while (i >= ilow && sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] > upperOuterFence) |
| 108 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, x, sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i --], U"o"); |
| 109 | |
| 110 | while (i >= ilow && sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] > upperInnerFence) |
| 111 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, x, sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i --], U"*"); |
| 112 | |
| 113 | const double upperWhisker = sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] > q75 ? sorted [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).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_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x - r, y1, x + r, y1); |
| 124 | y1 = q25; |
| 125 | if (ymax > y1 && ymin < y1) |
| 126 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x - w, y1, x + w, y1); |
| 127 | y1 = q50; |
| 128 | if (ymax > y1 && ymin < y1) |
| 129 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x - w, y1, x + w, y1); |
| 130 | y1 = q75; |
| 131 | if (ymax > y1 && ymin < y1) |
| 132 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x - w, y1, x + w, y1); |
| 133 | y1 = upperWhisker; |
| 134 | if (ymax > y1 && ymin < y1) |
| 135 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (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_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (g, Graphics_DOTTEDcxx . . $ `sys/Graphics.h:288:9`!); |
| 142 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x - w, y1, x + w, y1); |
| 143 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (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_linecxx . . $ Graphics_line(72bdb7937dbb13da). (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_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x - w, y1, x - w, y2); |
| 161 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (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_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, x, y1, x, y2); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void Graphics_quantileQuantilePlotcxx . . $ Graphics_quantileQuantilePlot(e0f45ab49ff29757). (Graphicscxx . . $ Graphics# g, integercxx . . $ integer# numberOfQuantiles, constVECcxx . . $ constVEC# x, constVECcxx . . $ constVEC# y, |
| 173 | double xmin, double xmax, double ymin, double ymax, double labelSize, conststring32cxx . . $ conststring32# plotLabel) { |
| 174 | const double fontSize = Graphics_inqFontSizecxx . . $ Graphics_inqFontSize(f906975acce41a59). (g); |
| 175 | |
| 176 | Graphics_setTextAlignmentcxx . . $ Graphics_setTextAlignment(a697afee548943ae). (g, kGraphics_horizontalAlignmentcxx . . $ kGraphics_horizontalAlignment#::CENTREcxx . . $ kGraphics_horizontalAlignment#CENTRE., Graphics_HALFcxx . . $ `sys/Graphics.h:253:9`!); |
| 177 | Graphics_setFontSizecxx . . $ Graphics_setFontSize(2514a1c07b6be5e2). (g, labelSize); |
| 178 | autoVECcxx . . $ autoVEC# xsorted = sort_e_VECcxx . . $ sort_e_VEC(d4415e68b25283a8). (xcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).); |
| 179 | autoVECcxx . . $ autoVEC# ysorted = sort_e_VECcxx . . $ sort_e_VEC(d4415e68b25283a8). (ycxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).); |
| 180 | |
| 181 | const integercxx . . $ integer# numberOfData = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (x.sizecxx . . $ constvector#size., y.sizecxx . . $ constvector#size.); |
| 182 | numberOfQuantiles = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (numberOfData, numberOfQuantiles); |
| 183 | const double un = powcxx . . $ pow(b71928d39fde4295). (0.5, 1.0 / numberOfQuantiles); |
| 184 | const double u1 = 1.0 - un; |
| 185 | if (xmin == xmax) { |
| 186 | xmin = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (xsortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), u1); |
| 187 | xmax = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (xsortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), un); |
| 188 | } |
| 189 | if (ymin == ymax) { |
| 190 | ymin = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (ysortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), u1); |
| 191 | ymax = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (ysortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), un); |
| 192 | } |
| 193 | for (integercxx . . $ 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 = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (xsortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), ui); |
| 196 | const double qy = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (ysortedcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), ui); |
| 197 | if (qx < xmin || qx > xmax || qy < ymin || qy > ymax) |
| 198 | continue; // outside area |
| 199 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, qx, qy, plotLabel); |
| 200 | } |
| 201 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (g, Graphics_DOTTEDcxx . . $ `sys/Graphics.h:288:9`!); |
| 202 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, xmin, ymin, xmax, ymax); |
| 203 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (g, Graphics_DRAWNcxx . . $ `sys/Graphics.h:287:9`!); |
| 204 | Graphics_setFontSizecxx . . $ Graphics_setFontSize(2514a1c07b6be5e2). (g, fontSize); |
| 205 | } |
| 206 | |
| 207 | void Graphics_lagPlotcxx . . $ Graphics_lagPlot(3997c7ea73d0a510). (Graphicscxx . . $ Graphics# g, constVECcxx . . $ constVEC# data, double xmin, double xmax, integercxx . . $ integer# lag, double labelSize, conststring32cxx . . $ conststring32# plotLabel) |
| 208 | { |
| 209 | if (lag < 0 || lag >= data.sizecxx . . $ constvector#size.) |
| 210 | return; |
| 211 | const double fontSize = Graphics_inqFontSizecxx . . $ Graphics_inqFontSize(f906975acce41a59). (g); |
| 212 | Graphics_setFontSizecxx . . $ Graphics_setFontSize(2514a1c07b6be5e2). (g, labelSize); |
| 213 | Graphics_setTextAlignmentcxx . . $ Graphics_setTextAlignment(a697afee548943ae). (g, kGraphics_horizontalAlignmentcxx . . $ kGraphics_horizontalAlignment#::CENTREcxx . . $ kGraphics_horizontalAlignment#CENTRE., Graphics_HALFcxx . . $ `sys/Graphics.h:253:9`!); |
| 214 | /* |
| 215 | Plot x [i] vertically and x [i-lag] horizontally |
| 216 | */ |
| 217 | for (integercxx . . $ integer# i = 1; i <= data.sizecxx . . $ constvector#size. - lag; i ++) { |
| 218 | const double x = data [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + lag], y = data [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]; |
| 219 | if (x >= xmin && x <= xmax && y >= xmin && y <= xmax) |
| 220 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, x, y, plotLabel); |
| 221 | } |
| 222 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (g, Graphics_DRAWNcxx . . $ `sys/Graphics.h:287:9`!); |
| 223 | Graphics_setFontSizecxx . . $ Graphics_setFontSize(2514a1c07b6be5e2). (g, fontSize); |
| 224 | } |
| 225 | |
| 226 | void getGridLayoutcxx . . $ getGridLayout(b76a3bd1263aa692). (integercxx . . $ integer# numberOfItems, integercxx . . $ integer# *out_numberOfRows, integercxx . . $ integer# *out_numberOfColumns) { |
| 227 | integercxx . . $ integer# numberOfColumns = sqrtcxx . . $ sqrt(7759b11ec49fe502). (numberOfItems); |
| 228 | integercxx . . $ integer# numberOfRows = 1 + Melder_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). ((numberOfItems - 1) / numberOfColumns); |
| 229 | if (numberOfColumns > numberOfRows) |
| 230 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (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 | integercxx . . $ integer# getGridCellIndexcxx . . $ getGridCellIndex(eeaa97481e11fa24). (double x, double y, integercxx . . $ integer# numberOfRows, integercxx . . $ integer# numberOfColumns) { |
| 243 | const integercxx . . $ integer# icol = 1 + (int) (x * numberOfColumns); |
| 244 | if (icol < 1 || icol > numberOfColumns) |
| 245 | return 0; |
| 246 | const integercxx . . $ 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 */ |