-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotter.c
2651 lines (2371 loc) · 95.1 KB
/
plotter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of the GNU plotutils package. Copyright (C) 1989,
1990, 1991, 1995, 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free
Software Foundation, Inc.
The GNU plotutils package is free software. You may redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software foundation; either version 2, or (at your
option) any later version.
The GNU plotutils package is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the GNU plotutils package; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
Boston, MA 02110-1301, USA. */
/* This file contains the point plotter half of GNU graph. The point
plotter could easily be linked with other software. It translates a
sequence of points, regarded as defining a polyline or a sequence of
polylines, to a sequence of libplot calls. There is support for
multigraphing, i.e. producing a plot consisting of more than a single
graph. Each graph may be drawn from more than one file, i.e., input
stream, and each input stream may provide more than a single polyline.
A `point' is a structure. Each point structure contains the following
fields:
x and y coordinates of the point
a `have_x_errorbar' flag (true or false)
a `have_y_errorbar' flag (true or false)
xmin and xmax (meaningful only if have_x_errorbar is set)
ymin and ymax (meaningful only if have_y_errorbar is set)
a `pendown' flag
a symbol type (a small integer, interpreted as a marker type)
a symbol size (a fraction of the size of the plotting box)
a symbol font name (relevant only for symbol types >= 32)
a linemode (a small integer)
a linewidth (a fraction of the size of the display device)
a polyline fill-fraction (in the interval [0,1], <0 means no fill)
a use_color flag (true or false)
The point plotter constructs a polyline from each successive run of
points that have the pendown flag set. It assumes that the final seven
fields are assumed to be the same for each point in such a run, i.e., it
takes their values from the first point of the run. At the location of
each point on the polyline, the appropriate marker symbol, if any, will
be plotted. Symbol types greater than or equal to 32 are interpreted as
single characters to be plotted, rather than symbols.
Points without the pendown flag set cause the polyline to be broken, and
a new one to begin, before the symbol (if any) is plotted.
The plotter supports five basic linemodes: 1 through 5. The
interpretation of `linemode' depends on the polyline's use_color flag.
linemode If monochrome If color
1 solid red
2 dotted green
3 dotdashed blue
4 shortdashed magenta
5 longdashed cyan
In the monochrome case, the pattern simply repeats: 6,7,8,9,10 are
equivalent to 1,2,3,4,5, etc. In the colored case, the sequence of
colors also repeats. But linemodes 1,2,3,4,5 are drawn solid, while
6,7,8,9,10 are drawn dotted, 11,12,13,14,15 are drawn dotdashed, etc.
So there are 25 distinct colored linemodes, and 5 distinct monochrome
(black) ones.
The color of a symbol will be the same as the color of the polyline on
which it is plotted.
linemodes -1, -2, etc. have a special interpretation. They are
`disconnected' linemodes: no polyline will appear, but if color is
being used, the color of the plotted symbols (if any) will be
linemode-dependent. -1,-2,-3,-4,5 signify red,green,blue,magenta,cyan
(the same sequence as for 1,2,3,4,5); thereafter the sequence repeats.
linemode 0 is special (for backward compatibility). No line is drawn;
symbol #1 (a point) will be used. So using linemode 0 is the same as
using linemode -1, symbol 1.
The point plotter is invoked by calling the following, in order.
new_multigrapher() creates a new point plotter.
begin_graph()
set_graph_parameters() initializes global structures used by
the draw_frame_of_graph() and plot_point() routines. These include
the structures that specify the linear transformation from user
coordinates to the coordinates used by libplot, and structures
that specify the style of the plot frame.
draw_frame_of_graph() plots the graph frame. [Optional.]
plot_point() uses libplot routines to plot a single point, together
with (possibly) a line extending to it from the last point, and
a symbol. [Alternatively, plot_point_array() can be used, to plot
an array of points.]
end_graph()
..
[The begin_graph()..end_graph() block can be repeated indefinitely
if desired, to create a multigraph. set_graph_parameters() allows
for repositioning of later graphs.]
..
delete_multigrapher() deletes the point plotter.
There is also a function end_polyline_and_flush(), which is useful for
real-time display. */
#include "sys-defines.h"
#include "libcommon.h"
#include "plot.h"
#include "extern.h"
/* we use floating point libplot coordinates in the range [0,PLOT_SIZE] */
#define PLOT_SIZE 4096.0
#define FUZZ 0.000001 /* bd. on floating pt. roundoff error */
#define NEAR_EQUALITY(a, b, scale) (fabs((a) - (b)) < (FUZZ * fabs(scale)))
typedef unsigned int outcode; /* for Cohen-Sutherland clipper */
enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };
enum { ACCEPTED = 0x1, CLIPPED_FIRST = 0x2, CLIPPED_SECOND = 0x4 };
#define TRIAL_NUMBER_OF_TICK_INTERVALS 5
#define MAX_NUM_SUBTICKS 29 /* max num. of linearly spaced subticks */
#define RELATIVE_SUBTICK_SIZE 0.4 /* subtick_size / tick_size */
/* if a log axis spans >5.0 orders of magnitude, don't plot log subsubticks */
#define MAX_DECADES_WITH_LOG_SUBSUBTICKS 5.0
/* inter-tick spacing types, returned by scale1() and spacing_type() */
#define S_ONE 0
#define S_TWO 1
#define S_FIVE 2
#define S_TWO_FIVE 3 /* we don't use this one, but user may request it */
#define S_UNKNOWN -2
/* valid graph axis layout types; A_LOG2, anyone? */
#define A_LINEAR 0
#define A_LOG10 1
/* The x_trans and y_trans elements of a Multigrapher specify the current
linear transformation from user coordinates to device coordinates. They
are used both in the plotting of a graph frame, and in the plotting of
data points within a graph. */
typedef struct
{
/* Input (user) coordinates, all floating point. These are the
coordinates used in the original data points (or their base-10 logs,
for an axis of log type). We'll map them to the unit interval
[0.0,1.0]. */
double input_min, input_max; /* min, max */
double input_range; /* max - min, precomputed for speed */
/* If we're reversing axes, we'll then map [0.0,1.0] to [1.0,0.0] */
bool reverse;
/* We'll map [0.0,1.0] to another (smaller) interval, linearly */
double squeezed_min, squeezed_max; /* min, max */
double squeezed_range; /* max - min */
/* Output [i.e., libplot] coordinates. The interval [0.0,1.0] will be
mapped to this range, and the squeezed interval to a sub-range. This
is so that the box within which points are plotted will be smaller
than the full area of the graphics display. */
double output_min, output_max; /* min */
double output_range; /* max - min */
} Transform;
/* Affine transformation macros */
/* X Scale: convert from user x value to normalized x coordinate (floating
point, 0.0 to 1.0). */
#define XS(x) (((x) - multigrapher->x_trans.input_min)/multigrapher->x_trans.input_range)
/* X Reflect: map [0,1] to [1,0], if that's called for */
#define XR(x) (multigrapher->x_trans.reverse ? 1.0 - (x) : (x))
/* X Squeeze: map [0,1] range for normalized x coordinate into a smaller
interval, the x range for the plotting area within the graphics display */
#define XSQ(x) (multigrapher->x_trans.squeezed_min + (x) * multigrapher->x_trans.squeezed_range)
/* X Plot: convert from normalized x coordinate to floating point libplot
coordinate. */
#define XP(x) (multigrapher->x_trans.output_min + (x) * multigrapher->x_trans.output_range)
/* X Value: convert from user x value (floating point) to floating point
libplot coordinate. */
#define XV(x) XP(XSQ(XR(XS(x))))
/* X Normalize: the same, but do not perform reflection if any. (We use
this for plotting of axes and their labels.) */
#define XN(y) XP(XSQ(XS(y)))
/* Y Scale: convert from user y value to normalized y coordinate (floating
point, 0.0 to 1.0). */
#define YS(y) (((y) - multigrapher->y_trans.input_min)/multigrapher->y_trans.input_range)
/* Y Reflect: map [0,1] to [1,0], if that's called for */
#define YR(y) (multigrapher->y_trans.reverse ? 1.0 - (y) : (y))
/* Y Squeeze: map [0,1] range for normalized y coordinate into a smaller
interval, the y range for the plotting area within the graphics display */
#define YSQ(y) (multigrapher->y_trans.squeezed_min + (y) * multigrapher->y_trans.squeezed_range)
/* Y Plot: convert from normalized y coordinate to floating point libplot
coordinate. */
#define YP(y) (multigrapher->y_trans.output_min + (y) * multigrapher->y_trans.output_range)
/* Y Value: convert from user y value (floating point) to floating point
libplot coordinate. (We use this for plotting of points.) */
#define YV(y) YP(YSQ(YR(YS(y))))
/* Y Normalize: the same, but do not perform reflection if any. (We use
this for plotting of axes and their labels.) */
#define YN(y) YP(YSQ(YS(y)))
/* Size Scale: convert distances, or sizes, from normalized coors to
libplot coordinates. (Used for tick, symbol, and font sizes.) The min
should really be precomputed. */
#define SS(x) \
(DMIN(multigrapher->x_trans.output_range * multigrapher->x_trans.squeezed_range, \
multigrapher->y_trans.output_range * multigrapher->y_trans.squeezed_range) * (x))
/* The `x_axis' and `y_axis' elements of a Multigrapher, which are of type
`Axis', specify the layout of the two axes of a graph. They are used in
the drawing of a graph frame. All elements that are doubles are
expressed in user coordinates (unless the axis is logarithmic, in which
case logs are taken before this structure is filled in). */
/* The `x_axis' and `y_axis' elements are filled in by calls to
prepare_axis() when set_graph_parameters() is called. The only
exceptions to this are the elements `max_width' and `non_user_ticks',
which are filled in by draw_frame_of_graph(), as the frame for a graph
is being drawn. */
typedef struct
{
const char *font_name; /* fontname for axis label and tick labels */
double font_size; /* font size for axis label and tick labels */
const char *label; /* axis label (a string) */
int type; /* axis layout type (A_LINEAR or A_LOG10) */
double tick_spacing; /* distance between ticks */
int min_tick_count, max_tick_count; /* tick location = count * spacing */
bool have_lin_subticks; /* does axis have linearly spaced subticks? */
double lin_subtick_spacing; /* distance between linearly spaced subticks */
int min_lin_subtick_count, max_lin_subtick_count;
bool have_normal_subsubticks; /* does axis have logarithmic subsubticks?*/
bool user_specified_subsubticks; /* axis has user-spec'd subsubticks? */
double subsubtick_spacing; /* spacing for user-specified ones */
double other_axis_loc; /* location of intersection w/ other axis */
double alt_other_axis_loc; /* alternative loc. (e.g. right end vs. left)*/
bool switch_axis_end; /* other axis at right/top, not left/bottom? */
bool omit_ticks; /* just plain omit them (and their labels) ? */
double max_label_width; /* max width of labels placed on axis, in
libplot coors (we update this during
plotting, for y axis only) */
int labelled_ticks; /* number of labelled ticks, subticks, and
subsubticks drawn on the axis
(we update this during plotting, so we
can advise the user to specify a tick
spacing by hand if labelled_ticks <= 2) */
} Axis;
/* The Multigrapher structure. A pointer to one of these is passed as the
first argument to each Multigrapher method (e.g., plot_point()). */
struct MultigrapherStruct
{
/* multigrapher parameters (not updated over a multigrapher's lifetime) */
plPlotter *plotter; /* GNU libplot Plotter handle */
const char *output_format; /* type of libplot device driver [unused] */
const char *bg_color; /* color of background, if non-NULL */
bool save_screen; /* erase display when opening plotter? */
/* graph parameters (constant over any single graph) */
Transform x_trans, y_trans; /* user->device coor transformations */
Axis x_axis, y_axis; /* information on each axis */
grid_type grid_spec; /* frame specification */
double blankout_fraction; /* 1.0 means blank whole box before drawing */
bool no_rotate_y_label; /* useful for pre-X11R6 X servers */
double tick_size; /* fractional tick size */
double subtick_size; /* fractional subtick size (for linear axes) */
double frame_line_width; /* fractional width of lines in the frame */
double half_line_width; /* approx. half of this, in libplot coors */
const char *frame_color; /* color for frame (and graph, if no -C) */
const char *title; /* graph title */
const char *title_font_name; /* font for graph title */
double title_font_size; /* fractional height of graph title */
int clip_mode; /* 0, 1, or 2 (cf. clipping in gnuplot) */
/* following elements are updated during plotting of points; they're the
chief repository for internal state */
bool first_point_of_polyline; /* true only at beginning of each polyline */
double oldpoint_x, oldpoint_y; /* last-plotted point */
int symbol; /* symbol being plotted at each point */
int linemode; /* linemode used for polyline */
};
/* forward references */
static int clip_line (Multigrapher *multigrapher, double *x0_p, double *y0_p, double *x1_p, double *y1_p);
static int spacing_type (double spacing);
static outcode compute_outcode (Multigrapher *multigrapher, double x, double y, bool tolerant);
static void plot_abscissa_log_subsubtick (Multigrapher *multigrapher, double xval);
static void plot_errorbar (Multigrapher *multigrapher, const Point *p);
static void plot_ordinate_log_subsubtick (Multigrapher *multigrapher, double xval);
static void prepare_axis (Axis *axisp, Transform *trans, double min, double max, double spacing, const char *font_name, double font_size, const char *label, double subsubtick_spacing, bool user_specified_subsubticks, bool round_to_next_tick, bool log_axis, bool reverse_axis, bool switch_axis_end, bool omit_ticks);
static void print_tick_label (char *labelbuf, const Axis *axis, const Transform *transform, double val);
static void scale1 (double min, double max, double *tick_spacing, int *tick_spacing_type);
static void set_line_style (Multigrapher *multigrapher, int style, bool use_color);
static void transpose_portmanteau (int *val);
/* print_tick_label() prints a label on an axis tick. The format depends
* on whether the axis is a log axis or a linear axis; also, the magnitude
* of the axis labels.
*/
static void
print_tick_label (char *labelbuf, const Axis *axis, const Transform *transform, double val)
{
int prec;
char *eloc, *ptr;
char labelbuf_tmp[64], incrbuf[64];
double spacing;
bool big_exponents;
double min, max;
/* two possibilities: large/small exponent magnitudes */
min = (axis->type == A_LOG10
? pow (10.0, transform->input_min) : transform->input_min);
max = (axis->type == A_LOG10
? pow (10.0, transform->input_max) : transform->input_max);
big_exponents = (((min != 0.0 && fabs (log10 (fabs (min))) >= 4.0)
|| (max != 0.0 && fabs (log10 (fabs (max))) >= 4.0))
? true : false);
if (big_exponents)
/* large exponents, rewrite as foo x 10^bar, using escape sequences */
{
char *src = labelbuf_tmp, *dst = labelbuf;
int exponent;
char floatbuf[64];
char *fptr = floatbuf;
double prefactor;
sprintf (labelbuf_tmp, "%e", val);
if ((eloc = strchr (labelbuf_tmp, (int)'e')) == NULL)
return;
if (axis->type == A_LOG10 && !axis->user_specified_subsubticks)
/* a hack: this must be a power of 10, so just print "10^bar" */
{
sscanf (++eloc, "%d", &exponent);
sprintf (dst, "10\\sp%d\\ep", exponent);
return;
}
/* special case: zero prints as `0', not 0.0x10^whatever */
if (val == 0.0)
{
*dst++ = '0';
*dst = '\0';
return;
}
while (src < eloc)
*fptr++ = *src++;
*fptr = '\0';
sscanf (floatbuf, "%lf", &prefactor); /* get foo */
sscanf (++src, "%d", &exponent); /* get bar */
spacing = (axis->type == A_LINEAR
? axis->tick_spacing
: axis->subsubtick_spacing); /* user-specified, for log axis*/
sprintf (incrbuf, "%f",
spacing / pow (10.0, (double)exponent));
ptr = strchr (incrbuf, (int)'.');
prec = 0;
if (ptr != NULL)
{
int count = 0;
while (*(++ptr))
{
count++;
if (*ptr != '0')
prec = count;
}
}
/* \sp ... \ep is start_superscript ... end_superscript, and \r6 is
right-shift by 1/6 em. \mu is the `times' character. */
sprintf (dst, "%.*f\\r6\\mu10\\sp%d\\ep",
prec, prefactor, exponent);
return;
}
else /* small-size exponent magnitudes */
{
if (axis->type == A_LOG10 && !axis->user_specified_subsubticks)
/* a hack: this must be a (small) power of 10, so we'll just use
%g format (same as %f, no trailing zeroes) */
{
sprintf (labelbuf, "%.9g", val);
return;
}
/* always use no. of digits of precision present in increment */
spacing = (axis->type == A_LINEAR
? axis->tick_spacing
: axis->subsubtick_spacing); /* user-specified, for log axis*/
sprintf (incrbuf, "%.9f", spacing);
ptr = strchr (incrbuf, (int)'.');
prec = 0;
if (ptr != NULL)
{
int count = 0;
while (*(++ptr))
{
count++;
if (*ptr != '0')
prec = count;
}
}
sprintf (labelbuf, "%.*f", prec, val);
return;
}
}
/* Algorithm SCALE1, for selecting an inter-tick spacing that will yield a
* good-looking linear-format axis. The spacing is always 1.0, 2.0, or 5.0
* times a power of ten.
*
* Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and
* SCALE3 for Determination of Scales on Computer Generated
* Plots", Communications of the ACM, 10 (1973), 639-640.
* Also cited as ACM Algorithm 463.
*
* We call this routine even when the axis is logarithmic rather than
* linear. In that case the arguments are the logs of the actual
* arguments, so that it computes an optimal inter-tick factor rather than
* an optimal inter-tick spacing.
*/
/* ARGS: min,max = data min, max
tick_spacing = inter-tick spacing
tick_spacing_type = 0,1,2 i.e. S_ONE,S_TWO,S_FIVE */
static void
scale1 (double min, double max, double *tick_spacing, int *tick_spacing_type)
{
int k;
double nal;
double a, b;
/* valid interval lengths */
static const double vint[] =
{
1.0, 2.0, 5.0, 10.0
};
/* Corresponding breakpoints. The published algorithm uses geometric
means, i.e. sqrt(2), sqrt(10), sqrt(50), but using sqrt(10)=3.16...
will (if nticks=5, as we choose it to be) cause intervals of length
1.5 to yield an inter-tick distance of 0.2 rather than 0.5. So we
could reduce it to 2.95. Similarly we could reduce sqrt(50) to 6.95
so that intervals of length 3.5 will yield an inter-tick distance of
1.0 rather than 0.5. */
static const double sqr[] =
{
M_SQRT2, 3.16228, 7.07107
};
/* compute trial inter-tick interval length */
a = (max - min) / TRIAL_NUMBER_OF_TICK_INTERVALS;
a *= (max > min) ? 1.0 : -1.0; /* paranoia, max>min always */
if (a <= 0.0)
{
fprintf(stderr, "%s: error: the trial inter-tick spacing '%g' is bad\n",
progname, a);
exit (EXIT_FAILURE);
}
nal = floor(log10(a));
b = a * pow (10.0, -nal); /* 1.0 <= b < 10.0 */
/* round to closest permissible inter-tick interval length */
k = 0;
do
{
if (b < sqr[k])
break;
k++;
}
while (k < 3);
*tick_spacing = (max > min ? 1.0 : -1.0) * vint[k] * pow (10.0, nal);
/* for increment type, 0,1,2 means 1,2,5 times a power of 10 */
*tick_spacing_type = (k == 3 ? 0 : k);
return;
}
/* Determine whether an inter-tick spacing (in practice, one specified by
the user) is 1.0, 2.0, or 5.0 times a power of 10. */
static int
spacing_type (double incr)
{
int i;
int i_tenpower = (int)(floor(log10(incr)));
double tenpower = 1.0;
bool neg_power = false;
if (i_tenpower < 0)
{
neg_power = true;
i_tenpower = -i_tenpower;
}
for (i = 0; i < i_tenpower; i++)
tenpower *= 10;
if (neg_power)
tenpower = 1.0 / tenpower;
if (NEAR_EQUALITY(incr, tenpower, tenpower))
return S_ONE;
else if (NEAR_EQUALITY(incr, 2 * tenpower, tenpower))
return S_TWO;
else if (NEAR_EQUALITY(incr, 2.5 * tenpower, tenpower))
return S_TWO_FIVE;
else if (NEAR_EQUALITY(incr, 5 * tenpower, tenpower))
return S_FIVE;
else
return S_UNKNOWN;
}
/* prepare_axis() fills in the Axis structure for an axis, and some of
* the linear transformation variables in the Transform structure also.
*/
/* ARGS: user_specified_subticks = linear ticks on a log axis?
round_to_next_tick = round limits to next tick mark?
log_axis = log axis?
reverse_axis = reverse min, max?
switch_axis_end = intersection w/ other axis in alt. pos.?
omit_ticks = suppress all ticks and tick labels? */
static void
prepare_axis (Axis *axisp, Transform *trans, double min, double max, double spacing, const char *font_name, double font_size, const char *label, double subsubtick_spacing, bool user_specified_subsubticks, bool round_to_next_tick, bool log_axis, bool reverse_axis, bool switch_axis_end, bool omit_ticks)
{
double range;
int tick_spacing_type = 0;
double tick_spacing, lin_subtick_spacing;
int min_tick_count, max_tick_count;
int min_lin_subtick_count, max_lin_subtick_count;
bool have_lin_subticks;
if (min > max)
/* paranoia, max < min is swapped at top level */
{
fprintf(stderr, "%s: error: min > max for an axis, which is not allowed\n",
progname);
exit (EXIT_FAILURE);
}
if (min == max) /* expand in a clever way */
{
max = floor (max + 1.0);
min = ceil (min - 1.0);
}
if (log_axis) /* log axis, data are stored in logarithmic form */
/* compute a tick spacing; user can't specify it */
{
scale1 (min, max, &tick_spacing, &tick_spacing_type);
if (tick_spacing <= 1.0)
{
tick_spacing = 1.0;
tick_spacing_type = S_ONE;
}
}
else /* linear axis */
{
if (spacing == 0.0) /* i.e., not specified by user */
scale1 (min, max, &tick_spacing, &tick_spacing_type);
else /* luser is boss, don't use SCALE1 */
{
tick_spacing = spacing;
tick_spacing_type = spacing_type (spacing);
}
}
range = max - min; /* range is not negative */
if (round_to_next_tick) /* expand both limits to next tick */
{
if (user_specified_subsubticks)
/* Special Case. If user specified the `spacing' argument to -x or
-y on a logarithmic axis, our usual tick-generating and
tick-plotting algorithms are disabled. So we don't bother with
min_tick_count or several other fields of the axis struct;
instead we just compute a new (rounded) max, min, and range.
Since most data are stored as logs, this is complicated. */
{
double true_min = pow (10.0, min), true_max = pow (10.0, max);
double true_range = true_max - true_min;
int min_count, max_count;
min_count = (int)(floor ((true_min + FUZZ * true_range)
/ subsubtick_spacing));
max_count = (int)(ceil ((true_max - FUZZ * true_range)
/ subsubtick_spacing));
/* avoid core dump, do *not* reduce minimum to zero! */
if (min_count > 0)
min = log10 (min_count * subsubtick_spacing);
max = log10 (max_count * subsubtick_spacing);
range = max - min;
min_tick_count = max_tick_count = 0; /* keep gcc happy */
}
else /* normal `expand limits to next tick' case */
{
min_tick_count = (int)(floor((min + FUZZ * range)/ tick_spacing));
max_tick_count = (int)(ceil((max - FUZZ * range)/ tick_spacing));
/* max_tick_count > min_tick_count always */
/* tickval = tick_spacing * count,
for all count in [min_count,max_count]; must have >=2 ticks */
min = tick_spacing * min_tick_count;
max = tick_spacing * max_tick_count;
range = max - min;
}
}
else /* don't expand limits to next tick */
{
min_tick_count = (int)(ceil((min - FUZZ * range)/ tick_spacing));
max_tick_count = (int)(floor((max + FUZZ * range)/ tick_spacing));
/* max_tick_count <= min_tick_count is possible */
/* tickval = incr * count,
for all count in [min_count,max_count]; can have 0,1,2,3... ticks */
}
/* Allow 5 subticks per tick if S_FIVE or S_TWO_FIVE, 2 if S_TWO. Case
S_ONE is special; we try 10, 5, and 2 in succession */
switch (tick_spacing_type)
{
case S_FIVE:
case S_TWO_FIVE:
lin_subtick_spacing = tick_spacing / 5;
break;
case S_TWO:
lin_subtick_spacing = tick_spacing / 2;
break;
case S_ONE:
lin_subtick_spacing = tick_spacing / 10;
min_lin_subtick_count = (int)(ceil((min - FUZZ * range)/ lin_subtick_spacing));
max_lin_subtick_count = (int)(floor((max + FUZZ * range)/ lin_subtick_spacing));
if (max_lin_subtick_count - min_lin_subtick_count > MAX_NUM_SUBTICKS)
{
lin_subtick_spacing = tick_spacing / 5;
min_lin_subtick_count = (int)(ceil((min - FUZZ * range)/ lin_subtick_spacing));
max_lin_subtick_count = (int)(floor((max + FUZZ * range)/ lin_subtick_spacing));
if (max_lin_subtick_count - min_lin_subtick_count > MAX_NUM_SUBTICKS)
lin_subtick_spacing = tick_spacing / 2;
}
break;
default:
/* in default case, i.e. S_UNKNOWN, we won't plot linear subticks */
lin_subtick_spacing = tick_spacing; /* not actually needed, since not plotted */
break;
}
/* smallest possible inter-subtick factor for a log axis is 10.0 */
if (log_axis && lin_subtick_spacing <= 1.0)
lin_subtick_spacing = 1.0;
min_lin_subtick_count = (int)(ceil((min - FUZZ * range)/ lin_subtick_spacing));
max_lin_subtick_count = (int)(floor((max + FUZZ * range)/ lin_subtick_spacing));
have_lin_subticks
= ((tick_spacing_type != S_UNKNOWN /* S_UNKNOWN -> no subticks */
&& (max_lin_subtick_count - min_lin_subtick_count) <= MAX_NUM_SUBTICKS)
? true : false);
/* fill in parameters for axis-specific affine transformation */
trans->input_min = min;
trans->input_max = max;
trans->input_range = range; /* precomputed for speed */
trans->reverse = reverse_axis;
/* fill in axis-specific plot frame variables */
axisp->switch_axis_end = switch_axis_end;
axisp->omit_ticks = omit_ticks;
axisp->label = label;
axisp->font_name = font_name;
axisp->font_size = font_size;
axisp->max_label_width = 0.0;
axisp->type = log_axis ? A_LOG10 : A_LINEAR;
axisp->tick_spacing = tick_spacing;
axisp->min_tick_count = min_tick_count;
axisp->max_tick_count = max_tick_count;
axisp->have_lin_subticks = have_lin_subticks;
axisp->lin_subtick_spacing = lin_subtick_spacing;
axisp->min_lin_subtick_count = min_lin_subtick_count;
axisp->max_lin_subtick_count = max_lin_subtick_count;
axisp->user_specified_subsubticks = user_specified_subsubticks;
axisp->subsubtick_spacing = subsubtick_spacing;
axisp->labelled_ticks = 0; /* updated during drawing of frame */
if (log_axis) /* logarithmic axis */
/* do we have special logarithmic subsubticks, and should we label them? */
{
if (max - min <=
MAX_DECADES_WITH_LOG_SUBSUBTICKS + FUZZ)
/* not too many orders of magnitude, so plot normal log subsubticks */
axisp->have_normal_subsubticks = true;
else
/* too many orders of magnitude, don't plot log subsubticks */
axisp->have_normal_subsubticks = false;
}
else /* linear axes don't have log subsubticks */
axisp->have_normal_subsubticks = false;
}
/* The following routines [new_multigrapher(), begin_graph(),
* set_graph_parameters(), draw_frame_of_graph(), plot_point(),
* end_graph(), delete_multigrapher()] are the basic routines of the
* point-plotter . See descriptions at the head of this file. */
/* Create a new Multigrapher. The arguments, after the first, are the
libplot Plotter parameters that the `graph' user can set on the command
line. */
Multigrapher *
new_multigrapher (const char *output_format, const char *bg_color, const char *bitmap_size, const char *emulate_color, const char *max_line_length, const char *meta_portable, const char *page_size, const char *rotation_angle, bool save_screen)
{
plPlotterParams *plotter_params;
plPlotter *plotter;
Multigrapher *multigrapher;
multigrapher = (Multigrapher *)xmalloc (sizeof (Multigrapher));
/* set Plotter parameters */
plotter_params = pl_newplparams ();
pl_setplparam (plotter_params, "BG_COLOR", (void *)bg_color);
pl_setplparam (plotter_params, "BITMAPSIZE", (void *)bitmap_size);
pl_setplparam (plotter_params, "EMULATE_COLOR", (void *)emulate_color);
pl_setplparam (plotter_params, "MAX_LINE_LENGTH", (void *)max_line_length);
pl_setplparam (plotter_params, "META_PORTABLE", (void *)meta_portable);
pl_setplparam (plotter_params, "PAGESIZE", (void *)page_size);
pl_setplparam (plotter_params, "ROTATION", (void *)rotation_angle);
/* create Plotter and open it */
plotter = pl_newpl_r (output_format, NULL, stdout, stderr, plotter_params);
if (plotter == (plPlotter *)NULL)
return (Multigrapher *)NULL;
pl_deleteplparams (plotter_params);
multigrapher->plotter = plotter;
if (pl_openpl_r (plotter) < 0)
return (Multigrapher *)NULL;
multigrapher->bg_color = bg_color;
/* if called for, erase it; set up the user->device coor map */
if (!save_screen || bg_color)
pl_erase_r (plotter);
pl_fspace_r (plotter, 0.0, 0.0, (double)PLOT_SIZE, (double)PLOT_SIZE);
return multigrapher;
}
int
delete_multigrapher (Multigrapher *multigrapher)
{
int retval;
retval = pl_closepl_r (multigrapher->plotter);
if (retval >= 0)
retval = pl_deletepl_r (multigrapher->plotter);
free (multigrapher);
return retval;
}
void
begin_graph (Multigrapher *multigrapher, double scale, double trans_x, double trans_y)
{
pl_savestate_r (multigrapher->plotter);
pl_fconcat_r (multigrapher->plotter,
scale, 0.0, 0.0, scale,
trans_x * PLOT_SIZE, trans_y * PLOT_SIZE);
}
void
end_graph (Multigrapher *multigrapher)
{
pl_restorestate_r (multigrapher->plotter);
}
/* ARGS:
Multigrapher *multigrapher
double frame_line_width = fractional width of lines in the frame
const char *frame_color = color for frame (and plot if no -C option)
const char *title = graph title
const char *title_font_name = font for graph title (string)
double title_font_size = font size for graph title
double tick_size = fractional size of ticks
grid_type grid_spec = gridstyle (and tickstyle) spec
double x_min, x_max, x_spacing
double y_min, y_max, y_spacing
bool spec_x_spacing
bool spec_y_spacing
double width, height, up, right
const char *x_font_name
double x_font_size
const char *x_label
const char *y_font_name
double y_font_size
const char *y_label
bool no_rotate_y_label
-- portmanteaux args --
int log_axis = whether axes should be logarithmic
int round_to_next_tick = round limits to the next tick mark
int switch_axis_end = put axes at right/top, not left/bottom?
int omit_ticks = omit all ticks, tick labels from an axis?
-- other args --
int clip_mode = 0, 1, or 2
double blankout_fraction = 1.0 means blank out whole box before plot
bool transpose_axes */
void
set_graph_parameters (Multigrapher *multigrapher, double frame_line_width, const char *frame_color, const char *title, const char *title_font_name, double title_font_size, double tick_size, grid_type grid_spec, double x_min, double x_max, double x_spacing, double y_min, double y_max, double y_spacing, bool spec_x_spacing, bool spec_y_spacing, double width, double height, double up, double right, const char *x_font_name, double x_font_size, const char *x_label, const char *y_font_name, double y_font_size, const char *y_label, bool no_rotate_y_label, int log_axis, int round_to_next_tick, int switch_axis_end, int omit_ticks, int clip_mode, double blankout_fraction, bool transpose_axes)
{
double x_subsubtick_spacing = 0.0, y_subsubtick_spacing = 0.0;
/* local portmanteau variables */
int reverse_axis = 0; /* min > max on an axis? */
int user_specified_subsubticks = 0; /* i.e. linear ticks on a log axis? */
if (log_axis & X_AXIS)
{
if (spec_x_spacing)
/* spacing is handled specially for log axes */
{
spec_x_spacing = false;
user_specified_subsubticks |= X_AXIS;
x_subsubtick_spacing = x_spacing;
}
}
if (log_axis & Y_AXIS)
{
if (spec_y_spacing)
{
/* spacing is handled specially for log axes */
spec_y_spacing = false;
user_specified_subsubticks |= Y_AXIS;
y_subsubtick_spacing = y_spacing;
}
}
/* check for reversed axes (min > max) */
if (x_max < x_min)
{
reverse_axis |= X_AXIS;
{
double temp;
temp = x_min;
x_min = x_max;
x_max = temp;
}
}
if (x_max == x_min)
{
fprintf (stderr,
"%s: identical upper and lower x limits are separated\n",
progname);
/* separate them */
x_max += 1.0;
x_min -= 1.0;
}
/* check for reversed axes (min > max) */
if (y_max < y_min)
{
reverse_axis |= Y_AXIS;
{
double temp;
temp = y_min;
y_min = y_max;
y_max = temp;
}
}
if (y_max == y_min)
{
fprintf (stderr,
"%s: identical upper and lower y limits are separated\n",
progname);
/* separate them */
y_max += 1.0;
y_min -= 1.0;
}
/* At this point, min < max for each axis, if the user specified the two
limits on an axis; reverse_axis portmanteau variable keeps track of
whether either axis was discovered to be reversed. */
/* silently accept negative spacing as equivalent as positive */
if (spec_x_spacing)
{
if (x_spacing == 0.0)
{
fprintf (stderr,
"%s: error: the spacing between ticks on an axis is zero\n",
progname);
exit (EXIT_FAILURE);
}
x_spacing = fabs (x_spacing);
}
if (spec_y_spacing)
{
if (y_spacing == 0.0)
{
fprintf (stderr,
"%s: error: the spacing between ticks on an axis is zero\n",
progname);
exit (EXIT_FAILURE);
}
y_spacing = fabs (y_spacing);
}
/* now transpose the two axes (i.e. their portmanteau variables, labels,
limits etc.) if transpose_axes was set */
if (transpose_axes)
{
const char *temp_string;
double temp_double;
transpose_portmanteau (&log_axis);
transpose_portmanteau (&round_to_next_tick);
transpose_portmanteau (&switch_axis_end);
transpose_portmanteau (&omit_ticks);
transpose_portmanteau (&reverse_axis);
transpose_portmanteau (&user_specified_subsubticks);
temp_string = x_label;
x_label = y_label;
y_label = temp_string;
temp_double = x_min;
x_min = y_min;
y_min = temp_double;
temp_double = x_max;
x_max = y_max;
y_max = temp_double;
temp_double = x_spacing;
x_spacing = y_spacing;
y_spacing = temp_double;
temp_double = x_subsubtick_spacing;
x_subsubtick_spacing = y_subsubtick_spacing;
y_subsubtick_spacing = temp_double;
}
/* fill in the Multigrapher struct */
multigrapher->frame_line_width = frame_line_width;
multigrapher->frame_color = frame_color;
multigrapher->no_rotate_y_label = no_rotate_y_label;
multigrapher->blankout_fraction = blankout_fraction;
if (title != NULL)
multigrapher->title = xstrdup (title);
else
multigrapher->title = NULL;
if (title_font_name != NULL)
multigrapher->title_font_name = xstrdup (title_font_name);
else
multigrapher->title_font_name = NULL;
multigrapher->title_font_size = title_font_size;
multigrapher->tick_size = tick_size;
multigrapher->subtick_size = RELATIVE_SUBTICK_SIZE * tick_size;
multigrapher->grid_spec = grid_spec;
multigrapher->clip_mode = clip_mode;
/* fill in the Transform and Axis elements for each coordinate */
prepare_axis (&multigrapher->x_axis, &multigrapher->x_trans,
x_min, x_max, x_spacing,
x_font_name, x_font_size, x_label,
x_subsubtick_spacing,
(bool)(user_specified_subsubticks & X_AXIS),
(bool)(round_to_next_tick & X_AXIS),
(bool)(log_axis & X_AXIS),
(bool)(reverse_axis & X_AXIS),
(bool)(switch_axis_end & X_AXIS),
(bool)(omit_ticks & X_AXIS));
prepare_axis (&multigrapher->y_axis, &multigrapher->y_trans,
y_min, y_max, y_spacing,
y_font_name, y_font_size, y_label,
y_subsubtick_spacing,
(bool)(user_specified_subsubticks & Y_AXIS),
(bool)(round_to_next_tick & Y_AXIS),
(bool)(log_axis & Y_AXIS),
(bool)(reverse_axis & Y_AXIS),
(bool)(switch_axis_end & Y_AXIS),
(bool)(omit_ticks & Y_AXIS));