FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gnuplot-old/src/axis.c at gnuplot-4-4-alpha · gnuplot/gnuplot-old · GitHub
gnuplot
gnuplot-old
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
gnuplot-old
/
src
/
axis.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1684 lines (1514 loc) · 51.7 KB
Breadcrumbs
gnuplot-old
/
src
/
axis.c
Copy path
File metadata and controls
1684 lines (1514 loc) · 51.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#ifndef
lint
static
char
*
RCSid
() {
return
RCSid
(
"$Id: axis.c,v 1.77 2009/04/22 23:20:28 sfeam Exp $"
); }
#endif
/* GNUPLOT - axis.c */
/*[
* Copyright 2000, 2004 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
]*/
#include
"axis.h"
#include
"stdfn.h"
#include
"alloc.h"
#include
"command.h"
#include
"gadgets.h"
#include
"gp_time.h"
#include
"graphics.h"
/* For label_width() */
/* #include "setshow.h" */
#include
"term_api.h"
#include
"variable.h"
/* HBB 20000416: this is the start of my try to centralize everything
* related to axes, once and for all. It'll probably end up as a
* global array of OO-style 'axis' objects, when it's done */
/* HBB 20000725: gather all per-axis variables into a struct, and set
* up a single large array of such structs. Next step might be to use
* isolated AXIS structs, instead of an array. At least for *some* of
* the axes... */
AXIS
axis_array
[
AXIS_ARRAY_SIZE
]
=
AXIS_ARRAY_INITIALIZER
(
DEFAULT_AXIS_STRUCT
);
/* Keep defaults varying by axis in their own array, to ease initialization
* of the main array */
const
AXIS_DEFAULTS
axis_defaults
[
AXIS_ARRAY_SIZE
]
=
{
{
-10
,
10
,
"z"
,
TICS_ON_BORDER
, },
{
-10
,
10
,
"y"
,
TICS_ON_BORDER
|
TICS_MIRROR
, },
{
-10
,
10
,
"x"
,
TICS_ON_BORDER
|
TICS_MIRROR
, },
{
-
5
,
5
,
"t"
,
NO_TICS
, },
{
-10
,
10
,
"z2"
,
NO_TICS
, },
{
-10
,
10
,
"y2"
,
NO_TICS
, },
{
-10
,
10
,
"x2"
,
NO_TICS
, },
{
-
0
,
10
,
"r"
,
NO_TICS
, },
{
-
5
,
5
,
"u"
,
NO_TICS
, },
{
-
5
,
5
,
"v"
,
NO_TICS
, },
{
-10
,
10
,
"cb"
,
TICS_ON_BORDER
|
TICS_MIRROR
, },
};
/* either the 'set format <axis>' or an automatically invented time
* format string */
static
char
ticfmt
[
AXIS_ARRAY_SIZE
][
MAX_ID_LEN
+
1
];
/* HBB 20010831: new enum typedef, to make code using this more
* self-explanatory */
/* The unit the tics of a given time/date axis are to interpreted in */
/* HBB 20040318: start at one, to avoid undershoot */
typedef
enum
e_timelevel
{
TIMELEVEL_SECONDS
=
1
,
TIMELEVEL_MINUTES
,
TIMELEVEL_HOURS
,
TIMELEVEL_DAYS
,
TIMELEVEL_WEEKS
,
TIMELEVEL_MONTHS
,
TIMELEVEL_YEARS
}
t_timelevel
;
static
t_timelevel
timelevel
[
AXIS_ARRAY_SIZE
];
/* The <increment> given in a 'set {x|y|...}tics', or an automatically
* generated one, if automatic tic placement is active */
static
double
ticstep
[
AXIS_ARRAY_SIZE
];
/* HBB 20000506 new variable: parsing table for use with the table
* module, to help generalizing set/show/unset/save, where possible */
const
struct
gen_table
axisname_tbl
[
AXIS_ARRAY_SIZE
+
1
]
=
{
{
"z"
,
FIRST_Z_AXIS
},
{
"y"
,
FIRST_Y_AXIS
},
{
"x"
,
FIRST_X_AXIS
},
{
"t"
,
T_AXIS
},
{
"z2"
,
SECOND_Z_AXIS
},
{
"y2"
,
SECOND_Y_AXIS
},
{
"x2"
,
SECOND_X_AXIS
},
{
"r"
,
R_AXIS
},
{
"u"
,
U_AXIS
},
{
"v"
,
V_AXIS
},
{
"cb"
,
COLOR_AXIS
},
{
NULL
,
-1
}
};
/* penalty for doing tics by callback in gen_tics is need for global
* variables to communicate with the tic routines. Dont need to be
* arrays for this */
/* HBB 20000416: they may not need to be array[]ed, but it'd sure
* make coding easier, in some places... */
/* HBB 20000416: for the testing, these are global... */
/* static */
int
tic_start
,
tic_direction
,
tic_text
,
rotate_tics
,
tic_hjust
,
tic_vjust
,
tic_mirror
;
const
struct
ticdef
default_axis_ticdef
=
DEFAULT_AXIS_TICDEF
;
/* axis labels */
const
text_label
default_axis_label
=
EMPTY_LABELSTRUCT
;
/* zeroaxis drawing */
const
lp_style_type
default_axis_zeroaxis
=
DEFAULT_AXIS_ZEROAXIS
;
/* grid drawing */
/* int grid_selection = GRID_OFF; */
# define
DEFAULT_GRID_LP
{ 0, -1, 0, 1.0, 1.0, 0 }
const
struct
lp_style_type
default_grid_lp
=
DEFAULT_GRID_LP
;
struct
lp_style_type
grid_lp
=
DEFAULT_GRID_LP
;
struct
lp_style_type
mgrid_lp
=
DEFAULT_GRID_LP
;
int
grid_layer
=
-1
;
double
polar_grid_angle
=
0
;
/* nonzero means a polar grid */
/* Length of the longest tics label, set by widest_tic_callback(): */
int
widest_tic_strlen
;
/* axes being used by the current plot */
/* These are mainly convenience variables, replacing separate copies of
* such variables originally found in the 2D and 3D plotting code */
AXIS_INDEX
x_axis
=
FIRST_X_AXIS
;
AXIS_INDEX
y_axis
=
FIRST_Y_AXIS
;
AXIS_INDEX
z_axis
=
FIRST_Z_AXIS
;
/* --------- internal prototypes ------------------------- */
static
double
dbl_raise
__PROTO
((
double
x
,
int
y
));
static
double
make_auto_time_minitics
__PROTO
((
t_timelevel
,
double
));
static
double
make_tics
__PROTO
((
AXIS_INDEX
,
int
));
static
double
quantize_time_tics
__PROTO
((
AXIS_INDEX
,
double
,
double
,
int
));
static
double
time_tic_just
__PROTO
((
t_timelevel
,
double
));
static
double
round_outward
__PROTO
((
AXIS_INDEX
,
TBOOLEAN
,
double
));
static
TBOOLEAN
axis_position_zeroaxis
__PROTO
((
AXIS_INDEX
));
static
double
quantize_duodecimal_tics
__PROTO
((
double
,
int
));
static
void
get_position_type
__PROTO
((
enum
position_type
*
type
,
int
*
axes
));
/* ---------------------- routines ----------------------- */
/* check range and take logs of min and max if logscale
* this also restores min and max for ranges like [10:-10]
*/
#define
LOG_MSG
(
x
) x " range must be greater than 0 for scale"
/* {{{ axis_unlog_interval() */
/* this is used in a few places all over the code: undo logscaling of
* a given range if necessary. If checkrange is TRUE, will int_error() if
* range is invalid */
void
axis_unlog_interval
(
AXIS_INDEX
axis
,
double
*
min
,
double
*
max
,
TBOOLEAN
checkrange
)
{
if
(
axis_array
[
axis
].
log
) {
if
(
checkrange
&&
(
*
min
<=
0.0
||
*
max
<=
0.0
))
int_error
(
NO_CARET
,
"%s range must be greater than 0 for log scale"
,
axis_defaults
[
axis
].
name
);
*
min
=
(
*
min
<=
0
) ?
-
VERYLARGE
:
AXIS_DO_LOG
(
axis
,
*
min
);
*
max
=
(
*
max
<=
0
) ?
-
VERYLARGE
:
AXIS_DO_LOG
(
axis
,
*
max
);
}
}
/* }}} */
/* {{{ axis_revert_and_unlog_range() */
void
axis_revert_and_unlog_range
(
AXIS_INDEX
axis
)
{
if
(
axis_array
[
axis
].
range_is_reverted
) {
double
temp
=
axis_array
[
axis
].
min
;
axis_array
[
axis
].
min
=
axis_array
[
axis
].
max
;
axis_array
[
axis
].
max
=
temp
;
}
axis_unlog_interval
(
axis
,
&
axis_array
[
axis
].
min
,
&
axis_array
[
axis
].
max
,
1
);
}
/* }}} */
/* {{{ axis_log_value_checked() */
double
axis_log_value_checked
(
AXIS_INDEX
axis
,
double
coord
,
const
char
*
what
)
{
if
(
axis_array
[
axis
].
log
) {
if
(
coord
<=
0.0
) {
graph_error
(
"%s has %s coord of %g; must be above 0 for log scale!"
,
what
,
axis_defaults
[
axis
].
name
,
coord
);
}
else
return
(
AXIS_DO_LOG
(
axis
,
coord
));
}
return
(
coord
);
}
/* }}} */
/* {{{ axis_checked_extend_empty_range() */
/*
* === SYNOPSIS ===
*
* This function checks whether the data and/or plot range in a given axis
* is too small (which would cause divide-by-zero and/or infinite-loop
* problems later on). If so,
* - if autoscaling is in effect for this axis, we widen the range
* - otherwise, we abort with a call to int_error() (which prints out
* a suitable error message, then (hopefully) aborts this command and
* returns to the command prompt or whatever).
*
*
* === HISTORY AND DESIGN NOTES ===
*
* 1998 Oct 4, Jonathan Thornburg <jthorn@galileo.thp.univie.ac.at>
*
* This function used to be a (long) macro FIXUP_RANGE(AXIS, WHICH)
* which was (identically!) defined in plot2d.c and plot3d.c . As
* well as now being a function instead of a macro, the logic is also
* changed: The "too small" range test no longer depends on 'set zero'
* and is now properly scaled relative to the data magnitude.
*
* The key question in designing this function is the policy for just how
* much to widen the data range by, as a function of the data magnitude.
* This is to some extent a matter of taste. IMHO the key criterion is
* that (at least) all of the following should (a) not infinite-loop, and
* (b) give correct plots, regardless of the 'set zero' setting:
* plot 6.02e23 # a huge number >> 1 / FP roundoff level
* plot 3 # a "reasonable-sized" number
* plot 1.23e-12 # a small number still > FP roundoff level
* plot 1.23e-12 * sin(x) # a small function still > FP roundoff level
* plot 1.23e-45 # a tiny number << FP roundoff level
* plot 1.23e-45 * sin(x) # a tiny function << FP roundoff level
* plot 0 # or (more commonly) a data file of all zeros
* That is, IMHO gnuplot should *never* infinite-loop, and it should *never*
* producing an incorrect or misleading plot. In contrast, the old code
* would infinite-loop on most of these examples with 'set zero 0.0' in
* effect, or would plot the small-amplitude sine waves as the zero function
* with 'zero' set larger than the sine waves' amplitude.
*
* The current code plots all the above examples correctly and without
* infinite looping.
*
* HBB 2000/05/01: added an additional up-front test, active only if
* the new 'mesg' parameter is non-NULL.
*
* === USAGE ===
*
* Arguments:
* axis = (in) An integer specifying which axis (x1, x2, y1, y2, z, etc)
* we should do our stuff for. We use this argument as an
* index into the global arrays {min,max,auto}_array . In
* practice this argument will typically be one of the constants
* {FIRST,SECOND}_{X,Y,Z}_AXIS defined in plot.h.
* mesg = (in) if non-NULL, will check if the axis range is valid (min
* not +VERYLARGE, max not -VERYLARGE), and int_error() out
* if it isn't.
*
* Global Variables:
* auto_array, min_array, max_array (in out) (defined in axis.[ch]):
* variables describing the status of autoscaling and range ends, for
* each of the possible axes.
*
* c_token = (in) (defined in plot.h) Used in formatting an error message.
*
*/
void
axis_checked_extend_empty_range
(
AXIS_INDEX
axis
,
const
char
*
mesg
)
{
/* These two macro definitions set the range-widening policy: */
/* widen [0:0] by +/- this absolute amount */
#define
FIXUP_RANGE__WIDEN_ZERO_ABS
1.0
/* widen [nonzero:nonzero] by -/+ this relative amount */
#define
FIXUP_RANGE__WIDEN_NONZERO_REL
0.01
double
dmin
=
axis_array
[
axis
].
min
;
double
dmax
=
axis_array
[
axis
].
max
;
/* HBB 20000501: this same action was taken just before most of
* the invocations of this function, so I moved it into here.
* Only do this if 'mesg' is non-NULL --> pass NULL if you don't
* want the test */
if
(
mesg
&&
(
axis_array
[
axis
].
min
==
VERYLARGE
||
axis_array
[
axis
].
max
==
-
VERYLARGE
))
int_error
(
c_token
,
mesg
);
if
(
dmax
-
dmin
==
0.0
) {
/* empty range */
if
(
axis_array
[
axis
].
autoscale
) {
/* range came from autoscaling ==> widen it */
double
widen
=
(
dmax
==
0.0
) ?
FIXUP_RANGE__WIDEN_ZERO_ABS
:
FIXUP_RANGE__WIDEN_NONZERO_REL
*
dmax
;
if
(!(
axis
==
FIRST_Z_AXIS
&&
!
mesg
))
/* set view map */
fprintf
(
stderr
,
"Warning: empty %s range [%g:%g], "
,
axis_defaults
[
axis
].
name
,
dmin
,
dmax
);
/* HBB 20010525: correctly handle single-ended
* autoscaling, too: */
if
(
axis_array
[
axis
].
autoscale
&
AUTOSCALE_MIN
)
axis_array
[
axis
].
min
-=
widen
;
if
(
axis_array
[
axis
].
autoscale
&
AUTOSCALE_MAX
)
axis_array
[
axis
].
max
+=
widen
;
if
(!(
axis
==
FIRST_Z_AXIS
&&
!
mesg
))
/* set view map */
fprintf
(
stderr
,
"adjusting to [%g:%g]\n"
,
axis_array
[
axis
].
min
,
axis_array
[
axis
].
max
);
}
else
{
/* user has explicitly set the range (to something empty)
==> we're in trouble */
/* FIXME HBB 20000416: is c_token always set properly,
* when this is called? We might be better off using
* NO_CARET..., here */
int_error
(
c_token
,
"Can't plot with an empty %s range!"
,
axis_defaults
[
axis
].
name
);
}
}
}
/* }}} */
/* {{{ make smalltics for time-axis */
static
double
make_auto_time_minitics
(
t_timelevel
tlevel
,
double
incr
)
{
double
tinc
=
0.0
;
if
((
int
)
tlevel
<
TIMELEVEL_SECONDS
)
tlevel
=
TIMELEVEL_SECONDS
;
switch
(
tlevel
) {
case
TIMELEVEL_SECONDS
:
case
TIMELEVEL_MINUTES
:
if
(
incr
>=
5
)
tinc
=
1
;
if
(
incr
>=
10
)
tinc
=
5
;
if
(
incr
>=
20
)
tinc
=
10
;
if
(
incr
>=
60
)
tinc
=
20
;
if
(
incr
>=
2
*
60
)
tinc
=
60
;
if
(
incr
>=
6
*
60
)
tinc
=
2
*
60
;
if
(
incr
>=
12
*
60
)
tinc
=
3
*
60
;
if
(
incr
>=
24
*
60
)
tinc
=
6
*
60
;
break
;
case
TIMELEVEL_HOURS
:
if
(
incr
>=
20
*
60
)
tinc
=
10
*
60
;
if
(
incr
>=
3600
)
tinc
=
30
*
60
;
if
(
incr
>=
2
*
3600
)
tinc
=
3600
;
if
(
incr
>=
6
*
3600
)
tinc
=
2
*
3600
;
if
(
incr
>=
12
*
3600
)
tinc
=
3
*
3600
;
if
(
incr
>=
24
*
3600
)
tinc
=
6
*
3600
;
break
;
case
TIMELEVEL_DAYS
:
if
(
incr
>
2
*
3600
)
tinc
=
3600
;
if
(
incr
>
4
*
3600
)
tinc
=
2
*
3600
;
if
(
incr
>
7
*
3600
)
tinc
=
3
*
3600
;
if
(
incr
>
13
*
3600
)
tinc
=
6
*
3600
;
if
(
incr
>
DAY_SEC
)
tinc
=
12
*
3600
;
if
(
incr
>
2
*
DAY_SEC
)
tinc
=
DAY_SEC
;
break
;
case
TIMELEVEL_WEEKS
:
if
(
incr
>
2
*
DAY_SEC
)
tinc
=
DAY_SEC
;
if
(
incr
>
7
*
DAY_SEC
)
tinc
=
7
*
DAY_SEC
;
break
;
case
TIMELEVEL_MONTHS
:
if
(
incr
>
2
*
DAY_SEC
)
tinc
=
DAY_SEC
;
if
(
incr
>
15
*
DAY_SEC
)
tinc
=
10
*
DAY_SEC
;
if
(
incr
>
2
*
MON_SEC
)
tinc
=
MON_SEC
;
if
(
incr
>
6
*
MON_SEC
)
tinc
=
3
*
MON_SEC
;
if
(
incr
>
2
*
YEAR_SEC
)
tinc
=
YEAR_SEC
;
break
;
case
TIMELEVEL_YEARS
:
if
(
incr
>
2
*
MON_SEC
)
tinc
=
MON_SEC
;
if
(
incr
>
6
*
MON_SEC
)
tinc
=
3
*
MON_SEC
;
if
(
incr
>
2
*
YEAR_SEC
)
tinc
=
YEAR_SEC
;
if
(
incr
>
10
*
YEAR_SEC
)
tinc
=
5
*
YEAR_SEC
;
if
(
incr
>
50
*
YEAR_SEC
)
tinc
=
10
*
YEAR_SEC
;
if
(
incr
>
100
*
YEAR_SEC
)
tinc
=
20
*
YEAR_SEC
;
if
(
incr
>
200
*
YEAR_SEC
)
tinc
=
50
*
YEAR_SEC
;
if
(
incr
>
300
*
YEAR_SEC
)
tinc
=
100
*
YEAR_SEC
;
break
;
}
return
(
tinc
);
}
/* }}} */
/* {{{ copy_or_invent_formatstring() */
/* Either copies the axis formatstring over to the ticfmt[] array, or
* in case that's not applicable because the format hasn't been
* specified correctly, invents a time/date output format by looking
* at the range of values. Considers time/date fields that don't
* change across the range to be unimportant */
/* HBB 20010803: removed two arguments, and renamed function */
char
*
copy_or_invent_formatstring
(
AXIS_INDEX
axis
)
{
struct
tm
t_min
,
t_max
;
/* HBB 20010803: moved this here ... was done whenever this was called,
* anyway */
if
(!
axis_array
[
axis
].
is_timedata
||
!
axis_array
[
axis
].
format_is_numeric
) {
/* The simple case: formatstring is usable, so use it! */
strcpy
(
ticfmt
[
axis
],
axis_array
[
axis
].
formatstring
);
return
ticfmt
[
axis
];
}
/* Else, have to invent an output format string. */
*
ticfmt
[
axis
]
=
0
;
/* make sure we strcat to empty string */
ggmtime
(
&
t_min
,
time_tic_just
(
timelevel
[
axis
],
axis_array
[
axis
].
min
));
ggmtime
(
&
t_max
,
time_tic_just
(
timelevel
[
axis
],
axis_array
[
axis
].
max
));
if
(
t_max
.
tm_year
==
t_min
.
tm_year
&&
t_max
.
tm_yday
==
t_min
.
tm_yday
) {
/* same day, skip date */
if
(
t_max
.
tm_hour
!=
t_min
.
tm_hour
) {
strcpy
(
ticfmt
[
axis
],
"%H"
);
}
if
(
timelevel
[
axis
]
<
TIMELEVEL_DAYS
) {
if
(
ticfmt
[
axis
][
0
])
strcat
(
ticfmt
[
axis
],
":"
);
strcat
(
ticfmt
[
axis
],
"%M"
);
}
if
(
timelevel
[
axis
]
<
TIMELEVEL_HOURS
) {
strcat
(
ticfmt
[
axis
],
":%S"
);
}
}
else
{
if
(
t_max
.
tm_year
!=
t_min
.
tm_year
) {
/* different years, include year in ticlabel */
/* check convention, day/month or month/day */
if
(
strchr
(
axis_array
[
axis
].
timefmt
,
'm'
)
<
strchr
(
axis_array
[
axis
].
timefmt
,
'd'
)) {
strcpy
(
ticfmt
[
axis
],
"%m/%d/%"
);
}
else
{
strcpy
(
ticfmt
[
axis
],
"%d/%m/%"
);
}
if
(((
int
) (
t_max
.
tm_year
/
100
))
!=
((
int
) (
t_min
.
tm_year
/
100
))) {
strcat
(
ticfmt
[
axis
],
"Y"
);
}
else
{
strcat
(
ticfmt
[
axis
],
"y"
);
}
}
else
{
/* Copy day/month order over from input format */
if
(
strchr
(
axis_array
[
axis
].
timefmt
,
'm'
)
<
strchr
(
axis_array
[
axis
].
timefmt
,
'd'
)) {
strcpy
(
ticfmt
[
axis
],
"%m/%d"
);
}
else
{
strcpy
(
ticfmt
[
axis
],
"%d/%m"
);
}
}
if
(
timelevel
[
axis
]
<
TIMELEVEL_WEEKS
) {
/* Note: seconds can't be useful if there's more than 1
* day's worth of data... */
strcat
(
ticfmt
[
axis
],
"\n%H:%M"
);
}
}
return
ticfmt
[
axis
];
}
/* }}} */
/* {{{ dbl_raise() used by quantize_normal_tics */
/* FIXME HBB 20000426: is this really useful? */
static
double
dbl_raise
(
double
x
,
int
y
)
{
int
i
=
abs
(
y
);
double
val
=
1.0
;
while
(
--
i
>=
0
)
val
*=
x
;
if
(
y
<
0
)
return
(
1.0
/
val
);
return
(
val
);
}
/* }}} */
/* {{{ quantize_normal_tics() */
/* the guide parameter was intended to allow the number of tics
* to depend on the relative sizes of the plot and the font.
* It is the approximate upper limit on number of tics allowed.
* But it did not go down well with the users.
* A value of 20 gives the same behaviour as 3.5, so that is
* hardwired into the calls to here. Maybe we will restore it
* to the automatic calculation one day
*/
/* HBB 20020220: Changed to use value itself as first argument, not
* log10(value). Done to allow changing the calculation method
* to avoid numerical problems */
double
quantize_normal_tics
(
double
arg
,
int
guide
)
{
/* order of magnitude of argument: */
double
power
=
dbl_raise
(
10.0
,
floor
(
log10
(
arg
)));
double
xnorm
=
arg
/
power
;
/* approx number of decades */
/* we expect 1 <= xnorm <= 10 */
double
posns
=
guide
/
xnorm
;
/* approx number of tic posns per decade */
/* with guide=20, we expect 2 <= posns <= 20 */
double
tics
;
/* FIXME HBB 20020220: Looking at these, I would normally expect
* to see posns*tics to be always about the same size. But we
* rather suddenly drop from 2.0 to 1.0 at tic step 0.5. Why? */
/* JRV 20021117: fixed this by changing next to last threshold
from 1 to 2. However, with guide=20, this doesn't matter. */
if
(
posns
>
40
)
tics
=
0.05
;
/* eg 0, .05, .10, ... */
else
if
(
posns
>
20
)
tics
=
0.1
;
/* eg 0, .1, .2, ... */
else
if
(
posns
>
10
)
tics
=
0.2
;
/* eg 0,0.2,0.4,... */
else
if
(
posns
>
4
)
tics
=
0.5
;
/* 0,0.5,1, */
else
if
(
posns
>
2
)
tics
=
1
;
/* 0,1,2,.... */
else
if
(
posns
>
0.5
)
tics
=
2
;
/* 0, 2, 4, 6 */
else
/* getting desperate... the ceil is to make sure we
* go over rather than under - eg plot [-10:10] x*x
* gives a range of about 99.999 - tics=xnorm gives
* tics at 0, 99.99 and 109.98 - BAD !
* This way, inaccuracy the other way will round
* up (eg 0->100.0001 => tics at 0 and 101
* I think latter is better than former
*/
tics
=
ceil
(
xnorm
);
return
(
tics
*
power
);
}
/* }}} */
/* {{{ make_tics() */
/* Implement TIC_COMPUTED case, i.e. automatically choose a usable
* ticking interval for the given axis. For the meaning of the guide
* parameter, see the comment on quantize_normal_tics() */
static
double
make_tics
(
AXIS_INDEX
axis
,
int
guide
)
{
double
xr
,
tic
;
xr
=
fabs
(
axis_array
[
axis
].
min
-
axis_array
[
axis
].
max
);
if
(
xr
==
0
)
return
1
;
/* Anything will do, since we'll never use it */
if
(
xr
>=
VERYLARGE
)
int_error
(
NO_CARET
,
"%s axis range undefined or overflow"
,
axis_defaults
[
axis
].
name
);
tic
=
quantize_normal_tics
(
xr
,
guide
);
/* FIXME HBB 20010831: disabling this might allow short log axis
* to receive better ticking... */
if
(
axis_array
[
axis
].
log
&&
tic
<
1.0
)
tic
=
1.0
;
if
(
axis_array
[
axis
].
is_timedata
)
return
quantize_time_tics
(
axis
,
tic
,
xr
,
guide
);
else
return
tic
;
}
/* }}} */
/* {{{ quantize_duodecimal_tics */
/* HBB 20020220: New function, to be used to properly tic axes with a
* duodecimal reference, as used in times (60 seconds, 60 minuts, 24
* hours, 12 months). Derived from quantize_normal_tics(). The default
* guide is assumed to be 12, here, not 20 */
static
double
quantize_duodecimal_tics
(
double
arg
,
int
guide
)
{
/* order of magnitude of argument: */
double
power
=
dbl_raise
(
12.0
,
floor
(
log
(
arg
)/
log
(
12.0
)));
double
xnorm
=
arg
/
power
;
/* approx number of decades */
double
posns
=
guide
/
xnorm
;
/* approx number of tic posns per decade */
if
(
posns
>
24
)
return
power
/
24
;
/* half a smaller unit --- shouldn't happen */
else
if
(
posns
>
12
)
return
power
/
12
;
/* one smaller unit */
else
if
(
posns
>
6
)
return
power
/
6
;
/* 2 smaller units = one-6th of a unit */
else
if
(
posns
>
4
)
return
power
/
4
;
/* 3 smaller units = quarter unit */
else
if
(
posns
>
2
)
return
power
/
2
;
/* 6 smaller units = half a unit */
else
if
(
posns
>
1
)
return
power
;
/* 0, 1, 2, ..., 11 */
else
if
(
posns
>
0.5
)
return
power
*
2
;
/* 0, 2, 4, ..., 10 */
else
if
(
posns
>
1.0
/
3
)
return
power
*
3
;
/* 0, 3, 6, 9 */
else
/* getting desperate... the ceil is to make sure we
* go over rather than under - eg plot [-10:10] x*x
* gives a range of about 99.999 - tics=xnorm gives
* tics at 0, 99.99 and 109.98 - BAD !
* This way, inaccuracy the other way will round
* up (eg 0->100.0001 => tics at 0 and 101
* I think latter is better than former
*/
return
power
*
ceil
(
xnorm
);
}
/* }}} */
/* {{{ quantize_time_tics */
/* HBB 20010831: newly isolated subfunction. Used to be part of
* make_tics() */
/* Look at the tic interval given, and round it to a nice figure
* suitable for time/data axes, i.e. a small integer number of
* seconds, minutes, hours, days, weeks or months. As a side effec,
* this routine also modifies the static timelevel[axis] to indicate
* the units these tics are calculated in. */
static
double
quantize_time_tics
(
AXIS_INDEX
axis
,
double
tic
,
double
xr
,
int
guide
)
{
int
guide12
=
guide
*
3
/
5
;
/* --> 12 for default of 20 */
timelevel
[
axis
]
=
TIMELEVEL_SECONDS
;
if
(
tic
>
5
) {
/* turn tic into units of minutes */
tic
=
quantize_duodecimal_tics
(
xr
/
60.0
,
guide12
)
*
60
;
if
(
tic
>=
60
)
timelevel
[
axis
]
=
TIMELEVEL_MINUTES
;
}
if
(
tic
>
5
*
60
) {
/* turn tic into units of hours */
tic
=
quantize_duodecimal_tics
(
xr
/
3600.0
,
guide12
)
*
3600
;
if
(
tic
>=
3600
)
timelevel
[
axis
]
=
TIMELEVEL_HOURS
;
}
if
(
tic
>
3600
) {
/* turn tic into units of days */
tic
=
quantize_duodecimal_tics
(
xr
/
DAY_SEC
,
guide12
)
*
DAY_SEC
;
if
(
tic
>=
DAY_SEC
)
timelevel
[
axis
]
=
TIMELEVEL_DAYS
;
}
if
(
tic
>
2
*
DAY_SEC
) {
/* turn tic into units of weeks */
tic
=
quantize_normal_tics
(
xr
/
WEEK_SEC
,
guide
)
*
WEEK_SEC
;
if
(
tic
<
WEEK_SEC
) {
/* force */
tic
=
WEEK_SEC
;
}
if
(
tic
>=
WEEK_SEC
)
timelevel
[
axis
]
=
TIMELEVEL_WEEKS
;
}
if
(
tic
>
3
*
WEEK_SEC
) {
/* turn tic into units of month */
tic
=
quantize_normal_tics
(
xr
/
MON_SEC
,
guide
)
*
MON_SEC
;
if
(
tic
<
MON_SEC
) {
/* force */
tic
=
MON_SEC
;
}
if
(
tic
>=
MON_SEC
)
timelevel
[
axis
]
=
TIMELEVEL_MONTHS
;
}
if
(
tic
>
MON_SEC
) {
/* turn tic into units of years */
tic
=
quantize_duodecimal_tics
(
xr
/
YEAR_SEC
,
guide12
)
*
YEAR_SEC
;
if
(
tic
>=
YEAR_SEC
)
timelevel
[
axis
]
=
TIMELEVEL_YEARS
;
}
return
(
tic
);
}
/* }}} */
/* {{{ round_outward */
/* HBB 20011204: new function (repeated code ripped out of setup_tics)
* that rounds an axis endpoint outward. If the axis is a time/date
* one, take care to round towards the next whole time unit, not just
* a multiple of the (averaged) tic size */
static
double
round_outward
(
AXIS_INDEX
axis
,
/* Axis to work on */
TBOOLEAN
upwards
,
/* extend upwards or downwards? */
double
input
)
/* the current endpoint */
{
double
tic
=
ticstep
[
axis
];
double
result
=
tic
*
(
upwards
?
ceil
(
input
/
tic
)
:
floor
(
input
/
tic
));
if
(
axis_array
[
axis
].
is_timedata
) {
double
ontime
=
time_tic_just
(
timelevel
[
axis
],
result
);
/* FIXME: how certain is it that we don't want to *always*
* return 'ontime'? */
if
((
upwards
&&
(
ontime
>
result
))
||
(!
upwards
&&
(
ontime
<
result
)))
return
ontime
;
}
return
result
;
}
/* }}} */
/* {{{ setup_tics */
/* setup_tics allows max number of tics to be specified but users dont
* like it to change with size and font, so we use value of 20, which
* is 3.5 behaviour. Note also that if format is '', yticlin = 0, so
* this gives division by zero. */
void
setup_tics
(
AXIS_INDEX
axis
,
int
max
)
{
double
tic
=
0
;
AXIS
*
this
=
axis_array
+
axis
;
struct
ticdef
*
ticdef
=
&
(
this
->
ticdef
);
/* HBB 20010703: New: allow _not_ to autoextend the axis endpoints
* to an integer multiple of the ticstep, for autoscaled axes with
* automatic tics */
TBOOLEAN
autoextend_min
=
(
this
->
autoscale
&
AUTOSCALE_MIN
)
&&
! (
this
->
autoscale
&
AUTOSCALE_FIXMIN
);
TBOOLEAN
autoextend_max
=
(
this
->
autoscale
&
AUTOSCALE_MAX
)
&&
! (
this
->
autoscale
&
AUTOSCALE_FIXMAX
);
/* HBB 20000506: if no tics required for this axis, do
* nothing. This used to be done exactly before each call of
* setup_tics, anyway... */
if
(!
this
->
ticmode
)
return
;
if
(
ticdef
->
type
==
TIC_SERIES
) {
ticstep
[
axis
]
=
tic
=
ticdef
->
def
.
series
.
incr
;
autoextend_min
=
autoextend_min
&&
(
ticdef
->
def
.
series
.
start
==
-
VERYLARGE
);
autoextend_max
=
autoextend_max
&&
(
ticdef
->
def
.
series
.
end
==
VERYLARGE
);
}
else
if
(
ticdef
->
type
==
TIC_COMPUTED
) {
ticstep
[
axis
]
=
tic
=
make_tics
(
axis
,
max
);
}
else
{
/* user-defined, day or month */
autoextend_min
=
autoextend_max
=
FALSE;
}
/* If an explicit stepsize was set, timelevel[axis] wasn't defined,
* leading to strange misbehaviours of minor tics on time axes.
* We used to call quantize_time_tics, but that also caused strangeness.
*/
if
(
this
->
is_timedata
&&
ticdef
->
type
==
TIC_SERIES
) {
if
(
tic
>=
365
*
24
*
60
*
60.
)
timelevel
[
axis
]
=
TIMELEVEL_YEARS
;
else
if
(
tic
>=
28
*
24
*
60
*
60.
)
timelevel
[
axis
]
=
TIMELEVEL_MONTHS
;
else
if
(
tic
>=
7
*
24
*
60
*
60.
)
timelevel
[
axis
]
=
TIMELEVEL_WEEKS
;
else
if
(
tic
>=
24
*
60
*
60.
)
timelevel
[
axis
]
=
TIMELEVEL_DAYS
;
else
if
(
tic
>=
60
*
60.
)
timelevel
[
axis
]
=
TIMELEVEL_HOURS
;
else
if
(
tic
>=
60.
)
timelevel
[
axis
]
=
TIMELEVEL_MINUTES
;
else
timelevel
[
axis
]
=
TIMELEVEL_SECONDS
;
}
if
(
autoextend_min
)
this
->
min
=
round_outward
(
axis
, ! (
this
->
min
<
this
->
max
),
this
->
min
);
if
(
autoextend_max
)
this
->
max
=
round_outward
(
axis
,
this
->
min
<
this
->
max
,
this
->
max
);
/* Set up ticfmt[axis] correctly. If necessary (time axis, but not
* time/date output format), make up a formatstring that suits the
* range of data */
copy_or_invent_formatstring
(
axis
);
}
/* }}} */
/* {{{ gen_tics */
/* uses global arrays ticstep[], ticfmt[], axis_array[],
* we use any of GRID_X/Y/X2/Y2 and _MX/_MX2/etc - caller is expected
* to clear the irrelevent fields from global grid bitmask
* note this is also called from graph3d, so we need GRID_Z too
*/
void
gen_tics
(
AXIS_INDEX
axis
,
tic_callback
callback
)
{
/* separate main-tic part of grid */
struct
lp_style_type
lgrd
,
mgrd
;
/* tic defn */
struct
ticdef
*
def
=
&
axis_array
[
axis
].
ticdef
;
/* minitics - off/default/auto/explicit */
int
minitics
=
axis_array
[
axis
].
minitics
;
/* minitic frequency */
double
minifreq
=
axis_array
[
axis
].
mtic_freq
;
memcpy
(
&
lgrd
,
&
grid_lp
,
sizeof
(
grid_lp
));
memcpy
(
&
mgrd
,
&
mgrid_lp
,
sizeof
(
mgrid_lp
));
if
(!
axis_array
[
axis
].
gridmajor
)
lgrd
.
l_type
=
LT_NODRAW
;
if
(!
axis_array
[
axis
].
gridminor
)
mgrd
.
l_type
=
LT_NODRAW
;
if
(
def
->
def
.
user
) {
/* user-defined tic entries */
struct
ticmark
*
mark
=
def
->
def
.
user
;
double
uncertain
=
(
axis_array
[
axis
].
max
-
axis_array
[
axis
].
min
) /
10
;
double
internal_min
=
axis_array
[
axis
].
min
-
SIGNIF
*
uncertain
;
double
internal_max
=
axis_array
[
axis
].
max
+
SIGNIF
*
uncertain
;
double
log10_base
=
axis_array
[
axis
].
log
?
log10
(
axis_array
[
axis
].
base
) :
1.0
;
/* polar labels always +ve, and if rmin has been set, they are
* relative to rmin. position is as user specified, but must
* be translated. I dont think it will work at all for
* log scale, so I shan't worry about it !
*/
double
polar_shift
=
(
polar
&&
! (
axis_array
[
R_AXIS
].
autoscale
&
AUTOSCALE_MIN
))
?
axis_array
[
R_AXIS
].
min
:
0
;
for
(
mark
=
def
->
def
.
user
;
mark
;
mark
=
mark
->
next
) {
char
label
[
64
];
double
internal
=
AXIS_LOG_VALUE
(
axis
,
mark
->
position
);
internal
-=
polar_shift
;
if
(!
inrange
(
internal
,
internal_min
,
internal_max
))
continue
;
if
(
mark
->
level
<
0
)
/* label read from data file */
strncpy
(
label
,
mark
->
label
,
sizeof
(
label
));
else
if
(
axis_array
[
axis
].
is_timedata
)
gstrftime
(
label
,
24
,
mark
->
label
?
mark
->
label
:
ticfmt
[
axis
],
mark
->
position
);
else
gprintf
(
label
,
sizeof
(
label
),
mark
->
label
?
mark
->
label
:
ticfmt
[
axis
],
log10_base
,
mark
->
position
);
/* use NULL instead of label for minitic */
(
*
callback
) (
axis
,
internal
, (
mark
->
level
>
0
)?
NULL
:
label
, (
mark
->
level
>
0
)?
mgrd
:
lgrd
);
}
if
(
def
->
type
==
TIC_USER
)
return
;
}
/* series-tics
* need to distinguish user co-ords from internal co-ords.
* - for logscale, internal = log(user), else internal = user
*
* The minitics are a bit of a drag - we need to distinuish
* the cases step>1 from step == 1.
* If step = 1, we are looking at 1,10,100,1000 for example, so
* minitics are 2,5,8, ... - done in user co-ordinates
* If step>1, we are looking at 1,1e6,1e12 for example, so
* minitics are 10,100,1000,... - done in internal co-ords
*/
{
double
tic
;
/* loop counter */
double
internal
;
/* in internal co-ords */
double
user
;
/* in user co-ords */
double
start
,
step
,
end
;
double
lmin
=
axis_array
[
axis
].
min
,
lmax
=
axis_array
[
axis
].
max
;
double
internal_min
,
internal_max
;
/* to allow for rounding errors */
double
ministart
=
0
,
ministep
=
1
,
miniend
=
1
;
/* internal or user - depends on step */
/* gprintf uses log10() of base - log_base_array is log() */
double
log10_base
=
axis_array
[
axis
].
log
?
log10
(
axis_array
[
axis
].
base
) :
1.0
;
if
(
lmax
<
lmin
) {
/* hmm - they have set reversed range for some reason */
double
temp
=
lmin
;
lmin
=
lmax
;
lmax
=
temp
;
}
/* {{{ choose start, step and end */
switch
(
def
->
type
) {
case
TIC_SERIES
:
if
(
axis_array
[
axis
].
log
) {
/* we can tolerate start <= 0 if step and end > 0 */
if
(
def
->
def
.
series
.
end
<=
0
||
def
->
def
.
series
.
incr
<=
0
)
return
;
/* just quietly ignore */
step
=
AXIS_DO_LOG
(
axis
,
def
->
def
.
series
.
incr
);
if
(
def
->
def
.
series
.
start
<=
0
)
/* includes case 'undefined, i.e. -VERYLARGE */
start
=
step
*
floor
(
lmin
/
step
);
else
start
=
AXIS_DO_LOG
(
axis
,
def
->
def
.
series
.
start
);
if
(
def
->
def
.
series
.
end
==
VERYLARGE
)
end
=
step
*
ceil
(
lmax
/
step
);
else
end
=
AXIS_DO_LOG
(
axis
,
def
->
def
.
series
.
end
);
}
else
{
start
=
def
->
def
.
series
.
start
;
step
=
def
->
def
.
series
.
incr
;
end
=
def
->
def
.
series
.
end
;
if
(
start
==
-
VERYLARGE
)
start
=
step
*
floor
(
lmin
/
step
);
if
(
end
==
VERYLARGE
)
end
=
step
*
ceil
(
lmax
/
step
);
}
break
;
case
TIC_COMPUTED
:
/* round to multiple of step */
start
=
ticstep
[
axis
]
*
floor
(
lmin
/
ticstep
[
axis
]);
step
=
ticstep
[
axis
];
end
=
ticstep
[
axis
]
*
ceil
(
lmax
/
ticstep
[
axis
]);
break
;
case
TIC_MONTH
:
start
=
floor
(
lmin
);
end
=
ceil
(
lmax
);
step
=
floor
((
end
-
start
) /
12
);
if
(
step
<
1
)
step
=
1
;
break
;
case
TIC_DAY
:
start
=
floor
(
lmin
);
end
=
ceil
(
lmax
);
step
=
floor
((
end
-
start
) /
14
);
if
(
step
<
1
)
step
=
1
;
break
;
default
:
graph_error
(
"Internal error : unknown tic type"
);
return
;
/* avoid gcc -Wall warning about start */
}
/* }}} */
/* {{{ ensure ascending order */
if
(
end
<
start
) {
double
temp
;
temp
=
end
;
end
=
start
;
start
=
temp
;
}
step
=
fabs
(
step
);
/* }}} */
if
(
minitics
) {
/* {{{ figure out ministart, ministep, miniend */
if
(
minitics
==
MINI_USER
) {
/* they have said what they want */
if
(
minifreq
<=
0
)
minitics
=
0
;
/* not much else we can do */
else
if
(
axis_array
[
axis
].
log
) {
ministart
=
ministep
=
step
/
minifreq
*
axis_array
[
axis
].
base
;
miniend
=
step
*
axis_array
[
axis
].
base
;
}
else
{
ministart
=
ministep
=
step
/
minifreq
;
miniend
=
step
;
}
}
else
if
(
axis_array
[
axis
].
log
) {
if
(
step
>
1.5
) {
/* beware rounding errors */
/* {{{ 10,100,1000 case */
/* no more than five minitics */
if
(
step
<
65535
)
/* should be MAXINT */
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL