ruby-gdchart 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ /* GDCHART 0.11.1b 3D Bar sample 17 June 2001 */
2
+
3
+ /* NOTE: fonts and locations specified are machine specific */
4
+ /* should be able to set font-search-path env GDFONTPATH */
5
+ /* see libgd documentation */
6
+ /* will default to builtin font, if specified TTF not found */
7
+
8
+ #include <stdio.h>
9
+
10
+ #include "gdc.h"
11
+ #include "gdchart.h"
12
+
13
+ #define NUM_SETS 1
14
+ #define NUM_POINTS 4
15
+
16
+ main()
17
+ {
18
+ float data [ NUM_SETS ][ NUM_POINTS ];
19
+ unsigned long extclr[ NUM_SETS ][ NUM_POINTS ];
20
+ char *lbls[] = { "angle - 45", "font - zirkle", "ptsz - 10", "Color - blue", "" };
21
+
22
+ get_data( data, 0, 500 );
23
+ get_individual_colors( extclr );
24
+
25
+ GDC_BGColor = 0xFFFFFF;
26
+ GDC_PlotColor = 0x4080FF;
27
+ GDC_ExtColor = &(extclr[0][0]); /* set color option */
28
+ GDC_title = "GDC_title\r\n(benjamingothic 12)";
29
+ GDC_title_font = "/usr/share/enlightenment/E-docs/benjamingothic.ttf";
30
+ GDC_title_ptsize = 12;
31
+
32
+ GDC_xtitle = "GDC_xtitle\r\n(x-files 12)";
33
+ GDC_xtitle_font = "/usr/share/enlightenment/E-docs/x-files.ttf";
34
+ GDC_xtitle_ptsize = 12;
35
+
36
+ GDC_ytitle = "GDC_ytitle\r\n(times 10 i)";
37
+ GDC_ytitle_font = "/dos/C/windows/fonts/timesi.ttf";
38
+ GDC_ytitle_ptsize = 10;
39
+
40
+ GDC_xaxis_angle = 45.0;
41
+ GDC_xaxis_font = "/usr/share/enlightenment/themes/DEFAULT/ttfonts/zirkle.ttf";
42
+ GDC_xaxis_ptsize = 11.0;
43
+ GDC_XLabelColor = 0x000080;
44
+
45
+ GDC_bar_width = 60; /* (%) */
46
+ GDC_image_type = GDC_PNG;
47
+
48
+ /* ---- call the lib V0.95b ----- */
49
+ GDC_out_graph( 400, 300, /* width, height */
50
+ stdout, /* open FILE pointer */
51
+ GDC_3DBAR, /* chart type */
52
+ NUM_POINTS, /* num points per data set */
53
+ lbls, /* X labels array of char* */
54
+ 1, /* number of data sets */
55
+ (float*)data, /* data */
56
+ (float*)NULL ); /* no right-hand-axis data */
57
+
58
+ exit(0);
59
+ }
60
+
61
+ /* --------------------------------------------------------- */
62
+ /* sample data gathering routine */
63
+ /* data can come from anywhere, generally a DB or data file */
64
+ /* here it's randomly generated */
65
+ /* --------------------------------------------------------- */
66
+ #include <stdlib.h> /* for rand() */
67
+ #include <time.h> /* for seed */
68
+ get_data( float data[NUM_SETS][NUM_POINTS],
69
+ int low,
70
+ int high )
71
+ {
72
+ int i, j;
73
+ srand( (unsigned int)time((time_t)NULL) );
74
+ for( i=0; i<NUM_SETS; ++i )
75
+ for( j=0; j<NUM_POINTS; ++j )
76
+ /* random number between low & high */
77
+ data[i][j] = 1.0+low+(high * rand()/(RAND_MAX+1.0));
78
+ }
79
+
80
+ /* -------- also random colors ----------------------------- */
81
+ get_individual_colors( unsigned long extclr[NUM_SETS][NUM_POINTS] )
82
+ {
83
+ int i, j;
84
+ for( i=0; i<NUM_SETS; ++i )
85
+ for( j=0; j<NUM_POINTS; ++j )
86
+ extclr[i][j] = (unsigned long)rand();
87
+ }
88
+
89
+
@@ -0,0 +1,89 @@
1
+ /* GDCHART 0.11.1b 3D Bar sample 17 June 2001 */
2
+
3
+ /* NOTE: fonts and locations specified are machine specific */
4
+ /* should be able to set font-search-path env GDFONTPATH */
5
+ /* see libgd documentation */
6
+ /* will default to builtin font, if specified TTF not found */
7
+
8
+ #include <stdio.h>
9
+
10
+ #include "gdc.h"
11
+ #include "gdchart.h"
12
+
13
+ #define NUM_SETS 1
14
+ #define NUM_POINTS 4
15
+
16
+ main()
17
+ {
18
+ float data [ NUM_SETS ][ NUM_POINTS ];
19
+ unsigned long extclr[ NUM_SETS ][ NUM_POINTS ];
20
+ char *lbls[] = { "angle - 45", "font - zirkle", "ptsz - 10", "Color - blue", "" };
21
+
22
+ get_data( data, 0, 500 );
23
+ get_individual_colors( extclr );
24
+
25
+ GDC_BGColor = 0xFFFFFF;
26
+ GDC_PlotColor = 0x4080FF;
27
+ GDC_ExtColor = &(extclr[0][0]); /* set color option */
28
+ GDC_title = "GDC_title\r\n(benjamingothic 12)";
29
+ GDC_title_font = "/usr/share/enlightenment/E-docs/benjamingothic.ttf";
30
+ GDC_title_ptsize = 12;
31
+
32
+ GDC_xtitle = "GDC_xtitle\r\n(x-files 12)";
33
+ GDC_xtitle_font = "/usr/share/enlightenment/E-docs/x-files.ttf";
34
+ GDC_xtitle_ptsize = 12;
35
+
36
+ GDC_ytitle = "GDC_ytitle\r\n(times 10 i)";
37
+ GDC_ytitle_font = "/dos/C/windows/fonts/timesi.ttf";
38
+ GDC_ytitle_ptsize = 10;
39
+
40
+ GDC_xaxis_angle = 45.0;
41
+ GDC_xaxis_font = "/usr/share/enlightenment/themes/DEFAULT/ttfonts/zirkle.ttf";
42
+ GDC_xaxis_ptsize = 11.0;
43
+ GDC_XLabelColor = 0x000080;
44
+
45
+ GDC_bar_width = 60; /* (%) */
46
+ GDC_image_type = GDC_PNG;
47
+
48
+ /* ---- call the lib V0.95b ----- */
49
+ GDC_out_graph( 400, 300, /* width, height */
50
+ stdout, /* open FILE pointer */
51
+ GDC_3DBAR, /* chart type */
52
+ NUM_POINTS, /* num points per data set */
53
+ lbls, /* X labels array of char* */
54
+ 1, /* number of data sets */
55
+ (float*)data, /* data */
56
+ (float*)NULL ); /* no right-hand-axis data */
57
+
58
+ exit(0);
59
+ }
60
+
61
+ /* --------------------------------------------------------- */
62
+ /* sample data gathering routine */
63
+ /* data can come from anywhere, generally a DB or data file */
64
+ /* here it's randomly generated */
65
+ /* --------------------------------------------------------- */
66
+ #include <stdlib.h> /* for rand() */
67
+ #include <time.h> /* for seed */
68
+ get_data( float data[NUM_SETS][NUM_POINTS],
69
+ int low,
70
+ int high )
71
+ {
72
+ int i, j;
73
+ srand( (unsigned int)time((time_t)NULL) );
74
+ for( i=0; i<NUM_SETS; ++i )
75
+ for( j=0; j<NUM_POINTS; ++j )
76
+ /* random number between low & high */
77
+ data[i][j] = 1.0+low+(high * rand()/(RAND_MAX+1.0));
78
+ }
79
+
80
+ /* -------- also random colors ----------------------------- */
81
+ get_individual_colors( unsigned long extclr[NUM_SETS][NUM_POINTS] )
82
+ {
83
+ int i, j;
84
+ for( i=0; i<NUM_SETS; ++i )
85
+ for( j=0; j<NUM_POINTS; ++j )
86
+ extclr[i][j] = (unsigned long)rand();
87
+ }
88
+
89
+
@@ -0,0 +1,310 @@
1
+ /* GDCHART 0.11.3dev GDC.C 11 Mar 2003 */
2
+ /* Copyright Bruce Verderaime 1998-2004 */
3
+
4
+ #define GDC_INCL
5
+ #define GDC_LIB
6
+ #include <math.h>
7
+ #include "gdc.h"
8
+
9
+ struct GDC_FONT_T GDC_fontc[GDC_numfonts] = { (gdFontPtr)NULL, 8, 5,
10
+ (gdFontPtr)NULL, 8, 5,
11
+ (gdFontPtr)NULL, 12, 6,
12
+ (gdFontPtr)NULL, 13, 7,
13
+ (gdFontPtr)NULL, 16, 8,
14
+ (gdFontPtr)NULL, 15, 9 };
15
+
16
+ /* ------------------------------------------------------------------- *\
17
+ * convert from enum GDC_font_size to gd fonts
18
+ * for now load them all
19
+ * #defines and #ifdefs might enable loading only needed fonts
20
+ * gd2.0 is to be built as a shared obj.
21
+ \* ------------------------------------------------------------------- */
22
+ void
23
+ load_font_conversions()
24
+ {
25
+ GDC_fontc[GDC_pad].f = gdFontTiny;
26
+ GDC_fontc[GDC_TINY].f = gdFontTiny;
27
+ GDC_fontc[GDC_SMALL].f = gdFontSmall;
28
+ GDC_fontc[GDC_MEDBOLD].f = gdFontMediumBold;
29
+ GDC_fontc[GDC_LARGE].f = gdFontLarge;
30
+ GDC_fontc[GDC_GIANT].f = gdFontGiant;
31
+ }
32
+
33
+ /* ------------------------------------------------------------------ *\
34
+ * count (natural) substrings (new line sep)
35
+ \* ------------------------------------------------------------------ */
36
+ short
37
+ cnt_nl( char *nstr,
38
+ int *len ) /* strlen - max seg */
39
+ {
40
+ short c = 1;
41
+ short max_seg_len = 0;
42
+ short tmplen = 0;
43
+
44
+ if( !nstr )
45
+ {
46
+ if( len )
47
+ *len = 0;
48
+ return 0;
49
+ }
50
+ while( *nstr )
51
+ {
52
+ if( *nstr == '\n' )
53
+ {
54
+ ++c;
55
+ max_seg_len = MAX( tmplen, max_seg_len );
56
+ tmplen = 0;
57
+ }
58
+ else
59
+ ++tmplen;
60
+ ++nstr;
61
+ }
62
+
63
+ if( len )
64
+ *len = MAX( tmplen, max_seg_len ); /* don't forget last seg */
65
+ return c;
66
+ }
67
+
68
+ /* ------------------------------------------------------------------ *\
69
+ * gd out a string with '\n's
70
+ * handle FTs (TTFs) and gd fonts
71
+ * gdImageString() draws from the upper left;
72
+ * gdImageStringFT() draws from lower left (one font height, even with '\n's)! >:-|
73
+ \* ------------------------------------------------------------------ */
74
+ int
75
+ GDCImageStringNL( gdImagePtr im,
76
+ struct GDC_FONT_T *f,
77
+ char *ftfont,
78
+ double ftptsz,
79
+ double rad,
80
+ int x,
81
+ int y,
82
+ char *str,
83
+ int clr,
84
+ GDC_justify_t justify,
85
+ char **sts )
86
+ {
87
+ int retval = 0;
88
+ char *err = NULL;
89
+
90
+ #ifdef HAVE_LIBFREETYPE
91
+ /* TODO: honor justifies */
92
+ if( ftfont && ftptsz )
93
+ {
94
+ /* need one line height */
95
+ /* remember last one (will likely be the same) */
96
+ /* is this needed? */
97
+ /* gdImageStringFT() utilizes some caching */
98
+ /* saves a couple floating point trig calls */
99
+ static int f1hgt = 0;
100
+ static double xs,
101
+ ys;
102
+ static double lftptsz = 0.0;
103
+ static char *lftfont = (char*)-1;
104
+
105
+ if( !f1hgt ||
106
+ ( lftfont != ftfont || lftptsz != ftptsz ) )
107
+ {
108
+ f1hgt = GDCfnt_sz( "Aj",
109
+ 0,
110
+ ftfont,
111
+ ftptsz,
112
+ rad,
113
+ NULL ).h;
114
+ xs = (double)f1hgt * sin(rad);
115
+ ys = (double)(f1hgt-1) * cos(rad);
116
+ }
117
+ x += (int)xs;
118
+ y += (int)ys;
119
+ if( (err = gdImageStringFT( im,
120
+ (int*)NULL,
121
+ clr,
122
+ ftfont,
123
+ ftptsz,
124
+ rad,
125
+ x,
126
+ y,
127
+ str)) == NULL )
128
+ {
129
+ if( sts ) *sts = err;
130
+ return 0;
131
+ }
132
+ else
133
+ {
134
+ /* TTF failed */
135
+ retval = 1;
136
+ /* fall through - default to gdFonts */
137
+ /* reinstate upper left reference */
138
+ x -= (int)xs;
139
+ y -= (int)ys;
140
+ }
141
+ }
142
+ #endif
143
+
144
+ {
145
+ int i;
146
+ int len;
147
+ int max_len;
148
+ short strs_num = cnt_nl( str, &max_len );
149
+ CREATE_ARRAY1( sub_str, unsigned char, max_len+1 ); /* char sub_str[max_len+1]; */
150
+
151
+ len = -1;
152
+ strs_num = -1;
153
+ i = -1;
154
+ do
155
+ {
156
+ ++i;
157
+ ++len;
158
+ sub_str[len] = *(str+i);
159
+ if( *(str+i) == '\n' ||
160
+ *(str+i) == '\0' )
161
+ {
162
+ int xpos;
163
+
164
+ sub_str[len] = '\0';
165
+ ++strs_num;
166
+ switch( justify )
167
+ {
168
+ case GDC_JUSTIFY_LEFT: xpos = 0; break;
169
+ case GDC_JUSTIFY_RIGHT: xpos = f->w*(max_len-len); break;
170
+ case GDC_JUSTIFY_CENTER:
171
+ default: xpos = f->w*(max_len-len)/2;
172
+ }
173
+ if( rad == 0.0 )
174
+ gdImageString( im,
175
+ f->f,
176
+ x + xpos,
177
+ y + (f->h-1)*strs_num,
178
+ sub_str,
179
+ clr );
180
+ else /* if( rad == M_PI/2.0 ) */
181
+ gdImageStringUp( im,
182
+ f->f,
183
+ x + (f->h-1)*strs_num,
184
+ y - xpos,
185
+ sub_str,
186
+ clr );
187
+ len = -1;
188
+ }
189
+ }
190
+ while( *(str+i) );
191
+ }
192
+
193
+ if( sts ) *sts = err;
194
+ return retval;
195
+ }
196
+
197
+ /* ------------------------------------------------------------------------ *\
198
+ * TODO: *
199
+ * really get a unique color from the color map *
200
+ \* ------------------------------------------------------------------------ */
201
+ long
202
+ get_uniq_color( gdImagePtr im )
203
+ {
204
+ return 0x123454;
205
+ }
206
+
207
+ /* ------------------------------------------------------------------------ */
208
+ struct fnt_sz_t
209
+ GDCfnt_sz( char *s,
210
+ enum GDC_font_size gdfontsz,
211
+ char *ftfont,
212
+ double ftfptsz,
213
+ double rad, /* w,h still relative to horiz. */
214
+ char **sts )
215
+ {
216
+ struct fnt_sz_t rtnval;
217
+ int len;
218
+ char *err = NULL;
219
+
220
+ #ifdef HAVE_LIBFREETYPE
221
+ if( ftfont && ftfptsz )
222
+ {
223
+ int brect[8];
224
+
225
+ /* obtain brect so that we can size the image */
226
+ if( (err = gdImageStringFT( (gdImagePtr)NULL,
227
+ &brect[0],
228
+ 0,
229
+ ftfont,
230
+ ftfptsz,
231
+ 0.0, /* rad, */ /* always match angled size??? */
232
+ 0,
233
+ 0,
234
+ s)) == NULL )
235
+ {
236
+ rtnval.h = brect[1] - brect[7];
237
+ rtnval.w = brect[2] - brect[0];
238
+ if( sts ) *sts = err;
239
+ return rtnval;
240
+ }
241
+ }
242
+ #endif
243
+
244
+ rtnval.h = cnt_nl(s,&len) * GDC_fontc[gdfontsz].h;
245
+ rtnval.w = len * GDC_fontc[gdfontsz].w;
246
+ if( sts ) *sts = err;
247
+ return rtnval;
248
+ }
249
+
250
+ /* ------------------------------------------------------------------------ */
251
+ void
252
+ GDC_destroy_image(void *im)
253
+ {
254
+ if( im )
255
+ gdImageDestroy( (gdImagePtr)im );
256
+ }
257
+
258
+ /* ------------------------------------------------------------------------ */
259
+ void
260
+ out_err( int IMGWIDTH,
261
+ int IMGHEIGHT,
262
+ FILE *fptr,
263
+ unsigned long BGColor,
264
+ unsigned long LineColor,
265
+ char *err_str )
266
+ {
267
+
268
+ gdImagePtr im;
269
+ int lineclr;
270
+ int bgclr;
271
+
272
+
273
+ if( (GDC_hold_img & GDC_REUSE_IMAGE) &&
274
+ GDC_image != (void*)NULL )
275
+ im = GDC_image;
276
+ else
277
+ im = gdImageCreate( IMGWIDTH, IMGHEIGHT );
278
+
279
+ bgclr = gdImageColorAllocate( im, l2gdcal(BGColor) );
280
+ lineclr = gdImageColorAllocate( im, l2gdcal(LineColor) );
281
+
282
+ gdImageString( im,
283
+ gdFontMediumBold,
284
+ IMGWIDTH/2 - GDC_fontc[GDC_MEDBOLD].w*strlen(err_str)/2,
285
+ IMGHEIGHT/3,
286
+ (unsigned char*)err_str,
287
+ lineclr );
288
+
289
+ /* usually GDC_generate_img is used in conjunction with hard or hold options */
290
+ if( GDC_generate_img )
291
+ {
292
+ fflush(fptr); /* clear anything buffered */
293
+ switch( GDC_image_type )
294
+ {
295
+ #ifdef HAVE_JPEG
296
+ case GDC_JPEG: gdImageJpeg( im, fptr, GDC_jpeg_quality ); break;
297
+ #endif
298
+ case GDC_WBMP: gdImageWBMP( im, lineclr, fptr ); break;
299
+ case GDC_GIF: gdImageGif( im, fptr); break;
300
+ case GDC_PNG:
301
+ default: gdImagePng( im, fptr );
302
+ }
303
+ }
304
+
305
+ if( GDC_hold_img & GDC_EXPOSE_IMAGE )
306
+ GDC_image = (void*)im;
307
+ else
308
+ gdImageDestroy(im);
309
+ return;
310
+ }