themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_Chart.cxx
blob5af4ed2187f86f10d1803e7c41152420baa57af9
1 //
2 // "$Id: Fl_Chart.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Forms-compatible chart widget for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
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
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include <FL/math.h>
29 #include <FL/Fl.H>
30 #include <FL/Fl_Chart.H>
31 #include <FL/fl_draw.H>
32 #include "flstring.h"
33 #include <stdlib.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,
45 Fl_Color textcolor)
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. */
49 double incr;
50 int zeroh;
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);
56 zeroh = int(y+h-lh);
57 } else {
58 zeroh = (int)rint(y+h+min * incr);
60 int bwidth = (int)rint(w/double(autosize?numb:maxnumb));
61 /* Draw base line */
62 fl_color(textcolor);
63 fl_line(x, zeroh, x+w, zeroh);
64 if (min == 0.0 && max == 0.0) return; /* Nothing else to draw */
65 int i;
66 /* Draw the bars */
67 for (i=0; i<numb; i++) {
68 int hh = (int)rint(entries[i].val*incr);
69 if (hh < 0)
70 fl_rectbound(x+i*bwidth,zeroh,bwidth+1,-hh+1, (Fl_Color)entries[i].col);
71 else if (hh > 0)
72 fl_rectbound(x+i*bwidth,zeroh-hh,bwidth+1,hh+1,(Fl_Color)entries[i].col);
74 /* Draw the labels */
75 fl_color(textcolor);
76 for (i=0; i<numb; i++)
77 fl_draw(entries[i].str,
78 x+i*bwidth+bwidth/2,zeroh,0,0,
79 FL_ALIGN_TOP);
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,
85 Fl_Color textcolor)
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. */
89 int i;
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);
94 if (w1 > lw) lw = w1;
96 if (lw > 0.0) lw += 4.0;
97 double incr;
98 int zeroh;
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);
104 } else {
105 zeroh = (int)rint(x-min * incr);
107 int bwidth = (int)rint(h/double(autosize?numb:maxnumb));
108 /* Draw base line */
109 fl_color(textcolor);
110 fl_line(zeroh, y, zeroh, y+h);
111 if (min == 0.0 && max == 0.0) return; /* Nothing else to draw */
112 /* Draw the bars */
113 for (i=0; i<numb; i++) {
114 int ww = (int)rint(entries[i].val*incr);
115 if (ww > 0)
116 fl_rectbound(zeroh,y+i*bwidth,ww+1,bwidth+1, (Fl_Color)entries[i].col);
117 else if (ww < 0)
118 fl_rectbound(zeroh+ww,y+i*bwidth,-ww+1,bwidth+1,(Fl_Color)entries[i].col);
120 /* Draw the labels */
121 fl_color(textcolor);
122 for (i=0; i<numb; i++)
123 fl_draw(entries[i].str,
124 zeroh-2,y+i*bwidth+bwidth/2,0,0,
125 FL_ALIGN_RIGHT);
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,
131 Fl_Color textcolor)
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. */
135 int i;
136 double lh = fl_height();
137 double incr;
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);
161 } else {
162 fl_polygon(x0,zeroh, x0,yy0, x1,yy1, x1,zeroh);
164 fl_color(textcolor);
165 fl_line(x0,yy0,x1,yy1);
168 /* Draw base line */
169 fl_color(textcolor);
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,
180 Fl_Color textcolor)
181 /* Draws a pie chart. x,y,w,h is the bounding box, entries the array of
182 numb entries */
184 int i;
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 */
196 tot = 0.0;
197 for (i=0; i<numb; i++)
198 if (entries[i].val > 0.0) tot += entries[i].val;
199 if (tot == 0.0) return;
200 incr = 360.0/tot;
201 /* Draw the pie */
202 curang = 0.0;
203 for (i=0; i<numb; i++)
204 if (entries[i].val > 0.0)
206 txc = xc; tyc = yc;
207 /* Correct for special pies */
208 if (special && i==0)
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);
216 fl_end_polygon();
217 fl_color(textcolor);
218 fl_begin_loop(); fl_vertex(txc,tyc);
219 fl_arc(txc,tyc,rad,curang, curang+incr*entries[i].val);
220 fl_end_loop();
221 curang += 0.5 * incr * entries[i].val;
222 /* draw the label */
223 double xl = txc + 1.1*rad*cos(ARCINC*curang);
224 fl_draw(entries[i].str,
225 (int)rint(xl),
226 (int)rint(tyc - 1.1*rad*sin(ARCINC*curang)),
227 0, 0,
228 xl<txc ? FL_ALIGN_RIGHT : FL_ALIGN_LEFT);
229 curang += 0.5 * incr * entries[i].val;
233 void Fl_Chart::draw() {
235 draw_box();
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
245 if (min >= max) {
246 min = max = 0.0;
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());
255 switch (type()) {
256 case FL_BAR_CHART:
257 ww++; // makes the bars fill box correctly
258 draw_barchart(xx,yy,ww,hh, numb, entries, min, max,
259 autosize(), maxnumb, textcolor());
260 break;
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());
265 break;
266 case FL_PIE_CHART:
267 draw_piechart(xx,yy,ww,hh,numb,entries,0, textcolor());
268 break;
269 case FL_SPECIALPIE_CHART:
270 draw_piechart(xx,yy,ww,hh,numb,entries,1,textcolor());
271 break;
272 default:
273 draw_linechart(type(),xx,yy,ww,hh, numb, entries, min, max,
274 autosize(), maxnumb, textcolor());
275 break;
277 draw_label();
278 fl_pop_clip();
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) {
296 box(FL_BORDER_BOX);
297 align(FL_ALIGN_BOTTOM);
298 numb = 0;
299 maxnumb = 0;
300 sizenumb = FL_CHART_MAX;
301 autosize_ = 1;
302 min = max = 0;
303 textfont_ = FL_HELVETICA;
304 textsize_ = 10;
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() {
313 free(entries);
317 Removes all values from the chart.
319 void Fl_Chart::clear() {
320 numb = 0;
321 min = max = 0;
322 redraw();
326 Add the data value \p val with optional label \p str and color \p col
327 to the chart.
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));
341 numb --;
343 entries[numb].val = float(val);
344 entries[numb].col = col;
345 if (str) {
346 strlcpy(entries[numb].str,str,FL_CHART_LABEL_MAX + 1);
347 } else {
348 entries[numb].str[0] = 0;
350 numb++;
351 redraw();
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) {
363 int i;
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;
376 if (str) {
377 strlcpy(entries[ind-1].str,str,FL_CHART_LABEL_MAX+1);
378 } else {
379 entries[ind-1].str[0] = 0;
381 redraw();
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;
396 if (str) {
397 strlcpy(entries[ind-1].str,str,FL_CHART_LABEL_MAX+1);
398 } else {
399 entries[ind-1].str[0] = 0;
401 redraw();
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) {
409 this->min = a;
410 this->max = b;
411 redraw();
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) {
421 int i;
422 /* Fill in the new number */
423 if (m < 0) return;
424 maxnumb = m;
425 /* Shift entries if required */
426 if (numb > maxnumb) {
427 for (i = 0; i<maxnumb; i++)
428 entries[i] = entries[i+numb-maxnumb];
429 numb = maxnumb;
430 redraw();
435 // End of "$Id: Fl_Chart.cxx 7903 2010-11-28 21:06:39Z matt $".