データエリアを塗りつぶすために、drawDataArea()を作成する。
drawDataArea関数内では、beginShape()とendShape(CLOSE)で挟んで、vertex(x, y)で点をつないで行けばよい。
1 2 3 4 5 6 7 8 9 10 |
beginShape() for row in range(rowCount): if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) vertex(x, y) vertex(plotX2, plotY2) vertex(plotX1, plotY2) endShape(CLOSE) |
figure_12_area_py.pyde
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
import FloatTable width = 720 height = 405 dataMin = 0 dataMax = 0 #Corners of the plotted time series plotX1 = 120 plotX2 = width - 80 labelX = 50 plotY1 = 60 plotY2 = height - 70 labelY = height - 25 currentColumn = 0 yearInterval = 10 volumeInterval = 10 volumeIntervalMinor = 5 years =[] value =[] def setup(): global data global dataMin global dataMax global value global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY global currentColumn global columnCount global rowCount global yearInterval global volumeInterval global volumeIntervalMinor size(width, height) data = FloatTable.FloatTable("milk-tea-coffee.tsv") columnCount = data.getColumnCount(); years_str = data.getRowNames() for i in range(len(years_str)): years.append(int(years_str[i])) print "years=:" print years yearMin = years[0] yearMax = years[len(years) - 1] dataMax = ceil(data.getTableMax() / volumeInterval) * volumeInterval plotFont = createFont("SansSerif", 20) textFont(plotFont) smooth() def draw(): global currentColumn global plotX1 global plotX2 global plotY1 global plotY2 background(224) #Show the plot area as a white box fill(255) rectMode(CORNERS) noStroke() rect(plotX1, plotY1, plotX2, plotY2) drawTitle() drawAxisLabels() drawYearLabels() drawVolumeLabels() fill('#5679C1') drawDataArea(currentColumn) def drawDataLine(col): global rowCount global data global dataMin global dataMax global value global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 beginShape() for row in range(rowCount): if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) vertex(x, y) endShape() def drawDataPoints(col): global rowCount global data global dataMin global dataMax global value global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 #dataMin = 0 #dataMax = data.getTableMax() #dataMax = 100 rowCount = data.getRowCount() for row in range(rowCount): #if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) point(x, y) def drawTitle(): global currentColumn global plotX1 global plotY1 fill(0) textSize(20) textAlign(LEFT) title = data.getColumnName(currentColumn) text(title, plotX1, plotY1 - 10) def drawYearLabels(): global rowCount global data global yearInterval global plotX1 global plotX2 global plotY1 global plotY2 global years global yearMin global yearMax fill(0) textSize(10) textAlign(CENTER, TOP) stroke(224) strokeWeight(1) rowCount = data.getRowCount() for row in range(rowCount): if (years[row] % yearInterval == 0): x = map(years[row], yearMin, yearMax, plotX1, plotX2) text(years[row], x, plotY2 + textAscent()+ 10) line(x, plotY1, x, plotY2) def drawVolumeLabels(): global dataMin global dataMax global plotX1 global plotX2 global plotY1 global plotY2 global volumeInterval global volumeIntervalMinor fill(0) textSize(10) textAlign(RIGHT) stroke(128) strokeWeight(1) for v in range(dataMin, dataMax+1, volumeIntervalMinor): if (v % volumeIntervalMinor == 0): y = map(v, dataMin, dataMax, plotY2, plotY1) if (v % volumeInterval == 0): if (v == dataMin): textAlign(RIGHT) elif (v == dataMax): textAlign(RIGHT, TOP) else: textAlign(RIGHT, CENTER) text(floor(v), plotX1 - 10, y) line(plotX1 - 4, y, plotX1, y) else: line(plotX1 - 2, y, plotX1, y) def drawAxisLabels(): global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY fill(0) textSize(13) textLeading(15) textAlign(CENTER, CENTER) text("Gallons\nconsumed\nper capita", labelX, (plotY1+plotY2)/2) textAlign(CENTER) text("Year", (plotX1+plotX2)/2, labelY) def drawDataHighlight(col): global rowCount global data global dataMin global dataMax global value global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 for row in range(rowCount): if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) if (dist(mouseX, mouseY, x, y) < 3): strokeWeight(10) point(x, y) fill(0) textSize(10) textAlign(CENTER) text(nf(float(value), 0, 2), x, y-8) text(years[row], x + 35, y-8) textAlign(LEFT) def drawDataCurve(col): global rowCount global dataMin global dataMax global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 beginShape(); for row in range(rowCount): if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) curveVertex(x, y) if ((row == 0) or (row == rowCount-1)): curveVertex(x, y) endShape() def drawDataArea(col): global rowCount global dataMin global dataMax global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 noStroke() beginShape() for row in range(rowCount): if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) vertex(x, y) vertex(plotX2, plotY2) vertex(plotX1, plotY2) endShape(CLOSE) def keyPressed(): global currentColumn global columnCount if (key == '['): currentColumn = currentColumn - 1 if (currentColumn < 0): currentColumn = columnCount - 1 elif (key == ']'): currentColumn = currentColumn + 1 if (currentColumn == columnCount): currentColumn = 0 |