次は、グラフの見かけに変化を加える。
figure_07a_shape_noFill_py.pyde
1 2 3 4 5 6 7 8 |
smooth() noFill() beginShape() vertex(10, 10) vertex(90, 30) vertex(40, 90) vertex(50, 40) endShape() |
figure_07b_shape_fill_py.pyde
1 2 3 4 5 6 7 |
smooth() beginShape() vertex(10, 10) vertex(90, 30) vertex(40, 90) vertex(50, 40) endShape() |
figure_07c_shape_close_py.pyde
1 2 3 4 5 6 7 |
smooth() beginShape() vertex(10, 10) vertex(90, 30) vertex(40, 90) vertex(50, 40) endShape(CLOSE) |
drawDataPoints(currentColumn)と、drawDataLine(currentColumn)を用いて、データポイントと繋ぐ線を別々に管理する。線は、以下の
1 2 3 |
beginShape() vertex(x, y) endShape() |
を用いて繋ぐ。
figure_08_draw_data_line_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 |
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 volumeIntervalMinor global volumeInterval 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 plotX1 global plotX2 global plotY1 global plotY2 global currentColumn background(224) #Show the plot area as a white box fill(255) rectMode(CORNERS) noStroke() rect(plotX1, plotY1, plotX2, plotY2) drawTitle() drawYearLabels() drawAxisLabels() drawDataPoints(currentColumn) drawVolumeLabels() strokeWeight(5) stroke('#5679C1') noFill() drawDataLine(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 years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 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(float(value), dataMin, dataMax, plotY2, plotY1) point(x, y) def drawTitle(): global plotX1 global plotY1 global currentColumn fill(0) textSize(20) textAlign(LEFT) title = data.getColumnName(currentColumn) text(title, plotX1, plotY1 - 10) def drawYearLabels(): global rowCount global data global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global yearInterval 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 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 |