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
|
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
|
-
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
+
+
-
+
-
+
-
-
+
-
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
+
+
|
/*
** WEBPAGE: test-piechart
**
** Generate a pie-chart based on data input from a form.
*/
void piechart_test_page(void){
const char *zData;
Stmt ins, q;
Stmt ins;
Blob all, line, token1, token2;
int n = 0;
int width;
int height;
int i, j;
login_check_credentials();
style_header("Pie Chart Test");
db_multi_exec("CREATE TEMP TABLE piechart(amt REAL, label TEXT);");
db_prepare(&ins, "INSERT INTO piechart(amt,label) VALUES(:amt,:label)");
zData = PD("data","");
width = atoi(PD("width","800"));
height = atoi(PD("height","400"));
blob_init(&all, zData, -1);
while( blob_line(&all, &line) ){
i = 0;
while( zData[i] ){
double rAmt;
char *zLabel;
while( fossil_isspace(zData[i]) ){ i++; }
j = i;
while( fossil_isdigit(zData[j]) ){ j++; }
if( zData[j]=='.' ){
j++;
while( fossil_isdigit(zData[j]) ){ j++; }
}
if( blob_token(&line, &token1)==0 ) continue;
rAmt = atof(blob_str(&token1));
if( i==j ) break;
rAmt = atof(&zData[i]);
if( rAmt<=0.0 ) continue;
blob_tail(&line, &token2);
i = j;
while( zData[i]==',' || fossil_isspace(zData[i]) ){ i++; }
n++;
zLabel = mprintf("label%02d-%g", n, rAmt);
db_bind_double(&ins, ":amt", rAmt);
db_bind_text(&ins, ":label", blob_str(&token2));
db_bind_text(&ins, ":label", zLabel);
db_step(&ins);
db_reset(&ins);
n++;
fossil_free(zLabel);
}
db_finalize(&ins);
blob_reset(&all);
if( n>0 ){
if( n>1 ){
@ <svg width=%d(width) height=%d(height) style="border:1px solid #d3d3d3;">
piechart_render(width,height, PIE_OTHER);
piechart_render(width,height, PIE_OTHER|PIE_PERCENT);
@ </svg>
@ <hr />
}
@ <form method="post" action='%R/test-piechart'>
@ <p>One slice per line. Value and then Label.<p>
@ <textarea name='data' rows='20' cols='80'>%h(zData)</textarea><br />
@ <form method="POST" action='%R/test-piechart'>
@ <p>Comma-separated list of slice widths:<br />
@ <input type='text' name='data' size='80' value='%h(zData)'/><br />
@ Width: <input type='text' size='8' name='width' value='%d(width)'/>
@ Height: <input type='text' size='8' name='height' value='%d(height)'/><br />
@ <input type='hidden' name='width' value='%d(width)'/>
@ <input type='hidden' name='height' value='%d(height)'/>
@ <input type='submit' value='Draw The Pie Chart'/>
@ </form>
@ <hr /><p>Previous Data:</p>
@ <table border="1">
db_prepare(&q, "SELECT rowid, amt, label FROM piechart");
@ <p>Interesting test cases:
@ <ul>
@ <li> <a href='test-piechart?data=44,2,2,2,2,2,3,2,2,2,2,2,44'>Case 1</a>
while( db_step(&q)==SQLITE_ROW ){
@ <tr><td>%d(db_column_int(&q,0))</td>
@ <td>%g(db_column_double(&q,1))</td>
@ <td>%h(db_column_text(&q,2))</td></tr>
}
db_finalize(&q);
@ </table>
@ <li> <a href='test-piechart?data=2,2,2,2,2,44,44,2,2,2,2,2'>Case 2</a>
@ </ul>
style_footer();
}
|