-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdchart.c
2193 lines (2040 loc) · 102 KB
/
gdchart.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
/* GDCHART 0.11.3dev GDCHART.C 11 Mar 2003 */
/* Copyright Bruce Verderaime 1998-2004 */
/* vi:set tabstop=4 */
#define GDC_INCL
#define GDC_LIB
#include "gdc.h" /* gdc.h before system includes to pick up features */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include "gdchart.h"
#define HIGHSET 0
#define LOWSET 1
#define CLOSESET 2
/* scaled translation onto graph */
#define PX( x ) (int)( xorig + (setno*xdepth_3D) + (x)*xscl )
#define PY( y ) (int)( yorig - (setno*ydepth_3D) + (y)*yscl )
#define PV( y ) (int)( vyorig - (setno*ydepth_3D) + (y)*vyscl )
#define SET_RECT( gdp, x1, x2, y1, y2 ) gdp[0].x = gdp[3].x = x1, \
gdp[0].y = gdp[1].y = y1, \
gdp[1].x = gdp[2].x = x2, \
gdp[2].y = gdp[3].y = y2
#define SET_3D_POLY( gdp, x1, x2, y1, y2, xoff, yoff ) \
gdp[0].x = x1, gdp[0].y = y1, \
gdp[1].x = x1+(xoff), gdp[1].y = y1-yoff, \
gdp[2].x = x2+(xoff), gdp[2].y = y2-yoff, \
gdp[3].x = x2, gdp[3].y = y2
/* ------------------------------------------------------------------------- */
/* vals in pixels */
/* ref is front plane */
/* allows for intersecting 3D lines */
/* (also used for single 3D lines >:-Q */
struct YS { int y1; int y2; float slope; int lnclr; int shclr; };
static int qcmpr( const void *a, const void *b )
{ if( ((struct YS*)a)->y2 < ((struct YS*)b)->y2 ) return 1;
if( ((struct YS*)a)->y2 > ((struct YS*)b)->y2 ) return -1;
return 0; }
void
draw_3d_line( gdImagePtr im,
int y0,
int x1,
int x2,
int y1[],
int y2[],
int xdepth,
int ydepth,
int num_sets,
int clr[],
int clrshd[] )
{
#define F(x,i) (int)( (float)((x)-x1)*slope[i]+(float)y1[i] )
float depth_slope = xdepth==0? FLT_MAX: (float)ydepth/(float)xdepth;
CREATE_ARRAY1( slope, float, num_sets ); /* float slope[num_sets] */
CREATE_ARRAY1( lnclr, int, num_sets ); /* int slope[num_sets] */
CREATE_ARRAY1( shclr, int, num_sets ); /* int slope[num_sets] */
CREATE_ARRAY1( ypts, struct YS, num_sets ); /* struct YS slope[num_sets] */
int i;
int x;
gdPoint poly[4];
for( i=0; i<num_sets; ++i )
{
/* lnclr[i] = clr[i]; */
/* shclr[i] = clrshd[i]; */
slope[i] = x2==x1? FLT_MAX: (float)(y2[i]-y1[i])/(float)(x2-x1);
}
for( x=x1+1; x<=x2; ++x )
{
for( i=0; i<num_sets; ++i ) /* load set of points */
{
ypts[i].y1 = F(x-1,i);
ypts[i].y2 = F(x,i);
ypts[i].lnclr = clr[i];
ypts[i].shclr = clrshd[i];
ypts[i].slope = slope[i];
} /* sorted "lowest" first */
qsort( ypts, num_sets, sizeof(struct YS), qcmpr );
/* put out in that order */
for( i=0; i<num_sets; ++i )
{ /* top */
SET_3D_POLY( poly, x-1, x, ypts[i].y1, ypts[i].y2, xdepth, ydepth );
gdImageFilledPolygon( im, poly, 4, /* depth_slope ever < 0 ? */
-ypts[i].slope>depth_slope? ypts[i].shclr: ypts[i].lnclr );
if( x == x1+1 ) /* edging */
gdImageLine( im,
x-1, ypts[i].y2,
x-1+xdepth, ypts[i].y2-ydepth,
-ypts[i].slope<=depth_slope? ypts[i].shclr: ypts[i].lnclr );
}
}
FREE_ARRAY1( slope );
FREE_ARRAY1( lnclr );
FREE_ARRAY1( shclr );
FREE_ARRAY1( ypts );
}
/* ------------------------------------------------------------------------- */
/* vals in pixels */
/* ref is front plane */
void
draw_3d_area( gdImagePtr im,
int x1,
int x2,
int y0, /* drawn from 0 */
int y1,
int y2,
int xdepth,
int ydepth,
int clr,
int clrshd )
{
gdPoint poly[4];
int y_intercept = 0; /* if xdepth || ydepth */
if( xdepth || ydepth )
{
float line_slope = x2==x1? FLT_MAX: (float)-(y2-y1) / (float)(x2-x1);
float depth_slope = xdepth==0? FLT_MAX: (float)ydepth/(float)xdepth;
y_intercept = (y1 > y0 && y2 < y0) || /* line crosses y0 */
(y1 < y0 && y2 > y0)?
(int)((1.0/ABS(line_slope))*(float)(ABS(y1-y0)))+x1:
0; /* never */
/* edging along y0 depth */
gdImageLine( im, x1+xdepth, y0-ydepth, x2+xdepth, y0-ydepth, clrshd );
SET_3D_POLY( poly, x1, x2, y1, y2, xdepth, ydepth ); /* top */
gdImageFilledPolygon( im, poly, 4, line_slope>depth_slope? clrshd: clr );
SET_3D_POLY( poly, x1, x2, y0, y0, xdepth, ydepth+1 ); /* along y axis */
gdImageFilledPolygon( im, poly, 4, clr );
SET_3D_POLY( poly, x2, x2, y0, y2, xdepth, ydepth ); /* side */
gdImageFilledPolygon( im, poly, 4, clrshd );
if( y_intercept )
gdImageLine( im, y_intercept, y0,
y_intercept+xdepth, y0-ydepth, clrshd ); /* edging */
gdImageLine( im, x1, y0, x1+xdepth, y0-ydepth, clrshd ); /* edging */
gdImageLine( im, x2, y0, x2+xdepth, y0-ydepth, clrshd ); /* edging */
/* SET_3D_POLY( poly, x2, x2, y0, y2, xdepth, ydepth ); // side */
/* gdImageFilledPolygon( im, poly, 4, clrshd ); */
gdImageLine( im, x1, y1, x1+xdepth, y1-ydepth, clrshd ); /* edging */
gdImageLine( im, x2, y2, x2+xdepth, y2-ydepth, clrshd ); /* edging */
}
if( y1 == y2 ) /* bar rect */
SET_RECT( poly, x1, x2, y0, y1 ); /* front */
else
{
poly[0].x = x1; poly[0].y = y0;
poly[1].x = x2; poly[1].y = y0;
poly[2].x = x2; poly[2].y = y2;
poly[3].x = x1; poly[3].y = y1;
}
gdImageFilledPolygon( im, poly, 4, clr );
gdImageLine( im, x1, y0, x2, y0, clrshd ); /* edging along y0 */
if( (xdepth || ydepth) && /* front edging only on 3D */
(y1<y0 || y2<y0) ) /* and only above y0 */
{
if( y1 > y0 && y2 < y0 ) /* line crosses from below y0 */
gdImageLine( im, y_intercept, y0, x2, y2, clrshd );
else
if( y1 < y0 && y2 > y0 ) /* line crosses from above y0 */
gdImageLine( im, x1, y1, y_intercept, y0, clrshd );
else /* completely above */
gdImageLine( im, x1, y1, x2, y2, clrshd );
}
}
/* ------------------------------------------------------------------------- */
/* vals in pixels */
/* ref is front plane */
void
draw_3d_bar( gdImagePtr im,
int x1,
int x2,
int y0,
int yhigh,
int xdepth,
int ydepth,
int clr,
int clrshd )
{
#define SET_3D_BAR( gdp, x1, x2, y1, y2, xoff, yoff ) \
gdp[0].x = x1, gdp[0].y = y1, \
gdp[1].x = x1+(xoff), gdp[1].y = y1-yoff, \
gdp[2].x = x2+(xoff), gdp[2].y = y2-yoff, \
gdp[3].x = x2, gdp[3].y = y2
gdPoint poly[4];
int usd = MIN( y0, yhigh ); /* up-side-down bars */
if( xdepth || ydepth )
{
if( y0 != yhigh ) /* 0 height? */
{
SET_3D_BAR( poly, x2, x2, y0, yhigh, xdepth, ydepth ); /* side */
gdImageFilledPolygon( im, poly, 4, clrshd );
}
SET_3D_BAR( poly, x1, x2, usd, usd, xdepth, ydepth ); /* top */
gdImageFilledPolygon( im, poly, 4, clr );
}
SET_RECT( poly, x1, x2, y0, yhigh ); /* front */
gdImageFilledPolygon( im, poly, 4, clr );
if( xdepth || ydepth )
gdImageLine( im, x1, usd, x2, usd, clrshd );
}
/* ------------------------------------------------------------------------- */
struct BS { float y1; float y2; int clr; int shclr; };
static int barcmpr( const void *a, const void *b )
{ if( ((struct BS*)a)->y2 < ((struct BS*)b)->y2 ) return -1;
if( ((struct BS*)a)->y2 > ((struct BS*)b)->y2 ) return 1;
return 0; }
/* ------------------------------------------------------------------------- */
/* simple two-point linear interpolation */
/* attempts between first, then nearest */
void
do_interpolations( int num_points,
int interp_point,
float vals[] )
{
int i, j;
float v1 = GDC_NOVALUE,
v2 = GDC_NOVALUE;
int p1 = -1,
p2 = -1;
/* find backwards */
for( i=interp_point-1; i>=0 && p1==-1; --i )
if( vals[i] != GDC_NOVALUE && vals[i] != GDC_INTERP_VALUE )
{
v1 = vals[i];
p1 = i;
}
/* find forwards */
for( j=interp_point+1; j<num_points && p2==-1; ++j )
if( vals[j] != GDC_NOVALUE && vals[j] != GDC_INTERP_VALUE )
{
v2 = vals[j];
p2 = j;
}
/* no forward sample, find backwards */
for( ; i>=0 && p2==-1; --i )
if( vals[i] != GDC_NOVALUE && vals[i] != GDC_INTERP_VALUE )
{
v2 = vals[i];
p2 = i;
}
/* no backwards sample, find forwards */
for( ; j<num_points && p1==-1; ++j )
if( vals[j] != GDC_NOVALUE && vals[j] != GDC_INTERP_VALUE )
{
v1 = vals[j];
p1 = j;
}
if( p1==-1 || p2==-1 || /* need both */
p1 == p2 ) /* idiot */
{
vals[interp_point] = GDC_NOVALUE;
return;
}
/* Point-slope formula */
vals[interp_point] = ((v2-v1)/(float)(p2-p1)) * (float)(interp_point-p1) + v1;
return;
}
/* ========================================================================= */
/* little error checking 0: ok, */
/* -ret: error no graph output */
/* ret: error graph out */
/* watch out for # params and array sizes==num_points */
/* ------------------------------------------------------------------------- */
/* original var arg interface */
int
out_graph( short IMGWIDTH, /* no check for a image that's too small to fit */
short IMGHEIGHT, /* needed info (labels, etc), could core dump */
FILE *img_fptr, /* open file pointer (img out) */
GDC_CHART_T type,
int num_points, /* points along x axis (even iterval) */
/* all arrays dependant on this */
char *xlbl[], /* array of xlabels */
int num_sets,
... )
{
char do_hlc = ( type == GDC_HILOCLOSE ||
type == GDC_3DHILOCLOSE ||
type == GDC_3DCOMBO_HLC_BAR ||
type == GDC_3DCOMBO_HLC_AREA ||
type == GDC_COMBO_HLC_BAR ||
type == GDC_COMBO_HLC_AREA );
char do_fb = ( type == GDC_FLOATINGBAR ||
type == GDC_3DFLOATINGBAR );
char do_vol = ( type == GDC_COMBO_HLC_BAR ||
type == GDC_COMBO_HLC_AREA ||
type == GDC_COMBO_LINE_BAR ||
type == GDC_COMBO_LINE_AREA ||
type == GDC_COMBO_LINE_LINE ||
type == GDC_3DCOMBO_HLC_BAR ||
type == GDC_3DCOMBO_HLC_AREA||
type == GDC_3DCOMBO_LINE_BAR||
type == GDC_3DCOMBO_LINE_AREA ||
type == GDC_3DCOMBO_LINE_LINE );
int num_arrays = num_sets * (do_hlc? 3:
do_fb? 2: 1);
CREATE_ARRAY1( data, float, num_arrays*num_points ); /* float data[num_arrays*num_points] */
float *combo_data = (float*)NULL;
va_list ap;
int i,
rtn;
va_start( ap, num_sets );
for( i=0; i<num_arrays; ++i )
memcpy( data+i*num_points, va_arg(ap, float*), num_points*sizeof(float) );
if( do_vol )
combo_data = va_arg(ap, float*);
va_end(ap);
rtn = GDC_out_graph( IMGWIDTH,
IMGHEIGHT,
img_fptr,
type,
num_points,
xlbl,
num_sets,
data,
combo_data );
FREE_ARRAY1( data );
return rtn;
}
/* ------------------------------------------------------------------------- */
/* multi array interface */
int
GDC_out_graph( short IMGWIDTH, /* no check for a img that's too small to fit */
short IMGHEIGHT, /* needed info (labels, etc), could core dump */
FILE *img_fptr, /* open file pointer (img out) */
GDC_CHART_T type,
int num_points, /* points along x axis (even iterval) */
/* all arrays dependant on this */
char *xlbl[], /* array of xlabels */
int num_sets,
float *data, /* (float*) cast on multi-dim array (num_sets > 1) */
float *combo_data ) /* only used on COMBO chart types */
{
int i, j, k;
int graphwidth;
int grapheight;
gdImagePtr im;
gdImagePtr bg_img = NULL;
float xorig, yorig, vyorig;
float yscl = 0.0;
float vyscl = 0.0;
float xscl = 0.0;
float vhighest = -FLT_MAX;
float vlowest = FLT_MAX;
float highest = -FLT_MAX;
float lowest = FLT_MAX;
gdPoint volpoly[4];
char do_vol = ( type == GDC_COMBO_HLC_BAR || /* aka: combo */
type == GDC_COMBO_HLC_AREA ||
type == GDC_COMBO_LINE_BAR ||
type == GDC_COMBO_LINE_AREA ||
type == GDC_COMBO_LINE_LINE ||
type == GDC_3DCOMBO_HLC_BAR ||
type == GDC_3DCOMBO_HLC_AREA||
type == GDC_3DCOMBO_LINE_BAR||
type == GDC_3DCOMBO_LINE_AREA ||
type == GDC_3DCOMBO_LINE_LINE );
char threeD = ( type == GDC_3DAREA ||
type == GDC_3DLINE ||
type == GDC_3DBAR ||
type == GDC_3DFLOATINGBAR ||
type == GDC_3DHILOCLOSE ||
type == GDC_3DCOMBO_HLC_BAR ||
type == GDC_3DCOMBO_HLC_AREA||
type == GDC_3DCOMBO_LINE_BAR||
type == GDC_3DCOMBO_LINE_AREA ||
type == GDC_3DCOMBO_LINE_LINE );
char num_groups = num_sets; /* set before num_sets gets adjusted */
char set_depth = ( GDC_stack_type == GDC_STACK_DEPTH )? num_groups:
1;
char do_bar = ( type == GDC_3DBAR || /* offset X objects to leave */
type == GDC_BAR || /* room at X(0) and X(n) */
type == GDC_3DFLOATINGBAR || /* i.e., not up against Y axes */
type == GDC_FLOATINGBAR);
char do_ylbl_fractions = /* %f format not given, or */
( !GDC_ylabel_fmt || /* format doesn't have a %,g,e,E,f or F */
strlen(GDC_ylabel_fmt) == strcspn(GDC_ylabel_fmt,"%geEfF") );
float ylbl_interval = 0.0;
int xlbl_hgt = 0;
int xdepth_3Dtotal = 0;
int ydepth_3Dtotal = 0;
int xdepth_3D = 0; /* affects PX() */
int ydepth_3D = 0; /* affects PY() and PV() */
int hlf_barwdth = 0; /* half bar widths */
int hlf_hlccapwdth = 0; /* half cap widths for HLC_I_CAP and DIAMOND */
int annote_len = 0,
annote_hgt = 0;
/* args */
int setno = 0; /* affects PX() and PY() */
CREATE_ARRAY1( uvals, float *, type == GDC_HILOCLOSE ||
type == GDC_3DHILOCLOSE ||
type == GDC_3DCOMBO_HLC_BAR ||
type == GDC_3DCOMBO_HLC_AREA ||
type == GDC_COMBO_HLC_BAR ||
type == GDC_COMBO_HLC_AREA? num_sets *= 3: /* 1 more last set is vol */
type == GDC_FLOATINGBAR ||
type == GDC_3DFLOATINGBAR? num_sets *= 2:
type == GDC_COMBO_LINE_BAR ||
type == GDC_3DCOMBO_LINE_BAR ||
type == GDC_3DCOMBO_LINE_AREA||
type == GDC_3DCOMBO_LINE_LINE||
type == GDC_COMBO_LINE_AREA ||
type == GDC_COMBO_LINE_LINE? num_sets: /* 1 more last set is vol */
num_sets );
CREATE_ARRAY1( ExtVolColor, int, num_points ); /* int ExtVolColor[num_points], */
CREATE_ARRAY2( ExtColor, int, num_sets, num_points ); /* ExtColor[num_sets][num_points], */
CREATE_ARRAY2( ExtColorShd, int, threeD?num_sets:1, /* ExtColorShd[num_sets][num_points]; */
threeD?num_points:1 ); /* shade colors only with 3D */
float *uvol;
int BGColor,
LineColor,
PlotColor,
GridColor,
VolColor,
ThumbDColor,
ThumbLblColor,
ThumbUColor,
/* ArrowDColor, */
/* ArrowUColor, */
AnnoteColor;
#ifdef HAVE_LIBFREETYPE
char *gdc_title_font = GDC_title_font; /* for convienience */
char *gdc_ytitle_font = GDC_ytitle_font; /* in func calls */
char *gdc_xtitle_font = GDC_xtitle_font;
/* char *gdc_yaxis_font = GDC_yaxis_font; */
char *gdc_xaxis_font = GDC_xaxis_font;
double gdc_title_ptsize = GDC_title_ptsize;
double gdc_ytitle_ptsize = GDC_ytitle_ptsize;
double gdc_xtitle_ptsize = GDC_xtitle_ptsize;
/* double gdc_yaxis_ptsize = GDC_yaxis_ptsize; */
double gdc_xaxis_ptsize = GDC_xaxis_ptsize;
double gdc_xaxis_rad = TO_RAD( GDC_xaxis_angle );
char *gdc_annotation_font = GDC_annotation_font;
double gdc_annotation_ptsize = GDC_annotation_ptsize;
#else
char *gdc_title_font = NULL;
char *gdc_ytitle_font = NULL;
char *gdc_xtitle_font = NULL;
/* char *gdc_yaxis_font = NULL; */
char *gdc_xaxis_font = NULL;
double gdc_title_ptsize = 0.0;
double gdc_ytitle_ptsize = 0.0;
double gdc_xtitle_ptsize = 0.0;
/* double gdc_yaxis_ptsize = 0.0; */
double gdc_xaxis_ptsize = 0.0;
double gdc_xaxis_rad = GDC_xaxis_angle==90.0? M_PI/2.0: 0.0;
char *gdc_annotation_font = NULL;
double gdc_annotation_ptsize=0.0;
#endif
double sin_xangle = 1.0, /* calc only when&if needed */
cos_xangle = 0.0;
/* idiot checks */
if( IMGWIDTH<=0 || IMGHEIGHT<=0 || (!img_fptr && GDC_generate_img) )
{
FREE_ARRAY1( uvals );
FREE_ARRAY1( ExtVolColor );
FREE_ARRAY2( ExtColor );
FREE_ARRAY2( ExtColorShd );
return -1;
}
if( num_points <= 0 )
{
out_err( IMGWIDTH, IMGHEIGHT, img_fptr, GDC_BGColor, GDC_LineColor, "No Data Available" );
FREE_ARRAY1( uvals );
FREE_ARRAY1( ExtVolColor );
FREE_ARRAY2( ExtColor );
FREE_ARRAY2( ExtColorShd );
return 1;
}
load_font_conversions();
if( GDC_thumbnail )
{
GDC_grid = FALSE;
GDC_xaxis = FALSE;
GDC_yaxis = FALSE;
}
/* ----- get args ----- */
for( i=0; i<num_sets; ++i )
uvals[i] = data+i*num_points;
if( do_vol )
if( !combo_data )
{
out_err( IMGWIDTH, IMGHEIGHT, img_fptr, GDC_BGColor, GDC_LineColor, "No Combo Data Available" );
FREE_ARRAY1( uvals );
FREE_ARRAY1( ExtVolColor );
FREE_ARRAY2( ExtColor );
FREE_ARRAY2( ExtColorShd );
return -2;
}
else
uvol = combo_data;
/* ----- calculate interpretations first ----- */
if( GDC_interpolations )
{
for( i=0; i<num_sets; ++i )
for( j=0; j<num_points; ++j )
if( uvals[i][j] == GDC_INTERP_VALUE )
{
do_interpolations( num_points, j, uvals[i] );
}
if( do_vol )
for( j=0; j<num_points; ++j )
if( uvol[j] == GDC_INTERP_VALUE )
{
do_interpolations( num_points, j, uvol );
}
}
/* ----- highest & lowest values ----- */
if( GDC_stack_type == GDC_STACK_SUM ) /* need to walk sideways */
for( j=0; j<num_points; ++j )
{
float set_sum = 0.0;
for( i=0; i<num_sets; ++i )
if( uvals[i][j] != GDC_NOVALUE )
{
set_sum += uvals[i][j];
highest = MAX( highest, set_sum );
lowest = MIN( lowest, set_sum );
}
}
else
if( GDC_stack_type == GDC_STACK_LAYER ) /* need to walk sideways */
for( j=0; j<num_points; ++j )
{
float neg_set_sum = 0.0,
pos_set_sum = 0.0;
for( i=0; i<num_sets; ++i )
if( uvals[i][j] != GDC_NOVALUE )
if( uvals[i][j] < 0.0 )
neg_set_sum += uvals[i][j];
else
pos_set_sum += uvals[i][j];
lowest = MIN( lowest, MIN(neg_set_sum,pos_set_sum) );
highest = MAX( highest, MAX(neg_set_sum,pos_set_sum) );
}
else
for( i=0; i<num_sets; ++i )
for( j=0; j<num_points; ++j )
if( uvals[i][j] != GDC_NOVALUE )
{
highest = MAX( uvals[i][j], highest );
lowest = MIN( uvals[i][j], lowest );
}
if( GDC_scatter )
for( i=0; i<GDC_num_scatter_pts; ++i )
{
highest = MAX( (GDC_scatter+i)->val, highest );
lowest = MIN( (GDC_scatter+i)->val, lowest );
}
if( do_vol ) /* for now only one combo set allowed */
{
/* vhighest = 1.0; */
/* vlowest = 0.0; */
for( j=0; j<num_points; ++j )
if( uvol[j] != GDC_NOVALUE )
{
vhighest = MAX( uvol[j], vhighest );
vlowest = MIN( uvol[j], vlowest );
}
if( vhighest == -FLT_MAX ) /* no values */
vhighest = 1.0; /* for scaling, need a range */
if( vlowest == FLT_MAX )
vlowest = 0.0;
if( type == GDC_COMBO_LINE_BAR ||
type == GDC_COMBO_HLC_BAR ||
type == GDC_COMBO_LINE_AREA ||
type == GDC_COMBO_HLC_AREA ||
type == GDC_3DCOMBO_LINE_BAR ||
type == GDC_3DCOMBO_LINE_AREA ||
type == GDC_3DCOMBO_HLC_BAR ||
type == GDC_3DCOMBO_HLC_AREA )
if( vhighest < 0.0 )
vhighest = 0.0;
else
if( vlowest > 0.0 )
vlowest = 0.0; /* bar, area should always start at 0 */
}
if( lowest == FLT_MAX )
lowest = 0.0;
if( highest == -FLT_MAX )
highest = 1.0; /* need a range */
if( type == GDC_AREA || /* bars and area should always start at 0 */
type == GDC_BAR ||
type == GDC_3DBAR ||
type == GDC_3DAREA )
if( highest < 0.0 )
highest = 0.0;
else
if( lowest > 0.0 ) /* negs should be drawn from 0 */
lowest = 0.0;
if( GDC_requested_ymin != GDC_NOVALUE && GDC_requested_ymin < lowest )
lowest = GDC_requested_ymin;
if( GDC_requested_ymax != GDC_NOVALUE && GDC_requested_ymax > highest )
highest = GDC_requested_ymax;
/* ----- graph height and width within the img height width ----- */
/* grapheight/height is the actual size of the scalable graph */
{
int title_hgt = GDC_title? 2 /* title? horizontal text line(s) */
+ GDCfnt_sz(GDC_title,GDC_title_size,gdc_title_font,gdc_title_ptsize,0.0,NULL).h
+ 2:
2;
int xlabel_hgt = 0;
int xtitle_hgt = GDC_xtitle? 1+GDCfnt_sz(GDC_xtitle,GDC_xtitle_size,gdc_xtitle_font,gdc_xtitle_ptsize,0.0,NULL).h+1: 0;
int ytitle_hgt = GDC_ytitle? 1+GDCfnt_sz(GDC_ytitle,GDC_ytitle_size,gdc_ytitle_font,gdc_ytitle_ptsize,M_PI/2.0,NULL).h+1: 0;
int vtitle_hgt = do_vol&&GDC_ytitle2? 1+GDCfnt_sz(GDC_ytitle2,GDC_ytitle_size,gdc_ytitle_font,gdc_ytitle_ptsize,M_PI/2.0,NULL).h+1: 0;
int ylabel_wth = 0;
int vlabel_wth = 0;
int xtics = GDC_ticks && (GDC_grid||GDC_xaxis)? 1+2: 0;
int ytics = GDC_ticks && (GDC_grid||GDC_yaxis)? 1+3: 0;
int vtics = GDC_ticks && (GDC_yaxis&&do_vol)? 3+1: 0;
#define HYP_DEPTH ( (double)((IMGWIDTH+IMGHEIGHT)/2) * ((double)GDC_3d_depth)/100.0 )
#define RAD_DEPTH ( (double)GDC_3d_angle*2*M_PI/360 )
xdepth_3D = threeD? (int)( cos(RAD_DEPTH) * HYP_DEPTH ): 0;
ydepth_3D = threeD? (int)( sin(RAD_DEPTH) * HYP_DEPTH ): 0;
xdepth_3Dtotal = xdepth_3D*set_depth;
ydepth_3Dtotal = ydepth_3D*set_depth;
annote_hgt = GDC_annotation && *(GDC_annotation->note)?
1 + /* space to note */
(1+GDCfnt_sz( GDC_annotation->note,GDC_annotation_font_size,
gdc_annotation_font,gdc_annotation_ptsize,0.0,NULL ).h) +
1 + /* space under note */
2: 0; /* space to chart */
annote_len = GDC_annotation && *(GDC_annotation->note)?
GDCfnt_sz( GDC_annotation->note,GDC_annotation_font_size,
gdc_annotation_font,gdc_annotation_ptsize,0.0,NULL ).w:
0;
/* find length of "longest" (Y) xaxis label */
/* find the average "width" (X) xaxis label */
/* avg method fails when 2 or 3 very wide are consecutive, with the rest being thin */
/* this is most evident with horizontal (0deg) xlabels */
/* assume in this case they are quite uniform, e.g., dates */
/* find affects on graphwidth/xorig of wildly overhanging angled labels */
if( GDC_xaxis && xlbl )
{
int biggest = -INT_MAX,
widest = -INT_MAX;
#ifdef HAVE_LIBFREETYPE
if( gdc_xaxis_rad!=M_PI/2.0 && gdc_xaxis_font && gdc_xaxis_ptsize )
{
sin_xangle = sin( gdc_xaxis_rad ),
cos_xangle = cos( gdc_xaxis_rad );
}
#endif
for( i=0; i<num_points; ++i )
{
int len = 0,
wdth = 0;
if( !GDC_xlabel_ctl ||
(GDC_xlabel_ctl && GDC_xlabel_ctl[i]) )
{
char *sts;
struct fnt_sz_t lftsz = GDCfnt_sz( xlbl[i], GDC_xaxisfont_size,
gdc_xaxis_font, gdc_xaxis_ptsize, gdc_xaxis_rad,
&sts );
if( gdc_xaxis_rad == M_PI/2.0 || /* no need to do the floating point math */
(sts && *sts) ) /* FT fail status, used default gdfont */
{
#ifdef DEBUG
fprintf( stderr, "TTF/FT failure: %s\n", sts );
#endif
len = lftsz.w;
wdth = lftsz.h;
}
else
if( gdc_xaxis_rad == 0.0 ) /* protect /0 */
{ /* reverse when horiz. */
len = lftsz.h;
wdth = lftsz.w;
}
else /* length & width due to the angle */
{
len = (int)( (double)lftsz.w * sin_xangle + (double)lftsz.h * cos_xangle );
wdth = (int)( (double)lftsz.h / sin_xangle );
}
}
biggest = MAX( len, biggest ); /* last seg */
widest = MAX( wdth, widest ); /* last seg */
}
xlbl_hgt = 1+ widest +1;
xlabel_hgt = 1+ biggest +1;
}
grapheight = IMGHEIGHT - ( xtics +
xtitle_hgt +
xlabel_hgt +
title_hgt +
annote_hgt +
ydepth_3Dtotal +
2 );
if( GDC_hard_size && GDC_hard_grapheight ) /* user wants to use his */
grapheight = GDC_hard_grapheight;
GDC_hard_grapheight = grapheight;
/* before width can be known... */
/* ----- y labels intervals ----- */
{
float tmp_highest;
/* possible y gridline points */
#define NUM_YPOINTS (sizeof(ypoints_2f) / sizeof(float))
float ypoints_2f[] = { 1.0/64.0, 1.0/32.0, 1.0/16.0, 1.0/8.0, 1.0/4.0, 1.0/2.0,
1.0, 2.0, 3.0, 5.0, 10.0, 25.0,
50.0, 100.0, 250.0, 500.0, 1000.0, 2500, 5000.0,
10000.0, 25000.0, 50000.0, 100000.0,500000.0,1000000, 5000000,
10000000, 50000000 },
ypoints_dec[NUM_YPOINTS] = /* "pretty" points for dec (non-fraction) */
{ 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.25, 0.5,
1.0, 2.0, 3.0, 5.0, 10.0, 25.0,
50.0, 100.0, 250.0, 500.0, 1000.0, 2500, 5000.0,
10000.0, 25000.0, 50000.0, 100000.0,500000.0,1000000, 5000000 },
*ypoints = do_ylbl_fractions? ypoints_2f: ypoints_dec;
int max_num_ylbls;
int longest_ylblen = 0;
/* maximum y lables that'll fit... */
max_num_ylbls = grapheight / (3+GDC_fontc[GDC_yaxisfont_size==GDC_TINY? GDC_yaxisfont_size+1:
GDC_yaxisfont_size].h);
if( max_num_ylbls < 3 )
{
/* gdImageDestroy(im); haven't yet created it */
out_err( IMGWIDTH, IMGHEIGHT,
img_fptr,
GDC_BGColor, GDC_LineColor,
"Insificient Height" );
FREE_ARRAY1( uvals );
FREE_ARRAY1( ExtVolColor );
FREE_ARRAY2( ExtColor );
FREE_ARRAY2( ExtColorShd );
return 2;
}
{ /* one "space" interval above + below */
float ylbl_density_space_intvl = ((float)max_num_ylbls-(1.0+1.0)) * (float)GDC_ylabel_density/100.0;
for( i=1; i<NUM_YPOINTS; ++i )
/* if( ypoints[i] > ylbl_interval ) */
/* break; */
if( (highest-lowest)/ypoints[i] < ylbl_density_space_intvl )
break;
/* gotta go through the above loop to catch the 'tweeners :-| */
}
ylbl_interval = GDC_requested_yinterval != GDC_NOVALUE &&
GDC_requested_yinterval > ypoints[i-1]? GDC_requested_yinterval:
ypoints[i-1];
/* perform floating point remainders */
/* gonculate largest interval-point < lowest */
if( lowest != 0.0 &&
lowest != GDC_requested_ymin )
{
if( lowest < 0.0 )
lowest -= ylbl_interval;
/* lowest = (lowest-ypoints[0]) - */
/* ( ( ((lowest-ypoints[0])/ylbl_interval)*ylbl_interval ) - */
/* ( (float)((int)((lowest-ypoints[0])/ylbl_interval))*ylbl_interval ) ); */
lowest = ylbl_interval * (float)(int)((lowest-ypoints[0])/ylbl_interval);
}
/* find smallest interval-point > highest */
tmp_highest = lowest;
do /* while( (tmp_highest += ylbl_interval) <= highest ) */
{
int nmrtr, dmntr, whole;
char *price_to_str( float, int*, int*, int*, char* );
int lbl_len;
char foo[32];
if( GDC_yaxis )
{ /* XPG2 compatibility */
sprintf( foo, do_ylbl_fractions? "%.0f": GDC_ylabel_fmt, tmp_highest );
lbl_len = ylbl_interval<1.0? strlen( price_to_str(tmp_highest,
&nmrtr,
&dmntr,
&whole,
do_ylbl_fractions? NULL: GDC_ylabel_fmt) ):
strlen( foo );
longest_ylblen = MAX( longest_ylblen, lbl_len );
}
} while( (tmp_highest += ylbl_interval) <= highest );
ylabel_wth = longest_ylblen * GDC_fontc[GDC_yaxisfont_size].w;
highest = GDC_requested_ymax==GDC_NOVALUE? tmp_highest:
MAX( GDC_requested_ymax, highest );
if( do_vol )
{
float num_yintrvls = (highest-lowest) / ylbl_interval;
/* no skyscrapers */
if( vhighest != 0.0 )
vhighest += (vhighest-vlowest) / (num_yintrvls*2.0);
if( vlowest != 0.0 )
vlowest -= (vhighest-vlowest) / (num_yintrvls*2.0);
if( GDC_yaxis2 )
{
char svlongest[32];
int lbl_len_low = sprintf( svlongest, GDC_ylabel2_fmt? GDC_ylabel2_fmt: "%.0f", vlowest );
int lbl_len_high = sprintf( svlongest, GDC_ylabel2_fmt? GDC_ylabel2_fmt: "%.0f", vhighest );
vlabel_wth = 1
+ MAX( lbl_len_low,lbl_len_high ) * GDC_fontc[GDC_yaxisfont_size].w;
}
}
}
graphwidth = IMGWIDTH - ( ( (GDC_hard_size && GDC_hard_xorig)? GDC_hard_xorig:
( ytitle_hgt +
ylabel_wth +
ytics ) )
+ vtics
+ vtitle_hgt
+ vlabel_wth
+ xdepth_3Dtotal );
if( GDC_hard_size && GDC_hard_graphwidth ) /* user wants to use his */
graphwidth = GDC_hard_graphwidth;
GDC_hard_graphwidth = graphwidth;
/* ----- scale to img size ----- */
/* offset to 0 at lower left (where it should be) */
xscl = (float)(graphwidth-xdepth_3Dtotal) / (float)(num_points + (do_bar?2:0));
yscl = -((float)grapheight) / (float)(highest-lowest);
if( do_vol )
{
float hilow_diff = vhighest-vlowest==0.0? 1.0: vhighest-vlowest;
vyscl = -((float)grapheight) / hilow_diff;
vyorig = (float)grapheight
+ ABS(vyscl) * MIN(vlowest,vhighest)
+ ydepth_3Dtotal
+ title_hgt
+ annote_hgt;
}
xorig = (float)( IMGWIDTH - ( graphwidth +
vtitle_hgt +
vtics +
vlabel_wth ) );
if( GDC_hard_size && GDC_hard_xorig )
xorig = GDC_hard_xorig;
GDC_hard_xorig = xorig;
/* yorig = (float)grapheight + ABS(yscl * lowest) + ydepth_3Dtotal + title_hgt; */
yorig = (float)grapheight
+ ABS(yscl) * MIN(lowest,highest)
+ ydepth_3Dtotal
+ title_hgt
+ annote_hgt;
/*???? if( GDC_hard_size && GDC_hard_yorig ) /* vyorig too? */
/*???? yorig = GDC_hard_yorig; FRED - check email */
GDC_hard_yorig = yorig;
hlf_barwdth = (int)( (float)(PX(2)-PX(1)) * (((float)GDC_bar_width/100.0)/2.0) ); /* used only for bars */
hlf_hlccapwdth = (int)( (float)(PX(2)-PX(1)) * (((float)GDC_HLC_cap_width/100.0)/2.0) );
}
/* scaled, sized, ready */
/* ----- OK start the graphic ----- */
if( (GDC_hold_img & GDC_REUSE_IMAGE) &&
GDC_image != (void*)NULL )
im = GDC_image;
else
im = gdImageCreate( IMGWIDTH, IMGHEIGHT );
BGColor = gdImageColorAllocate( im, l2gdcal(GDC_BGColor) );
LineColor = clrallocate( im, GDC_LineColor );
PlotColor = clrallocate( im, GDC_PlotColor );
GridColor = clrallocate( im, GDC_GridColor );
if( do_vol )
{
VolColor = clrallocate( im, GDC_VolColor );
for( i=0; i<num_points; ++i )
if( GDC_ExtVolColor )
ExtVolColor[i] = clrallocate( im, GDC_ExtVolColor[i] );
else
ExtVolColor[i] = VolColor;
}
/* ArrowDColor = gdImageColorAllocate( im, 0xFF, 0, 0 ); */
/* ArrowUColor = gdImageColorAllocate( im, 0, 0xFF, 0 ); */
if( GDC_annotation )
AnnoteColor = clrallocate( im, GDC_annotation->color );
/* attempt to import optional background image */
if( GDC_BGImage )
{
FILE *in = fopen(GDC_BGImage, "rb");
if( !in )
{
; /* Cant load background image, drop it */
}
else
{
/* assume GIF */
/* should determine type by file extension, option, ... */
if( bg_img = gdImageCreateFromGif(in) ) /* = */
{
int bgxpos = gdImageSX(bg_img)<IMGWIDTH? IMGWIDTH/2 - gdImageSX(bg_img)/2: 0,
bgypos = gdImageSY(bg_img)<IMGHEIGHT? IMGHEIGHT/2 - gdImageSY(bg_img)/2: 0;
if( gdImageSX(bg_img) > IMGWIDTH || /* resize only if too big */
gdImageSY(bg_img) > IMGHEIGHT ) /* [and center] */
{
gdImageCopyResized( im, bg_img, /* dst, src */
bgxpos, bgypos, /* dstX, dstY */
0, 0, /* srcX, srcY */
IMGWIDTH, IMGHEIGHT, /* dstW, dstH */
IMGWIDTH, IMGHEIGHT ); /* srcW, srcH */
}
else /* just center */
gdImageCopy( im, bg_img, /* dst, src */
bgxpos, bgypos, /* dstX, dstY */
0, 0, /* srcX, srcY */
IMGWIDTH, IMGHEIGHT ); /* W, H */
}
fclose(in);
}
}
for( j=0; j<num_sets; ++j )
for( i=0; i<num_points; ++i )
if( GDC_ExtColor )
{
unsigned long ext_clr = *(GDC_ExtColor+num_points*j+i);
ExtColor[j][i] = clrallocate( im, ext_clr );
if( threeD )
ExtColorShd[j][i] = clrshdallocate( im, ext_clr );
}
else if( GDC_SetColor )
{
int set_clr = GDC_SetColor[j];
ExtColor[j][i] = clrallocate( im, set_clr );
if( threeD )
ExtColorShd[j][i] = clrshdallocate( im, set_clr );
}
else
{
ExtColor[j][i] = PlotColor;
if( threeD )
ExtColorShd[j][i] = clrshdallocate( im, GDC_PlotColor );
}