2 // "$Id: Fl_Chart.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Forms-compatible chart widget for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
30 #include <FL/Fl_Chart.H>
31 #include <FL/fl_draw.H>
35 #define ARCINC (2.0*M_PI/360.0)
37 // this function is in fl_boxtype.cxx:
38 void fl_rectbound(int x
,int y
,int w
,int h
, Fl_Color color
);
40 /* Widget specific information */
42 static void draw_barchart(int x
,int y
,int w
,int h
,
43 int numb
, FL_CHART_ENTRY entries
[],
44 double min
, double max
, int autosize
, int maxnumb
,
46 /* Draws a bar chart. x,y,w,h is the bounding box, entries the array of
47 numb entries and min and max the boundaries. */
51 double lh
= fl_height();
52 if (max
== min
) incr
= h
;
53 else incr
= h
/(max
-min
);
54 if ( (-min
*incr
) < lh
) {
55 incr
= (h
- lh
+ min
*incr
)/(max
-min
);
58 zeroh
= (int)rint(y
+h
+min
* incr
);
60 int bwidth
= (int)rint(w
/double(autosize
?numb
:maxnumb
));
63 fl_line(x
, zeroh
, x
+w
, zeroh
);
64 if (min
== 0.0 && max
== 0.0) return; /* Nothing else to draw */
67 for (i
=0; i
<numb
; i
++) {
68 int hh
= (int)rint(entries
[i
].val
*incr
);
70 fl_rectbound(x
+i
*bwidth
,zeroh
,bwidth
+1,-hh
+1, (Fl_Color
)entries
[i
].col
);
72 fl_rectbound(x
+i
*bwidth
,zeroh
-hh
,bwidth
+1,hh
+1,(Fl_Color
)entries
[i
].col
);
76 for (i
=0; i
<numb
; i
++)
77 fl_draw(entries
[i
].str
,
78 x
+i
*bwidth
+bwidth
/2,zeroh
,0,0,
82 static void draw_horbarchart(int x
,int y
,int w
,int h
,
83 int numb
, FL_CHART_ENTRY entries
[],
84 double min
, double max
, int autosize
, int maxnumb
,
86 /* Draws a horizontal bar chart. x,y,w,h is the bounding box, entries the
87 array of numb entries and min and max the boundaries. */
90 double lw
= 0.0; /* Maximal label width */
91 /* Compute maximal label width */
92 for (i
=0; i
<numb
; i
++) {
93 double w1
= fl_width(entries
[i
].str
);
96 if (lw
> 0.0) lw
+= 4.0;
99 if (max
== min
) incr
= w
;
100 else incr
= w
/(max
-min
);
101 if ( (-min
*incr
) < lw
) {
102 incr
= (w
- lw
+ min
*incr
)/(max
-min
);
103 zeroh
= x
+(int)rint(lw
);
105 zeroh
= (int)rint(x
-min
* incr
);
107 int bwidth
= (int)rint(h
/double(autosize
?numb
:maxnumb
));
110 fl_line(zeroh
, y
, zeroh
, y
+h
);
111 if (min
== 0.0 && max
== 0.0) return; /* Nothing else to draw */
113 for (i
=0; i
<numb
; i
++) {
114 int ww
= (int)rint(entries
[i
].val
*incr
);
116 fl_rectbound(zeroh
,y
+i
*bwidth
,ww
+1,bwidth
+1, (Fl_Color
)entries
[i
].col
);
118 fl_rectbound(zeroh
+ww
,y
+i
*bwidth
,-ww
+1,bwidth
+1,(Fl_Color
)entries
[i
].col
);
120 /* Draw the labels */
122 for (i
=0; i
<numb
; i
++)
123 fl_draw(entries
[i
].str
,
124 zeroh
-2,y
+i
*bwidth
+bwidth
/2,0,0,
128 static void draw_linechart(int type
, int x
,int y
,int w
,int h
,
129 int numb
, FL_CHART_ENTRY entries
[],
130 double min
, double max
, int autosize
, int maxnumb
,
132 /* Draws a line chart. x,y,w,h is the bounding box, entries the array of
133 numb entries and min and max the boundaries. */
136 double lh
= fl_height();
138 if (max
== min
) incr
= h
-2.0*lh
;
139 else incr
= (h
-2.0*lh
)/ (max
-min
);
140 int zeroh
= (int)rint(y
+h
-lh
+min
* incr
);
141 double bwidth
= w
/double(autosize
?numb
:maxnumb
);
142 /* Draw the values */
143 for (i
=0; i
<numb
; i
++) {
144 int x0
= x
+ (int)rint((i
-.5)*bwidth
);
145 int x1
= x
+ (int)rint((i
+.5)*bwidth
);
146 int yy0
= i
? zeroh
- (int)rint(entries
[i
-1].val
*incr
) : 0;
147 int yy1
= zeroh
- (int)rint(entries
[i
].val
*incr
);
148 if (type
== FL_SPIKE_CHART
) {
149 fl_color((Fl_Color
)entries
[i
].col
);
150 fl_line(x1
, zeroh
, x1
, yy1
);
151 } else if (type
== FL_LINE_CHART
&& i
!= 0) {
152 fl_color((Fl_Color
)entries
[i
-1].col
);
153 fl_line(x0
,yy0
,x1
,yy1
);
154 } else if (type
== FL_FILLED_CHART
&& i
!= 0) {
155 fl_color((Fl_Color
)entries
[i
-1].col
);
156 if ((entries
[i
-1].val
>0.0)!=(entries
[i
].val
>0.0)) {
157 double ttt
= entries
[i
-1].val
/(entries
[i
-1].val
-entries
[i
].val
);
158 int xt
= x
+ (int)rint((i
-.5+ttt
)*bwidth
);
159 fl_polygon(x0
,zeroh
, x0
,yy0
, xt
,zeroh
);
160 fl_polygon(xt
,zeroh
, x1
,yy1
, x1
,zeroh
);
162 fl_polygon(x0
,zeroh
, x0
,yy0
, x1
,yy1
, x1
,zeroh
);
165 fl_line(x0
,yy0
,x1
,yy1
);
170 fl_line(x
,zeroh
,x
+w
,zeroh
);
171 /* Draw the labels */
172 for (i
=0; i
<numb
; i
++)
173 fl_draw(entries
[i
].str
,
174 x
+(int)rint((i
+.5)*bwidth
), zeroh
- (int)rint(entries
[i
].val
*incr
),0,0,
175 entries
[i
].val
>=0 ? FL_ALIGN_BOTTOM
: FL_ALIGN_TOP
);
178 static void draw_piechart(int x
,int y
,int w
,int h
,
179 int numb
, FL_CHART_ENTRY entries
[], int special
,
181 /* Draws a pie chart. x,y,w,h is the bounding box, entries the array of
185 double xc
,yc
,rad
; /* center and radius */
186 double tot
; /* sum of values */
187 double incr
; /* increment in angle */
188 double curang
; /* current angle we are drawing */
189 double txc
,tyc
; /* temporary center */
190 double lh
= fl_height();
191 /* compute center and radius */
192 double h_denom
= (special
? 2.3 : 2.0);
193 rad
= (h
- 2*lh
)/h_denom
/1.1;
194 xc
= x
+w
/2.0; yc
= y
+h
-1.1*rad
-lh
;
195 /* compute sum of values */
197 for (i
=0; i
<numb
; i
++)
198 if (entries
[i
].val
> 0.0) tot
+= entries
[i
].val
;
199 if (tot
== 0.0) return;
203 for (i
=0; i
<numb
; i
++)
204 if (entries
[i
].val
> 0.0)
207 /* Correct for special pies */
210 txc
+= 0.3*rad
*cos(ARCINC
*(curang
+0.5*incr
*entries
[i
].val
));
211 tyc
-= 0.3*rad
*sin(ARCINC
*(curang
+0.5*incr
*entries
[i
].val
));
213 fl_color((Fl_Color
)entries
[i
].col
);
214 fl_begin_polygon(); fl_vertex(txc
,tyc
);
215 fl_arc(txc
,tyc
,rad
,curang
, curang
+incr
*entries
[i
].val
);
218 fl_begin_loop(); fl_vertex(txc
,tyc
);
219 fl_arc(txc
,tyc
,rad
,curang
, curang
+incr
*entries
[i
].val
);
221 curang
+= 0.5 * incr
* entries
[i
].val
;
223 double xl
= txc
+ 1.1*rad
*cos(ARCINC
*curang
);
224 fl_draw(entries
[i
].str
,
226 (int)rint(tyc
- 1.1*rad
*sin(ARCINC
*curang
)),
228 xl
<txc
? FL_ALIGN_RIGHT
: FL_ALIGN_LEFT
);
229 curang
+= 0.5 * incr
* entries
[i
].val
;
233 void Fl_Chart::draw() {
236 Fl_Boxtype b
= box();
237 int xx
= x()+Fl::box_dx(b
); // was 9 instead of dx...
238 int yy
= y()+Fl::box_dy(b
);
239 int ww
= w()-Fl::box_dw(b
);
240 int hh
= h()-Fl::box_dh(b
);
241 fl_push_clip(xx
, yy
, ww
, hh
);
243 ww
--; hh
--; // adjust for line thickness
247 for (int i
=0; i
<numb
; i
++) {
248 if (entries
[i
].val
< min
) min
= entries
[i
].val
;
249 if (entries
[i
].val
> max
) max
= entries
[i
].val
;
253 fl_font(textfont(),textsize());
257 ww
++; // makes the bars fill box correctly
258 draw_barchart(xx
,yy
,ww
,hh
, numb
, entries
, min
, max
,
259 autosize(), maxnumb
, textcolor());
261 case FL_HORBAR_CHART
:
262 hh
++; // makes the bars fill box correctly
263 draw_horbarchart(xx
,yy
,ww
,hh
, numb
, entries
, min
, max
,
264 autosize(), maxnumb
, textcolor());
267 draw_piechart(xx
,yy
,ww
,hh
,numb
,entries
,0, textcolor());
269 case FL_SPECIALPIE_CHART
:
270 draw_piechart(xx
,yy
,ww
,hh
,numb
,entries
,1,textcolor());
273 draw_linechart(type(),xx
,yy
,ww
,hh
, numb
, entries
, min
, max
,
274 autosize(), maxnumb
, textcolor());
281 /*------------------------------*/
283 #define FL_CHART_BOXTYPE FL_BORDER_BOX
284 #define FL_CHART_COL1 FL_COL1
285 #define FL_CHART_LCOL FL_LCOL
286 #define FL_CHART_ALIGN FL_ALIGN_BOTTOM
289 Create a new Fl_Chart widget using the given position, size and label string.
290 The default boxstyle is \c FL_NO_BOX.
291 \param[in] X, Y, W, H position and size of the widget
292 \param[in] L widget label, default is no label
294 Fl_Chart::Fl_Chart(int X
, int Y
, int W
, int H
,const char *L
) :
295 Fl_Widget(X
,Y
,W
,H
,L
) {
297 align(FL_ALIGN_BOTTOM
);
300 sizenumb
= FL_CHART_MAX
;
303 textfont_
= FL_HELVETICA
;
305 textcolor_
= FL_FOREGROUND_COLOR
;
306 entries
= (FL_CHART_ENTRY
*)calloc(sizeof(FL_CHART_ENTRY
), FL_CHART_MAX
+ 1);
310 Destroys the Fl_Chart widget and all of its data.
312 Fl_Chart::~Fl_Chart() {
317 Removes all values from the chart.
319 void Fl_Chart::clear() {
326 Add the data value \p val with optional label \p str and color \p col
328 \param[in] val data value
329 \param[in] str optional data label
330 \param[in] col optional data color
332 void Fl_Chart::add(double val
, const char *str
, unsigned col
) {
333 /* Allocate more entries if required */
334 if (numb
>= sizenumb
) {
335 sizenumb
+= FL_CHART_MAX
;
336 entries
= (FL_CHART_ENTRY
*)realloc(entries
, sizeof(FL_CHART_ENTRY
) * (sizenumb
+ 1));
338 // Shift entries as needed
339 if (numb
>= maxnumb
&& maxnumb
> 0) {
340 memmove(entries
, entries
+ 1, sizeof(FL_CHART_ENTRY
) * (numb
- 1));
343 entries
[numb
].val
= float(val
);
344 entries
[numb
].col
= col
;
346 strlcpy(entries
[numb
].str
,str
,FL_CHART_LABEL_MAX
+ 1);
348 entries
[numb
].str
[0] = 0;
355 Inserts a data value \p val at the given position \p ind.
356 Position 1 is the first data value.
357 \param[in] ind insertion position
358 \param[in] val data value
359 \param[in] str optional data label
360 \param[in] col optional data color
362 void Fl_Chart::insert(int ind
, double val
, const char *str
, unsigned col
) {
364 if (ind
< 1 || ind
> numb
+1) return;
365 /* Allocate more entries if required */
366 if (numb
>= sizenumb
) {
367 sizenumb
+= FL_CHART_MAX
;
368 entries
= (FL_CHART_ENTRY
*)realloc(entries
, sizeof(FL_CHART_ENTRY
) * (sizenumb
+ 1));
370 // Shift entries as needed
371 for (i
=numb
; i
>= ind
; i
--) entries
[i
] = entries
[i
-1];
372 if (numb
< maxnumb
|| maxnumb
== 0) numb
++;
373 /* Fill in the new entry */
374 entries
[ind
-1].val
= float(val
);
375 entries
[ind
-1].col
= col
;
377 strlcpy(entries
[ind
-1].str
,str
,FL_CHART_LABEL_MAX
+1);
379 entries
[ind
-1].str
[0] = 0;
385 Replace a data value \p val at the given position \p ind.
386 Position 1 is the first data value.
387 \param[in] ind insertion position
388 \param[in] val data value
389 \param[in] str optional data label
390 \param[in] col optional data color
392 void Fl_Chart::replace(int ind
,double val
, const char *str
, unsigned col
) {
393 if (ind
< 1 || ind
> numb
) return;
394 entries
[ind
-1].val
= float(val
);
395 entries
[ind
-1].col
= col
;
397 strlcpy(entries
[ind
-1].str
,str
,FL_CHART_LABEL_MAX
+1);
399 entries
[ind
-1].str
[0] = 0;
405 Sets the lower and upper bounds of the chart values.
406 \param[in] a, b are used to set lower, upper
408 void Fl_Chart::bounds(double a
, double b
) {
415 Set the maximum number of data values for a chart.
416 If you do not call this method then the chart will be allowed to grow
417 to any size depending on available memory.
418 \param[in] m maximum number of data values allowed.
420 void Fl_Chart::maxsize(int m
) {
422 /* Fill in the new number */
425 /* Shift entries if required */
426 if (numb
> maxnumb
) {
427 for (i
= 0; i
<maxnumb
; i
++)
428 entries
[i
] = entries
[i
+numb
-maxnumb
];
435 // End of "$Id: Fl_Chart.cxx 7903 2010-11-28 21:06:39Z matt $".