Fossil SCM

Simplification to the /test-piechart webpage. Include sample test cases.

drh 2016-08-24 15:14 trunk
Commit 8086dd96120bb684c093270d2995b871c09b21e6
1 file changed +30 -28
+30 -28
--- src/piechart.c
+++ src/piechart.c
@@ -267,60 +267,62 @@
267267
**
268268
** Generate a pie-chart based on data input from a form.
269269
*/
270270
void piechart_test_page(void){
271271
const char *zData;
272
- Stmt ins, q;
273
- Blob all, line, token1, token2;
272
+ Stmt ins;
274273
int n = 0;
275274
int width;
276275
int height;
276
+ int i, j;
277277
278278
login_check_credentials();
279279
style_header("Pie Chart Test");
280280
db_multi_exec("CREATE TEMP TABLE piechart(amt REAL, label TEXT);");
281281
db_prepare(&ins, "INSERT INTO piechart(amt,label) VALUES(:amt,:label)");
282282
zData = PD("data","");
283283
width = atoi(PD("width","800"));
284284
height = atoi(PD("height","400"));
285
- blob_init(&all, zData, -1);
286
- while( blob_line(&all, &line) ){
285
+ i = 0;
286
+ while( zData[i] ){
287287
double rAmt;
288
- if( blob_token(&line, &token1)==0 ) continue;
289
- rAmt = atof(blob_str(&token1));
290
- if( rAmt<=0.0 ) continue;
291
- blob_tail(&line, &token2);
288
+ char *zLabel;
289
+ while( fossil_isspace(zData[i]) ){ i++; }
290
+ j = i;
291
+ while( fossil_isdigit(zData[j]) ){ j++; }
292
+ if( zData[j]=='.' ){
293
+ j++;
294
+ while( fossil_isdigit(zData[j]) ){ j++; }
295
+ }
296
+ if( i==j ) break;
297
+ rAmt = atof(&zData[i]);
298
+ i = j;
299
+ while( zData[i]==',' || fossil_isspace(zData[i]) ){ i++; }
300
+ n++;
301
+ zLabel = mprintf("label%02d-%g", n, rAmt);
292302
db_bind_double(&ins, ":amt", rAmt);
293
- db_bind_text(&ins, ":label", blob_str(&token2));
303
+ db_bind_text(&ins, ":label", zLabel);
294304
db_step(&ins);
295305
db_reset(&ins);
296
- n++;
306
+ fossil_free(zLabel);
297307
}
298308
db_finalize(&ins);
299
- blob_reset(&all);
300
- if( n>0 ){
309
+ if( n>1 ){
301310
@ <svg width=%d(width) height=%d(height) style="border:1px solid #d3d3d3;">
302
- piechart_render(width,height, PIE_OTHER);
311
+ piechart_render(width,height, PIE_OTHER|PIE_PERCENT);
303312
@ </svg>
304313
@ <hr />
305314
}
306
- @ <form method="post" action='%R/test-piechart'>
307
- @ <p>One slice per line. Value and then Label.<p>
308
- @ <textarea name='data' rows='20' cols='80'>%h(zData)</textarea><br />
315
+ @ <form method="POST" action='%R/test-piechart'>
316
+ @ <p>Comma-separated list of slice widths:<br />
317
+ @ <input type='text' name='data' size='80' value='%h(zData)'/><br />
309318
@ Width: <input type='text' size='8' name='width' value='%d(width)'/>
310319
@ Height: <input type='text' size='8' name='height' value='%d(height)'/><br />
311
- @ <input type='hidden' name='width' value='%d(width)'/>
312
- @ <input type='hidden' name='height' value='%d(height)'/>
313320
@ <input type='submit' value='Draw The Pie Chart'/>
314321
@ </form>
315
- @ <hr /><p>Previous Data:</p>
316
- @ <table border="1">
317
- db_prepare(&q, "SELECT rowid, amt, label FROM piechart");
318
- while( db_step(&q)==SQLITE_ROW ){
319
- @ <tr><td>%d(db_column_int(&q,0))</td>
320
- @ <td>%g(db_column_double(&q,1))</td>
321
- @ <td>%h(db_column_text(&q,2))</td></tr>
322
- }
323
- db_finalize(&q);
324
- @ </table>
322
+ @ <p>Interesting test cases:
323
+ @ <ul>
324
+ @ <li> <a href='test-piechart?data=44,2,2,2,2,2,3,2,2,2,2,2,44'>Case 1</a>
325
+ @ <li> <a href='test-piechart?data=2,2,2,2,2,44,44,2,2,2,2,2'>Case 2</a>
326
+ @ </ul>
325327
style_footer();
326328
}
327329
--- src/piechart.c
+++ src/piechart.c
@@ -267,60 +267,62 @@
267 **
268 ** Generate a pie-chart based on data input from a form.
269 */
270 void piechart_test_page(void){
271 const char *zData;
272 Stmt ins, q;
273 Blob all, line, token1, token2;
274 int n = 0;
275 int width;
276 int height;
 
277
278 login_check_credentials();
279 style_header("Pie Chart Test");
280 db_multi_exec("CREATE TEMP TABLE piechart(amt REAL, label TEXT);");
281 db_prepare(&ins, "INSERT INTO piechart(amt,label) VALUES(:amt,:label)");
282 zData = PD("data","");
283 width = atoi(PD("width","800"));
284 height = atoi(PD("height","400"));
285 blob_init(&all, zData, -1);
286 while( blob_line(&all, &line) ){
287 double rAmt;
288 if( blob_token(&line, &token1)==0 ) continue;
289 rAmt = atof(blob_str(&token1));
290 if( rAmt<=0.0 ) continue;
291 blob_tail(&line, &token2);
 
 
 
 
 
 
 
 
 
 
292 db_bind_double(&ins, ":amt", rAmt);
293 db_bind_text(&ins, ":label", blob_str(&token2));
294 db_step(&ins);
295 db_reset(&ins);
296 n++;
297 }
298 db_finalize(&ins);
299 blob_reset(&all);
300 if( n>0 ){
301 @ <svg width=%d(width) height=%d(height) style="border:1px solid #d3d3d3;">
302 piechart_render(width,height, PIE_OTHER);
303 @ </svg>
304 @ <hr />
305 }
306 @ <form method="post" action='%R/test-piechart'>
307 @ <p>One slice per line. Value and then Label.<p>
308 @ <textarea name='data' rows='20' cols='80'>%h(zData)</textarea><br />
309 @ Width: <input type='text' size='8' name='width' value='%d(width)'/>
310 @ Height: <input type='text' size='8' name='height' value='%d(height)'/><br />
311 @ <input type='hidden' name='width' value='%d(width)'/>
312 @ <input type='hidden' name='height' value='%d(height)'/>
313 @ <input type='submit' value='Draw The Pie Chart'/>
314 @ </form>
315 @ <hr /><p>Previous Data:</p>
316 @ <table border="1">
317 db_prepare(&q, "SELECT rowid, amt, label FROM piechart");
318 while( db_step(&q)==SQLITE_ROW ){
319 @ <tr><td>%d(db_column_int(&q,0))</td>
320 @ <td>%g(db_column_double(&q,1))</td>
321 @ <td>%h(db_column_text(&q,2))</td></tr>
322 }
323 db_finalize(&q);
324 @ </table>
325 style_footer();
326 }
327
--- src/piechart.c
+++ src/piechart.c
@@ -267,60 +267,62 @@
267 **
268 ** Generate a pie-chart based on data input from a form.
269 */
270 void piechart_test_page(void){
271 const char *zData;
272 Stmt ins;
 
273 int n = 0;
274 int width;
275 int height;
276 int i, j;
277
278 login_check_credentials();
279 style_header("Pie Chart Test");
280 db_multi_exec("CREATE TEMP TABLE piechart(amt REAL, label TEXT);");
281 db_prepare(&ins, "INSERT INTO piechart(amt,label) VALUES(:amt,:label)");
282 zData = PD("data","");
283 width = atoi(PD("width","800"));
284 height = atoi(PD("height","400"));
285 i = 0;
286 while( zData[i] ){
287 double rAmt;
288 char *zLabel;
289 while( fossil_isspace(zData[i]) ){ i++; }
290 j = i;
291 while( fossil_isdigit(zData[j]) ){ j++; }
292 if( zData[j]=='.' ){
293 j++;
294 while( fossil_isdigit(zData[j]) ){ j++; }
295 }
296 if( i==j ) break;
297 rAmt = atof(&zData[i]);
298 i = j;
299 while( zData[i]==',' || fossil_isspace(zData[i]) ){ i++; }
300 n++;
301 zLabel = mprintf("label%02d-%g", n, rAmt);
302 db_bind_double(&ins, ":amt", rAmt);
303 db_bind_text(&ins, ":label", zLabel);
304 db_step(&ins);
305 db_reset(&ins);
306 fossil_free(zLabel);
307 }
308 db_finalize(&ins);
309 if( n>1 ){
 
310 @ <svg width=%d(width) height=%d(height) style="border:1px solid #d3d3d3;">
311 piechart_render(width,height, PIE_OTHER|PIE_PERCENT);
312 @ </svg>
313 @ <hr />
314 }
315 @ <form method="POST" action='%R/test-piechart'>
316 @ <p>Comma-separated list of slice widths:<br />
317 @ <input type='text' name='data' size='80' value='%h(zData)'/><br />
318 @ Width: <input type='text' size='8' name='width' value='%d(width)'/>
319 @ Height: <input type='text' size='8' name='height' value='%d(height)'/><br />
 
 
320 @ <input type='submit' value='Draw The Pie Chart'/>
321 @ </form>
322 @ <p>Interesting test cases:
323 @ <ul>
324 @ <li> <a href='test-piechart?data=44,2,2,2,2,2,3,2,2,2,2,2,44'>Case 1</a>
325 @ <li> <a href='test-piechart?data=2,2,2,2,2,44,44,2,2,2,2,2'>Case 2</a>
326 @ </ul>
 
 
 
 
 
327 style_footer();
328 }
329

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button