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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/*
* Smithsonian Astrophysical Observatory, Cambridge, MA, USA
* This code has been modified under the terms listed below and is made
* available under the same terms.
*/
/*
* Copyright 1991-2004 George A Howlett.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "bltGraphBar.h"
#include "bltGraphOp.h"
#include "bltGrAxis.h"
#include "bltGrXAxisOp.h"
#include "bltGrPen.h"
#include "bltGrPenOp.h"
#include "bltGrPenBar.h"
#include "bltGrPenLine.h"
#include "bltGrElem.h"
#include "bltGrElemOp.h"
#include "bltGrElemBar.h"
#include "bltGrElemLine.h"
#include "bltGrMarker.h"
#include "bltGrLegd.h"
#include "bltGrHairs.h"
#include "bltGrDef.h"
using namespace Blt;
// BarGroup
BarGroup::BarGroup()
{
nSegments =0;
xAxis =NULL;
yAxis =NULL;
sum =0;
count =0;
lastY =0;
index =0;
}
// BarGraph
static const char* barmodeObjOption[] =
{"normal", "stacked", "aligned", "overlap", NULL};
static const char* searchModeObjOption[] = {"points", "traces", "auto", NULL};
static const char* searchAlongObjOption[] = {"x", "y", "both", NULL};
static Tk_OptionSpec optionSpecs[] = {
{TK_OPTION_DOUBLE, "-aspect", "aspect", "Aspect",
"0", -1, Tk_Offset(BarGraphOptions, aspect), 0, NULL, RESET},
{TK_OPTION_BORDER, "-background", "background", "Background",
STD_NORMAL_BACKGROUND, -1, Tk_Offset(BarGraphOptions, normalBg),
0, NULL, CACHE},
{TK_OPTION_STRING_TABLE, "-barmode", "barMode", "BarMode",
"normal", -1, Tk_Offset(BarGraphOptions, barMode),
0, &barmodeObjOption, RESET},
{TK_OPTION_DOUBLE, "-barwidth", "barWidth", "BarWidth",
".9", -1, Tk_Offset(BarGraphOptions, barWidth), 0, NULL, RESET},
{TK_OPTION_DOUBLE, "-baseline", "baseline", "Baseline",
"0", -1, Tk_Offset(BarGraphOptions, baseline), 0, NULL, RESET},
{TK_OPTION_SYNONYM, "-bd", NULL, NULL, NULL, -1, 0, 0, "-borderwidth", 0},
{TK_OPTION_SYNONYM, "-bg", NULL, NULL, NULL, -1, 0, 0, "-background", 0},
{TK_OPTION_SYNONYM, "-bm", NULL, NULL, NULL, -1, 0, 0, "-bottommargin", 0},
{TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
STD_BORDERWIDTH, -1, Tk_Offset(BarGraphOptions, borderWidth),
0, NULL, RESET},
{TK_OPTION_PIXELS, "-bottommargin", "bottomMargin", "BottomMargin",
"0", -1, Tk_Offset(BarGraphOptions, bottomMargin.reqSize), 0, NULL, RESET},
{TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
"crosshair", -1, Tk_Offset(BarGraphOptions, cursor),
TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_SYNONYM, "-fg", NULL, NULL, NULL, -1, 0, 0, "-foreground", 0},
{TK_OPTION_FONT, "-font", "font", "Font",
STD_FONT_MEDIUM, -1, Tk_Offset(BarGraphOptions, titleTextStyle.font),
0, NULL, RESET},
{TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
STD_NORMAL_FOREGROUND, -1, Tk_Offset(BarGraphOptions, titleTextStyle.color),
0, NULL, CACHE},
{TK_OPTION_SYNONYM, "-halo", NULL, NULL, NULL, -1, 0, 0, "-searchhalo", 0},
{TK_OPTION_PIXELS, "-height", "height", "Height",
"4i", -1, Tk_Offset(BarGraphOptions, reqHeight), 0, NULL, RESET},
{TK_OPTION_COLOR, "-highlightbackground", "highlightBackground",
"HighlightBackground",
STD_NORMAL_BACKGROUND, -1, Tk_Offset(BarGraphOptions, highlightBgColor),
0, NULL, CACHE},
{TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
STD_NORMAL_FOREGROUND, -1, Tk_Offset(BarGraphOptions, highlightColor),
0, NULL, CACHE},
{TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
"HighlightThickness",
"2", -1, Tk_Offset(BarGraphOptions, highlightWidth), 0, NULL, RESET},
{TK_OPTION_BOOLEAN, "-invertxy", "invertXY", "InvertXY",
"no", -1, Tk_Offset(BarGraphOptions, inverted), 0, NULL, RESET},
{TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
"center", -1, Tk_Offset(BarGraphOptions, titleTextStyle.justify),
0, NULL, RESET},
{TK_OPTION_PIXELS, "-leftmargin", "leftMargin", "Margin",
"0", -1, Tk_Offset(BarGraphOptions, leftMargin.reqSize), 0, NULL, RESET},
{TK_OPTION_SYNONYM, "-lm", NULL, NULL, NULL, -1, 0, 0, "-leftmargin", 0},
{TK_OPTION_BORDER, "-plotbackground", "plotbackground", "PlotBackground",
STD_NORMAL_BACKGROUND, -1, Tk_Offset(BarGraphOptions, plotBg),
0, NULL, CACHE},
{TK_OPTION_PIXELS, "-plotborderwidth", "plotBorderWidth", "PlotBorderWidth",
STD_BORDERWIDTH, -1, Tk_Offset(BarGraphOptions, plotBW), 0, NULL, RESET},
{TK_OPTION_PIXELS, "-plotpadx", "plotPadX", "PlotPad",
"0", -1, Tk_Offset(BarGraphOptions, xPad), 0, NULL, RESET},
{TK_OPTION_PIXELS, "-plotpady", "plotPadY", "PlotPad",
"0", -1, Tk_Offset(BarGraphOptions, yPad), 0, NULL, RESET},
{TK_OPTION_RELIEF, "-plotrelief", "plotRelief", "Relief",
"flat", -1, Tk_Offset(BarGraphOptions, plotRelief), 0, NULL, RESET},
{TK_OPTION_RELIEF, "-relief", "relief", "Relief",
"flat", -1, Tk_Offset(BarGraphOptions, relief), 0, NULL, RESET},
{TK_OPTION_PIXELS, "-rightmargin", "rightMargin", "Margin",
"0", -1, Tk_Offset(BarGraphOptions, rightMargin.reqSize), 0, NULL, RESET},
{TK_OPTION_SYNONYM, "-rm", NULL, NULL, NULL, -1, 0, 0, "-rightmargin", 0},
{TK_OPTION_PIXELS, "-searchhalo", "searchhalo", "SearchHalo",
"2m", -1, Tk_Offset(BarGraphOptions, search.halo), 0, NULL, RESET},
{TK_OPTION_STRING_TABLE, "-searchmode", "searchMode", "SearchMode",
"points", -1, Tk_Offset(BarGraphOptions, search.mode),
0, &searchModeObjOption, 0},
{TK_OPTION_STRING_TABLE, "-searchalong", "searchAlong", "SearchAlong",
"both", -1, Tk_Offset(BarGraphOptions, search.along),
0, &searchAlongObjOption, 0},
{TK_OPTION_BOOLEAN, "-stackaxes", "stackAxes", "StackAxes",
"no", -1, Tk_Offset(BarGraphOptions, stackAxes), 0, NULL, RESET},
{TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
NULL, -1, Tk_Offset(BarGraphOptions, takeFocus),
TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_STRING, "-title", "title", "Title",
NULL, -1, Tk_Offset(BarGraphOptions, title), TK_OPTION_NULL_OK, NULL, RESET},
{TK_OPTION_SYNONYM, "-tm", NULL, NULL, NULL, -1, 0, 0, "-topmargin", 0},
{TK_OPTION_PIXELS, "-topmargin", "topMargin", "TopMargin",
"0", -1, Tk_Offset(BarGraphOptions, topMargin.reqSize), 0, NULL, RESET},
{TK_OPTION_PIXELS, "-width", "width", "Width",
"5i", -1, Tk_Offset(BarGraphOptions, reqWidth), 0, NULL, RESET},
{TK_OPTION_PIXELS, "-plotwidth", "plotWidth", "PlotWidth",
"0", -1, Tk_Offset(BarGraphOptions, reqPlotWidth), 0, NULL, RESET},
{TK_OPTION_PIXELS, "-plotheight", "plotHeight", "PlotHeight",
"0", -1, Tk_Offset(BarGraphOptions, reqPlotHeight), 0, NULL, RESET},
{TK_OPTION_END, NULL, NULL, NULL, NULL, -1, 0, 0, NULL, 0}
};
// Create
BarGraph::BarGraph(ClientData clientData, Tcl_Interp* interp,
int objc, Tcl_Obj* const objv[])
: Graph(clientData, interp, objc, objv)
{
ops_ = (BarGraphOptions*)calloc(1, sizeof(BarGraphOptions));
BarGraphOptions* ops = (BarGraphOptions*)ops_;
Tk_SetClass(tkwin_, "Barchart");
classId_ = CID_ELEM_BAR;
barGroups_ =NULL;
nBarGroups_ =0;
maxBarSetSize_ =0;
Tcl_InitHashTable(&setTable_, sizeof(BarSetKey)/sizeof(int));
ops->bottomMargin.site = MARGIN_BOTTOM;
ops->leftMargin.site = MARGIN_LEFT;
ops->topMargin.site = MARGIN_TOP;
ops->rightMargin.site = MARGIN_RIGHT;
ops->titleTextStyle.anchor = TK_ANCHOR_N;
ops->titleTextStyle.color =NULL;
ops->titleTextStyle.font =NULL;
ops->titleTextStyle.angle =0;
ops->titleTextStyle.justify =TK_JUSTIFY_LEFT;
optionTable_ = Tk_CreateOptionTable(interp_, optionSpecs);
if ((Tk_InitOptions(interp_, (char*)ops_, optionTable_, tkwin_) != TCL_OK) || (GraphObjConfigure(this, interp_, objc-2, objv+2) != TCL_OK)) {
valid_ =0;
return;
}
if (createPen("active", 0, NULL) != TCL_OK) {
valid_ =0;
return;
}
adjustAxes();
Tcl_SetStringObj(Tcl_GetObjResult(interp_), Tk_PathName(tkwin_), -1);
}
BarGraph::~BarGraph()
{
destroyBarSets();
}
int BarGraph::configure()
{
BarGraphOptions* ops = (BarGraphOptions*)ops_;
// Don't allow negative bar widths. Reset to an arbitrary value (0.1)
if (ops->barWidth <= 0.0f)
ops->barWidth = 0.9f;
return Graph::configure();
}
int BarGraph::createPen(const char* penName, int objc, Tcl_Obj* const objv[])
{
int isNew;
Tcl_HashEntry *hPtr =
Tcl_CreateHashEntry(&penTable_, penName, &isNew);
if (!isNew) {
Tcl_AppendResult(interp_, "pen \"", penName, "\" already exists in \"",
Tk_PathName(tkwin_), "\"", (char *)NULL);
return TCL_ERROR;
}
Pen* penPtr = new BarPen(this, penName, hPtr);
if (!penPtr)
return TCL_ERROR;
Tcl_SetHashValue(hPtr, penPtr);
if ((Tk_InitOptions(interp_, (char*)penPtr->ops(), penPtr->optionTable(), tkwin_) != TCL_OK) || (PenObjConfigure(this, penPtr, interp_, objc-4, objv+4) != TCL_OK)) {
delete penPtr;
return TCL_ERROR;
}
flags |= RESET;
eventuallyRedraw();
return TCL_OK;
}
int BarGraph::createElement(int objc, Tcl_Obj* const objv[])
{
char *name = Tcl_GetString(objv[3]);
if (name[0] == '-') {
Tcl_AppendResult(interp_, "name of element \"", name,
"\" can't start with a '-'", NULL);
return TCL_ERROR;
}
int isNew;
Tcl_HashEntry* hPtr =
Tcl_CreateHashEntry(&elements_.table, name, &isNew);
if (!isNew) {
Tcl_AppendResult(interp_, "element \"", name,
"\" already exists in \"", Tcl_GetString(objv[0]),
"\"", NULL);
return TCL_ERROR;
}
Element* elemPtr = new BarElement(this, name, hPtr);
if (!elemPtr)
return TCL_ERROR;
Tcl_SetHashValue(hPtr, elemPtr);
if ((Tk_InitOptions(interp_, (char*)elemPtr->ops(), elemPtr->optionTable(), tkwin_) != TCL_OK) || (ElementObjConfigure(elemPtr, interp_, objc-4, objv+4) != TCL_OK)) {
delete elemPtr;
return TCL_ERROR;
}
elemPtr->link = Blt_Chain_Append(elements_.displayList, elemPtr);
return TCL_OK;
}
void BarGraph::mapElements()
{
BarGraphOptions* ops = (BarGraphOptions*)ops_;
if ((BarMode)ops->barMode != INFRONT)
resetBarSets();
Graph::mapElements();
}
void BarGraph::resetAxes()
{
BarGraphOptions* ops = (BarGraphOptions*)ops_;
/* FIXME: This should be called whenever the display list of
* elements change. Maybe yet another flag INIT_STACKS to
* indicate that the element display list has changed.
* Needs to be done before the axis limits are set.
*/
initBarSets();
if (((BarMode)ops->barMode == STACKED) && (nBarGroups_ > 0))
computeBarStacks();
Graph::resetAxes();
}
void BarGraph::initBarSets()
{
BarGraphOptions* ops = (BarGraphOptions*)ops_;
// Free resources associated with a previous frequency table. This includes
// the array of frequency information and the table itself
destroyBarSets();
if ((BarMode)ops->barMode == INFRONT)
return;
// Initialize a hash table and fill it with unique abscissas. Keep track
// of the frequency of each x-coordinate and how many abscissas have
// duplicate mappings.
Tcl_HashTable setTable;
Tcl_InitHashTable(&setTable, sizeof(BarSetKey)/sizeof(int));
int nSegs =0;
for (Blt_ChainLink link = Blt_Chain_FirstLink(elements_.displayList);
link; link = Blt_Chain_NextLink(link)) {
BarElement* bePtr = (BarElement*)Blt_Chain_GetValue(link);
BarElementOptions* ops = (BarElementOptions*)bePtr->ops();
if (ops->hide)
continue;
nSegs++;
if (ops->coords.x) {
int nPoints = ops->coords.x->nValues;
double *x, *xend;
for (x = ops->coords.x->values, xend = x + nPoints; x < xend; x++) {
BarSetKey key;
key.value =*x;
key.xAxis =ops->xAxis;
key.yAxis =NULL;
int isNew;
Tcl_HashEntry* hhPtr =Tcl_CreateHashEntry(&setTable, &key, &isNew);
Tcl_HashTable* tablePtr;
if (isNew) {
tablePtr = (Tcl_HashTable*)malloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(tablePtr, TCL_STRING_KEYS);
Tcl_SetHashValue(hhPtr, tablePtr);
}
else
tablePtr = (Tcl_HashTable*)Tcl_GetHashValue(hhPtr);
const char* name = (ops->groupName) ?
ops->groupName : ops->yAxis->name_;
Tcl_HashEntry* hhPtr2 = Tcl_CreateHashEntry(tablePtr, name, &isNew);
size_t count =1;
if (!isNew) {
count = (size_t)Tcl_GetHashValue(hhPtr2);
count++;
}
Tcl_SetHashValue(hhPtr2, count);
}
}
}
// no bar elements to be displayed
if (setTable.numEntries == 0)
return;
int sum =0;
int max =0;
Tcl_HashSearch iter;
for (Tcl_HashEntry *hhPtr = Tcl_FirstHashEntry(&setTable, &iter); hhPtr;
hhPtr = Tcl_NextHashEntry(&iter)) {
BarSetKey* keyPtr = (BarSetKey*)Tcl_GetHashKey(&setTable, hhPtr);
Tcl_HashTable* tablePtr = (Tcl_HashTable*)Tcl_GetHashValue(hhPtr);
int isNew;
Tcl_HashEntry* hPtr = Tcl_CreateHashEntry(&setTable_, keyPtr, &isNew);
Tcl_SetHashValue(hPtr, tablePtr);
if (max < tablePtr->numEntries)
max = tablePtr->numEntries; // # of stacks in group
sum += tablePtr->numEntries;
}
Tcl_DeleteHashTable(&setTable);
if (sum > 0) {
barGroups_ = new BarGroup[sum];
BarGroup* groupPtr = barGroups_;
Tcl_HashSearch iter;
for (Tcl_HashEntry* hPtr = Tcl_FirstHashEntry(&setTable_, &iter);
hPtr; hPtr = Tcl_NextHashEntry(&iter)) {
BarSetKey* keyPtr = (BarSetKey*)Tcl_GetHashKey(&setTable_, hPtr);
Tcl_HashTable* tablePtr = (Tcl_HashTable*)Tcl_GetHashValue(hPtr);
size_t xcount = 0;
Tcl_HashSearch iter2;
for (Tcl_HashEntry *hPtr2 = Tcl_FirstHashEntry(tablePtr, &iter2);
hPtr2; hPtr2 = Tcl_NextHashEntry(&iter2)) {
size_t count = (size_t)Tcl_GetHashValue(hPtr2);
groupPtr->nSegments = count;
groupPtr->xAxis = keyPtr->xAxis;
groupPtr->yAxis = keyPtr->yAxis;
groupPtr->index = xcount++;
Tcl_SetHashValue(hPtr2, groupPtr);
groupPtr++;
}
}
}
maxBarSetSize_ = max;
nBarGroups_ = sum;
}
void BarGraph::destroyBarSets()
{
if (barGroups_) {
delete [] barGroups_;
barGroups_ = NULL;
}
nBarGroups_ = 0;
Tcl_HashSearch iter;
for (Tcl_HashEntry* hPtr=Tcl_FirstHashEntry(&setTable_, &iter); hPtr;
hPtr=Tcl_NextHashEntry(&iter)) {
Tcl_HashTable* tablePtr = (Tcl_HashTable*)Tcl_GetHashValue(hPtr);
Tcl_DeleteHashTable(tablePtr);
free(tablePtr);
}
Tcl_DeleteHashTable(&setTable_);
Tcl_InitHashTable(&setTable_, sizeof(BarSetKey)/sizeof(int));
}
void BarGraph::resetBarSets()
{
BarGroup *gp, *gend;
for (gp = barGroups_, gend = gp + nBarGroups_; gp < gend; gp++) {
gp->lastY = 0.0;
gp->count = 0;
}
}
void BarGraph::computeBarStacks()
{
BarGraphOptions* ops = (BarGraphOptions*)ops_;
Blt_ChainLink link;
if (((BarMode)ops->barMode != STACKED) || (nBarGroups_ == 0))
return;
// Initialize the stack sums to zero
BarGroup *gp, *gend;
for (gp = barGroups_, gend = gp + nBarGroups_; gp < gend; gp++)
gp->sum = 0.0;
// Consider each bar x-y coordinate. Add the ordinates of duplicate
// abscissas
for (link = Blt_Chain_FirstLink(elements_.displayList);
link; link = Blt_Chain_NextLink(link)) {
BarElement* bePtr = (BarElement*)Blt_Chain_GetValue(link);
BarElementOptions* ops = (BarElementOptions*)bePtr->ops();
if (ops->hide)
continue;
if (ops->coords.x && ops->coords.y) {
double *x, *y, *xend;
for (x = ops->coords.x->values, y = ops->coords.y->values,
xend = x + ops->coords.x->nValues; x < xend; x++, y++) {
BarSetKey key;
key.value =*x;
key.xAxis =ops->xAxis;
key.yAxis =NULL;
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&setTable_, &key);
if (!hPtr)
continue;
Tcl_HashTable *tablePtr = (Tcl_HashTable*)Tcl_GetHashValue(hPtr);
const char *name = (ops->groupName) ?
ops->groupName : ops->yAxis->name_;
hPtr = Tcl_FindHashEntry(tablePtr, name);
if (!hPtr)
continue;
BarGroup *groupPtr = (BarGroup*)Tcl_GetHashValue(hPtr);
groupPtr->sum += *y;
}
}
}
}
|