Debian Codegraph @

generic/bltGrAxis.c

raw symbols ◀ ▶ SCIP
4 Debian patches touch this file
1
2 /*
3 * bltGrAxis.c --
4 *
5 * This module implements coordinate axes for the BLT graph widget.
6 *
7 * Copyright 1993-1998 Lucent Technologies, Inc.
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that the above copyright notice appear in all
12 * copies and that both that the copyright notice and warranty
13 * disclaimer appear in supporting documentation, and that the names
14 * of Lucent Technologies any of their entities not be used in
15 * advertising or publicity pertaining to distribution of the software
16 * without specific, written prior permission.
17 *
18 * Lucent Technologies disclaims all warranties with regard to this
19 * software, including all implied warranties of merchantability and
20 * fitness. In no event shall Lucent Technologies be liable for any
21 * special, indirect or consequential damages or any damages
22 * whatsoever resulting from loss of use, data or profits, whether in
23 * an action of contract, negligence or other tortuous action, arising
24 * out of or in connection with the use or performance of this
25 * software.
26 */
27
28 #include "bltGraph.h"
29 #include "bltGrElem.h"
30 #include <X11/Xutil.h>
31
32 #define DEF_NUM_TICKS 4 /* Each minor tick is 20% */
33 #define STATIC_TICK_SPACE 10
34
35 #define TICK_LABEL_SIZE 200
36 #define MAXTICKS 10001
37
38 #define CLAMP(val,low,high) \
39 (((val) < (low)) ? (low) : ((val) > (high)) ? (high) : (val))
40
41 /*
42 * Round x in terms of units
43 */
44 #define UROUND(x,u) (Round((x)/(u))*(u))
45 #define UCEIL(x,u) (ceil((x)/(u))*(u))
46 #define UFLOOR(x,u) (floor((x)/(u))*(u))
47
48 #define LENGTH_MAJOR_TICK 0.030 /* Length of a major tick */
49 #define LENGTH_MINOR_TICK 0.015 /* Length of a minor (sub)tick */
50 #define LENGTH_LABEL_TICK 0.040 /* Distance from graph to start of the
51 * label */
52 #define NUMDIGITS 15 /* Specifies the number of
53 * digits of accuracy used when
54 * outputting axis tick labels. */
55 #define AVG_TICK_NUM_CHARS 16 /* Assumed average tick label size */
56
57 #define TICK_RANGE_TIGHT 0
58 #define TICK_RANGE_LOOSE 1
59 #define TICK_RANGE_ALWAYS_LOOSE 2
60
61 #define AXIS_TITLE_PAD 2 /* Padding for axis title. */
62 #define AXIS_LINE_PAD 1 /* Padding for axis line. */
63
64 #define HORIZMARGIN(m) (!((m)->site & 0x1)) /* Even sites are horizontal */
65
66 typedef enum AxisComponents {
67 MAJOR_TICK, MINOR_TICK, TICK_LABEL, AXIS_LINE
68 } AxisComponent;
69
70
71 typedef struct {
72 int axis; /* Length of the axis. */
73 int t1; /* Length of a major tick (in pixels). */
74 int t2; /* Length of a minor tick (in pixels). */
75 int label; /* Distance from axis to tick label. */
76 } AxisInfo;
77
78 extern Tk_CustomOption bltDistanceOption;
79 extern Tk_CustomOption bltPositiveDistanceOption;
80 extern Tk_CustomOption bltShadowOption;
81 extern Tk_CustomOption bltListOption;
82
83 static Tk_OptionParseProc StringToLimit;
84 static Tk_OptionPrintProc LimitToString;
85 static Tk_OptionParseProc StringToTicks;
86 static Tk_OptionPrintProc TicksToString;
87 static Tk_OptionParseProc StringToAxis;
88 static Tk_OptionPrintProc AxisToString;
89 static Tk_OptionParseProc StringToAnyAxis;
90 static Tk_OptionParseProc StringToFormat;
91 static Tk_OptionPrintProc FormatToString;
92 static Tk_OptionParseProc StringToLoose;
93 static Tk_OptionPrintProc LooseToString;
94
95 static Tk_CustomOption limitOption =
96 {
97 StringToLimit, LimitToString, (ClientData)0
98 };
99
100 static Tk_CustomOption majorTicksOption =
101 {
102 StringToTicks, TicksToString, (ClientData)AXIS_CONFIG_MAJOR,
103 };
104 static Tk_CustomOption minorTicksOption =
105 {
106 StringToTicks, TicksToString, (ClientData)AXIS_CONFIG_MINOR,
107 };
108 Tk_CustomOption bltXAxisOption =
109 {
110 StringToAxis, AxisToString, (ClientData)&bltXAxisUid
111 };
112 Tk_CustomOption bltYAxisOption =
113 {
114 StringToAxis, AxisToString, (ClientData)&bltYAxisUid
115 };
116 Tk_CustomOption bltAnyXAxisOption =
117 {
118 StringToAnyAxis, AxisToString, (ClientData)&bltXAxisUid
119 };
120 Tk_CustomOption bltAnyYAxisOption =
121 {
122 StringToAnyAxis, AxisToString, (ClientData)&bltYAxisUid
123 };
124 static Tk_CustomOption formatOption =
125 {
126 StringToFormat, FormatToString, (ClientData)0,
127 };
128 static Tk_CustomOption looseOption =
129 {
130 StringToLoose, LooseToString, (ClientData)0,
131 };
132
133 /* Axis flags: */
134
135 #define DEF_AXIS_COMMAND (char *)NULL
136 #define DEF_AXIS_DESCENDING "no"
137 #define DEF_AXIS_FOREGROUND RGB_BLACK
138 #define DEF_AXIS_FG_MONO RGB_BLACK
139 #define DEF_AXIS_HIDE "no"
140 #define DEF_AXIS_JUSTIFY "center"
141 #define DEF_AXIS_LIMITS_FORMAT (char *)NULL
142 #define DEF_AXIS_LINE_WIDTH "1"
143 #define DEF_AXIS_LOGSCALE "no"
144 #define DEF_AXIS_LOOSE "no"
145 #define DEF_AXIS_RANGE "0.0"
146 #define DEF_AXIS_ROTATE "0.0"
147 #define DEF_AXIS_SCROLL_INCREMENT "10"
148 #define DEF_AXIS_SHIFTBY "0.0"
149 #define DEF_AXIS_SHOWTICKS "yes"
150 #define DEF_AXIS_STEP "0.0"
151 #define DEF_AXIS_STEP "0.0"
152 #define DEF_AXIS_SUBDIVISIONS "2"
153 #define DEF_AXIS_TAGS "all"
154 #define DEF_AXIS_TICKS "0"
155 #ifdef WIN32
156 #define DEF_AXIS_TICK_FONT "{Arial Narrow} 8"
157 #else
158 #define DEF_AXIS_TICK_FONT "*-Helvetica-Medium-R-Normal-*-10-*"
159 #endif
160 #define DEF_AXIS_TICK_LENGTH "8"
161 #define DEF_AXIS_TITLE_ALTERNATE "0"
162 #define DEF_AXIS_TITLE_FG RGB_BLACK
163 #define DEF_AXIS_TITLE_FONT STD_FONT
164 #define DEF_AXIS_X_STEP_BARCHART "1.0"
165 #define DEF_AXIS_X_SUBDIVISIONS_BARCHART "0"
166 #define DEF_AXIS_BACKGROUND (char *)NULL
167 #define DEF_AXIS_BORDERWIDTH "0"
168 #define DEF_AXIS_RELIEF "flat"
169
170 static Tk_ConfigSpec configSpecs[] =
171 {
172 {TK_CONFIG_DOUBLE, "-autorange", "autoRange", "AutoRange",
173 DEF_AXIS_RANGE, Tk_Offset(Axis, windowSize),
174 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
175 {TK_CONFIG_BORDER, "-background", "background", "Background",
176 DEF_AXIS_BACKGROUND, Tk_Offset(Axis, border),
177 ALL_GRAPHS | TK_CONFIG_NULL_OK},
178 {TK_CONFIG_SYNONYM, "-bg", "background", (char *)NULL, (char *)NULL, 0, 0},
179 {TK_CONFIG_CUSTOM, "-bindtags", "bindTags", "BindTags",
180 DEF_AXIS_TAGS, Tk_Offset(Axis, tags),
181 ALL_GRAPHS | TK_CONFIG_NULL_OK, &bltListOption},
182 {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *)NULL,
183 (char *)NULL, 0, ALL_GRAPHS},
184 {TK_CONFIG_CUSTOM, "-borderwidth", "borderWidth", "BorderWidth",
185 DEF_AXIS_BORDERWIDTH, Tk_Offset(Axis, borderWidth),
186 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
187 {TK_CONFIG_COLOR, "-color", "color", "Color",
188 DEF_AXIS_FOREGROUND, Tk_Offset(Axis, tickTextStyle.color),
189 TK_CONFIG_COLOR_ONLY | ALL_GRAPHS},
190 {TK_CONFIG_COLOR, "-color", "color", "Color",
191 DEF_AXIS_FG_MONO, Tk_Offset(Axis, tickTextStyle.color),
192 TK_CONFIG_MONO_ONLY | ALL_GRAPHS},
193 {TK_CONFIG_STRING, "-command", "command", "Command",
194 DEF_AXIS_COMMAND, Tk_Offset(Axis, formatCmd),
195 TK_CONFIG_NULL_OK | ALL_GRAPHS},
196 {TK_CONFIG_BOOLEAN, "-descending", "descending", "Descending",
197 DEF_AXIS_DESCENDING, Tk_Offset(Axis, descending),
198 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
199 {TK_CONFIG_BOOLEAN, "-hide", "hide", "Hide",
200 DEF_AXIS_HIDE, Tk_Offset(Axis, hidden),
201 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
202 {TK_CONFIG_JUSTIFY, "-justify", "justify", "Justify",
203 DEF_AXIS_JUSTIFY, Tk_Offset(Axis, titleTextStyle.justify),
204 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
205 {TK_CONFIG_BOOLEAN, "-labeloffset", "labelOffset", "LabelOffset",
206 (char *)NULL, Tk_Offset(Axis, labelOffset), ALL_GRAPHS},
207 {TK_CONFIG_COLOR, "-limitscolor", "limitsColor", "Color",
208 DEF_AXIS_FOREGROUND, Tk_Offset(Axis, limitsTextStyle.color),
209 TK_CONFIG_COLOR_ONLY | ALL_GRAPHS},
210 {TK_CONFIG_COLOR, "-limitscolor", "limitsColor", "Color",
211 DEF_AXIS_FG_MONO, Tk_Offset(Axis, limitsTextStyle.color),
212 TK_CONFIG_MONO_ONLY | ALL_GRAPHS},
213 {TK_CONFIG_FONT, "-limitsfont", "limitsFont", "Font",
214 DEF_AXIS_TICK_FONT, Tk_Offset(Axis, limitsTextStyle.font), ALL_GRAPHS},
215 {TK_CONFIG_CUSTOM, "-limitsformat", "limitsFormat", "LimitsFormat",
216 (char *)NULL, Tk_Offset(Axis, limitsFormats),
217 TK_CONFIG_NULL_OK | ALL_GRAPHS, &formatOption},
218 {TK_CONFIG_CUSTOM, "-limitsshadow", "limitsShadow", "Shadow",
219 (char *)NULL, Tk_Offset(Axis, limitsTextStyle.shadow),
220 TK_CONFIG_COLOR_ONLY | ALL_GRAPHS, &bltShadowOption},
221 {TK_CONFIG_CUSTOM, "-limitsshadow", "limitsShadow", "Shadow",
222 (char *)NULL, Tk_Offset(Axis, limitsTextStyle.shadow),
223 TK_CONFIG_MONO_ONLY | ALL_GRAPHS, &bltShadowOption},
224 {TK_CONFIG_CUSTOM, "-linewidth", "lineWidth", "LineWidth",
225 DEF_AXIS_LINE_WIDTH, Tk_Offset(Axis, lineWidth),
226 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
227 {TK_CONFIG_BOOLEAN, "-logscale", "logScale", "LogScale",
228 DEF_AXIS_LOGSCALE, Tk_Offset(Axis, logScale),
229 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
230 {TK_CONFIG_CUSTOM, "-loose", "loose", "Loose",
231 DEF_AXIS_LOOSE, 0, ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT,
232 &looseOption},
233 {TK_CONFIG_CUSTOM, "-majorticks", "majorTicks", "MajorTicks",
234 (char *)NULL, Tk_Offset(Axis, t1Ptr),
235 TK_CONFIG_NULL_OK | ALL_GRAPHS, &majorTicksOption},
236 {TK_CONFIG_CUSTOM, "-max", "max", "Max",
237 (char *)NULL, Tk_Offset(Axis, reqMax),
238 TK_CONFIG_NULL_OK | ALL_GRAPHS, &limitOption},
239 {TK_CONFIG_CUSTOM, "-min", "min", "Min",
240 (char *)NULL, Tk_Offset(Axis, reqMin),
241 TK_CONFIG_NULL_OK | ALL_GRAPHS, &limitOption},
242 {TK_CONFIG_CUSTOM, "-minorticks", "minorTicks", "MinorTicks",
243 (char *)NULL, Tk_Offset(Axis, t2Ptr),
244 TK_CONFIG_NULL_OK | ALL_GRAPHS, &minorTicksOption},
245 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
246 DEF_AXIS_RELIEF, Tk_Offset(Axis, relief),
247 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
248 {TK_CONFIG_DOUBLE, "-rotate", "rotate", "Rotate",
249 DEF_AXIS_ROTATE, Tk_Offset(Axis, tickTextStyle.theta),
250 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
251 {TK_CONFIG_STRING, "-scrollcommand", "scrollCommand", "ScrollCommand",
252 (char *)NULL, Tk_Offset(Axis, scrollCmdPrefix),
253 ALL_GRAPHS | TK_CONFIG_NULL_OK},
254 {TK_CONFIG_CUSTOM, "-scrollincrement", "scrollIncrement", "ScrollIncrement",
255 DEF_AXIS_SCROLL_INCREMENT, Tk_Offset(Axis, scrollUnits),
256 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT, &bltPositiveDistanceOption},
257 {TK_CONFIG_CUSTOM, "-scrollmax", "scrollMax", "ScrollMax",
258 (char *)NULL, Tk_Offset(Axis, scrollMax),
259 TK_CONFIG_NULL_OK | ALL_GRAPHS, &limitOption},
260 {TK_CONFIG_CUSTOM, "-scrollmin", "scrollMin", "ScrollMin",
261 (char *)NULL, Tk_Offset(Axis, scrollMin),
262 TK_CONFIG_NULL_OK | ALL_GRAPHS, &limitOption},
263 {TK_CONFIG_DOUBLE, "-shiftby", "shiftBy", "ShiftBy",
264 DEF_AXIS_SHIFTBY, Tk_Offset(Axis, shiftBy),
265 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
266 {TK_CONFIG_BOOLEAN, "-showticks", "showTicks", "ShowTicks",
267 DEF_AXIS_SHOWTICKS, Tk_Offset(Axis, showTicks),
268 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
269 {TK_CONFIG_DOUBLE, "-stepsize", "stepSize", "StepSize",
270 DEF_AXIS_STEP, Tk_Offset(Axis, reqStep),
271 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
272 {TK_CONFIG_DOUBLE, "-tickdivider", "tickDivider", "TickDivider",
273 DEF_AXIS_STEP, Tk_Offset(Axis, tickZoom),
274 ALL_GRAPHS | TK_CONFIG_DONT_SET_DEFAULT},
275 {TK_CONFIG_INT, "-subdivisions", "subdivisions", "Subdivisions",
276 DEF_AXIS_SUBDIVISIONS, Tk_Offset(Axis, reqNumMinorTicks),
277 ALL_GRAPHS},
278 {TK_CONFIG_FONT, "-tickfont", "tickFont", "Font",
279 DEF_AXIS_TICK_FONT, Tk_Offset(Axis, tickTextStyle.font), ALL_GRAPHS},
280 {TK_CONFIG_PIXELS, "-ticklength", "tickLength", "TickLength",
281 DEF_AXIS_TICK_LENGTH, Tk_Offset(Axis, tickLength), ALL_GRAPHS},
282 {TK_CONFIG_CUSTOM, "-tickshadow", "tickShadow", "Shadow",
283 (char *)NULL, Tk_Offset(Axis, tickTextStyle.shadow),
284 TK_CONFIG_COLOR_ONLY | ALL_GRAPHS, &bltShadowOption},
285 {TK_CONFIG_CUSTOM, "-tickshadow", "tickShadow", "Shadow",
286 (char *)NULL, Tk_Offset(Axis, tickTextStyle.shadow),
287 TK_CONFIG_MONO_ONLY | ALL_GRAPHS, &bltShadowOption},
288 {TK_CONFIG_STRING, "-title", "title", "Title",
289 (char *)NULL, Tk_Offset(Axis, title),
290 TK_CONFIG_DONT_SET_DEFAULT | TK_CONFIG_NULL_OK | ALL_GRAPHS},
291 {TK_CONFIG_BOOLEAN, "-titlealternate", "titleAlternate", "TitleAlternate",
292 DEF_AXIS_TITLE_ALTERNATE, Tk_Offset(Axis, titleAlternate),
293 TK_CONFIG_DONT_SET_DEFAULT | ALL_GRAPHS},
294 {TK_CONFIG_COLOR, "-titlecolor", "titleColor", "Color",
295 DEF_AXIS_FOREGROUND, Tk_Offset(Axis, titleTextStyle.color),
296 TK_CONFIG_COLOR_ONLY | ALL_GRAPHS},
297 {TK_CONFIG_COLOR, "-titlecolor", "titleColor", "TitleColor",
298 DEF_AXIS_FG_MONO, Tk_Offset(Axis, titleTextStyle.color),
299 TK_CONFIG_MONO_ONLY | ALL_GRAPHS},
300 {TK_CONFIG_FONT, "-titlefont", "titleFont", "Font",
301 DEF_AXIS_TITLE_FONT, Tk_Offset(Axis, titleTextStyle.font), ALL_GRAPHS},
302 {TK_CONFIG_CUSTOM, "-titleshadow", "titleShadow", "Shadow",
303 (char *)NULL, Tk_Offset(Axis, titleTextStyle.shadow),
304 TK_CONFIG_COLOR_ONLY | ALL_GRAPHS, &bltShadowOption},
305 {TK_CONFIG_CUSTOM, "-titleshadow", "titleShadow", "Shadow",
306 (char *)NULL, Tk_Offset(Axis, titleTextStyle.shadow),
307 TK_CONFIG_MONO_ONLY | ALL_GRAPHS, &bltShadowOption},
308 {TK_CONFIG_END, NULL, NULL, NULL, NULL, 0, 0}
309 };
310
311 /* Forward declarations */
312 static void DestroyAxis _ANSI_ARGS_((Graph *graphPtr, Axis *axisPtr));
313 static int GetAxis _ANSI_ARGS_((Graph *graphPtr, char *name, Blt_Uid classUid,
314 Axis **axisPtrPtr));
315 static void FreeAxis _ANSI_ARGS_((Graph *graphPtr, Axis *axisPtr));
316
317 INLINE static int
318 Round(register double x)
319 {
320 return (int) (x + ((x < 0.0) ? -0.5 : 0.5));
321 }
322
323 static void
324 SetAxisRange(AxisRange *rangePtr, double min, double max)
325 {
326 rangePtr->min = min;
327 rangePtr->max = max;
328 rangePtr->range = max - min;
329 if (FABS(rangePtr->range) < DBL_EPSILON) {
330 rangePtr->range = 1.0;
331 }
332 rangePtr->scale = 1.0 / rangePtr->range;
333 }
334
335 /*
336 * ----------------------------------------------------------------------
337 *
338 * InRange --
339 *
340 * Determines if a value lies within a given range.
341 *
342 * The value is normalized and compared against the interval
343 * [0..1], where 0.0 is the minimum and 1.0 is the maximum.
344 * DBL_EPSILON is the smallest number that can be represented
345 * on the host machine, such that (1.0 + epsilon) != 1.0.
346 *
347 * Please note, *max* can't equal *min*.
348 *
349 * Results:
350 * If the value is within the interval [min..max], 1 is
351 * returned; 0 otherwise.
352 *
353 * ----------------------------------------------------------------------
354 */
355 INLINE static int
356 InRange(x, rangePtr)
357 register double x;
358 AxisRange *rangePtr;
359 {
360 if (rangePtr->range < DBL_EPSILON) {
361 #ifdef notdef
362 return (((rangePtr->max - x) >= (FABS(x) * DBL_EPSILON)) &&
363 ((x - rangePtr->min) >= (FABS(x) * DBL_EPSILON)));
364 #endif
365 return (FABS(rangePtr->max - x) >= DBL_EPSILON);
366 } else {
367 double norm;
368
369 norm = (x - rangePtr->min) * rangePtr->scale;
370 return ((norm >= -DBL_EPSILON) && ((norm - 1.0) < DBL_EPSILON));
371 }
372 }
373
374 INLINE static int
375 AxisIsHorizontal(graphPtr, axisPtr)
376 Graph *graphPtr;
377 Axis *axisPtr;
378 {
379 return ((axisPtr->classUid == bltYAxisUid) == graphPtr->inverted);
380 }
381
382
383 /* ----------------------------------------------------------------------
384 * Custom option parse and print procedures
385 * ----------------------------------------------------------------------
386 */
387
388 /*
389 *----------------------------------------------------------------------
390 *
391 * StringToAnyAxis --
392 *
393 * Converts the name of an axis to a pointer to its axis structure.
394 *
395 * Results:
396 * The return value is a standard Tcl result. The axis flags are
397 * written into the widget record.
398 *
399 *----------------------------------------------------------------------
400 */
401 /*ARGSUSED*/
402 static int
403 StringToAnyAxis(clientData, interp, tkwin, string, widgRec, offset)
404 ClientData clientData; /* Class identifier of the type of
405 * axis we are looking for. */
406 Tcl_Interp *interp; /* Interpreter to send results back to. */
407 Tk_Window tkwin; /* Used to look up pointer to graph. */
408 char *string; /* String representing new value. */
409 char *widgRec; /* Pointer to structure record. */
410 int offset; /* Offset of field in structure. */
411 {
412 Axis **axisPtrPtr = (Axis **)(widgRec + offset);
413 Blt_Uid classUid = *(Blt_Uid *)clientData;
414 Graph *graphPtr;
415 Axis *axisPtr;
416
417 graphPtr = Blt_GetGraphFromWindowData(tkwin);
418 if (*axisPtrPtr != NULL) {
419 FreeAxis(graphPtr, *axisPtrPtr);
420 }
421 if (string[0] == '\0') {
422 axisPtr = NULL;
423 } else if (GetAxis(graphPtr, string, classUid, &axisPtr) != TCL_OK) {
424 return TCL_ERROR;
425 }
426 *axisPtrPtr = axisPtr;
427 return TCL_OK;
428 }
429
430 /*
431 *----------------------------------------------------------------------
432 *
433 * StringToAxis --
434 *
435 * Converts the name of an axis to a pointer to its axis structure.
436 *
437 * Results:
438 * The return value is a standard Tcl result. The axis flags are
439 * written into the widget record.
440 *
441 *----------------------------------------------------------------------
442 */
443 /*ARGSUSED*/
444 static int
445 StringToAxis(clientData, interp, tkwin, string, widgRec, offset)
446 ClientData clientData; /* Class identifier of the type of
447 * axis we are looking for. */
448 Tcl_Interp *interp; /* Interpreter to send results back to. */
449 Tk_Window tkwin; /* Used to look up pointer to graph. */
450 char *string; /* String representing new value. */
451 char *widgRec; /* Pointer to structure record. */
452 int offset; /* Offset of field in structure. */
453 {
454 Axis **axisPtrPtr = (Axis **)(widgRec + offset);
455 Blt_Uid classUid = *(Blt_Uid *)clientData;
456 Graph *graphPtr;
457
458 graphPtr = Blt_GetGraphFromWindowData(tkwin);
459 if (*axisPtrPtr != NULL) {
460 FreeAxis(graphPtr, *axisPtrPtr);
461 }
462 if (GetAxis(graphPtr, string, classUid, axisPtrPtr) != TCL_OK) {
463 return TCL_ERROR;
464 }
465 return TCL_OK;
466 }
467
468 /*
469 *----------------------------------------------------------------------
470 *
471 * AxisToString --
472 *
473 * Convert the window coordinates into a string.
474 *
475 * Results:
476 * The string representing the coordinate position is returned.
477 *
478 *----------------------------------------------------------------------
479 */
480 /*ARGSUSED*/
481 static char *
482 AxisToString(clientData, tkwin, widgRec, offset, freeProcPtr)
483 ClientData clientData; /* Not used. */
484 Tk_Window tkwin; /* Not used. */
485 char *widgRec; /* Pointer to structure record .*/
486 int offset; /* Offset of field in structure. */
487 Tcl_FreeProc **freeProcPtr; /* Not used. */
488 {
489 Axis *axisPtr = *(Axis **)(widgRec + offset);
490
491 if (axisPtr == NULL) {
492 return "";
493 }
494 return axisPtr->name;
495 }
496
497 /*
498 *----------------------------------------------------------------------
499 *
500 * StringToFormat --
501 *
502 * Convert the name of virtual axis to an pointer.
503 *
504 * Results:
505 * The return value is a standard Tcl result. The axis flags are
506 * written into the widget record.
507 *
508 *----------------------------------------------------------------------
509 */
510 /*ARGSUSED*/
511 static int
512 StringToFormat(clientData, interp, tkwin, string, widgRec, offset)
513 ClientData clientData; /* Not used. */
514 Tcl_Interp *interp; /* Interpreter to send results back to. */
515 Tk_Window tkwin; /* Used to look up pointer to graph */
516 char *string; /* String representing new value. */
517 char *widgRec; /* Pointer to structure record. */
518 int offset; /* Offset of field in structure. */
519 {
520 Axis *axisPtr = (Axis *)(widgRec);
521 char **argv;
522 int argc;
523
524 if (axisPtr->limitsFormats != NULL) {
525 Blt_Free(axisPtr->limitsFormats);
526 }
527 axisPtr->limitsFormats = NULL;
528 axisPtr->nFormats = 0;
529
530 if ((string == NULL) || (*string == '\0')) {
531 return TCL_OK;
532 }
533 if (Tcl_SplitList(interp, string, &argc, &argv) != TCL_OK) {
534 return TCL_ERROR;
535 }
536 if (argc > 2) {
537 Tcl_AppendResult(interp, "too many elements in limits format list \"",
538 string, "\"", (char *)NULL);
539 Blt_Free(argv);
540 return TCL_ERROR;
541 }
542 axisPtr->limitsFormats = argv;
543 axisPtr->nFormats = argc;
544 return TCL_OK;
545 }
546
547 /*
548 *----------------------------------------------------------------------
549 *
550 * FormatToString --
551 *
552 * Convert the window coordinates into a string.
553 *
554 * Results:
555 * The string representing the coordinate position is returned.
556 *
557 *----------------------------------------------------------------------
558 */
559 /*ARGSUSED*/
560 static char *
561 FormatToString(clientData, tkwin, widgRec, offset, freeProcPtr)
562 ClientData clientData; /* Not used. */
563 Tk_Window tkwin; /* Not used. */
564 char *widgRec; /* Widget record */
565 int offset; /* offset of limits field */
566 Tcl_FreeProc **freeProcPtr; /* Not used. */
567 {
568 Axis *axisPtr = (Axis *)(widgRec);
569
570 if (axisPtr->nFormats == 0) {
571 return "";
572 }
573 *freeProcPtr = (Tcl_FreeProc *)Blt_Free;
574 return Tcl_Merge(axisPtr->nFormats, axisPtr->limitsFormats);
575 }
576
577 /*
578 * ----------------------------------------------------------------------
579 *
580 * StringToLimit --
581 *
582 * Convert the string representation of an axis limit into its numeric
583 * form.
584 *
585 * Results:
586 * The return value is a standard Tcl result. The symbol type is
587 * written into the widget record.
588 *
589 * ----------------------------------------------------------------------
590 */
591 /*ARGSUSED*/
592 static int
593 StringToLimit(clientData, interp, tkwin, string, widgRec, offset)
594 ClientData clientData; /* Either AXIS_CONFIG_MIN or AXIS_CONFIG_MAX.
595 * Indicates which axis limit to set. */
596 Tcl_Interp *interp; /* Interpreter to send results back to */
597 Tk_Window tkwin; /* Not used. */
598 char *string; /* String representing new value. */
599 char *widgRec; /* Pointer to structure record. */
600 int offset; /* Offset of field in structure. */
601 {
602 double *limitPtr = (double *)(widgRec + offset);
603
604 if ((string == NULL) || (*string == '\0')) {
605 *limitPtr = VALUE_UNDEFINED;
606 } else if (Tcl_ExprDouble(interp, string, limitPtr) != TCL_OK) {
607 return TCL_ERROR;
608 }
609 return TCL_OK;
610 }
611
612 /*
613 * ----------------------------------------------------------------------
614 *
615 * LimitToString --
616 *
617 * Convert the floating point axis limits into a string.
618 *
619 * Results:
620 * The string representation of the limits is returned.
621 *
622 * ----------------------------------------------------------------------
623 */
624 /*ARGSUSED*/
625 static char *
626 LimitToString(clientData, tkwin, widgRec, offset, freeProcPtr)
627 ClientData clientData; /* Either LMIN or LMAX */
628 Tk_Window tkwin; /* Not used. */
629 char *widgRec; /* */
630 int offset;
631 Tcl_FreeProc **freeProcPtr;
632 {
633 double limit = *(double *)(widgRec + offset);
634 char *result;
635
636 result = "";
637 if (DEFINED(limit)) {
638 char string[TCL_DOUBLE_SPACE + 1];
639 Graph *graphPtr;
640
641 graphPtr = Blt_GetGraphFromWindowData(tkwin);
642 Tcl_PrintDouble(graphPtr->interp, limit, string);
643 result = Blt_Strdup(string);
644 if (result == NULL) {
645 return "";
646 }
647 *freeProcPtr = (Tcl_FreeProc *)Blt_Free;
648 }
649 return result;
650 }
651
652 /*
653 * ----------------------------------------------------------------------
654 *
655 * StringToTicks --
656 *
657 *
658 * Results:
659 *
660 * ----------------------------------------------------------------------
661 */
662 /*ARGSUSED*/
663 static int
664 StringToTicks(clientData, interp, tkwin, string, widgRec, offset)
665 ClientData clientData; /* Not used. */
666 Tcl_Interp *interp; /* Interpreter to send results back to */
667 Tk_Window tkwin; /* Not used. */
668 char *string; /* String representing new value. */
669 char *widgRec; /* Pointer to structure record. */
670 int offset; /* Offset of field in structure. */
671 {
•672 unsigned int mask = (uintptr_t)clientData;
673 Axis *axisPtr = (Axis *)widgRec;
674 Ticks **ticksPtrPtr = (Ticks **) (widgRec + offset);
675 int nTicks;
676 Ticks *ticksPtr;
677
678 nTicks = 0;
679 ticksPtr = NULL;
680 if ((string != NULL) && (*string != '\0')) {
681 int nExprs;
682 char **exprArr;
683
684 if (Tcl_SplitList(interp, string, &nExprs, &exprArr) != TCL_OK) {
685 return TCL_ERROR;
686 }
687 if (nExprs > 0) {
688 register int i;
689 int result = TCL_ERROR;
690 double value;
691
692 ticksPtr = Blt_Malloc(sizeof(Ticks) + (nExprs * sizeof(double)));
693 assert(ticksPtr);
694 for (i = 0; i < nExprs; i++) {
695 result = Tcl_ExprDouble(interp, exprArr[i], &value);
696 if (result != TCL_OK) {
697 break;
698 }
699 ticksPtr->values[i] = value;
700 }
701 Blt_Free(exprArr);
702 if (result != TCL_OK) {
703 Blt_Free(ticksPtr);
704 return TCL_ERROR;
705 }
706 nTicks = nExprs;
707 }
708 }
709 axisPtr->flags &= ~mask;
710 if (ticksPtr != NULL) {
711 axisPtr->flags |= mask;
712 ticksPtr->nTicks = nTicks;
713 }
714 if (*ticksPtrPtr != NULL) {
715 Blt_Free(*ticksPtrPtr);
716 }
717 *ticksPtrPtr = ticksPtr;
718 return TCL_OK;
719 }
720
721 /*
722 * ----------------------------------------------------------------------
723 *
724 * TicksToString --
725 *
726 * Convert array of tick coordinates to a list.
727 *
728 * Results:
729 *
730 * ----------------------------------------------------------------------
731 */
732 /*ARGSUSED*/
733 static char *
734 TicksToString(clientData, tkwin, widgRec, offset, freeProcPtr)
735 ClientData clientData; /* Not used. */
736 Tk_Window tkwin; /* Not used. */
737 char *widgRec; /* */
738 int offset;
739 Tcl_FreeProc **freeProcPtr;
740 {
741 Ticks *ticksPtr = *(Ticks **) (widgRec + offset);
742 char string[TCL_DOUBLE_SPACE + 1];
743 register int i;
744 char *result;
745 Tcl_DString dString;
746 Graph *graphPtr;
747
748 if (ticksPtr == NULL) {
749 return "";
750 }
751 Tcl_DStringInit(&dString);
752 graphPtr = Blt_GetGraphFromWindowData(tkwin);
753 for (i = 0; i < ticksPtr->nTicks; i++) {
754 Tcl_PrintDouble(graphPtr->interp, ticksPtr->values[i], string);
755 Tcl_DStringAppendElement(&dString, string);
756 }
757 *freeProcPtr = (Tcl_FreeProc *)Blt_Free;
758 result = Blt_Strdup(Tcl_DStringValue(&dString));
759 Tcl_DStringFree(&dString);
760 return result;
761 }
762
763 /*
764 *----------------------------------------------------------------------
765 *
766 * StringToLoose --
767 *
768 * Convert a string to one of three values.
769 * 0 - false, no, off
770 * 1 - true, yes, on
771 * 2 - always
772 * Results:
773 * If the string is successfully converted, TCL_OK is returned.
774 * Otherwise, TCL_ERROR is returned and an error message is left in
775 * interpreter's result field.
776 *
777 *----------------------------------------------------------------------
778 */
779 /*ARGSUSED*/
780 static int
781 StringToLoose(clientData, interp, tkwin, string, widgRec, offset)
782 ClientData clientData; /* Not used. */
783 Tcl_Interp *interp; /* Interpreter to send results back to */
784 Tk_Window tkwin; /* Not used. */
785 char *string; /* String representing new value. */
786 char *widgRec; /* Pointer to structure record. */
787 int offset; /* Offset of field in structure. */
788 {
789 Axis *axisPtr = (Axis *)(widgRec);
790 register int i;
791 int argc;
792 char **argv;
793 int values[2];
794
795 if (Tcl_SplitList(interp, string, &argc, &argv) != TCL_OK) {
796 return TCL_ERROR;
797 }
798 if ((argc < 1) || (argc > 2)) {
799 Tcl_AppendResult(interp, "wrong # elements in loose value \"",
800 string, "\"", (char *)NULL);
801 return TCL_ERROR;
802 }
803 for (i = 0; i < argc; i++) {
804 if ((argv[i][0] == 'a') && (strcmp(argv[i], "always") == 0)) {
805 values[i] = TICK_RANGE_ALWAYS_LOOSE;
806 } else {
807 int bool;
808
809 if (Tcl_GetBoolean(interp, argv[i], &bool) != TCL_OK) {
810 Blt_Free(argv);
811 return TCL_ERROR;
812 }
813 values[i] = bool;
814 }
815 }
816 axisPtr->looseMin = axisPtr->looseMax = values[0];
817 if (argc > 1) {
818 axisPtr->looseMax = values[1];
819 }
820 Blt_Free(argv);
821 return TCL_OK;
822 }
823
824 /*
825 *----------------------------------------------------------------------
826 *
827 * LooseToString --
828 *
829 * Results:
830 * The string representation of the auto boolean is returned.
831 *
832 *----------------------------------------------------------------------
833 */
834 /*ARGSUSED*/
835 static char *
836 LooseToString(clientData, tkwin, widgRec, offset, freeProcPtr)
837 ClientData clientData; /* Not used. */
838 Tk_Window tkwin; /* Not used. */
839 char *widgRec; /* Widget record */
840 int offset; /* offset of flags field in record */
841 Tcl_FreeProc **freeProcPtr; /* Memory deallocation scheme to use */
842 {
843 Axis *axisPtr = (Axis *)widgRec;
844 Tcl_DString dString;
845 char *result;
846
847 Tcl_DStringInit(&dString);
848 if (axisPtr->looseMin == TICK_RANGE_TIGHT) {
849 Tcl_DStringAppendElement(&dString, "0");
850 } else if (axisPtr->looseMin == TICK_RANGE_LOOSE) {
851 Tcl_DStringAppendElement(&dString, "1");
852 } else if (axisPtr->looseMin == TICK_RANGE_ALWAYS_LOOSE) {
853 Tcl_DStringAppendElement(&dString, "always");
854 }
855 if (axisPtr->looseMin != axisPtr->looseMax) {
856 if (axisPtr->looseMax == TICK_RANGE_TIGHT) {
857 Tcl_DStringAppendElement(&dString, "0");
858 } else if (axisPtr->looseMax == TICK_RANGE_LOOSE) {
859 Tcl_DStringAppendElement(&dString, "1");
860 } else if (axisPtr->looseMax == TICK_RANGE_ALWAYS_LOOSE) {
861 Tcl_DStringAppendElement(&dString, "always");
862 }
863 }
864 result = Blt_Strdup(Tcl_DStringValue(&dString));
865 Tcl_DStringFree(&dString);
866 *freeProcPtr = (Tcl_FreeProc *)Blt_Free;
867 return result;
868 }
869
870 static void
871 FreeLabels(chainPtr)
872 Blt_Chain *chainPtr;
873 {
874 Blt_ChainLink *linkPtr;
875 TickLabel *labelPtr;
876
877 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL;
878 linkPtr = Blt_ChainNextLink(linkPtr)) {
879 labelPtr = Blt_ChainGetValue(linkPtr);
880 Blt_Free(labelPtr);
881 }
882 Blt_ChainReset(chainPtr);
883 }
884
885 /*
886 * ----------------------------------------------------------------------
887 *
888 * MakeLabel --
889 *
890 * Converts a floating point tick value to a string to be used as its
891 * label.
892 *
893 * Results:
894 * None.
895 *
896 * Side Effects:
897 * Returns a new label in the string character buffer. The formatted
898 * tick label will be displayed on the graph.
899 *
900 * ----------------------------------------------------------------------
901 */
902 static TickLabel *
903 MakeLabel(graphPtr, axisPtr, value)
904 Graph *graphPtr;
905 Axis *axisPtr; /* Axis structure */
906 double value; /* Value to be convert to a decimal string */
907 {
908 char string[TICK_LABEL_SIZE + 1];
909 TickLabel *labelPtr;
910
911 /* Generate a default tick label based upon the tick value. */
912 if (axisPtr->logScale) {
913 sprintf(string, "1E%d", ROUND(value));
914 } else {
915 sprintf(string, "%.*g", NUMDIGITS, value);
916 }
917
918 if (axisPtr->formatCmd != NULL) {
919 Tcl_Interp *interp = graphPtr->interp;
920 Tk_Window tkwin = graphPtr->tkwin;
921
922 /*
923 * A Tcl proc was designated to format tick labels. Append the path
924 * name of the widget and the default tick label as arguments when
925 * invoking it. Copy and save the new label from interp->result.
926 */
927 Tcl_ResetResult(interp);
928 if (Tcl_VarEval(interp, axisPtr->formatCmd, " ", Tk_PathName(tkwin),
929 " ", string, (char *)NULL) != TCL_OK) {
930 Tcl_BackgroundError(interp);
931 } else {
932 /*
933 * The proc could return a string of any length, so arbitrarily
934 * limit it to what will fit in the return string.
935 */
936 strncpy(string, Tcl_GetStringResult(interp), TICK_LABEL_SIZE);
937 string[TICK_LABEL_SIZE] = '\0';
938
939 Tcl_ResetResult(interp); /* Clear the interpreter's result. */
940 }
941 }
942 labelPtr = Blt_Malloc(sizeof(TickLabel) + strlen(string));
943 assert(labelPtr);
944 strcpy(labelPtr->string, string);
945 labelPtr->anchorPos.x = labelPtr->anchorPos.y = DBL_MAX;
946 return labelPtr;
947 }
948
949 /*
950 * ----------------------------------------------------------------------
951 *
952 * Blt_InvHMap --
953 *
954 * Maps the given screen coordinate back to a graph coordinate.
955 * Called by the graph locater routine.
956 *
957 * Results:
958 * Returns the graph coordinate value at the given window
959 * y-coordinate.
960 *
961 * ----------------------------------------------------------------------
962 */
963 double
964 Blt_InvHMap(graphPtr, axisPtr, x)
965 Graph *graphPtr;
966 Axis *axisPtr;
967 double x;
968 {
969 double value;
970
971 x = (double)(x - graphPtr->hOffset) * graphPtr->hScale;
972 if (axisPtr->descending) {
973 x = 1.0 - x;
974 }
975 value = (x * axisPtr->axisRange.range) + axisPtr->axisRange.min;
976 if (axisPtr->logScale) {
977 value = EXP10(value);
978 }
979 return value;
980 }
981
982 /*
983 * ----------------------------------------------------------------------
984 *
985 * Blt_InvVMap --
986 *
987 * Maps the given window y-coordinate back to a graph coordinate
988 * value. Called by the graph locater routine.
989 *
990 * Results:
991 * Returns the graph coordinate value at the given window
992 * y-coordinate.
993 *
994 * ----------------------------------------------------------------------
995 */
996 double
997 Blt_InvVMap(graphPtr, axisPtr, y)
998 Graph *graphPtr;
999 Axis *axisPtr;
1000 double y;
1001 {
1002 double value;
1003
1004 y = (double)(y - graphPtr->vOffset) * graphPtr->vScale;
1005 if (axisPtr->descending) {
1006 y = 1.0 - y;
1007 }
1008 value = ((1.0 - y) * axisPtr->axisRange.range) + axisPtr->axisRange.min;
1009 if (axisPtr->logScale) {
1010 value = EXP10(value);
1011 }
1012 return value;
1013 }
1014
1015 /*
1016 * ----------------------------------------------------------------------
1017 *
1018 * Blt_HMap --
1019 *
1020 * Map the given graph coordinate value to its axis, returning a window
1021 * position.
1022 *
1023 * Results:
1024 * Returns a double precision number representing the window coordinate
1025 * position on the given axis.
1026 *
1027 * ----------------------------------------------------------------------
1028 */
1029 double
1030 Blt_HMap(graphPtr, axisPtr, x)
1031 Graph *graphPtr;
1032 Axis *axisPtr;
1033 double x;
1034 {
1035 if ((axisPtr->logScale) && (x != 0.0)) {
1036 x = log10(FABS(x));
1037 }
1038 /* Map graph coordinate to normalized coordinates [0..1] */
1039 x = (x - axisPtr->axisRange.min) * axisPtr->axisRange.scale;
1040 if (axisPtr->descending) {
1041 x = 1.0 - x;
1042 }
1043 return (x * graphPtr->hRange + graphPtr->hOffset);
1044 }
1045
1046 /*
1047 * ----------------------------------------------------------------------
1048 *
1049 * Blt_VMap --
1050 *
1051 * Map the given graph coordinate value to its axis, returning a window
1052 * position.
1053 *
1054 * Results:
1055 * Returns a double precision number representing the window coordinate
1056 * position on the given axis.
1057 *
1058 * ----------------------------------------------------------------------
1059 */
1060 double
1061 Blt_VMap(graphPtr, axisPtr, y)
1062 Graph *graphPtr;
1063 Axis *axisPtr;
1064 double y;
1065 {
1066 if ((axisPtr->logScale) && (y != 0.0)) {
1067 y = log10(FABS(y));
1068 }
1069 /* Map graph coordinate to normalized coordinates [0..1] */
1070 y = (y - axisPtr->axisRange.min) * axisPtr->axisRange.scale;
1071 if (axisPtr->descending) {
1072 y = 1.0 - y;
1073 }
1074 return (((1.0 - y) * graphPtr->vRange) + graphPtr->vOffset);
1075 }
1076
1077 /*
1078 * ----------------------------------------------------------------------
1079 *
1080 * Blt_Map2D --
1081 *
1082 * Maps the given graph x,y coordinate values to a window position.
1083 *
1084 * Results:
1085 * Returns a XPoint structure containing the window coordinates of
1086 * the given graph x,y coordinate.
1087 *
1088 * ----------------------------------------------------------------------
1089 */
1090 Point2D
1091 Blt_Map2D(graphPtr, x, y, axesPtr)
1092 Graph *graphPtr;
1093 double x, y; /* Graph x and y coordinates */
1094 Axis2D *axesPtr; /* Specifies which axes to use */
1095 {
1096 Point2D point;
1097
1098 if (graphPtr->inverted) {
1099 point.x = Blt_HMap(graphPtr, axesPtr->y, y);
1100 point.y = Blt_VMap(graphPtr, axesPtr->x, x);
1101 } else {
1102 point.x = Blt_HMap(graphPtr, axesPtr->x, x);
1103 point.y = Blt_VMap(graphPtr, axesPtr->y, y);
1104 }
1105 return point;
1106 }
1107
1108 /*
1109 * ----------------------------------------------------------------------
1110 *
1111 * Blt_InvMap2D --
1112 *
1113 * Maps the given window x,y coordinates to graph values.
1114 *
1115 * Results:
1116 * Returns a structure containing the graph coordinates of
1117 * the given window x,y coordinate.
1118 *
1119 * ----------------------------------------------------------------------
1120 */
1121 Point2D
1122 Blt_InvMap2D(graphPtr, x, y, axesPtr)
1123 Graph *graphPtr;
1124 double x, y; /* Window x and y coordinates */
1125 Axis2D *axesPtr; /* Specifies which axes to use */
1126 {
1127 Point2D point;
1128
1129 if (graphPtr->inverted) {
1130 point.x = Blt_InvVMap(graphPtr, axesPtr->x, y);
1131 point.y = Blt_InvHMap(graphPtr, axesPtr->y, x);
1132 } else {
1133 point.x = Blt_InvHMap(graphPtr, axesPtr->x, x);
1134 point.y = Blt_InvVMap(graphPtr, axesPtr->y, y);
1135 }
1136 return point;
1137 }
1138
1139
1140 static void
1141 GetDataLimits(axisPtr, min, max)
1142 Axis *axisPtr;
1143 double min, max;
1144 {
1145 if (axisPtr->valueRange.min > min) {
1146 axisPtr->valueRange.min = min;
1147 }
1148 if (axisPtr->valueRange.max < max) {
1149 axisPtr->valueRange.max = max;
1150 }
1151 }
1152
1153 static void
1154 FixAxisRange(axisPtr)
1155 Axis *axisPtr;
1156 {
1157 double min, max;
1158 /*
1159 * When auto-scaling, the axis limits are the bounds of the element
1160 * data. If no data exists, set arbitrary limits (wrt to log/linear
1161 * scale).
1162 */
1163 min = axisPtr->valueRange.min;
1164 max = axisPtr->valueRange.max;
1165
1166 if (min == DBL_MAX) {
1167 if (DEFINED(axisPtr->reqMin)) {
1168 min = axisPtr->reqMin;
1169 } else {
1170 min = (axisPtr->logScale) ? 0.001 : 0.0;
1171 }
1172 }
1173 if (max == -DBL_MAX) {
1174 if (DEFINED(axisPtr->reqMax)) {
1175 max = axisPtr->reqMax;
1176 } else {
1177 max = 1.0;
1178 }
1179 }
1180 if (min >= max) {
1181 double value;
1182
1183 /*
1184 * There is no range of data (i.e. min is not less than max),
1185 * so manufacture one.
1186 */
1187 value = min;
1188 if (value == 0.0) {
1189 min = -0.1, max = 0.1;
1190 } else {
1191 double x;
1192
1193 x = FABS(value) * 0.1;
1194 min = value - x, max = value + x;
1195 }
1196 }
1197 SetAxisRange(&axisPtr->valueRange, min, max);
1198
1199 /*
1200 * The axis limits are either the current data range or overridden
1201 * by the values selected by the user with the -min or -max
1202 * options.
1203 */
1204 axisPtr->min = min;
1205 axisPtr->max = max;
1206 if (DEFINED(axisPtr->reqMin)) {
1207 axisPtr->min = axisPtr->reqMin;
1208 }
1209 if (DEFINED(axisPtr->reqMax)) {
1210 axisPtr->max = axisPtr->reqMax;
1211 }
1212
1213 if (axisPtr->max < axisPtr->min) {
1214
1215 /*
1216 * If the limits still don't make sense, it's because one
1217 * limit configuration option (-min or -max) was set and the
1218 * other default (based upon the data) is too small or large.
1219 * Remedy this by making up a new min or max from the
1220 * user-defined limit.
1221 */
1222
1223 if (!DEFINED(axisPtr->reqMin)) {
1224 axisPtr->min = axisPtr->max - (FABS(axisPtr->max) * 0.1);
1225 }
1226 if (!DEFINED(axisPtr->reqMax)) {
1227 axisPtr->max = axisPtr->min + (FABS(axisPtr->max) * 0.1);
1228 }
1229 }
1230 /*
1231 * If a window size is defined, handle auto ranging by shifting
1232 * the axis limits.
1233 */
1234 if ((axisPtr->windowSize > 0.0) &&
1235 (!DEFINED(axisPtr->reqMin)) && (!DEFINED(axisPtr->reqMax))) {
1236 if (axisPtr->shiftBy < 0.0) {
1237 axisPtr->shiftBy = 0.0;
1238 }
1239 max = axisPtr->min + axisPtr->windowSize;
1240 if (axisPtr->max >= max) {
1241 if (axisPtr->shiftBy > 0.0) {
1242 max = UCEIL(axisPtr->max, axisPtr->shiftBy);
1243 }
1244 axisPtr->min = max - axisPtr->windowSize;
1245 }
1246 axisPtr->max = max;
1247 }
1248 if ((axisPtr->max != axisPtr->prevMax) ||
1249 (axisPtr->min != axisPtr->prevMin)) {
1250 /* Indicate if the axis limits have changed */
1251 axisPtr->flags |= AXIS_DIRTY;
1252 /* and save the previous minimum and maximum values */
1253 axisPtr->prevMin = axisPtr->min;
1254 axisPtr->prevMax = axisPtr->max;
1255 }
1256 }
1257
1258 /*
1259 * ----------------------------------------------------------------------
1260 *
1261 * NiceNum --
1262 *
1263 * Reference: Paul Heckbert, "Nice Numbers for Graph Labels",
1264 * Graphics Gems, pp 61-63.
1265 *
1266 * Finds a "nice" number approximately equal to x.
1267 *
1268 * ----------------------------------------------------------------------
1269 */
1270 static double
1271 NiceNum(x, doround)
1272 double x;
1273 int doround; /* If non-zero, round. Otherwise take ceiling
1274 * of value. */
1275 {
1276 double expt; /* Exponent of x */
1277 double frac; /* Fractional part of x */
1278 double fnice; /* Nice, rounded fraction */
1279
1280 expt = floor(log10(x));
1281 frac = x / EXP10(expt); /* between 1 and 10 */
1282 if (doround) {
1283 if (frac < 1.5) {
1284 fnice = 1.0;
1285 } else if (frac < 3.0) {
1286 fnice = 2.0;
1287 } else if (frac < 7.0) {
1288 fnice = 5.0;
1289 } else {
1290 fnice = 10.0;
1291 }
1292 } else {
1293 if (frac <= 1.0) {
1294 fnice = 1.0;
1295 } else if (frac <= 2.0) {
1296 fnice = 2.0;
1297 } else if (frac <= 5.0) {
1298 fnice = 5.0;
1299 } else {
1300 fnice = 10.0;
1301 }
1302 }
1303 return fnice * EXP10(expt);
1304 }
1305
1306 static Ticks *
1307 GenerateTicks(sweepPtr)
1308 TickSweep *sweepPtr;
1309 {
1310 Ticks *ticksPtr;
1311 register int i;
1312
1313 ticksPtr = Blt_Malloc(sizeof(Ticks) + (sweepPtr->nSteps * sizeof(double)));
1314 assert(ticksPtr);
1315
1316 if (sweepPtr->step == 0.0) {
1317 static double logTable[] = /* Precomputed log10 values [1..10] */
1318 {
1319 0.0,
1320 0.301029995663981, 0.477121254719662,
1321 0.602059991327962, 0.698970004336019,
1322 0.778151250383644, 0.845098040014257,
1323 0.903089986991944, 0.954242509439325,
1324 1.0
1325 };
1326 /* Hack: A zero step indicates to use log values. */
1327 for (i = 0; i < sweepPtr->nSteps; i++) {
1328 ticksPtr->values[i] = logTable[i];
1329 }
1330 } else {
1331 double value;
1332
1333 value = sweepPtr->initial; /* Start from smallest axis tick */
1334 for (i = 0; i < sweepPtr->nSteps; i++) {
1335 value = UROUND(value, sweepPtr->step);
1336 ticksPtr->values[i] = value;
1337 value += sweepPtr->step;
1338 }
1339 }
1340 ticksPtr->nTicks = sweepPtr->nSteps;
1341 return ticksPtr;
1342 }
1343
1344 /*
1345 * ----------------------------------------------------------------------
1346 *
1347 * LogScaleAxis --
1348 *
1349 * Determine the range and units of a log scaled axis.
1350 *
1351 * Unless the axis limits are specified, the axis is scaled
1352 * automatically, where the smallest and largest major ticks encompass
1353 * the range of actual data values. When an axis limit is specified,
1354 * that value represents the smallest(min)/largest(max) value in the
1355 * displayed range of values.
1356 *
1357 * Both manual and automatic scaling are affected by the step used. By
1358 * default, the step is the largest power of ten to divide the range in
1359 * more than one piece.
1360 *
1361 * Automatic scaling:
1362 * Find the smallest number of units which contain the range of values.
1363 * The minimum and maximum major tick values will be represent the
1364 * range of values for the axis. This greatest number of major ticks
1365 * possible is 10.
1366 *
1367 * Manual scaling:
1368 * Make the minimum and maximum data values the represent the range of
1369 * the values for the axis. The minimum and maximum major ticks will be
1370 * inclusive of this range. This provides the largest area for plotting
1371 * and the expected results when the axis min and max values have be set
1372 * by the user (.e.g zooming). The maximum number of major ticks is 20.
1373 *
1374 * For log scale, there's the possibility that the minimum and
1375 * maximum data values are the same magnitude. To represent the
1376 * points properly, at least one full decade should be shown.
1377 * However, if you zoom a log scale plot, the results should be
1378 * predictable. Therefore, in that case, show only minor ticks.
1379 * Lastly, there should be an appropriate way to handle numbers
1380 * <=0.
1381 *
1382 * maxY
1383 * | units = magnitude (of least significant digit)
1384 * | high = largest unit tick < max axis value
1385 * high _| low = smallest unit tick > min axis value
1386 * |
1387 * | range = high - low
1388 * | # ticks = greatest factor of range/units
1389 * _|
1390 * U |
1391 * n |
1392 * i |
1393 * t _|
1394 * |
1395 * |
1396 * |
1397 * low _|
1398 * |
1399 * |_minX________________maxX__
1400 * | | | | |
1401 * minY low high
1402 * minY
1403 *
1404 *
1405 * numTicks = Number of ticks
1406 * min = Minimum value of axis
1407 * max = Maximum value of axis
1408 * range = Range of values (max - min)
1409 *
1410 * If the number of decades is greater than ten, it is assumed
1411 * that the full set of log-style ticks can't be drawn properly.
1412 *
1413 * Results:
1414 * None
1415 *
1416 * ---------------------------------------------------------------------- */
1417 static void
1418 LogScaleAxis(axisPtr, min, max)
1419 Axis *axisPtr;
1420 double min, max;
1421 {
1422 double range;
1423 double tickMin, tickMax;
1424 double majorStep, minorStep;
1425 int nMajor, nMinor;
1426
1427 nMajor = nMinor = 0;
1428 majorStep = minorStep = 0.0;
•1429 if (min > max) {
1430 double m;
1431 m = min;
1432 min = max;
1433 max = m;
1434 } else if (min == max) {
1435 max = min + 1.0;
1436 }
1437 /* if (min < max) { */
1438 min = (min != 0.0) ? log10(FABS(min)) : 0.0;
1439 max = (max != 0.0) ? log10(FABS(max)) : 1.0;
1440
1441 tickMin = floor(min);
1442 tickMax = ceil(max);
1443 range = tickMax - tickMin;
1444
1445 if (range > 10) {
1446 /* There are too many decades to display a major tick at every
1447 * decade. Instead, treat the axis as a linear scale. */
1448 range = NiceNum(range, 0);
1449 majorStep = NiceNum(range / DEF_NUM_TICKS, 1);
1450 tickMin = UFLOOR(tickMin, majorStep);
1451 tickMax = UCEIL(tickMax, majorStep);
1452 nMajor = (int)((tickMax - tickMin) / majorStep) + 1;
1453 minorStep = EXP10(floor(log10(majorStep)));
1454 if (minorStep == majorStep) {
1455 nMinor = 4, minorStep = 0.2;
1456 } else {
1457 nMinor = Round(majorStep / minorStep) - 1;
1458 }
1459 } else {
1460 if (tickMin == tickMax) {
1461 tickMax++;
1462 }
1463 majorStep = 1.0;
1464 nMajor = (int)(tickMax - tickMin + 1); /* FIXME: Check this. */
1465
1466 minorStep = 0.0; /* This is a special hack to pass
1467 * information to the GenerateTicks
1468 * routine. An interval of 0.0 tells
1469 * 1) this is a minor sweep and
1470 * 2) the axis is log scale.
1471 */
1472 nMinor = 10;
1473 }
1474 if ((axisPtr->looseMin == TICK_RANGE_TIGHT) ||
1475 ((axisPtr->looseMin == TICK_RANGE_LOOSE) &&
1476 (DEFINED(axisPtr->reqMin)))) {
1477 tickMin = min;
1478 nMajor++;
1479 }
1480 if ((axisPtr->looseMax == TICK_RANGE_TIGHT) ||
1481 ((axisPtr->looseMax == TICK_RANGE_LOOSE) &&
1482 (DEFINED(axisPtr->reqMax)))) {
1483 tickMax = max;
1484 }
•1485 /* } */
1486 axisPtr->majorSweep.step = majorStep;
1487 axisPtr->majorSweep.initial = floor(tickMin);
1488 axisPtr->majorSweep.nSteps = nMajor;
1489 axisPtr->minorSweep.initial = axisPtr->minorSweep.step = minorStep;
1490 axisPtr->minorSweep.nSteps = nMinor;
1491 SetAxisRange(&axisPtr->axisRange, tickMin, tickMax);
1492 }
1493
1494 /*
1495 * ----------------------------------------------------------------------
1496 *
1497 * LinearScaleAxis --
1498 *
1499 * Determine the units of a linear scaled axis.
1500 *
1501 * The axis limits are either the range of the data values mapped
1502 * to the axis (autoscaled), or the values specified by the -min
1503 * and -max options (manual).
1504 *
1505 * If autoscaled, the smallest and largest major ticks will
1506 * encompass the range of data values. If the -loose option is
1507 * selected, the next outer ticks are choosen. If tight, the
1508 * ticks are at or inside of the data limits are used.
1509 *
1510 * If manually set, the ticks are at or inside the data limits
1511 * are used. This makes sense for zooming. You want the
1512 * selected range to represent the next limit, not something a
1513 * bit bigger.
1514 *
1515 * Note: I added an "always" value to the -loose option to force
1516 * the manually selected axes to be loose. It's probably
1517 * not a good idea.
1518 *
1519 * maxY
1520 * | units = magnitude (of least significant digit)
1521 * | high = largest unit tick < max axis value
1522 * high _| low = smallest unit tick > min axis value
1523 * |
1524 * | range = high - low
1525 * | # ticks = greatest factor of range/units
1526 * _|
1527 * U |
1528 * n |
1529 * i |
1530 * t _|
1531 * |
1532 * |
1533 * |
1534 * low _|
1535 * |
1536 * |_minX________________maxX__
1537 * | | | | |
1538 * minY low high
1539 * minY
1540 *
1541 * numTicks = Number of ticks
1542 * min = Minimum value of axis
1543 * max = Maximum value of axis
1544 * range = Range of values (max - min)
1545 *
1546 * Results:
1547 * None.
1548 *
1549 * Side Effects:
1550 * The axis tick information is set. The actual tick values will
1551 * be generated later.
1552 *
1553 * ----------------------------------------------------------------------
1554 */
1555 static void
1556 LinearScaleAxis(axisPtr, min, max)
1557 Axis *axisPtr;
1558 double min, max;
1559 {
1560 double range, step;
1561 double tickMin, tickMax;
1562 double axisMin, axisMax;
1563 int nTicks;
1564
1565 nTicks = 0;
1566 tickMin = tickMax = 0.0;
•1567 if (min > max) {
1568 double m;
1569 m = min;
1570 min = max;
1571 max = m;
1572 } else if (min == max) {
1573 max = min + 1.0;
1574 }
1575 /* if (min < max) { */
1576 range = max - min;
1577
1578 /* Calculate the major tick stepping. */
1579 if (axisPtr->reqStep > 0.0) {
1580 /* An interval was designated by the user. Keep scaling it
1581 * until it fits comfortably within the current range of the
1582 * axis. */
1583 step = axisPtr->reqStep;
1584 while ((2 * step) >= range) {
1585 step *= 0.5;
1586 }
1587 } else {
1588 range = NiceNum(range, 0);
1589 step = NiceNum(range / DEF_NUM_TICKS, 1);
1590 }
1591
1592 /* Find the outer tick values. Add 0.0 to prevent getting -0.0. */
1593 axisMin = tickMin = floor(min / step) * step + 0.0;
1594 axisMax = tickMax = ceil(max / step) * step + 0.0;
1595
1596 nTicks = Round((tickMax - tickMin) / step) + 1;
•1597 /* } */
1598 axisPtr->majorSweep.step = step;
1599 axisPtr->majorSweep.initial = tickMin;
1600 axisPtr->majorSweep.nSteps = nTicks;
1601
1602 /*
1603 * The limits of the axis are either the range of the data
1604 * ("tight") or at the next outer tick interval ("loose"). The
1605 * looseness or tightness has to do with how the axis fits the
1606 * range of data values. This option is overridden when
1607 * the user sets an axis limit (by either -min or -max option).
1608 * The axis limit is always at the selected limit (otherwise we
1609 * assume that user would have picked a different number).
1610 */
1611 if ((axisPtr->looseMin == TICK_RANGE_TIGHT) ||
1612 ((axisPtr->looseMin == TICK_RANGE_LOOSE) &&
1613 (DEFINED(axisPtr->reqMin)))) {
1614 axisMin = min;
1615 }
1616 if ((axisPtr->looseMax == TICK_RANGE_TIGHT) ||
1617 ((axisPtr->looseMax == TICK_RANGE_LOOSE) &&
1618 (DEFINED(axisPtr->reqMax)))) {
1619 axisMax = max;
1620 }
1621 SetAxisRange(&axisPtr->axisRange, axisMin, axisMax);
1622
1623 /* Now calculate the minor tick step and number. */
1624
1625 if ((axisPtr->reqNumMinorTicks > 0) &&
1626 ((axisPtr->flags & AXIS_CONFIG_MAJOR) == 0)) {
1627 nTicks = axisPtr->reqNumMinorTicks - 1;
1628 step = 1.0 / (nTicks + 1);
1629 } else {
1630 nTicks = 0; /* No minor ticks. */
1631 step = 0.5; /* Don't set the minor tick interval
1632 * to 0.0. It makes the GenerateTicks
1633 * routine create minor log-scale tick
1634 * marks. */
1635 }
1636 axisPtr->minorSweep.initial = axisPtr->minorSweep.step = step;
1637 axisPtr->minorSweep.nSteps = nTicks;
1638 }
1639
1640 static void
1641 SweepTicks(axisPtr)
1642 Axis *axisPtr;
1643 {
1644 if ((axisPtr->flags & AXIS_CONFIG_MAJOR) == 0) {
1645 if (axisPtr->t1Ptr != NULL) {
1646 Blt_Free(axisPtr->t1Ptr);
1647 }
1648 axisPtr->t1Ptr = GenerateTicks(&axisPtr->majorSweep);
1649 }
1650 if ((axisPtr->flags & AXIS_CONFIG_MINOR) == 0) {
1651 if (axisPtr->t2Ptr != NULL) {
1652 Blt_Free(axisPtr->t2Ptr);
1653 }
1654 axisPtr->t2Ptr = GenerateTicks(&axisPtr->minorSweep);
1655 }
1656 }
1657
1658 /*
1659 * ----------------------------------------------------------------------
1660 *
1661 * Blt_ResetAxes --
1662 *
1663 * Results:
1664 * None.
1665 *
1666 * ----------------------------------------------------------------------
1667 */
1668 void
1669 Blt_ResetAxes(graphPtr)
1670 Graph *graphPtr;
1671 {
1672 Blt_ChainLink *linkPtr;
1673 Element *elemPtr;
1674 Axis *axisPtr;
1675 Blt_HashEntry *hPtr;
1676 Blt_HashSearch cursor;
1677 Extents2D exts;
1678 double min, max;
1679
1680 /* FIXME: This should be called whenever the display list of
1681 * elements change. Maybe yet another flag INIT_STACKS to
1682 * indicate that the element display list has changed.
1683 * Needs to be done before the axis limits are set.
1684 */
1685 Blt_InitFreqTable(graphPtr);
1686 if ((graphPtr->mode == MODE_STACKED) && (graphPtr->nStacks > 0)) {
1687 Blt_ComputeStacks(graphPtr);
1688 }
1689 /*
1690 * Step 1: Reset all axes. Initialize the data limits of the axis to
1691 * impossible values.
1692 */
1693 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
1694 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1695 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
1696 axisPtr->min = axisPtr->valueRange.min = DBL_MAX;
1697 axisPtr->max = axisPtr->valueRange.max = -DBL_MAX;
1698 }
1699
1700 /*
1701 * Step 2: For each element that's to be displayed, get the smallest
1702 * and largest data values mapped to each X and Y-axis. This
1703 * will be the axis limits if the user doesn't override them
1704 * with -min and -max options.
1705 */
1706 for (linkPtr = Blt_ChainFirstLink(graphPtr->elements.displayList);
1707 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
1708 elemPtr = Blt_ChainGetValue(linkPtr);
1709 if (!elemPtr->hidden) {
1710 (*elemPtr->procsPtr->extentsProc) (elemPtr, &exts);
1711 GetDataLimits(elemPtr->axes.x, exts.left, exts.right);
1712 GetDataLimits(elemPtr->axes.y, exts.top, exts.bottom);
1713 }
1714 }
1715 /*
1716 * Step 3: Now that we know the range of data values for each axis,
1717 * set axis limits and compute a sweep to generate tick values.
1718 */
1719 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
1720 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1721 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
1722 FixAxisRange(axisPtr);
1723
1724 /* Calculate min/max tick (major/minor) layouts */
1725 min = axisPtr->min;
1726 max = axisPtr->max;
1727 if ((DEFINED(axisPtr->scrollMin)) && (min < axisPtr->scrollMin)) {
1728 min = axisPtr->scrollMin;
1729 }
1730 if ((DEFINED(axisPtr->scrollMax)) && (max > axisPtr->scrollMax)) {
1731 max = axisPtr->scrollMax;
1732 }
1733 if (axisPtr->logScale) {
1734 LogScaleAxis(axisPtr, min, max);
1735 } else {
1736 LinearScaleAxis(axisPtr, min, max);
1737 }
1738
1739 if ((axisPtr->flags & (AXIS_DIRTY | AXIS_ONSCREEN)) ==
1740 (AXIS_DIRTY | AXIS_ONSCREEN)) {
1741 graphPtr->flags |= REDRAW_BACKING_STORE;
1742 }
1743 }
1744
1745 graphPtr->flags &= ~RESET_AXES;
1746
1747 /*
1748 * When any axis changes, we need to layout the entire graph.
1749 */
1750 graphPtr->flags |= (GET_AXIS_GEOMETRY | LAYOUT_NEEDED |
1751 MAP_ALL | REDRAW_WORLD);
1752 }
1753
1754 /*
1755 * ----------------------------------------------------------------------
1756 *
1757 * ResetTextStyles --
1758 *
1759 * Configures axis attributes (font, line width, label, etc) and
1760 * allocates a new (possibly shared) graphics context. Line cap
1761 * style is projecting. This is for the problem of when a tick
1762 * sits directly at the end point of the axis.
1763 *
1764 * Results:
1765 * The return value is a standard Tcl result.
1766 *
1767 * Side Effects:
1768 * Axis resources are allocated (GC, font). Axis layout is
1769 * deferred until the height and width of the window are known.
1770 *
1771 * ----------------------------------------------------------------------
1772 */
1773 static void
1774 ResetTextStyles(graphPtr, axisPtr)
1775 Graph *graphPtr;
1776 Axis *axisPtr;
1777 {
1778 GC newGC;
1779 XGCValues gcValues;
1780 unsigned long gcMask;
1781
1782 Blt_ResetTextStyle(graphPtr->tkwin, &axisPtr->titleTextStyle);
1783 Blt_ResetTextStyle(graphPtr->tkwin, &axisPtr->tickTextStyle);
1784 Blt_ResetTextStyle(graphPtr->tkwin, &axisPtr->limitsTextStyle);
1785
1786 gcMask = (GCForeground | GCLineWidth | GCCapStyle);
1787 gcValues.foreground = axisPtr->tickTextStyle.color->pixel;
1788 gcValues.line_width = LineWidth(axisPtr->lineWidth);
1789 gcValues.cap_style = CapProjecting;
1790
1791 newGC = Tk_GetGC(graphPtr->tkwin, gcMask, &gcValues);
1792 if (axisPtr->tickGC != NULL) {
1793 Tk_FreeGC(graphPtr->display, axisPtr->tickGC);
1794 }
1795 axisPtr->tickGC = newGC;
1796 }
1797
1798 /*
1799 * ----------------------------------------------------------------------
1800 *
1801 * DestroyAxis --
1802 *
1803 * Results:
1804 * None.
1805 *
1806 * Side effects:
1807 * Resources (font, color, gc, labels, etc.) associated with the
1808 * axis are deallocated.
1809 *
1810 * ----------------------------------------------------------------------
1811 */
1812 static void
1813 DestroyAxis(graphPtr, axisPtr)
1814 Graph *graphPtr;
1815 Axis *axisPtr;
1816 {
1817 int flags;
1818
1819 flags = Blt_GraphType(graphPtr);
1820 Tk_FreeOptions(configSpecs, (char *)axisPtr, graphPtr->display, flags);
1821 if (graphPtr->bindTable != NULL) {
1822 Blt_DeleteBindings(graphPtr->bindTable, axisPtr);
1823 }
1824 if (axisPtr->linkPtr != NULL) {
1825 Blt_ChainDeleteLink(axisPtr->chainPtr, axisPtr->linkPtr);
1826 }
1827 if (axisPtr->name != NULL) {
1828 Blt_Free(axisPtr->name);
1829 }
1830 if (axisPtr->hashPtr != NULL) {
1831 Blt_DeleteHashEntry(&graphPtr->axes.table, axisPtr->hashPtr);
1832 }
1833 Blt_FreeTextStyle(graphPtr->display, &axisPtr->titleTextStyle);
1834 Blt_FreeTextStyle(graphPtr->display, &axisPtr->limitsTextStyle);
1835 Blt_FreeTextStyle(graphPtr->display, &axisPtr->tickTextStyle);
1836
1837 if (axisPtr->tickGC != NULL) {
1838 Tk_FreeGC(graphPtr->display, axisPtr->tickGC);
1839 }
1840 if (axisPtr->t1Ptr != NULL) {
1841 Blt_Free(axisPtr->t1Ptr);
1842 }
1843 if (axisPtr->t2Ptr != NULL) {
1844 Blt_Free(axisPtr->t2Ptr);
1845 }
1846 if (axisPtr->limitsFormats != NULL) {
1847 Blt_Free(axisPtr->limitsFormats);
1848 }
1849 FreeLabels(axisPtr->tickLabels);
1850 Blt_ChainDestroy(axisPtr->tickLabels);
1851 if (axisPtr->segments != NULL) {
1852 Blt_Free(axisPtr->segments);
1853 }
1854 if (axisPtr->tags != NULL) {
1855 Blt_Free(axisPtr->tags);
1856 }
1857 Blt_Free(axisPtr);
1858 }
1859
1860 static double titleRotate[4] = /* Rotation for each axis title */
1861 {
1862 0.0, 90.0, 0.0, 270.0
1863 };
1864
1865 /*
1866 * ----------------------------------------------------------------------
1867 *
1868 * AxisOffsets --
1869 *
1870 * Determines the sites of the axis, major and minor ticks,
1871 * and title of the axis.
1872 *
1873 * Results:
1874 * None.
1875 *
1876 * ----------------------------------------------------------------------
1877 */
1878 static void
1879 AxisOffsets(graphPtr, axisPtr, margin, axisOffset, infoPtr)
1880 Graph *graphPtr;
1881 Axis *axisPtr;
1882 int margin;
1883 int axisOffset;
1884 AxisInfo *infoPtr;
1885 {
1886 int pad; /* Offset of axis from interior region. This
1887 * includes a possible border and the axis
1888 * line width. */
1889 int p;
1890 int majorOffset, minorOffset, labelOffset;
1891 int offset;
1892 int x, y;
1893
1894 axisPtr->titleTextStyle.theta = titleRotate[margin];
1895
1896 majorOffset = minorOffset = 0;
1897 labelOffset = AXIS_TITLE_PAD;
1898 if (axisPtr->lineWidth > 0) {
1899 majorOffset = ABS(axisPtr->tickLength);
1900 minorOffset = 10 * majorOffset / 15;
1901 labelOffset = majorOffset + AXIS_TITLE_PAD + axisPtr->lineWidth / 2;
1902 }
1903 /* Adjust offset for the interior border width and the line width */
1904 pad = axisPtr->lineWidth + 1;
1905 if (graphPtr->plotBorderWidth > 0) {
1906 pad += graphPtr->plotBorderWidth + 1;
1907 }
1908 offset = axisOffset + 1 + pad;
1909 if ((margin == MARGIN_LEFT) || (margin == MARGIN_TOP)) {
1910 majorOffset = -majorOffset;
1911 minorOffset = -minorOffset;
1912 labelOffset = -labelOffset;
1913 }
1914 /*
1915 * Pre-calculate the x-coordinate positions of the axis, tick labels, and
1916 * the individual major and minor ticks.
1917 */
1918 p = 0; /* Suppress compiler warning */
1919
1920 switch (margin) {
1921 case MARGIN_TOP:
1922 p = graphPtr->top - axisOffset - pad;
1923 if (axisPtr->titleAlternate) {
1924 x = graphPtr->right + AXIS_TITLE_PAD;
1925 y = graphPtr->top - axisOffset - (axisPtr->height / 2);
1926 axisPtr->titleTextStyle.anchor = TK_ANCHOR_W;
1927 } else {
1928 x = (graphPtr->right + graphPtr->left) / 2;
1929 y = graphPtr->top - axisOffset - axisPtr->height - AXIS_TITLE_PAD;
1930 axisPtr->titleTextStyle.anchor = TK_ANCHOR_N;
1931 }
1932 axisPtr->tickTextStyle.anchor = TK_ANCHOR_S;
1933 offset = axisPtr->borderWidth + axisPtr->lineWidth / 2;
1934 axisPtr->region.left = graphPtr->hOffset - offset - 2;
1935 axisPtr->region.right = graphPtr->hOffset + graphPtr->hRange +
1936 offset - 1;
1937 axisPtr->region.top = p + labelOffset - 1;
1938 axisPtr->region.bottom = p;
1939 axisPtr->titlePos.x = x;
1940 axisPtr->titlePos.y = y;
1941 break;
1942
1943 case MARGIN_BOTTOM:
1944 p = graphPtr->bottom + axisOffset + pad;
1945 if (axisPtr->titleAlternate) {
1946 x = graphPtr->right + AXIS_TITLE_PAD;
1947 y = graphPtr->bottom + axisOffset + (axisPtr->height / 2);
1948 axisPtr->titleTextStyle.anchor = TK_ANCHOR_W;
1949 } else {
1950 x = (graphPtr->right + graphPtr->left) / 2;
1951 y = graphPtr->bottom + axisOffset + axisPtr->height +
1952 AXIS_TITLE_PAD;
1953 axisPtr->titleTextStyle.anchor = TK_ANCHOR_S;
1954 }
1955 axisPtr->tickTextStyle.anchor = TK_ANCHOR_N;
1956 offset = axisPtr->borderWidth + axisPtr->lineWidth / 2;
1957 axisPtr->region.left = graphPtr->hOffset - offset - 2;
1958 axisPtr->region.right = graphPtr->hOffset + graphPtr->hRange +
1959 offset - 1;
1960
1961 axisPtr->region.top = graphPtr->bottom + axisOffset +
1962 axisPtr->lineWidth - axisPtr->lineWidth / 2;
1963 axisPtr->region.bottom = graphPtr->bottom + axisOffset +
1964 axisPtr->lineWidth + labelOffset + 1;
1965 axisPtr->titlePos.x = x;
1966 axisPtr->titlePos.y = y;
1967 break;
1968
1969 case MARGIN_LEFT:
1970 p = graphPtr->left - axisOffset - pad;
1971 if (axisPtr->titleAlternate) {
1972 x = graphPtr->left - axisOffset - (axisPtr->width / 2);
1973 y = graphPtr->top - AXIS_TITLE_PAD;
1974 axisPtr->titleTextStyle.anchor = TK_ANCHOR_SW;
1975 } else {
1976 x = graphPtr->left - axisOffset - axisPtr->width -
1977 graphPtr->plotBorderWidth;
1978 y = (graphPtr->bottom + graphPtr->top) / 2;
1979 axisPtr->titleTextStyle.anchor = TK_ANCHOR_W;
1980 }
1981 axisPtr->tickTextStyle.anchor = TK_ANCHOR_E;
1982 axisPtr->region.left = graphPtr->left - offset + labelOffset - 1;
1983 axisPtr->region.right = graphPtr->left - offset + 2;
1984
1985 offset = axisPtr->borderWidth + axisPtr->lineWidth / 2;
1986 axisPtr->region.top = graphPtr->vOffset - offset - 2;
1987 axisPtr->region.bottom = graphPtr->vOffset + graphPtr->vRange +
1988 offset - 1;
1989 axisPtr->titlePos.x = x;
1990 axisPtr->titlePos.y = y;
1991 break;
1992
1993 case MARGIN_RIGHT:
1994 p = graphPtr->right + axisOffset + pad;
1995 if (axisPtr->titleAlternate) {
1996 x = graphPtr->right + axisOffset + (axisPtr->width / 2);
1997 y = graphPtr->top - AXIS_TITLE_PAD;
1998 axisPtr->titleTextStyle.anchor = TK_ANCHOR_SE;
1999 } else {
2000 x = graphPtr->right + axisOffset + axisPtr->width +
2001 AXIS_TITLE_PAD;
2002 y = (graphPtr->bottom + graphPtr->top) / 2;
2003 axisPtr->titleTextStyle.anchor = TK_ANCHOR_E;
2004 }
2005 axisPtr->tickTextStyle.anchor = TK_ANCHOR_W;
2006
2007 axisPtr->region.left = graphPtr->right + axisOffset +
2008 axisPtr->lineWidth - axisPtr->lineWidth / 2;
2009 axisPtr->region.right = graphPtr->right + axisOffset +
2010 labelOffset + axisPtr->lineWidth + 1;
2011
2012 offset = axisPtr->borderWidth + axisPtr->lineWidth / 2;
2013 axisPtr->region.top = graphPtr->vOffset - offset - 2;
2014 axisPtr->region.bottom = graphPtr->vOffset + graphPtr->vRange +
2015 offset - 1;
2016 axisPtr->titlePos.x = x;
2017 axisPtr->titlePos.y = y;
2018 break;
2019
2020 case MARGIN_NONE:
2021 break;
2022 }
2023 infoPtr->axis = p - (axisPtr->lineWidth / 2);
2024 infoPtr->t1 = p + majorOffset;
2025 infoPtr->t2 = p + minorOffset;
2026 infoPtr->label = p + labelOffset;
2027
2028 if (axisPtr->tickLength < 0) {
2029 int hold;
2030
2031 hold = infoPtr->t1;
2032 infoPtr->t1 = infoPtr->axis;
2033 infoPtr->axis = hold;
2034 }
2035 }
2036
2037 static void
2038 MakeAxisLine(graphPtr, axisPtr, line, segPtr)
2039 Graph *graphPtr;
2040 Axis *axisPtr; /* Axis information */
2041 int line;
2042 Segment2D *segPtr;
2043 {
2044 double min, max;
2045
2046 min = axisPtr->axisRange.min;
2047 max = axisPtr->axisRange.max;
2048 if (axisPtr->logScale) {
2049 min = EXP10(min);
2050 max = EXP10(max);
2051 }
2052 if (AxisIsHorizontal(graphPtr, axisPtr)) {
2053 segPtr->p.x = Blt_HMap(graphPtr, axisPtr, min);
2054 segPtr->q.x = Blt_HMap(graphPtr, axisPtr, max);
2055 segPtr->p.y = segPtr->q.y = line;
2056 } else {
2057 segPtr->q.x = segPtr->p.x = line;
2058 segPtr->p.y = Blt_VMap(graphPtr, axisPtr, min);
2059 segPtr->q.y = Blt_VMap(graphPtr, axisPtr, max);
2060 }
2061 }
2062
2063
2064 static void
2065 MakeTick(graphPtr, axisPtr, value, tick, line, segPtr)
2066 Graph *graphPtr;
2067 Axis *axisPtr;
2068 double value;
2069 int tick, line; /* Lengths of tick and axis line. */
2070 Segment2D *segPtr;
2071 {
2072 if (axisPtr->logScale) {
2073 value = EXP10(value);
2074 }
2075 if (AxisIsHorizontal(graphPtr, axisPtr)) {
2076 segPtr->p.x = segPtr->q.x = Blt_HMap(graphPtr, axisPtr, value);
2077 segPtr->p.y = line;
2078 segPtr->q.y = tick;
2079 } else {
2080 segPtr->p.x = line;
2081 segPtr->p.y = segPtr->q.y = Blt_VMap(graphPtr, axisPtr, value);
2082 segPtr->q.x = tick;
2083 }
2084 }
2085
2086 /*
2087 * -----------------------------------------------------------------
2088 *
2089 * MapAxis --
2090 *
2091 * Pre-calculates positions of the axis, ticks, and labels (to be
2092 * used later when displaying the axis). Calculates the values
2093 * for each major and minor tick and checks to see if they are in
2094 * range (the outer ticks may be outside of the range of plotted
2095 * values).
2096 *
2097 * Line segments for the minor and major ticks are saved into one
2098 * XSegment array so that they can be drawn by a single
2099 * XDrawSegments call. The positions of the tick labels are also
2100 * computed and saved.
2101 *
2102 * Results:
2103 * None.
2104 *
2105 * Side Effects:
2106 * Line segments and tick labels are saved and used later to draw
2107 * the axis.
2108 *
2109 * -----------------------------------------------------------------
2110 */
2111 static void
2112 MapAxis(graphPtr, axisPtr, offset, margin)
2113 Graph *graphPtr;
2114 Axis *axisPtr;
2115 int offset;
2116 int margin;
2117 {
2118 int arraySize;
2119 int nMajorTicks, nMinorTicks;
2120 AxisInfo info;
2121 Segment2D *segments;
2122 Segment2D *segPtr;
2123
2124 AxisOffsets(graphPtr, axisPtr, margin, offset, &info);
2125
2126 /* Save all line coordinates in an array of line segments. */
2127
2128 if (axisPtr->segments != NULL) {
2129 Blt_Free(axisPtr->segments);
2130 }
2131 nMajorTicks = nMinorTicks = 0;
2132 if (axisPtr->t1Ptr != NULL) {
2133 nMajorTicks = axisPtr->t1Ptr->nTicks;
2134 }
2135 if (axisPtr->t2Ptr != NULL) {
2136 nMinorTicks = axisPtr->t2Ptr->nTicks;
2137 }
2138 arraySize = 1 + (nMajorTicks * (nMinorTicks + 1));
2139 segments = Blt_Malloc(arraySize * sizeof(Segment2D));
2140 assert(segments);
2141
2142 segPtr = segments;
2143 if (axisPtr->lineWidth > 0) {
2144 /* Axis baseline */
2145 MakeAxisLine(graphPtr, axisPtr, info.axis, segPtr);
2146 segPtr++;
2147 }
2148 if (axisPtr->showTicks) {
2149 double t1, t2;
2150 double labelPos;
2151 register int i, j;
2152 int isHoriz;
2153 TickLabel *labelPtr;
2154 Blt_ChainLink *linkPtr;
2155 Segment2D seg;
2156
2157 isHoriz = AxisIsHorizontal(graphPtr, axisPtr);
2158 for (i = 0; i < axisPtr->t1Ptr->nTicks; i++) {
2159 t1 = axisPtr->t1Ptr->values[i];
2160 /* Minor ticks */
2161 for (j = 0; j < axisPtr->t2Ptr->nTicks; j++) {
2162 t2 = t1 +
2163 (axisPtr->majorSweep.step * axisPtr->t2Ptr->values[j]);
2164 if (InRange(t2, &axisPtr->axisRange)) {
2165 MakeTick(graphPtr, axisPtr, t2, info.t2, info.axis,
2166 segPtr);
2167 segPtr++;
2168 }
2169 }
2170 if (!InRange(t1, &axisPtr->axisRange)) {
2171 continue;
2172 }
2173 /* Major tick */
2174 MakeTick(graphPtr, axisPtr, t1, info.t1, info.axis, segPtr);
2175 segPtr++;
2176 }
2177
2178 linkPtr = Blt_ChainFirstLink(axisPtr->tickLabels);
2179 labelPos = (double)info.label;
2180
2181 for (i = 0; i < axisPtr->t1Ptr->nTicks; i++) {
2182 t1 = axisPtr->t1Ptr->values[i];
2183 if (axisPtr->labelOffset) {
2184 t1 += axisPtr->majorSweep.step * 0.5;
2185 }
2186 if (!InRange(t1, &axisPtr->axisRange)) {
2187 continue;
2188 }
2189 labelPtr = Blt_ChainGetValue(linkPtr);
2190 linkPtr = Blt_ChainNextLink(linkPtr);
2191 MakeTick(graphPtr, axisPtr, t1, info.t1, info.axis, &seg);
2192 /* Save tick label X-Y position. */
2193 if (isHoriz) {
2194 labelPtr->anchorPos.x = seg.p.x;
2195 labelPtr->anchorPos.y = labelPos;
2196 } else {
2197 labelPtr->anchorPos.x = labelPos;
2198 labelPtr->anchorPos.y = seg.p.y;
2199 }
2200 }
2201 }
2202 if (AxisIsHorizontal(graphPtr, axisPtr)) {
2203 axisPtr->width = graphPtr->right - graphPtr->left;
2204 } else {
2205 axisPtr->height = graphPtr->bottom - graphPtr->top;
2206 }
2207 axisPtr->segments = segments;
2208 axisPtr->nSegments = segPtr - segments;
2209 assert(axisPtr->nSegments <= arraySize);
2210 }
2211
2212 /*
2213 *----------------------------------------------------------------------
2214 *
2215 * AdjustViewport --
2216 *
2217 * Adjusts the offsets of the viewport according to the scroll mode.
2218 * This is to accommodate both "listbox" and "canvas" style scrolling.
2219 *
2220 * "canvas" The viewport scrolls within the range of world
2221 * coordinates. This way the viewport always displays
2222 * a full page of the world. If the world is smaller
2223 * than the viewport, then (bizarrely) the world and
2224 * viewport are inverted so that the world moves up
2225 * and down within the viewport.
2226 *
2227 * "listbox" The viewport can scroll beyond the range of world
2228 * coordinates. Every entry can be displayed at the
2229 * top of the viewport. This also means that the
2230 * scrollbar thumb weirdly shrinks as the last entry
2231 * is scrolled upward.
2232 *
2233 * Results:
2234 * The corrected offset is returned.
2235 *
2236 *----------------------------------------------------------------------
2237 */
2238 static double
2239 AdjustViewport(offset, windowSize)
2240 double offset, windowSize;
2241 {
2242 /*
2243 * Canvas-style scrolling allows the world to be scrolled
2244 * within the window.
2245 */
2246 if (windowSize > 1.0) {
2247 if (windowSize < (1.0 - offset)) {
2248 offset = 1.0 - windowSize;
2249 }
2250 if (offset > 0.0) {
2251 offset = 0.0;
2252 }
2253 } else {
2254 if ((offset + windowSize) > 1.0) {
2255 offset = 1.0 - windowSize;
2256 }
2257 if (offset < 0.0) {
2258 offset = 0.0;
2259 }
2260 }
2261 return offset;
2262 }
2263
2264 static int
2265 GetAxisScrollInfo(interp, argc, argv, offsetPtr, windowSize, scrollUnits)
2266 Tcl_Interp *interp;
2267 int argc;
2268 char **argv;
2269 double *offsetPtr;
2270 double windowSize;
2271 double scrollUnits;
2272 {
2273 char c;
2274 unsigned int length;
2275 double offset;
2276 int count;
2277 double fract;
2278
2279 offset = *offsetPtr;
2280 c = argv[0][0];
2281 length = strlen(argv[0]);
2282 if ((c == 's') && (strncmp(argv[0], "scroll", length) == 0)) {
2283 /* assert(argc == 3); */
2284 if (argc != 3) {
2285 Tcl_AppendResult(interp, "expected arg", 0);
2286 return TCL_ERROR;
2287 }
2288 /* scroll number unit/page */
2289 if (Tcl_GetInt(interp, argv[1], &count) != TCL_OK) {
2290 return TCL_ERROR;
2291 }
2292 c = argv[2][0];
2293 length = strlen(argv[2]);
2294 if ((c == 'u') && (strncmp(argv[2], "units", length) == 0)) {
2295 fract = (double)count * scrollUnits;
2296 } else if ((c == 'p') && (strncmp(argv[2], "pages", length) == 0)) {
2297 /* A page is 90% of the view-able window. */
2298 fract = (double)count * windowSize * 0.9;
2299 } else {
2300 Tcl_AppendResult(interp, "unknown \"scroll\" units \"", argv[2],
2301 "\"", (char *)NULL);
2302 return TCL_ERROR;
2303 }
2304 offset += fract;
2305 } else if ((c == 'm') && (strncmp(argv[0], "moveto", length) == 0)) {
2306 /*assert(argc == 2);*/
2307 if (argc != 2) {
2308 Tcl_AppendResult(interp, "expected arg", 0);
2309 return TCL_ERROR;
2310 }
2311 /* moveto fraction */
2312 if (Tcl_GetDouble(interp, argv[1], &fract) != TCL_OK) {
2313 return TCL_ERROR;
2314 }
2315 offset = fract;
2316 } else {
2317 /* Treat like "scroll units" */
2318 if (Tcl_GetInt(interp, argv[0], &count) != TCL_OK) {
2319 return TCL_ERROR;
2320 }
2321 fract = (double)count * scrollUnits;
2322 offset += fract;
2323 /* CHECK THIS: return TCL_OK; */
2324 }
2325 *offsetPtr = AdjustViewport(offset, windowSize);
2326 return TCL_OK;
2327 }
2328
2329 /*
2330 * -----------------------------------------------------------------
2331 *
2332 * DrawAxis --
2333 *
2334 * Draws the axis, ticks, and labels onto the canvas.
2335 *
2336 * Initializes and passes text attribute information through
2337 * TextStyle structure.
2338 *
2339 * Results:
2340 * None.
2341 *
2342 * Side Effects:
2343 * Axis gets drawn on window.
2344 *
2345 * -----------------------------------------------------------------
2346 */
2347 static void
2348 DrawAxis(graphPtr, drawable, axisPtr)
2349 Graph *graphPtr;
2350 Drawable drawable;
2351 Axis *axisPtr;
2352 {
2353 if (axisPtr->border != NULL) {
2354 Blt_Fill3DRectangle(graphPtr->tkwin, drawable, axisPtr->border,
2355 axisPtr->region.left + graphPtr->plotBorderWidth,
2356 axisPtr->region.top + graphPtr->plotBorderWidth,
2357 axisPtr->region.right - axisPtr->region.left,
2358 axisPtr->region.bottom - axisPtr->region.top,
2359 axisPtr->borderWidth, axisPtr->relief);
2360 }
2361 if (axisPtr->title != NULL) {
2362 Blt_DrawText(graphPtr->tkwin, drawable, axisPtr->title,
2363 &axisPtr->titleTextStyle, (int)axisPtr->titlePos.x,
2364 (int)axisPtr->titlePos.y);
2365 }
2366 if (axisPtr->scrollCmdPrefix != NULL) {
2367 double viewWidth, viewMin, viewMax;
2368 double worldWidth, worldMin, worldMax;
2369 double fract;
2370 int isHoriz;
2371
2372 worldMin = axisPtr->valueRange.min;
2373 worldMax = axisPtr->valueRange.max;
2374 if (DEFINED(axisPtr->scrollMin)) {
2375 worldMin = axisPtr->scrollMin;
2376 }
2377 if (DEFINED(axisPtr->scrollMax)) {
2378 worldMax = axisPtr->scrollMax;
2379 }
2380 viewMin = axisPtr->min;
2381 viewMax = axisPtr->max;
2382 if (viewMin < worldMin) {
2383 viewMin = worldMin;
2384 }
2385 if (viewMax > worldMax) {
2386 viewMax = worldMax;
2387 }
2388 if (axisPtr->logScale) {
2389 worldMin = log10(worldMin);
2390 worldMax = log10(worldMax);
2391 viewMin = log10(viewMin);
2392 viewMax = log10(viewMax);
2393 }
2394 worldWidth = worldMax - worldMin;
2395 viewWidth = viewMax - viewMin;
2396 isHoriz = AxisIsHorizontal(graphPtr, axisPtr);
2397
2398 if (isHoriz != axisPtr->descending) {
2399 fract = (viewMin - worldMin) / worldWidth;
2400 } else {
2401 fract = (worldMax - viewMax) / worldWidth;
2402 }
2403 fract = AdjustViewport(fract, viewWidth / worldWidth);
2404
2405 if (isHoriz != axisPtr->descending) {
2406 viewMin = (fract * worldWidth);
2407 axisPtr->min = viewMin + worldMin;
2408 axisPtr->max = axisPtr->min + viewWidth;
2409 viewMax = viewMin + viewWidth;
2410 if (axisPtr->logScale) {
2411 axisPtr->min = EXP10(axisPtr->min);
2412 axisPtr->max = EXP10(axisPtr->max);
2413 }
2414 Blt_UpdateScrollbar(graphPtr->interp, axisPtr->scrollCmdPrefix,
2415 (viewMin / worldWidth), (viewMax / worldWidth));
2416 } else {
2417 viewMax = (fract * worldWidth);
2418 axisPtr->max = worldMax - viewMax;
2419 axisPtr->min = axisPtr->max - viewWidth;
2420 viewMin = viewMax + viewWidth;
2421 if (axisPtr->logScale) {
2422 axisPtr->min = EXP10(axisPtr->min);
2423 axisPtr->max = EXP10(axisPtr->max);
2424 }
2425 Blt_UpdateScrollbar(graphPtr->interp, axisPtr->scrollCmdPrefix,
2426 (viewMax / worldWidth), (viewMin / worldWidth));
2427 }
2428 }
2429 if (axisPtr->showTicks) {
2430 register Blt_ChainLink *linkPtr;
2431 TickLabel *labelPtr;
2432
2433 for (linkPtr = Blt_ChainFirstLink(axisPtr->tickLabels); linkPtr != NULL;
2434 linkPtr = Blt_ChainNextLink(linkPtr)) {
2435 /* Draw major tick labels */
2436 labelPtr = Blt_ChainGetValue(linkPtr);
2437 Blt_DrawText(graphPtr->tkwin, drawable, labelPtr->string,
2438 &axisPtr->tickTextStyle, (int)labelPtr->anchorPos.x,
2439 (int)labelPtr->anchorPos.y);
2440 }
2441 }
2442 if ((axisPtr->nSegments > 0) && (axisPtr->lineWidth > 0)) {
2443 /* Draw the tick marks and axis line. */
2444 Blt_Draw2DSegments(graphPtr->display, drawable, axisPtr->tickGC,
2445 axisPtr->segments, axisPtr->nSegments);
2446 }
2447 }
2448
2449 /*
2450 * -----------------------------------------------------------------
2451 *
2452 * AxisToPostScript --
2453 *
2454 * Generates PostScript output to draw the axis, ticks, and
2455 * labels.
2456 *
2457 * Initializes and passes text attribute information through
2458 * TextStyle structure.
2459 *
2460 * Results:
2461 * None.
2462 *
2463 * Side Effects:
2464 * PostScript output is left in graphPtr->interp->result;
2465 *
2466 * -----------------------------------------------------------------
2467 */
2468 /* ARGSUSED */
2469 static void
2470 AxisToPostScript(psToken, axisPtr)
2471 PsToken psToken;
2472 Axis *axisPtr;
2473 {
2474 if (axisPtr->title != NULL) {
2475 Blt_TextToPostScript(psToken, axisPtr->title, &axisPtr->titleTextStyle,
2476 axisPtr->titlePos.x, axisPtr->titlePos.y);
2477 }
2478 if (axisPtr->showTicks) {
2479 register Blt_ChainLink *linkPtr;
2480 TickLabel *labelPtr;
2481
2482 for (linkPtr = Blt_ChainFirstLink(axisPtr->tickLabels);
2483 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2484 labelPtr = Blt_ChainGetValue(linkPtr);
2485 Blt_TextToPostScript(psToken, labelPtr->string,
2486 &axisPtr->tickTextStyle, labelPtr->anchorPos.x,
2487 labelPtr->anchorPos.y);
2488 }
2489 }
2490 if ((axisPtr->nSegments > 0) && (axisPtr->lineWidth > 0)) {
2491 Blt_LineAttributesToPostScript(psToken, axisPtr->tickTextStyle.color,
2492 axisPtr->lineWidth, (Blt_Dashes *)NULL, CapButt, JoinMiter);
2493 Blt_2DSegmentsToPostScript(psToken, axisPtr->segments,
2494 axisPtr->nSegments);
2495 }
2496 }
2497
2498 static void
2499 MakeGridLine(graphPtr, axisPtr, value, segPtr)
2500 Graph *graphPtr;
2501 Axis *axisPtr;
2502 double value;
2503 Segment2D *segPtr;
2504 {
2505 if (axisPtr->logScale) {
2506 value = EXP10(value);
2507 }
2508 /* Grid lines run orthogonally to the axis */
2509 if (AxisIsHorizontal(graphPtr, axisPtr)) {
2510 segPtr->p.y = graphPtr->top;
2511 segPtr->q.y = graphPtr->bottom;
2512 segPtr->p.x = segPtr->q.x = Blt_HMap(graphPtr, axisPtr, value);
2513 } else {
2514 segPtr->p.x = graphPtr->left;
2515 segPtr->q.x = graphPtr->right;
2516 segPtr->p.y = segPtr->q.y = Blt_VMap(graphPtr, axisPtr, value);
2517 }
2518 }
2519
2520 /*
2521 *----------------------------------------------------------------------
2522 *
2523 * Blt_GetAxisSegments --
2524 *
2525 * Assembles the grid lines associated with an axis. Generates
2526 * tick positions if necessary (this happens when the axis is
2527 * not a logical axis too).
2528 *
2529 * Results:
2530 * None.
2531 *
2532 *----------------------------------------------------------------------
2533 */
2534 void
2535 Blt_GetAxisSegments(graphPtr, axisPtr, segPtrPtr, nSegmentsPtr)
2536 Graph *graphPtr;
2537 Axis *axisPtr;
2538 Segment2D **segPtrPtr;
2539 int *nSegmentsPtr;
2540 {
2541 int needed;
2542 Ticks *t1Ptr, *t2Ptr;
2543 register int i;
2544 double value;
2545 Segment2D *segments, *segPtr;
2546
2547 *nSegmentsPtr = 0;
2548 *segPtrPtr = NULL;
2549 if (axisPtr == NULL) {
2550 return;
2551 }
2552 t1Ptr = axisPtr->t1Ptr;
2553 if (t1Ptr == NULL) {
2554 t1Ptr = GenerateTicks(&axisPtr->majorSweep);
2555 }
2556 t2Ptr = axisPtr->t2Ptr;
2557 if (t2Ptr == NULL) {
2558 t2Ptr = GenerateTicks(&axisPtr->minorSweep);
2559 }
2560
2561 needed = t1Ptr->nTicks;
2562 if (graphPtr->gridPtr->minorGrid) {
2563 needed += (t1Ptr->nTicks * t2Ptr->nTicks);
2564 }
2565 if (needed == 0) {
2566 return;
2567 }
2568 segments = Blt_Malloc(sizeof(Segment2D) * needed);
2569 if (segments == NULL) {
2570 return; /* Can't allocate memory for grid. */
2571 }
2572
2573 segPtr = segments;
2574 for (i = 0; i < t1Ptr->nTicks; i++) {
2575 value = t1Ptr->values[i];
2576 if (graphPtr->gridPtr->minorGrid) {
2577 register int j;
2578 double subValue;
2579
2580 for (j = 0; j < t2Ptr->nTicks; j++) {
2581 subValue = value +
2582 (axisPtr->majorSweep.step * t2Ptr->values[j]);
2583 if (InRange(subValue, &axisPtr->axisRange)) {
2584 MakeGridLine(graphPtr, axisPtr, subValue, segPtr);
2585 segPtr++;
2586 }
2587 }
2588 }
2589 if (InRange(value, &axisPtr->axisRange)) {
2590 MakeGridLine(graphPtr, axisPtr, value, segPtr);
2591 segPtr++;
2592 }
2593 }
2594
2595 if (t1Ptr != axisPtr->t1Ptr) {
2596 Blt_Free(t1Ptr); /* Free generated ticks. */
2597 }
2598 if (t2Ptr != axisPtr->t2Ptr) {
2599 Blt_Free(t2Ptr); /* Free generated ticks. */
2600 }
2601 *nSegmentsPtr = segPtr - segments;
2602 assert(*nSegmentsPtr <= needed);
2603 *segPtrPtr = segments;
2604 }
2605
2606 /*
2607 *----------------------------------------------------------------------
2608 *
2609 * GetAxisGeometry --
2610 *
2611 * Results:
2612 * None.
2613 *
2614 *----------------------------------------------------------------------
2615 */
2616 static void
2617 GetAxisGeometry(graphPtr, axisPtr)
2618 Graph *graphPtr;
2619 Axis *axisPtr;
2620 {
2621 int height;
2622
2623 FreeLabels(axisPtr->tickLabels);
2624 height = 0;
2625 if (axisPtr->lineWidth > 0) {
2626 /* Leave room for axis baseline (and pad) */
2627 height += axisPtr->lineWidth + 2;
2628 }
2629 if (axisPtr->showTicks) {
2630 int pad;
2631 register int i, nLabels;
2632 int lw, lh;
2633 double x, x2;
2634 int maxWidth, maxHeight;
2635 TickLabel *labelPtr;
2636
2637 SweepTicks(axisPtr);
2638
2639 if (axisPtr->t1Ptr->nTicks < 0) {
2640 fprintf(stderr, "%s major ticks can't be %d\n",
2641 axisPtr->name, axisPtr->t1Ptr->nTicks);
2642 abort();
2643 }
2644 if (axisPtr->t1Ptr->nTicks > MAXTICKS) {
2645 fprintf(stderr, "too big, %s major ticks can't be %d\n",
2646 axisPtr->name, axisPtr->t1Ptr->nTicks);
2647 abort();
2648 }
2649
2650 maxHeight = maxWidth = 0;
2651 nLabels = 0;
2652 for (i = 0; i < axisPtr->t1Ptr->nTicks; i++) {
2653 x2 = x = axisPtr->t1Ptr->values[i];
2654 if (axisPtr->labelOffset) {
2655 x2 += axisPtr->majorSweep.step * 0.5;
2656 }
2657 if (!InRange(x2, &axisPtr->axisRange)) {
2658 continue;
2659 }
2660 labelPtr = MakeLabel(graphPtr, axisPtr, x);
2661 Blt_ChainAppend(axisPtr->tickLabels, labelPtr);
2662 nLabels++;
2663 /*
2664 * Get the dimensions of each tick label.
2665 * Remember tick labels can be multi-lined and/or rotated.
2666 */
2667 Blt_GetTextExtents(&axisPtr->tickTextStyle, labelPtr->string,
2668 &lw, &lh);
2669 labelPtr->width = lw;
2670 labelPtr->height = lh;
2671
2672 if (axisPtr->tickTextStyle.theta > 0.0) {
2673 double rotWidth, rotHeight;
2674
2675 Blt_GetBoundingBox(lw, lh, axisPtr->tickTextStyle.theta,
2676 &rotWidth, &rotHeight, (Point2D *)NULL);
2677 lw = ROUND(rotWidth);
2678 lh = ROUND(rotHeight);
2679 }
2680 if (maxWidth < lw) {
2681 maxWidth = lw;
2682 }
2683 if (maxHeight < lh) {
2684 maxHeight = lh;
2685 }
2686 }
2687 assert(nLabels <= axisPtr->t1Ptr->nTicks);
2688
2689 /* Because the axis cap style is "CapProjecting", we need to
2690 * account for an extra 1.5 linewidth at the end of each
2691 * line. */
2692
2693 pad = ((axisPtr->lineWidth * 15) / 10);
2694
2695 if (AxisIsHorizontal(graphPtr, axisPtr)) {
2696 height += maxHeight + pad;
2697 } else {
2698 height += maxWidth + pad;
2699 }
2700 if (axisPtr->lineWidth > 0) {
2701 /* Distance from axis line to tick label. */
2702 height += AXIS_TITLE_PAD;
2703 height += ABS(axisPtr->tickLength);
2704 }
2705 }
2706
2707 if (axisPtr->title != NULL) {
2708 if (axisPtr->titleAlternate) {
2709 if (height < axisPtr->titleHeight) {
2710 height = axisPtr->titleHeight;
2711 }
2712 } else {
2713 height += axisPtr->titleHeight + AXIS_TITLE_PAD;
2714 }
2715 }
2716
2717 /* Correct for orientation of the axis. */
2718 if (AxisIsHorizontal(graphPtr, axisPtr)) {
2719 axisPtr->height = height;
2720 } else {
2721 axisPtr->width = height;
2722 }
2723 }
2724
2725 /*
2726 *----------------------------------------------------------------------
2727 *
2728 * GetMarginGeometry --
2729 *
2730 * Examines all the axes in the given margin and determines the
2731 * area required to display them.
2732 *
2733 * Note: For multiple axes, the titles are displayed in another
2734 * margin. So we must keep track of the widest title.
2735 *
2736 * Results:
2737 * Returns the width or height of the margin, depending if it
2738 * runs horizontally along the graph or vertically.
2739 *
2740 * Side Effects:
2741 * The area width and height set in the margin. Note again that
2742 * this may be corrected later (mulitple axes) to adjust for
2743 * the longest title in another margin.
2744 *
2745 *----------------------------------------------------------------------
2746 */
2747 static int
2748 GetMarginGeometry(graphPtr, marginPtr)
2749 Graph *graphPtr;
2750 Margin *marginPtr;
2751 {
2752 Blt_ChainLink *linkPtr;
2753 Axis *axisPtr;
2754 int width, height;
2755 int isHoriz;
2756 int length, count;
2757
2758 isHoriz = HORIZMARGIN(marginPtr);
2759 /* Count the number of visible axes. */
2760 count = 0;
2761 length = width = height = 0;
2762 for (linkPtr = Blt_ChainFirstLink(marginPtr->axes); linkPtr != NULL;
2763 linkPtr = Blt_ChainNextLink(linkPtr)) {
2764 axisPtr = Blt_ChainGetValue(linkPtr);
2765 if ((!axisPtr->hidden) && (axisPtr->flags & AXIS_ONSCREEN)) {
2766 count++;
2767 if (graphPtr->flags & GET_AXIS_GEOMETRY) {
2768 GetAxisGeometry(graphPtr, axisPtr);
2769 }
2770 if ((axisPtr->titleAlternate) && (length < axisPtr->titleWidth)) {
2771 length = axisPtr->titleWidth;
2772 }
2773 if (isHoriz) {
2774 height += axisPtr->height;
2775 } else {
2776 width += axisPtr->width;
2777 }
2778 }
2779 }
2780 /* Enforce a minimum size for margins. */
2781 if (width < 3) {
2782 width = 3;
2783 }
2784 if (height < 3) {
2785 height = 3;
2786 }
2787 marginPtr->nAxes = count;
2788 marginPtr->axesTitleLength = length;
2789 marginPtr->width = width;
2790 marginPtr->height = height;
2791 marginPtr->axesOffset = (HORIZMARGIN(marginPtr)) ? height : width;
2792 return marginPtr->axesOffset;
2793 }
2794
2795 /*
2796 *----------------------------------------------------------------------
2797 *
2798 * ComputeMargins --
2799 *
2800 * Computes the size of the margins and the plotting area. We
2801 * first compute the space needed for the axes in each margin.
2802 * Then how much space the legend will occupy. Finally, if the
2803 * user has requested a margin size, we override the computed
2804 * value.
2805 *
2806 * Results:
2807 *
2808 *---------------------------------------------------------------------- */
2809 static void
2810 ComputeMargins(graphPtr)
2811 Graph *graphPtr;
2812 {
2813 int left, right, top, bottom;
2814 int width, height;
2815 int insets;
2816
2817 /*
2818 * Step 1: Compute the amount of space needed to display the
2819 * axes (there many be 0 or more) associated with the
2820 * margin.
2821 */
2822 top = GetMarginGeometry(graphPtr, &graphPtr->topMargin);
2823 bottom = GetMarginGeometry(graphPtr, &graphPtr->bottomMargin);
2824 left = GetMarginGeometry(graphPtr, &graphPtr->leftMargin);
2825 right = GetMarginGeometry(graphPtr, &graphPtr->rightMargin);
2826
2827 /*
2828 * Step 2: Add the graph title height to the top margin.
2829 */
2830 if (graphPtr->title != NULL) {
2831 top += graphPtr->titleTextStyle.height;
2832 }
2833 insets = 2 * (graphPtr->inset + graphPtr->plotBorderWidth);
2834
2835 /*
2836 * Step 3: Use the current estimate of the plot area to compute
2837 * the legend size. Add it to the proper margin.
2838 */
2839 width = graphPtr->width - (insets + left + right);
2840 height = graphPtr->height - (insets + top + bottom);
2841 Blt_MapLegend(graphPtr->legend, width, height);
2842 if (!Blt_LegendIsHidden(graphPtr->legend)) {
2843 switch (Blt_LegendSite(graphPtr->legend)) {
2844 case LEGEND_RIGHT:
2845 right += Blt_LegendWidth(graphPtr->legend) + 2;
2846 break;
2847 case LEGEND_LEFT:
2848 left += Blt_LegendWidth(graphPtr->legend) + 2;
2849 break;
2850 case LEGEND_TOP:
2851 top += Blt_LegendHeight(graphPtr->legend) + 2;
2852 break;
2853 case LEGEND_BOTTOM:
2854 bottom += Blt_LegendHeight(graphPtr->legend) + 2;
2855 break;
2856 case LEGEND_XY:
2857 case LEGEND_PLOT:
2858 case LEGEND_WINDOW:
2859 /* Do nothing. */
2860 break;
2861 }
2862 }
2863
2864 /*
2865 * Recompute the plotarea, now accounting for the legend.
2866 */
2867 width = graphPtr->width - (insets + left + right);
2868 height = graphPtr->height - (insets + top + bottom);
2869
2870 /*
2871 * Step 5: If necessary, correct for the requested plot area
2872 * aspect ratio.
2873 */
2874 if (graphPtr->aspect > 0.0) {
2875 double ratio;
2876
2877 /*
2878 * Shrink one dimension of the plotarea to fit the requested
2879 * width/height aspect ratio.
2880 */
2881 ratio = (double)width / (double)height;
2882 if (ratio > graphPtr->aspect) {
2883 int scaledWidth;
2884
2885 /* Shrink the width. */
2886 scaledWidth = (int)(height * graphPtr->aspect);
2887 if (scaledWidth < 1) {
2888 scaledWidth = 1;
2889 }
2890 right += (width - scaledWidth); /* Add the difference to
2891 * the right margin. */
2892 /* CHECK THIS: width = scaledWidth; */
2893 } else {
2894 int scaledHeight;
2895
2896 /* Shrink the height. */
2897 scaledHeight = (int)(width / graphPtr->aspect);
2898 if (scaledHeight < 1) {
2899 scaledHeight = 1;
2900 }
2901 top += (height - scaledHeight); /* Add the difference to
2902 * the top margin. */
2903 /* CHECK THIS: height = scaledHeight; */
2904 }
2905 }
2906
2907 /*
2908 * Step 6: If there's multiple axes in a margin, the axis
2909 * titles will be displayed in the adjoining marging.
2910 * Make sure there's room for the longest axis titles.
2911 */
2912
2913 if (top < graphPtr->leftMargin.axesTitleLength) {
2914 top = graphPtr->leftMargin.axesTitleLength;
2915 }
2916 if (right < graphPtr->bottomMargin.axesTitleLength) {
2917 right = graphPtr->bottomMargin.axesTitleLength;
2918 }
2919 if (top < graphPtr->rightMargin.axesTitleLength) {
2920 top = graphPtr->rightMargin.axesTitleLength;
2921 }
2922 if (right < graphPtr->topMargin.axesTitleLength) {
2923 right = graphPtr->topMargin.axesTitleLength;
2924 }
2925
2926 /*
2927 * Step 7: Override calculated values with requested margin
2928 * sizes.
2929 */
2930
2931 graphPtr->leftMargin.width = left;
2932 graphPtr->rightMargin.width = right;
2933 graphPtr->topMargin.height = top;
2934 graphPtr->bottomMargin.height = bottom;
2935
2936 if (graphPtr->leftMargin.reqSize > 0) {
2937 graphPtr->leftMargin.width = graphPtr->leftMargin.reqSize;
2938 }
2939 if (graphPtr->rightMargin.reqSize > 0) {
2940 graphPtr->rightMargin.width = graphPtr->rightMargin.reqSize;
2941 }
2942 if (graphPtr->topMargin.reqSize > 0) {
2943 graphPtr->topMargin.height = graphPtr->topMargin.reqSize;
2944 }
2945 if (graphPtr->bottomMargin.reqSize > 0) {
2946 graphPtr->bottomMargin.height = graphPtr->bottomMargin.reqSize;
2947 }
2948 }
2949
2950 /*
2951 * -----------------------------------------------------------------
2952 *
2953 * Blt_LayoutMargins --
2954 *
2955 * Calculate the layout of the graph. Based upon the data,
2956 * axis limits, X and Y titles, and title height, determine
2957 * the cavity left which is the plotting surface. The first
2958 * step get the data and axis limits for calculating the space
2959 * needed for the top, bottom, left, and right margins.
2960 *
2961 * 1) The LEFT margin is the area from the left border to the
2962 * Y axis (not including ticks). It composes the border
2963 * width, the width an optional Y axis label and its padding,
2964 * and the tick numeric labels. The Y axis label is rotated
2965 * 90 degrees so that the width is the font height.
2966 *
2967 * 2) The RIGHT margin is the area from the end of the graph
2968 * to the right window border. It composes the border width,
2969 * some padding, the font height (this may be dubious. It
2970 * appears to provide a more even border), the max of the
2971 * legend width and 1/2 max X tick number. This last part is
2972 * so that the last tick label is not clipped.
2973 *
2974 * Window Width
2975 * ___________________________________________________________
2976 * | | | |
2977 * | | TOP height of title | |
2978 * | | | |
2979 * | | x2 title | |
2980 * | | | |
2981 * | | height of x2-axis | |
2982 * |__________|_______________________________|_______________| W
2983 * | | -plotpady | | i
2984 * |__________|_______________________________|_______________| n
2985 * | | top right | | d
2986 * | | | | o
2987 * | LEFT | | RIGHT | w
2988 * | | | |
2989 * | y | Free area = 104% | y2 | H
2990 * | | Plotting surface = 100% | | e
2991 * | t | Tick length = 2 + 2% | t | i
2992 * | i | | i | g
2993 * | t | | t legend| h
2994 * | l | | l width| t
2995 * | e | | e |
2996 * | height| |height |
2997 * | of | | of |
2998 * | y-axis| |y2-axis |
2999 * | | | |
3000 * | |origin 0,0 | |
3001 * |__________|_left___________________bottom___|_______________|
3002 * | |-plotpady | |
3003 * |__________|_______________________________|_______________|
3004 * | | (xoffset, yoffset) | |
3005 * | | | |
3006 * | | height of x-axis | |
3007 * | | | |
3008 * | | BOTTOM x title | |
3009 * |__________|_______________________________|_______________|
3010 *
3011 * 3) The TOP margin is the area from the top window border to the top
3012 * of the graph. It composes the border width, twice the height of
3013 * the title font (if one is given) and some padding between the
3014 * title.
3015 *
3016 * 4) The BOTTOM margin is area from the bottom window border to the
3017 * X axis (not including ticks). It composes the border width, the height
3018 * an optional X axis label and its padding, the height of the font
3019 * of the tick labels.
3020 *
3021 * The plotting area is between the margins which includes the X and Y axes
3022 * including the ticks but not the tick numeric labels. The length of
3023 * the ticks and its padding is 5% of the entire plotting area. Hence the
3024 * entire plotting area is scaled as 105% of the width and height of the
3025 * area.
3026 *
3027 * The axis labels, ticks labels, title, and legend may or may not be
3028 * displayed which must be taken into account.
3029 *
3030 *
3031 * -----------------------------------------------------------------
3032 */
3033 void
3034 Blt_LayoutMargins(graphPtr)
3035 Graph *graphPtr;
3036 {
3037 int width, height;
3038 int titleY;
3039 int left, right, top, bottom;
3040
3041 ComputeMargins(graphPtr);
3042 left = graphPtr->leftMargin.width + graphPtr->inset +
3043 graphPtr->plotBorderWidth;
3044 right = graphPtr->rightMargin.width + graphPtr->inset +
3045 graphPtr->plotBorderWidth;
3046 top = graphPtr->topMargin.height + graphPtr->inset +
3047 graphPtr->plotBorderWidth;
3048 bottom = graphPtr->bottomMargin.height + graphPtr->inset +
3049 graphPtr->plotBorderWidth;
3050
3051 /* Based upon the margins, calculate the space left for the graph. */
3052 width = graphPtr->width - (left + right);
3053 height = graphPtr->height - (top + bottom);
3054 if (width < 1) {
3055 width = 1;
3056 }
3057 if (height < 1) {
3058 height = 1;
3059 }
3060 graphPtr->left = left;
3061 graphPtr->right = left + width;
3062 graphPtr->bottom = top + height;
3063 graphPtr->top = top;
3064
3065 graphPtr->vOffset = top + graphPtr->padTop;
3066 graphPtr->vRange = height - PADDING(graphPtr->padY);
3067 graphPtr->hOffset = left + graphPtr->padLeft;
3068 graphPtr->hRange = width - PADDING(graphPtr->padX);
3069
3070 if (graphPtr->vRange < 1) {
3071 graphPtr->vRange = 1;
3072 }
3073 if (graphPtr->hRange < 1) {
3074 graphPtr->hRange = 1;
3075 }
3076 graphPtr->hScale = 1.0 / (double)graphPtr->hRange;
3077 graphPtr->vScale = 1.0 / (double)graphPtr->vRange;
3078
3079 /*
3080 * Calculate the placement of the graph title so it is centered within the
3081 * space provided for it in the top margin
3082 */
3083 titleY = graphPtr->titleTextStyle.height;
3084 graphPtr->titleY = (titleY / 2) + graphPtr->inset;
3085 graphPtr->titleX = (graphPtr->right + graphPtr->left) / 2;
3086
3087 }
3088
3089 /*
3090 * ----------------------------------------------------------------------
3091 *
3092 * ConfigureAxis --
3093 *
3094 * Configures axis attributes (font, line width, label, etc).
3095 *
3096 * Results:
3097 * The return value is a standard Tcl result.
3098 *
3099 * Side Effects:
3100 * Axis layout is deferred until the height and width of the
3101 * window are known.
3102 *
3103 * ----------------------------------------------------------------------
3104 */
3105
3106 static int
3107 ConfigureAxis(graphPtr, axisPtr)
3108 Graph *graphPtr;
3109 Axis *axisPtr;
3110 {
3111 char errMsg[200];
3112
3113 /* Check the requested axis limits. Can't allow -min to be greater
3114 * than -max, or have undefined log scale limits. */
3115 if (((DEFINED(axisPtr->reqMin)) && (DEFINED(axisPtr->reqMax))) &&
3116 (axisPtr->reqMin >= axisPtr->reqMax)) {
3117 sprintf(errMsg, "impossible limits (min %g >= max %g) for axis \"%s\"",
3118 axisPtr->reqMin, axisPtr->reqMax, axisPtr->name);
3119 Tcl_AppendResult(graphPtr->interp, errMsg, (char *)NULL);
3120 /* Bad values, turn on axis auto-scaling */
3121 axisPtr->reqMin = axisPtr->reqMax = VALUE_UNDEFINED;
3122 return TCL_ERROR;
3123 }
3124 if ((axisPtr->logScale) && (DEFINED(axisPtr->reqMin)) &&
3125 (axisPtr->reqMin <= 0.0)) {
3126 sprintf(errMsg, "bad logscale limits (min=%g,max=%g) for axis \"%s\"",
3127 axisPtr->reqMin, axisPtr->reqMax, axisPtr->name);
3128 Tcl_AppendResult(graphPtr->interp, errMsg, (char *)NULL);
3129 /* Bad minimum value, turn on auto-scaling */
3130 axisPtr->reqMin = VALUE_UNDEFINED;
3131 return TCL_ERROR;
3132 }
3133 axisPtr->tickTextStyle.theta = FMOD(axisPtr->tickTextStyle.theta, 360.0);
3134 if (axisPtr->tickTextStyle.theta < 0.0) {
3135 axisPtr->tickTextStyle.theta += 360.0;
3136 }
3137 ResetTextStyles(graphPtr, axisPtr);
3138
3139 axisPtr->titleWidth = axisPtr->titleHeight = 0;
3140 if (axisPtr->title != NULL) {
3141 int w, h;
3142
3143 Blt_GetTextExtents(&axisPtr->titleTextStyle, axisPtr->title, &w, &h);
3144 axisPtr->titleWidth = (short int)w;
3145 axisPtr->titleHeight = (short int)h;
3146 }
3147
3148 /*
3149 * Don't bother to check what configuration options have changed.
3150 * Almost every option changes the size of the plotting area
3151 * (except for -color and -titlecolor), requiring the graph and
3152 * its contents to be completely redrawn.
3153 *
3154 * Recompute the scale and offset of the axis in case -min, -max
3155 * options have changed.
3156 */
3157 graphPtr->flags |= REDRAW_WORLD;
3158 if (!Blt_ConfigModified(configSpecs, graphPtr->interp, "-*color", "-background", "-bg",
3159 (char *)NULL)) {
3160 graphPtr->flags |= (MAP_WORLD | RESET_AXES);
3161 axisPtr->flags |= AXIS_DIRTY;
3162 }
3163 Blt_EventuallyRedrawGraph(graphPtr);
3164
3165 return TCL_OK;
3166 }
3167
3168 /*
3169 * ----------------------------------------------------------------------
3170 *
3171 * CreateAxis --
3172 *
3173 * Create and initialize a structure containing information to
3174 * display a graph axis.
3175 *
3176 * Results:
3177 * The return value is a standard Tcl result.
3178 *
3179 * ----------------------------------------------------------------------
3180 */
3181 static Axis *
3182 CreateAxis(graphPtr, name, margin)
3183 Graph *graphPtr;
3184 char *name; /* Identifier for axis. */
3185 int margin;
3186 {
3187 Axis *axisPtr;
3188 Blt_HashEntry *hPtr;
3189 int isNew;
3190
3191 if (name[0] == '-') {
3192 Tcl_AppendResult(graphPtr->interp, "name of axis \"", name,
3193 "\" can't start with a '-'", (char *)NULL);
3194 return NULL;
3195 }
3196 hPtr = Blt_CreateHashEntry(&graphPtr->axes.table, name, &isNew);
3197 if (!isNew) {
3198 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
3199 if (!axisPtr->deletePending) {
3200 Tcl_AppendResult(graphPtr->interp, "axis \"", name,
3201 "\" already exists in \"", Tk_PathName(graphPtr->tkwin), "\"",
3202 (char *)NULL);
3203 return NULL;
3204 }
3205 axisPtr->deletePending = FALSE;
3206 } else {
3207 axisPtr = Blt_Calloc(1, sizeof(Axis));
3208 assert(axisPtr);
3209
3210 axisPtr->name = Blt_Strdup(name);
3211 axisPtr->hashPtr = hPtr;
3212 axisPtr->classUid = NULL;
3213 axisPtr->looseMin = axisPtr->looseMax = TICK_RANGE_TIGHT;
3214 axisPtr->reqNumMinorTicks = 2;
3215 axisPtr->scrollUnits = 10;
3216 axisPtr->showTicks = TRUE;
3217 axisPtr->reqMin = axisPtr->reqMax = VALUE_UNDEFINED;
3218 axisPtr->scrollMin = axisPtr->scrollMax = VALUE_UNDEFINED;
3219
3220 if ((graphPtr->classUid == bltBarElementUid) &&
3221 ((margin == MARGIN_TOP) || (margin == MARGIN_BOTTOM))) {
3222 axisPtr->reqStep = 1.0;
3223 axisPtr->reqNumMinorTicks = 0;
3224 }
3225 if ((margin == MARGIN_RIGHT) || (margin == MARGIN_TOP)) {
3226 axisPtr->hidden = TRUE;
3227 }
3228 Blt_InitTextStyle(&axisPtr->titleTextStyle);
3229 Blt_InitTextStyle(&axisPtr->limitsTextStyle);
3230 Blt_InitTextStyle(&axisPtr->tickTextStyle);
3231 axisPtr->tickLabels = Blt_ChainCreate();
3232 axisPtr->lineWidth = 1;
3233 axisPtr->tickTextStyle.padX.side1 = 2;
3234 axisPtr->tickTextStyle.padX.side2 = 2;
3235 Blt_SetHashValue(hPtr, axisPtr);
3236 }
3237 return axisPtr;
3238 }
3239
3240 static int
3241 NameToAxis(graphPtr, name, axisPtrPtr)
3242 Graph *graphPtr; /* Graph widget record. */
3243 char *name; /* Name of the axis to be searched for. */
3244 Axis **axisPtrPtr; /* (out) Pointer to found axis structure. */
3245 {
3246 Blt_HashEntry *hPtr;
3247
3248 hPtr = Blt_FindHashEntry(&graphPtr->axes.table, name);
3249 if (hPtr != NULL) {
3250 Axis *axisPtr;
3251
3252 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
3253 if (!axisPtr->deletePending) {
3254 *axisPtrPtr = axisPtr;
3255 return TCL_OK;
3256 }
3257 }
3258 Tcl_AppendResult(graphPtr->interp, "can't find axis \"", name,
3259 "\" in \"", Tk_PathName(graphPtr->tkwin), "\"", (char *)NULL);
3260 *axisPtrPtr = NULL;
3261 return TCL_ERROR;
3262 }
3263
3264 static int
3265 GetAxis(graphPtr, axisName, classUid, axisPtrPtr)
3266 Graph *graphPtr;
3267 char *axisName;
3268 Blt_Uid classUid;
3269 Axis **axisPtrPtr;
3270 {
3271 Axis *axisPtr;
3272
3273 if (NameToAxis(graphPtr, axisName, &axisPtr) != TCL_OK) {
3274 return TCL_ERROR;
3275 }
3276 if (classUid != NULL) {
3277 if ((axisPtr->refCount == 0) || (axisPtr->classUid == NULL)) {
3278 /* Set the axis type on the first use of it. */
3279 axisPtr->classUid = classUid;
3280 } else if (axisPtr->classUid != classUid) {
3281 Tcl_AppendResult(graphPtr->interp, "axis \"", axisName,
3282 "\" is already in use on an opposite ", axisPtr->classUid,
3283 "-axis", (char *)NULL);
3284 return TCL_ERROR;
3285 }
3286 axisPtr->refCount++;
3287 }
3288 *axisPtrPtr = axisPtr;
3289 return TCL_OK;
3290 }
3291
3292 static void
3293 FreeAxis(graphPtr, axisPtr)
3294 Graph *graphPtr;
3295 Axis *axisPtr;
3296 {
3297 axisPtr->refCount--;
3298 if ((axisPtr->deletePending) && (axisPtr->refCount == 0)) {
3299 DestroyAxis(graphPtr, axisPtr);
3300 }
3301 }
3302
3303
3304 void
3305 Blt_DestroyAxes(graphPtr)
3306 Graph *graphPtr;
3307 {
3308 Blt_HashEntry *hPtr;
3309 Blt_HashSearch cursor;
3310 Axis *axisPtr;
3311 int i;
3312
3313 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
3314 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3315 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
3316 axisPtr->hashPtr = NULL;
3317 DestroyAxis(graphPtr, axisPtr);
3318 }
3319 Blt_DeleteHashTable(&graphPtr->axes.table);
3320 for (i = 0; i < 4; i++) {
3321 Blt_ChainDestroy(graphPtr->axisChain[i]);
3322 }
3323 Blt_DeleteHashTable(&graphPtr->axes.tagTable);
3324 Blt_ChainDestroy(graphPtr->axes.displayList);
3325 }
3326
3327 int Blt_ConfigureAxes(graphPtr)
3328 Graph *graphPtr;
3329 {
3330 Blt_HashEntry *hPtr;
3331 Blt_HashSearch cursor;
3332 Axis *axisPtr;
3333
3334 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
3335 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3336 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
3337 ConfigureAxis(graphPtr, axisPtr);
3338 }
3339 return TCL_OK;
3340 }
3341
3342 int
3343 Blt_DefaultAxes(graphPtr)
3344 Graph *graphPtr;
3345 {
3346 register int i;
3347 Axis *axisPtr;
3348 Blt_Chain *chainPtr;
3349 static char *axisNames[4] = { "x", "y", "x2", "y2" } ;
3350 int flags;
3351
3352 flags = Blt_GraphType(graphPtr);
3353 for (i = 0; i < 4; i++) {
3354 chainPtr = Blt_ChainCreate();
3355 graphPtr->axisChain[i] = chainPtr;
3356
3357 /* Create a default axis for each chain. */
3358 axisPtr = CreateAxis(graphPtr, axisNames[i], i);
3359 if (axisPtr == NULL) {
3360 return TCL_ERROR;
3361 }
3362 axisPtr->refCount = 1; /* Default axes are assumed in use. */
3363 axisPtr->classUid = (i & 1) ? bltYAxisUid : bltXAxisUid;
3364 axisPtr->flags |= AXIS_ONSCREEN;
3365
3366 /*
3367 * Blt_ConfigureWidgetComponent creates a temporary child window
3368 * by the name of the axis. It's used so that the Tk routines
3369 * that access the X resource database can describe a single
3370 * component and not the entire graph.
3371 */
3372 if (Blt_ConfigureWidgetComponent(graphPtr->interp, graphPtr->tkwin,
3373 axisPtr->name, "Axis", configSpecs, 0, (char **)NULL,
3374 (char *)axisPtr, flags) != TCL_OK) {
3375 return TCL_ERROR;
3376 }
3377 if (ConfigureAxis(graphPtr, axisPtr) != TCL_OK) {
3378 return TCL_ERROR;
3379 }
3380 axisPtr->linkPtr = Blt_ChainAppend(chainPtr, axisPtr);
3381 axisPtr->chainPtr = chainPtr;
3382 }
3383 return TCL_OK;
3384 }
3385
3386
3387 /*----------------------------------------------------------------------
3388 *
3389 * BindOp --
3390 *
3391 * .g axis bind axisName sequence command
3392 *
3393 *----------------------------------------------------------------------
3394 */
3395 static int
3396 BindOp(graphPtr, axisPtr, argc, argv)
3397 Graph *graphPtr;
3398 Axis *axisPtr;
3399 int argc;
3400 char **argv;
3401 {
3402 Tcl_Interp *interp = graphPtr->interp;
3403
3404 return Blt_ConfigureBindings(interp, graphPtr->bindTable,
3405 Blt_MakeAxisTag(graphPtr, axisPtr->name), argc, argv);
3406 }
3407
3408 /*
3409 * ----------------------------------------------------------------------
3410 *
3411 * CgetOp --
3412 *
3413 * Queries axis attributes (font, line width, label, etc).
3414 *
3415 * Results:
3416 * Return value is a standard Tcl result. If querying configuration
3417 * values, interp->result will contain the results.
3418 *
3419 * ----------------------------------------------------------------------
3420 */
3421 /* ARGSUSED */
3422 static int
3423 CgetOp(graphPtr, axisPtr, argc, argv)
3424 Graph *graphPtr;
3425 Axis *axisPtr;
3426 int argc; /* Not used. */
3427 char *argv[];
3428 {
3429 return Tk_ConfigureValue(graphPtr->interp, graphPtr->tkwin, configSpecs,
3430 (char *)axisPtr, argv[0], Blt_GraphType(graphPtr));
3431 }
3432
3433 /*
3434 * ----------------------------------------------------------------------
3435 *
3436 * ConfigureOp --
3437 *
3438 * Queries or resets axis attributes (font, line width, label, etc).
3439 *
3440 * Results:
3441 * Return value is a standard Tcl result. If querying configuration
3442 * values, interp->result will contain the results.
3443 *
3444 * Side Effects:
3445 * Axis resources are possibly allocated (GC, font). Axis layout is
3446 * deferred until the height and width of the window are known.
3447 *
3448 * ----------------------------------------------------------------------
3449 */
3450 static int
3451 ConfigureOp(graphPtr, axisPtr, argc, argv)
3452 Graph *graphPtr;
3453 Axis *axisPtr;
3454 int argc;
•3455 CONST char *argv[];
3456 {
3457 int flags;
3458
3459 flags = TK_CONFIG_ARGV_ONLY | Blt_GraphType(graphPtr);
3460 if (argc == 0) {
3461 return Tk_ConfigureInfo(graphPtr->interp, graphPtr->tkwin, configSpecs,
3462 (char *)axisPtr, (char *)NULL, flags);
3463 } else if (argc == 1) {
3464 return Tk_ConfigureInfo(graphPtr->interp, graphPtr->tkwin, configSpecs,
3465 (char *)axisPtr, argv[0], flags);
3466 }
•3467 if (Blt_ConfigureWidget(graphPtr->interp, graphPtr->tkwin, configSpecs,
3468 argc, argv, (char *)axisPtr, flags) != TCL_OK) {
3469 return TCL_ERROR;
3470 }
3471 if (ConfigureAxis(graphPtr, axisPtr) != TCL_OK) {
3472 return TCL_ERROR;
3473 }
3474 if (axisPtr->flags & AXIS_ONSCREEN) {
3475 if (!Blt_ConfigModified(configSpecs, graphPtr->interp, "-*color", "-background", "-bg",
3476 (char *)NULL)) {
3477 graphPtr->flags |= REDRAW_BACKING_STORE;
3478 }
3479 graphPtr->flags |= DRAW_MARGINS;
3480 Blt_EventuallyRedrawGraph(graphPtr);
3481 }
3482 return TCL_OK;
3483 }
3484
3485
3486 /*
3487 * ----------------------------------------------------------------------
3488 *
3489 * GetOp --
3490 *
3491 * Returns the name of the picked axis (using the axis
3492 * bind operation). Right now, the only name accepted is
3493 * "current".
3494 *
3495 * Results:
3496 * A standard Tcl result. The interpreter result will contain
3497 * the name of the axis.
3498 *
3499 * ----------------------------------------------------------------------
3500 */
3501 /*ARGSUSED*/
3502 static int
3503 GetOp(graphPtr, argc, argv)
3504 Graph *graphPtr;
3505 int argc; /* Not used. */
3506 char *argv[];
3507 {
3508 Tcl_Interp *interp = graphPtr->interp;
3509 register Axis *axisPtr;
3510
3511 axisPtr = (Axis *)Blt_GetCurrentItem(graphPtr->bindTable);
3512 /* Report only on axes. */
3513 if ((axisPtr != NULL) &&
3514 ((axisPtr->classUid == bltXAxisUid) ||
3515 (axisPtr->classUid == bltYAxisUid) ||
3516 (axisPtr->classUid == NULL))) {
3517 char c;
3518
3519 c = argv[3][0];
3520 if ((c == 'c') && (strcmp(argv[3], "current") == 0)) {
3521 Tcl_SetResult(interp, axisPtr->name, TCL_VOLATILE);
3522 } else if ((c == 'd') && (strcmp(argv[3], "detail") == 0)) {
3523 Tcl_SetResult(interp, axisPtr->detail, TCL_VOLATILE);
3524 }
3525 }
3526 return TCL_OK;
3527 }
3528
3529 /*
3530 *--------------------------------------------------------------
3531 *
3532 * LimitsOp --
3533 *
3534 * This procedure returns a string representing the axis limits
3535 * of the graph. The format of the string is { left top right bottom}.
3536 *
3537 * Results:
3538 * Always returns TCL_OK. The interp->result field is
3539 * a list of the graph axis limits.
3540 *
3541 *--------------------------------------------------------------
3542 */
3543 /*ARGSUSED*/
3544 static int
3545 LimitsOp(graphPtr, axisPtr, argc, argv)
3546 Graph *graphPtr;
3547 Axis *axisPtr;
3548 int argc; /* Not used. */
3549 char **argv; /* Not used. */
3550
3551 {
3552 Tcl_Interp *interp = graphPtr->interp;
3553 double min, max;
3554
3555 if (graphPtr->flags & RESET_AXES) {
3556 Blt_ResetAxes(graphPtr);
3557 }
3558 if (axisPtr->logScale) {
3559 min = EXP10(axisPtr->axisRange.min);
3560 max = EXP10(axisPtr->axisRange.max);
3561 } else {
3562 min = axisPtr->axisRange.min;
3563 max = axisPtr->axisRange.max;
3564 }
3565 Tcl_AppendElement(interp, Blt_Dtoa(interp, min));
3566 Tcl_AppendElement(interp, Blt_Dtoa(interp, max));
3567 return TCL_OK;
3568 }
3569
3570 /*
3571 * ----------------------------------------------------------------------
3572 *
3573 * InvTransformOp --
3574 *
3575 * Maps the given window coordinate into an axis-value.
3576 *
3577 * Results:
3578 * Returns a standard Tcl result. interp->result contains
3579 * the axis value. If an error occurred, TCL_ERROR is returned
3580 * and interp->result will contain an error message.
3581 *
3582 * ----------------------------------------------------------------------
3583 */
3584 /*ARGSUSED*/
3585 static int
3586 InvTransformOp(graphPtr, axisPtr, argc, argv)
3587 Graph *graphPtr;
3588 Axis *axisPtr;
3589 int argc; /* Not used. */
3590 char **argv;
3591 {
3592 int x; /* Integer window coordinate*/
3593 double y; /* Real graph coordinate */
3594
3595 if (graphPtr->flags & RESET_AXES) {
3596 Blt_ResetAxes(graphPtr);
3597 }
3598 if (Tcl_GetInt(graphPtr->interp, argv[0], &x) != TCL_OK) {
3599 return TCL_ERROR;
3600 }
3601 /*
3602 * Is the axis vertical or horizontal?
3603 *
3604 * Check the site where the axis was positioned. If the axis is
3605 * virtual, all we have to go on is how it was mapped to an
3606 * element (using either -mapx or -mapy options).
3607 */
3608 if (AxisIsHorizontal(graphPtr, axisPtr)) {
3609 y = Blt_InvHMap(graphPtr, axisPtr, (double)x);
3610 } else {
3611 y = Blt_InvVMap(graphPtr, axisPtr, (double)x);
3612 }
3613 Tcl_AppendElement(graphPtr->interp, Blt_Dtoa(graphPtr->interp, y));
3614 return TCL_OK;
3615 }
3616
3617 /*
3618 * ----------------------------------------------------------------------
3619 *
3620 * TransformOp --
3621 *
3622 * Maps the given axis-value to a window coordinate.
3623 *
3624 * Results:
3625 * Returns a standard Tcl result. interp->result contains
3626 * the window coordinate. If an error occurred, TCL_ERROR
3627 * is returned and interp->result will contain an error
3628 * message.
3629 *
3630 * ----------------------------------------------------------------------
3631 */
3632 /*ARGSUSED*/
3633 static int
3634 TransformOp(graphPtr, axisPtr, argc, argv)
3635 Graph *graphPtr;
3636 Axis *axisPtr; /* Axis */
3637 int argc; /* Not used. */
3638 char **argv;
3639 {
3640 double x;
3641
3642 if (graphPtr->flags & RESET_AXES) {
3643 Blt_ResetAxes(graphPtr);
3644 }
3645 if (Tcl_ExprDouble(graphPtr->interp, argv[0], &x) != TCL_OK) {
3646 return TCL_ERROR;
3647 }
3648 if (AxisIsHorizontal(graphPtr, axisPtr)) {
3649 x = Blt_HMap(graphPtr, axisPtr, x);
3650 } else {
3651 x = Blt_VMap(graphPtr, axisPtr, x);
3652 }
3653 Tcl_SetResult(graphPtr->interp, Blt_Itoa((int)x), TCL_VOLATILE);
3654 return TCL_OK;
3655 }
3656
3657 /*
3658 *--------------------------------------------------------------
3659 *
3660 * UseOp --
3661 *
3662 * Changes the virtual axis used by the logical axis.
3663 *
3664 * Results:
3665 * A standard Tcl result. If the named axis doesn't exist
3666 * an error message is put in interp->result.
3667 *
3668 * .g xaxis use "abc def gah"
3669 * .g xaxis use [lappend abc [.g axis use]]
3670 *
3671 *--------------------------------------------------------------
3672 */
3673 /*ARGSUSED*/
3674 static int
3675 UseOp(graphPtr, axisPtr, argc, argv)
3676 Graph *graphPtr;
3677 Axis *axisPtr; /* Not used. */
3678 int argc;
3679 char **argv;
3680 {
3681 Blt_Chain *chainPtr;
3682 int nNames;
3683 char **names;
3684 Blt_ChainLink *linkPtr;
3685 int i;
3686 Blt_Uid classUid;
3687 int margin;
3688
3689 /* TODO: fix bug where "$g xaxis x2" leaves x unavailable. */
•3690 margin = (intptr_t)argv[-1];
3691 chainPtr = graphPtr->margins[margin].axes;
3692 if (argc == 0) {
3693 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr!= NULL;
3694 linkPtr = Blt_ChainNextLink(linkPtr)) {
3695 axisPtr = Blt_ChainGetValue(linkPtr);
3696 Tcl_AppendElement(graphPtr->interp, axisPtr->name);
3697 }
3698 return TCL_OK;
3699 }
3700 if ((margin == MARGIN_BOTTOM) || (margin == MARGIN_TOP)) {
3701 classUid = (graphPtr->inverted) ? bltYAxisUid : bltXAxisUid;
3702 } else {
3703 classUid = (graphPtr->inverted) ? bltXAxisUid : bltYAxisUid;
3704 }
3705 if (Tcl_SplitList(graphPtr->interp, argv[0], &nNames, &names) != TCL_OK) {
3706 return TCL_ERROR;
3707 }
3708 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr!= NULL;
3709 linkPtr = Blt_ChainNextLink(linkPtr)) {
3710 axisPtr = Blt_ChainGetValue(linkPtr);
3711 axisPtr->linkPtr = NULL;
3712 axisPtr->flags &= ~AXIS_ONSCREEN;
3713 /* Clear the axis type if it's not currently used.*/
3714 if (axisPtr->refCount == 0) {
3715 axisPtr->classUid = NULL;
3716 }
3717 }
3718 Blt_ChainReset(chainPtr);
3719 for (i = 0; i < nNames; i++) {
3720 if (NameToAxis(graphPtr, names[i], &axisPtr) != TCL_OK) {
3721 Blt_Free(names);
3722 return TCL_ERROR;
3723 }
3724 if (axisPtr->classUid == NULL) {
3725 axisPtr->classUid = classUid;
3726 } else if (axisPtr->classUid != classUid) {
3727 Tcl_AppendResult(graphPtr->interp, "wrong type axis \"",
3728 axisPtr->name, "\": can't use ", classUid, " type axis.",
3729 (char *)NULL);
3730 Blt_Free(names);
3731 return TCL_ERROR;
3732 }
3733 if (axisPtr->linkPtr != NULL) {
3734 /* Move the axis from the old margin's "use" list to the new. */
3735 Blt_ChainUnlinkLink(axisPtr->chainPtr, axisPtr->linkPtr);
3736 Blt_ChainAppendLink(chainPtr, axisPtr->linkPtr);
3737 } else {
3738 axisPtr->linkPtr = Blt_ChainAppend(chainPtr, axisPtr);
3739 }
3740 axisPtr->chainPtr = chainPtr;
3741 axisPtr->flags |= AXIS_ONSCREEN;
3742 }
3743 graphPtr->flags |= (GET_AXIS_GEOMETRY | LAYOUT_NEEDED | RESET_AXES);
3744 /* When any axis changes, we need to layout the entire graph. */
3745 graphPtr->flags |= (MAP_WORLD | REDRAW_WORLD);
3746 Blt_EventuallyRedrawGraph(graphPtr);
3747
3748 Blt_Free(names);
3749 return TCL_OK;
3750 }
3751
3752 /*
3753 * ----------------------------------------------------------------------
3754 *
3755 * CreateVirtualOp --
3756 *
3757 * Creates a new axis.
3758 *
3759 * Results:
3760 * Returns a standard Tcl result.
3761 *
3762 * ----------------------------------------------------------------------
3763 */
3764 /*ARGSUSED*/
3765 static int
3766 CreateVirtualOp(graphPtr, argc, argv)
3767 Graph *graphPtr;
3768 int argc;
3769 char **argv;
3770 {
3771 Axis *axisPtr;
3772 int flags;
3773
3774 axisPtr = CreateAxis(graphPtr, argv[3], MARGIN_NONE);
3775 if (axisPtr == NULL) {
3776 return TCL_ERROR;
3777 }
3778 flags = Blt_GraphType(graphPtr);
3779 if (Blt_ConfigureWidgetComponent(graphPtr->interp, graphPtr->tkwin,
3780 axisPtr->name, "Axis", configSpecs, argc - 4, argv + 4,
3781 (char *)axisPtr, flags) != TCL_OK) {
3782 goto error;
3783 }
3784 if (ConfigureAxis(graphPtr, axisPtr) != TCL_OK) {
3785 goto error;
3786 }
3787 Tcl_SetResult(graphPtr->interp, axisPtr->name, TCL_VOLATILE);
3788 return TCL_OK;
3789 error:
3790 DestroyAxis(graphPtr, axisPtr);
3791 return TCL_ERROR;
3792 }
3793
3794 /*----------------------------------------------------------------------
3795 *
3796 * BindVirtualOp --
3797 *
3798 * .g axis bind axisName sequence command
3799 *
3800 *----------------------------------------------------------------------
3801 */
3802 /*ARGSUSED*/
3803 static int
3804 BindVirtualOp(graphPtr, argc, argv)
3805 Graph *graphPtr;
3806 int argc;
3807 char **argv;
3808 {
3809 Tcl_Interp *interp = graphPtr->interp;
3810
3811 if (argc == 3) {
3812 Blt_HashEntry *hPtr;
3813 Blt_HashSearch cursor;
3814 char *tagName;
3815
3816 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.tagTable, &cursor);
3817 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3818 tagName = Blt_GetHashKey(&graphPtr->axes.tagTable, hPtr);
3819 Tcl_AppendElement(interp, tagName);
3820 }
3821 return TCL_OK;
3822 }
3823 return Blt_ConfigureBindings(interp, graphPtr->bindTable,
3824 Blt_MakeAxisTag(graphPtr, argv[3]), argc - 4, argv + 4);
3825 }
3826
3827
3828 /*
3829 * ----------------------------------------------------------------------
3830 *
3831 * CgetVirtualOp --
3832 *
3833 * Queries axis attributes (font, line width, label, etc).
3834 *
3835 * Results:
3836 * Return value is a standard Tcl result. If querying configuration
3837 * values, interp->result will contain the results.
3838 *
3839 * ----------------------------------------------------------------------
3840 */
3841 /* ARGSUSED */
3842 static int
3843 CgetVirtualOp(graphPtr, argc, argv)
3844 Graph *graphPtr;
3845 int argc;
3846 char *argv[];
3847 {
3848 Axis *axisPtr;
3849
3850 if (NameToAxis(graphPtr, argv[3], &axisPtr) != TCL_OK) {
3851 return TCL_ERROR;
3852 }
3853 return CgetOp(graphPtr, axisPtr, argc - 4, argv + 4);
3854 }
3855
3856 /*
3857 * ----------------------------------------------------------------------
3858 *
3859 * ConfigureVirtualOp --
3860 *
3861 * Queries or resets axis attributes (font, line width, label, etc).
3862 *
3863 * Results:
3864 * Return value is a standard Tcl result. If querying configuration
3865 * values, interp->result will contain the results.
3866 *
3867 * Side Effects:
3868 * Axis resources are possibly allocated (GC, font). Axis layout is
3869 * deferred until the height and width of the window are known.
3870 *
3871 * ----------------------------------------------------------------------
3872 */
3873 static int
3874 ConfigureVirtualOp(graphPtr, argc, argv)
3875 Graph *graphPtr;
3876 int argc;
3877 char *argv[];
3878 {
3879 Axis *axisPtr;
3880 int nNames, nOpts;
3881 char **options;
3882 register int i;
3883
3884 /* Figure out where the option value pairs begin */
3885 argc -= 3;
3886 argv += 3;
3887 for (i = 0; i < argc; i++) {
3888 if (argv[i][0] == '-') {
3889 break;
3890 }
3891 if (NameToAxis(graphPtr, argv[i], &axisPtr) != TCL_OK) {
3892 return TCL_ERROR;
3893 }
3894 }
3895 nNames = i; /* Number of pen names specified */
3896 nOpts = argc - i; /* Number of options specified */
3897 options = argv + i; /* Start of options in argv */
3898
3899 for (i = 0; i < nNames; i++) {
3900 if (NameToAxis(graphPtr, argv[i], &axisPtr) != TCL_OK) {
3901 return TCL_ERROR;
3902 }
3903 if (ConfigureOp(graphPtr, axisPtr, nOpts, options) != TCL_OK) {
3904 break;
3905 }
3906 }
3907 if (i < nNames) {
3908 return TCL_ERROR;
3909 }
3910 return TCL_OK;
3911 }
3912
3913 /*
3914 * ----------------------------------------------------------------------
3915 *
3916 * DeleteVirtualOp --
3917 *
3918 * Deletes one or more axes. The actual removal may be deferred
3919 * until the axis is no longer used by any element. The axis
3920 * can't be referenced by its name any longer and it may be
3921 * recreated.
3922 *
3923 * Results:
3924 * Returns a standard Tcl result.
3925 *
3926 * ----------------------------------------------------------------------
3927 */
3928 /*ARGSUSED*/
3929 static int
3930 DeleteVirtualOp(graphPtr, argc, argv)
3931 Graph *graphPtr;
3932 int argc;
3933 char **argv;
3934 {
3935 register int i;
3936 Axis *axisPtr;
3937
3938 for (i = 3; i < argc; i++) {
3939 if (NameToAxis(graphPtr, argv[i], &axisPtr) != TCL_OK) {
3940 return TCL_ERROR;
3941 }
3942 axisPtr->deletePending = TRUE;
3943 if (axisPtr->refCount == 0) {
3944 DestroyAxis(graphPtr, axisPtr);
3945 }
3946 }
3947 return TCL_OK;
3948 }
3949
3950 /*
3951 * ----------------------------------------------------------------------
3952 *
3953 * InvTransformVirtualOp --
3954 *
3955 * Maps the given window coordinate into an axis-value.
3956 *
3957 * Results:
3958 * Returns a standard Tcl result. interp->result contains
3959 * the axis value. If an error occurred, TCL_ERROR is returned
3960 * and interp->result will contain an error message.
3961 *
3962 * ----------------------------------------------------------------------
3963 */
3964 /*ARGSUSED*/
3965 static int
3966 InvTransformVirtualOp(graphPtr, argc, argv)
3967 Graph *graphPtr;
3968 int argc; /* Not used. */
3969 char **argv;
3970 {
3971 Axis *axisPtr;
3972
3973 if (NameToAxis(graphPtr, argv[3], &axisPtr) != TCL_OK) {
3974 return TCL_ERROR;
3975 }
3976 return InvTransformOp(graphPtr, axisPtr, argc - 4, argv + 4);
3977 }
3978
3979 /*
3980 *--------------------------------------------------------------
3981 *
3982 * LimitsVirtualOp --
3983 *
3984 * This procedure returns a string representing the axis limits
3985 * of the graph. The format of the string is { left top right bottom}.
3986 *
3987 * Results:
3988 * Always returns TCL_OK. The interp->result field is
3989 * a list of the graph axis limits.
3990 *
3991 *--------------------------------------------------------------
3992 */
3993 /*ARGSUSED*/
3994 static int
3995 LimitsVirtualOp(graphPtr, argc, argv)
3996 Graph *graphPtr;
3997 int argc; /* Not used. */
3998 char **argv; /* Not used. */
3999
4000 {
4001 Axis *axisPtr;
4002
4003 if (NameToAxis(graphPtr, argv[3], &axisPtr) != TCL_OK) {
4004 return TCL_ERROR;
4005 }
4006 return LimitsOp(graphPtr, axisPtr, argc - 4, argv + 4);
4007 }
4008
4009 /*
4010 * ----------------------------------------------------------------------
4011 *
4012 * NamesVirtualOp --
4013 *
4014 * Return a list of the names of all the axes.
4015 *
4016 * Results:
4017 * Returns a standard Tcl result.
4018 *
4019 * ----------------------------------------------------------------------
4020 */
4021
4022 /*ARGSUSED*/
4023 static int
4024 NamesVirtualOp(graphPtr, argc, argv)
4025 Graph *graphPtr;
4026 int argc; /* Not used. */
4027 char **argv; /* Not used. */
4028 {
4029 Blt_HashEntry *hPtr;
4030 Blt_HashSearch cursor;
4031 Axis *axisPtr;
4032 register int i;
4033
4034 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
4035 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4036 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
4037 if (axisPtr->deletePending) {
4038 continue;
4039 }
4040 if (argc == 3) {
4041 Tcl_AppendElement(graphPtr->interp, axisPtr->name);
4042 continue;
4043 }
4044 for (i = 3; i < argc; i++) {
4045 if (Tcl_StringMatch(axisPtr->name, argv[i])) {
4046 Tcl_AppendElement(graphPtr->interp, axisPtr->name);
4047 break;
4048 }
4049 }
4050 }
4051 return TCL_OK;
4052 }
4053
4054 /*
4055 * ----------------------------------------------------------------------
4056 *
4057 * TransformVirtualOp --
4058 *
4059 * Maps the given axis-value to a window coordinate.
4060 *
4061 * Results:
4062 * Returns a standard Tcl result. interp->result contains
4063 * the window coordinate. If an error occurred, TCL_ERROR
4064 * is returned and interp->result will contain an error
4065 * message.
4066 *
4067 * ----------------------------------------------------------------------
4068 */
4069 /*ARGSUSED*/
4070 static int
4071 TransformVirtualOp(graphPtr, argc, argv)
4072 Graph *graphPtr;
4073 int argc; /* Not used. */
4074 char **argv;
4075 {
4076 Axis *axisPtr;
4077
4078 if (NameToAxis(graphPtr, argv[3], &axisPtr) != TCL_OK) {
4079 return TCL_ERROR;
4080 }
4081 return TransformOp(graphPtr, axisPtr, argc - 4, argv + 4);
4082 }
4083
4084 static int
4085 ViewOp(graphPtr, argc, argv)
4086 Graph *graphPtr;
4087 int argc;
4088 char **argv;
4089 {
4090 Axis *axisPtr;
4091 Tcl_Interp *interp = graphPtr->interp;
4092 double axisOffset, scrollUnits;
4093 double fract;
4094 double viewMin, viewMax, worldMin, worldMax;
4095 double viewWidth, worldWidth;
4096
4097 if (NameToAxis(graphPtr, argv[3], &axisPtr) != TCL_OK) {
4098 return TCL_ERROR;
4099 }
4100 worldMin = axisPtr->valueRange.min;
4101 worldMax = axisPtr->valueRange.max;
4102 /* Override data dimensions with user-selected limits. */
4103 if (DEFINED(axisPtr->scrollMin)) {
4104 worldMin = axisPtr->scrollMin;
4105 }
4106 if (DEFINED(axisPtr->scrollMax)) {
4107 worldMax = axisPtr->scrollMax;
4108 }
4109 viewMin = axisPtr->min;
4110 viewMax = axisPtr->max;
4111 /* Bound the view within scroll region. */
4112 if (viewMin < worldMin) {
4113 viewMin = worldMin;
4114 }
4115 if (viewMax > worldMax) {
4116 viewMax = worldMax;
4117 }
4118 if (axisPtr->logScale) {
4119 worldMin = log10(worldMin);
4120 worldMax = log10(worldMax);
4121 viewMin = log10(viewMin);
4122 viewMax = log10(viewMax);
4123 }
4124 worldWidth = worldMax - worldMin;
4125 viewWidth = viewMax - viewMin;
4126
4127 /* Unlike horizontal axes, vertical axis values run opposite of
4128 * the scrollbar first/last values. So instead of pushing the
4129 * axis minimum around, we move the maximum instead. */
4130
4131 if (AxisIsHorizontal(graphPtr, axisPtr) != axisPtr->descending) {
4132 axisOffset = viewMin - worldMin;
4133 scrollUnits = (double)axisPtr->scrollUnits * graphPtr->hScale;
4134 } else {
4135 axisOffset = worldMax - viewMax;
4136 scrollUnits = (double)axisPtr->scrollUnits * graphPtr->vScale;
4137 }
4138 if (argc == 4) {
4139 /* Note: Bound the fractions between 0.0 and 1.0 to support
4140 * "canvas"-style scrolling. */
4141 fract = axisOffset / worldWidth;
4142 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
4143 fract = (axisOffset + viewWidth) / worldWidth;
4144 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
4145 return TCL_OK;
4146 }
4147 fract = axisOffset / worldWidth;
4148 if (GetAxisScrollInfo(interp, argc - 4, argv + 4, &fract,
4149 viewWidth / worldWidth, scrollUnits) != TCL_OK) {
4150 return TCL_ERROR;
4151 }
4152 if (AxisIsHorizontal(graphPtr, axisPtr) != axisPtr->descending) {
4153 axisPtr->reqMin = (fract * worldWidth) + worldMin;
4154 axisPtr->reqMax = axisPtr->reqMin + viewWidth;
4155 } else {
4156 axisPtr->reqMax = worldMax - (fract * worldWidth);
4157 axisPtr->reqMin = axisPtr->reqMax - viewWidth;
4158 }
4159 if (axisPtr->logScale) {
4160 axisPtr->reqMin = EXP10(axisPtr->reqMin);
4161 axisPtr->reqMax = EXP10(axisPtr->reqMax);
4162 }
4163 graphPtr->flags |= (GET_AXIS_GEOMETRY | LAYOUT_NEEDED | RESET_AXES);
4164 Blt_EventuallyRedrawGraph(graphPtr);
4165 return TCL_OK;
4166 }
4167
4168 int
4169 Blt_VirtualAxisOp(graphPtr, interp, argc, argv)
4170 Graph *graphPtr;
4171 Tcl_Interp *interp;
4172 int argc;
4173 char **argv;
4174 {
4175 Blt_Op proc;
4176 int result;
4177 static Blt_OpSpec axisOps[] =
4178 {
4179 {"bind", 1, (Blt_Op)BindVirtualOp, 3, 6,
4180 "axisName sequence command",},
4181 {"cget", 2, (Blt_Op)CgetVirtualOp, 5, 5, "axisName option",},
4182 {"configure", 2, (Blt_Op)ConfigureVirtualOp, 4, 0,
4183 "axisName ?axisName?... ?option value?...",},
4184 {"create", 2, (Blt_Op)CreateVirtualOp, 4, 0,
4185 "axisName ?option value?...",},
4186 {"delete", 1, (Blt_Op)DeleteVirtualOp, 3, 0, "?axisName?...",},
4187 {"get", 1, (Blt_Op)GetOp, 4, 4, "name",},
4188 {"invtransform", 1, (Blt_Op)InvTransformVirtualOp, 5, 5,
4189 "axisName value",},
4190 {"limits", 1, (Blt_Op)LimitsVirtualOp, 4, 4, "axisName",},
4191 {"names", 1, (Blt_Op)NamesVirtualOp, 3, 0, "?pattern?...",},
4192 {"transform", 1, (Blt_Op)TransformVirtualOp, 5, 5, "axisName value",},
4193 {"view", 1, (Blt_Op)ViewOp, 4, 7,
4194 "axisName ?moveto fract? ?scroll number what?",},
4195 };
4196 static int nAxisOps = sizeof(axisOps) / sizeof(Blt_OpSpec);
4197
4198 proc = Blt_GetOp(interp, nAxisOps, axisOps, BLT_OP_ARG2, argc, argv, 0);
4199 if (proc == NULL) {
4200 return TCL_ERROR;
4201 }
4202 result = (*proc) (graphPtr, argc, argv);
4203 return result;
4204 }
4205
4206 int
4207 Blt_AxisOp(graphPtr, margin, argc, argv)
4208 Graph *graphPtr;
4209 int margin;
4210 int argc;
4211 char **argv;
4212 {
4213 int result;
4214 Blt_Op proc;
4215 Axis *axisPtr;
4216 static Blt_OpSpec axisOps[] =
4217 {
4218 {"bind", 1, (Blt_Op)BindOp, 2, 5, "sequence command",},
4219 {"cget", 2, (Blt_Op)CgetOp, 4, 4, "option",},
4220 {"configure", 2, (Blt_Op)ConfigureOp, 3, 0, "?option value?...",},
4221 {"invtransform", 1, (Blt_Op)InvTransformOp, 4, 4, "value",},
4222 {"limits", 1, (Blt_Op)LimitsOp, 3, 3, "",},
4223 {"transform", 1, (Blt_Op)TransformOp, 4, 4, "value",},
4224 {"use", 1, (Blt_Op)UseOp, 3, 4, "?axisName?",},
4225 };
4226 static int nAxisOps = sizeof(axisOps) / sizeof(Blt_OpSpec);
4227
4228 proc = Blt_GetOp(graphPtr->interp, nAxisOps, axisOps, BLT_OP_ARG2,
4229 argc, argv, 0);
4230 if (proc == NULL) {
4231 return TCL_ERROR;
4232 }
4233 if (proc == UseOp) {
•4234 argv[2] = (char *)(intptr_t)margin; /* Hack. Slide a reference to the margin in
4235 * the argument list. Needed only for UseOp.
4236 */
4237 result = (*proc)(graphPtr, NULL, argc - 3, argv +3);
4238 } else {
4239 axisPtr = Blt_GetFirstAxis(graphPtr->margins[margin].axes);
4240 if (axisPtr == NULL) {
4241 Tcl_AppendResult(graphPtr->interp, "bad axis", (char *)NULL);
4242 return TCL_ERROR;
4243 }
4244 result = (*proc)(graphPtr, axisPtr, argc - 3, argv + 3);
4245 }
4246 return result;
4247 }
4248
4249 void
4250 Blt_MapAxes(graphPtr)
4251 Graph *graphPtr;
4252 {
4253 Axis *axisPtr;
4254 Blt_Chain *chainPtr;
4255 Blt_ChainLink *linkPtr;
4256 register int margin;
4257 int offset;
4258
4259 for (margin = 0; margin < 4; margin++) {
4260 chainPtr = graphPtr->margins[margin].axes;
4261 offset = 0;
4262 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL;
4263 linkPtr = Blt_ChainNextLink(linkPtr)) {
4264 axisPtr = Blt_ChainGetValue(linkPtr);
4265 if ((!axisPtr->hidden) && (axisPtr->flags & AXIS_ONSCREEN)) {
4266 MapAxis(graphPtr, axisPtr, offset, margin);
4267 if (AxisIsHorizontal(graphPtr, axisPtr)) {
4268 offset += axisPtr->height;
4269 } else {
4270 offset += axisPtr->width;
4271 }
4272 }
4273 }
4274 }
4275 }
4276
4277 void
4278 Blt_DrawAxes(graphPtr, drawable)
4279 Graph *graphPtr;
4280 Drawable drawable;
4281 {
4282 Axis *axisPtr;
4283 Blt_ChainLink *linkPtr;
4284 register int i;
4285
4286 for (i = 0; i < 4; i++) {
4287 for (linkPtr = Blt_ChainFirstLink(graphPtr->margins[i].axes);
4288 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
4289 axisPtr = Blt_ChainGetValue(linkPtr);
4290 if ((!axisPtr->hidden) && (axisPtr->flags & AXIS_ONSCREEN)) {
4291 DrawAxis(graphPtr, drawable, axisPtr);
4292 }
4293 }
4294 }
4295 }
4296
4297 void
4298 Blt_AxesToPostScript(graphPtr, psToken)
4299 Graph *graphPtr;
4300 PsToken psToken;
4301 {
4302 Axis *axisPtr;
4303 Blt_ChainLink *linkPtr;
4304 register int i;
4305
4306 for (i = 0; i < 4; i++) {
4307 for (linkPtr = Blt_ChainFirstLink(graphPtr->margins[i].axes);
4308 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
4309 axisPtr = Blt_ChainGetValue(linkPtr);
4310 if ((!axisPtr->hidden) && (axisPtr->flags & AXIS_ONSCREEN)) {
4311 AxisToPostScript(psToken, axisPtr);
4312 }
4313 }
4314 }
4315 }
4316
4317
4318 /*
4319 * ----------------------------------------------------------------------
4320 *
4321 * Blt_DrawAxisLimits --
4322 *
4323 * Draws the min/max values of the axis in the plotting area.
4324 * The text strings are formatted according to the "sprintf"
4325 * format descriptors in the limitsFormats array.
4326 *
4327 * Results:
4328 * None.
4329 *
4330 * Side Effects:
4331 * Draws the numeric values of the axis limits into the outer
4332 * regions of the plotting area.
4333 *
4334 * ----------------------------------------------------------------------
4335 */
4336 void
4337 Blt_DrawAxisLimits(graphPtr, drawable)
4338 Graph *graphPtr;
4339 Drawable drawable;
4340 {
4341 Axis *axisPtr;
4342 Blt_HashEntry *hPtr;
4343 Blt_HashSearch cursor;
4344 Dim2D textDim;
4345 int isHoriz;
4346 char *minPtr, *maxPtr;
4347 char *minFormat, *maxFormat;
4348 char minString[200], maxString[200];
4349 int vMin, hMin, vMax, hMax;
4350
4351 #define SPACING 8
4352 vMin = vMax = graphPtr->left + graphPtr->padLeft + 2;
4353 hMin = hMax = graphPtr->bottom - graphPtr->padBottom - 2; /* Offsets */
4354
4355 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
4356 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4357 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
4358
4359 if (axisPtr->nFormats == 0) {
4360 continue;
4361 }
4362 isHoriz = AxisIsHorizontal(graphPtr, axisPtr);
4363 minPtr = maxPtr = NULL;
4364 minFormat = maxFormat = axisPtr->limitsFormats[0];
4365 if (axisPtr->nFormats > 1) {
4366 maxFormat = axisPtr->limitsFormats[1];
4367 }
4368 if (minFormat[0] != '\0') {
4369 minPtr = minString;
4370 sprintf(minString, minFormat, axisPtr->axisRange.min);
4371 }
4372 if (maxFormat[0] != '\0') {
4373 maxPtr = maxString;
4374 sprintf(maxString, maxFormat, axisPtr->axisRange.max);
4375 }
4376 if (axisPtr->descending) {
4377 char *tmp;
4378
4379 tmp = minPtr, minPtr = maxPtr, maxPtr = tmp;
4380 }
4381 if (maxPtr != NULL) {
4382 if (isHoriz) {
4383 axisPtr->limitsTextStyle.theta = 90.0;
4384 axisPtr->limitsTextStyle.anchor = TK_ANCHOR_SE;
4385 Blt_DrawText2(graphPtr->tkwin, drawable, maxPtr,
4386 &axisPtr->limitsTextStyle, graphPtr->right, hMax, &textDim);
4387 hMax -= (textDim.height + SPACING);
4388 } else {
4389 axisPtr->limitsTextStyle.theta = 0.0;
4390 axisPtr->limitsTextStyle.anchor = TK_ANCHOR_NW;
4391 Blt_DrawText2(graphPtr->tkwin, drawable, maxPtr,
4392 &axisPtr->limitsTextStyle, vMax, graphPtr->top, &textDim);
4393 vMax += (textDim.width + SPACING);
4394 }
4395 }
4396 if (minPtr != NULL) {
4397 axisPtr->limitsTextStyle.anchor = TK_ANCHOR_SW;
4398 if (isHoriz) {
4399 axisPtr->limitsTextStyle.theta = 90.0;
4400 Blt_DrawText2(graphPtr->tkwin, drawable, minPtr,
4401 &axisPtr->limitsTextStyle, graphPtr->left, hMin, &textDim);
4402 hMin -= (textDim.height + SPACING);
4403 } else {
4404 axisPtr->limitsTextStyle.theta = 0.0;
4405 Blt_DrawText2(graphPtr->tkwin, drawable, minPtr,
4406 &axisPtr->limitsTextStyle, vMin, graphPtr->bottom, &textDim);
4407 vMin += (textDim.width + SPACING);
4408 }
4409 }
4410 } /* Loop on axes */
4411 }
4412
4413 void
4414 Blt_AxisLimitsToPostScript(graphPtr, psToken)
4415 Graph *graphPtr;
4416 PsToken psToken;
4417 {
4418 Axis *axisPtr;
4419 Blt_HashEntry *hPtr;
4420 Blt_HashSearch cursor;
4421 double vMin, hMin, vMax, hMax;
4422 char string[200];
4423 int textWidth, textHeight;
4424 char *minFmt, *maxFmt;
4425
4426 #define SPACING 8
4427 vMin = vMax = graphPtr->left + graphPtr->padLeft + 2;
4428 hMin = hMax = graphPtr->bottom - graphPtr->padBottom - 2; /* Offsets */
4429 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
4430 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4431 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
4432
4433 if (axisPtr->nFormats == 0) {
4434 continue;
4435 }
4436 minFmt = maxFmt = axisPtr->limitsFormats[0];
4437 if (axisPtr->nFormats > 1) {
4438 maxFmt = axisPtr->limitsFormats[1];
4439 }
4440 if (*maxFmt != '\0') {
4441 sprintf(string, maxFmt, axisPtr->axisRange.max);
4442 Blt_GetTextExtents(&axisPtr->tickTextStyle, string, &textWidth,
4443 &textHeight);
4444 if ((textWidth > 0) && (textHeight > 0)) {
4445 if (axisPtr->classUid == bltXAxisUid) {
4446 axisPtr->limitsTextStyle.theta = 90.0;
4447 axisPtr->limitsTextStyle.anchor = TK_ANCHOR_SE;
4448 Blt_TextToPostScript(psToken, string,
4449 &axisPtr->limitsTextStyle,
4450 (double)graphPtr->right, hMax);
4451 hMax -= (textWidth + SPACING);
4452 } else {
4453 axisPtr->limitsTextStyle.theta = 0.0;
4454 axisPtr->limitsTextStyle.anchor = TK_ANCHOR_NW;
4455 Blt_TextToPostScript(psToken, string,
4456 &axisPtr->limitsTextStyle, vMax, (double)graphPtr->top);
4457 vMax += (textWidth + SPACING);
4458 }
4459 }
4460 }
4461 if (*minFmt != '\0') {
4462 sprintf(string, minFmt, axisPtr->axisRange.min);
4463 Blt_GetTextExtents(&axisPtr->tickTextStyle, string, &textWidth,
4464 &textHeight);
4465 if ((textWidth > 0) && (textHeight > 0)) {
4466 axisPtr->limitsTextStyle.anchor = TK_ANCHOR_SW;
4467 if (axisPtr->classUid == bltXAxisUid) {
4468 axisPtr->limitsTextStyle.theta = 90.0;
4469 Blt_TextToPostScript(psToken, string,
4470 &axisPtr->limitsTextStyle,
4471 (double)graphPtr->left, hMin);
4472 hMin -= (textWidth + SPACING);
4473 } else {
4474 axisPtr->limitsTextStyle.theta = 0.0;
4475 Blt_TextToPostScript(psToken, string,
4476 &axisPtr->limitsTextStyle,
4477 vMin, (double)graphPtr->bottom);
4478 vMin += (textWidth + SPACING);
4479 }
4480 }
4481 }
4482 }
4483 }
4484
4485 Axis *
4486 Blt_GetFirstAxis(chainPtr)
4487 Blt_Chain *chainPtr;
4488 {
4489 Blt_ChainLink *linkPtr;
4490
4491 linkPtr = Blt_ChainFirstLink(chainPtr);
4492 if (linkPtr == NULL) {
4493 return NULL;
4494 }
4495 return Blt_ChainGetValue(linkPtr);
4496 }
4497
4498 Axis *
4499 Blt_NearestAxis(graphPtr, x, y)
4500 Graph *graphPtr;
4501 int x, y; /* Point to be tested */
4502 {
4503 register Blt_HashEntry *hPtr;
4504 Blt_HashSearch cursor;
4505 Axis *axisPtr;
4506 int width, height;
4507 double rotWidth, rotHeight;
4508 Point2D bbox[5];
4509
4510 for (hPtr = Blt_FirstHashEntry(&graphPtr->axes.table, &cursor);
4511 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4512 axisPtr = (Axis *)Blt_GetHashValue(hPtr);
4513 if ((axisPtr->hidden) || (!(axisPtr->flags & AXIS_ONSCREEN))) {
4514 continue; /* Don't check hidden axes or axes
4515 * that are virtual. */
4516 }
4517 if (axisPtr->showTicks) {
4518 register Blt_ChainLink *linkPtr;
4519 TickLabel *labelPtr;
4520 Point2D t;
4521
4522 for (linkPtr = Blt_ChainFirstLink(axisPtr->tickLabels);
4523 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
4524 labelPtr = Blt_ChainGetValue(linkPtr);
4525 Blt_GetBoundingBox(labelPtr->width, labelPtr->height,
4526 axisPtr->tickTextStyle.theta, &rotWidth, &rotHeight, bbox);
4527 width = ROUND(rotWidth);
4528 height = ROUND(rotHeight);
4529 t = Blt_TranslatePoint(&labelPtr->anchorPos, width, height,
4530 axisPtr->tickTextStyle.anchor);
4531 t.x = x - t.x - (width * 0.5);
4532 t.y = y - t.y - (height * 0.5);
4533
4534 bbox[4] = bbox[0];
4535 if (Blt_PointInPolygon(&t, bbox, 5)) {
4536 axisPtr->detail = "label";
4537 return axisPtr;
4538 }
4539 }
4540 }
4541 if (axisPtr->title != NULL) { /* and then the title string. */
4542 Point2D t;
4543
4544 Blt_GetTextExtents(&axisPtr->titleTextStyle, axisPtr->title,&width,
4545 &height);
4546 Blt_GetBoundingBox(width, height, axisPtr->titleTextStyle.theta,
4547 &rotWidth, &rotHeight, bbox);
4548 width = ROUND(rotWidth);
4549 height = ROUND(rotHeight);
4550 t = Blt_TranslatePoint(&axisPtr->titlePos, width, height,
4551 axisPtr->titleTextStyle.anchor);
4552 /* Translate the point so that the 0,0 is the upper left
4553 * corner of the bounding box. */
4554 t.x = x - t.x - (width / 2);
4555 t.y = y - t.y - (height / 2);
4556
4557 bbox[4] = bbox[0];
4558 if (Blt_PointInPolygon(&t, bbox, 5)) {
4559 axisPtr->detail = "title";
4560 return axisPtr;
4561 }
4562 }
4563 if (axisPtr->lineWidth > 0) { /* Check for the axis region */
4564 if (PointInRegion(&axisPtr->region, x, y)) {
4565 axisPtr->detail = "line";
4566 return axisPtr;
4567 }
4568 }
4569 }
4570 return NULL;
4571 }
4572
4573 ClientData
4574 Blt_MakeAxisTag(graphPtr, tagName)
4575 Graph *graphPtr;
4576 char *tagName;
4577 {
4578 Blt_HashEntry *hPtr;
4579 int isNew;
4580
4581 hPtr = Blt_CreateHashEntry(&graphPtr->axes.tagTable, tagName, &isNew);
4582 assert(hPtr);
4583 return Blt_GetHashKey(&graphPtr->axes.tagTable, hPtr);
4584 }