fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Chart.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
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. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include <FL/math.h>
20 #include <FL/Fl.H>
21 #include <FL/Fl_Chart.H>
22 #include <FL/fl_draw.H>
23 #include "flstring.h"
24 #include <stdlib.h>
25 
26 #define ARCINC (2.0*M_PI/360.0)
27 
28 // this function is in fl_boxtype.cxx:
29 void fl_rectbound(int x,int y,int w,int h, Fl_Color color);
30 
31 /* Widget specific information */
32 
33 static void draw_barchart(int x,int y,int w,int h,
34  int numb, FL_CHART_ENTRY entries[],
35  double min, double max, int autosize, int maxnumb,
36  Fl_Color textcolor)
37 /* Draws a bar chart. x,y,w,h is the bounding box, entries the array of
38  numb entries and min and max the boundaries. */
39 {
40  double incr;
41  int zeroh;
42  double lh = fl_height();
43  if (max == min) incr = h;
44  else incr = h/(max-min);
45  if ( (-min*incr) < lh) {
46  incr = (h - lh + min*incr)/(max-min);
47  zeroh = int(y+h-lh);
48  } else {
49  zeroh = (int)rint(y+h+min * incr);
50  }
51  int bwidth = (int)rint(w/double(autosize?numb:maxnumb));
52  /* Draw base line */
53  fl_color(textcolor);
54  fl_line(x, zeroh, x+w, zeroh);
55  if (min == 0.0 && max == 0.0) return; /* Nothing else to draw */
56  int i;
57  /* Draw the bars */
58  for (i=0; i<numb; i++) {
59  int hh = (int)rint(entries[i].val*incr);
60  if (hh < 0)
61  fl_rectbound(x+i*bwidth,zeroh,bwidth+1,-hh+1, (Fl_Color)entries[i].col);
62  else if (hh > 0)
63  fl_rectbound(x+i*bwidth,zeroh-hh,bwidth+1,hh+1,(Fl_Color)entries[i].col);
64  }
65  /* Draw the labels */
66  fl_color(textcolor);
67  for (i=0; i<numb; i++)
68  fl_draw(entries[i].str,
69  x+i*bwidth+bwidth/2,zeroh,0,0,
70  FL_ALIGN_TOP);
71 }
72 
73 static void draw_horbarchart(int x,int y,int w,int h,
74  int numb, FL_CHART_ENTRY entries[],
75  double min, double max, int autosize, int maxnumb,
76  Fl_Color textcolor)
77 /* Draws a horizontal bar chart. x,y,w,h is the bounding box, entries the
78  array of numb entries and min and max the boundaries. */
79 {
80  int i;
81  double lw = 0.0; /* Maximal label width */
82  /* Compute maximal label width */
83  for (i=0; i<numb; i++) {
84  double w1 = fl_width(entries[i].str);
85  if (w1 > lw) lw = w1;
86  }
87  if (lw > 0.0) lw += 4.0;
88  double incr;
89  int zeroh;
90  if (max == min) incr = w;
91  else incr = w/(max-min);
92  if ( (-min*incr) < lw) {
93  incr = (w - lw + min*incr)/(max-min);
94  zeroh = x+(int)rint(lw);
95  } else {
96  zeroh = (int)rint(x-min * incr);
97  }
98  int bwidth = (int)rint(h/double(autosize?numb:maxnumb));
99  /* Draw base line */
100  fl_color(textcolor);
101  fl_line(zeroh, y, zeroh, y+h);
102  if (min == 0.0 && max == 0.0) return; /* Nothing else to draw */
103  /* Draw the bars */
104  for (i=0; i<numb; i++) {
105  int ww = (int)rint(entries[i].val*incr);
106  if (ww > 0)
107  fl_rectbound(zeroh,y+i*bwidth,ww+1,bwidth+1, (Fl_Color)entries[i].col);
108  else if (ww < 0)
109  fl_rectbound(zeroh+ww,y+i*bwidth,-ww+1,bwidth+1,(Fl_Color)entries[i].col);
110  }
111  /* Draw the labels */
112  fl_color(textcolor);
113  for (i=0; i<numb; i++)
114  fl_draw(entries[i].str,
115  zeroh-2,y+i*bwidth+bwidth/2,0,0,
117 }
118 
119 static void draw_linechart(int type, int x,int y,int w,int h,
120  int numb, FL_CHART_ENTRY entries[],
121  double min, double max, int autosize, int maxnumb,
122  Fl_Color textcolor)
123 /* Draws a line chart. x,y,w,h is the bounding box, entries the array of
124  numb entries and min and max the boundaries. */
125 {
126  int i;
127  double lh = fl_height();
128  double incr;
129  if (max == min) incr = h-2.0*lh;
130  else incr = (h-2.0*lh)/ (max-min);
131  int zeroh = (int)rint(y+h-lh+min * incr);
132  double bwidth = w/double(autosize?numb:maxnumb);
133  /* Draw the values */
134  for (i=0; i<numb; i++) {
135  int x0 = x + (int)rint((i-.5)*bwidth);
136  int x1 = x + (int)rint((i+.5)*bwidth);
137  int yy0 = i ? zeroh - (int)rint(entries[i-1].val*incr) : 0;
138  int yy1 = zeroh - (int)rint(entries[i].val*incr);
139  if (type == FL_SPIKE_CHART) {
140  fl_color((Fl_Color)entries[i].col);
141  fl_line(x1, zeroh, x1, yy1);
142  } else if (type == FL_LINE_CHART && i != 0) {
143  fl_color((Fl_Color)entries[i-1].col);
144  fl_line(x0,yy0,x1,yy1);
145  } else if (type == FL_FILLED_CHART && i != 0) {
146  fl_color((Fl_Color)entries[i-1].col);
147  if ((entries[i-1].val>0.0)!=(entries[i].val>0.0)) {
148  double ttt = entries[i-1].val/(entries[i-1].val-entries[i].val);
149  int xt = x + (int)rint((i-.5+ttt)*bwidth);
150  fl_polygon(x0,zeroh, x0,yy0, xt,zeroh);
151  fl_polygon(xt,zeroh, x1,yy1, x1,zeroh);
152  } else {
153  fl_polygon(x0,zeroh, x0,yy0, x1,yy1, x1,zeroh);
154  }
155  fl_color(textcolor);
156  fl_line(x0,yy0,x1,yy1);
157  }
158  }
159  /* Draw base line */
160  fl_color(textcolor);
161  fl_line(x,zeroh,x+w,zeroh);
162  /* Draw the labels */
163  for (i=0; i<numb; i++)
164  fl_draw(entries[i].str,
165  x+(int)rint((i+.5)*bwidth), zeroh - (int)rint(entries[i].val*incr),0,0,
166  entries[i].val>=0 ? FL_ALIGN_BOTTOM : FL_ALIGN_TOP);
167 }
168 
169 static void draw_piechart(int x,int y,int w,int h,
170  int numb, FL_CHART_ENTRY entries[], int special,
171  Fl_Color textcolor)
172 /* Draws a pie chart. x,y,w,h is the bounding box, entries the array of
173  numb entries */
174 {
175  int i;
176  double xc,yc,rad; /* center and radius */
177  double tot; /* sum of values */
178  double incr; /* increment in angle */
179  double curang; /* current angle we are drawing */
180  double txc,tyc; /* temporary center */
181  double lh = fl_height();
182  /* compute center and radius */
183  double h_denom = (special ? 2.3 : 2.0);
184  rad = (h - 2*lh)/h_denom/1.1;
185  xc = x+w/2.0; yc = y+h-1.1*rad-lh;
186  /* compute sum of values */
187  tot = 0.0;
188  for (i=0; i<numb; i++)
189  if (entries[i].val > 0.0) tot += entries[i].val;
190  if (tot == 0.0) return;
191  incr = 360.0/tot;
192  /* Draw the pie */
193  curang = 0.0;
194  for (i=0; i<numb; i++)
195  if (entries[i].val > 0.0)
196  {
197  txc = xc; tyc = yc;
198  /* Correct for special pies */
199  if (special && i==0)
200  {
201  txc += 0.3*rad*cos(ARCINC*(curang+0.5*incr*entries[i].val));
202  tyc -= 0.3*rad*sin(ARCINC*(curang+0.5*incr*entries[i].val));
203  }
204  fl_color((Fl_Color)entries[i].col);
205  fl_begin_polygon(); fl_vertex(txc,tyc);
206  fl_arc(txc,tyc,rad,curang, curang+incr*entries[i].val);
207  fl_end_polygon();
208  fl_color(textcolor);
209  fl_begin_loop(); fl_vertex(txc,tyc);
210  fl_arc(txc,tyc,rad,curang, curang+incr*entries[i].val);
211  fl_end_loop();
212  curang += 0.5 * incr * entries[i].val;
213  /* draw the label */
214  double xl = txc + 1.1*rad*cos(ARCINC*curang);
215  fl_draw(entries[i].str,
216  (int)rint(xl),
217  (int)rint(tyc - 1.1*rad*sin(ARCINC*curang)),
218  0, 0,
219  xl<txc ? FL_ALIGN_RIGHT : FL_ALIGN_LEFT);
220  curang += 0.5 * incr * entries[i].val;
221  }
222 }
223 
225 
226  draw_box();
227  Fl_Boxtype b = box();
228  int xx = x()+Fl::box_dx(b); // was 9 instead of dx...
229  int yy = y()+Fl::box_dy(b);
230  int ww = w()-Fl::box_dw(b);
231  int hh = h()-Fl::box_dh(b);
232  fl_push_clip(xx, yy, ww, hh);
233 
234  ww--; hh--; // adjust for line thickness
235 
236  if (min >= max) {
237  min = max = 0.0;
238  for (int i=0; i<numb; i++) {
239  if (entries[i].val < min) min = entries[i].val;
240  if (entries[i].val > max) max = entries[i].val;
241  }
242  }
243 
245 
246  switch (type()) {
247  case FL_BAR_CHART:
248  ww++; // makes the bars fill box correctly
249  draw_barchart(xx,yy,ww,hh, numb, entries, min, max,
250  autosize(), maxnumb, textcolor());
251  break;
252  case FL_HORBAR_CHART:
253  hh++; // makes the bars fill box correctly
254  draw_horbarchart(xx,yy,ww,hh, numb, entries, min, max,
255  autosize(), maxnumb, textcolor());
256  break;
257  case FL_PIE_CHART:
258  draw_piechart(xx,yy,ww,hh,numb,entries,0, textcolor());
259  break;
260  case FL_SPECIALPIE_CHART:
261  draw_piechart(xx,yy,ww,hh,numb,entries,1,textcolor());
262  break;
263  default:
264  draw_linechart(type(),xx,yy,ww,hh, numb, entries, min, max,
265  autosize(), maxnumb, textcolor());
266  break;
267  }
268  draw_label();
269  fl_pop_clip();
270 }
271 
272 /*------------------------------*/
273 
274 #define FL_CHART_BOXTYPE FL_BORDER_BOX
275 #define FL_CHART_COL1 FL_COL1
276 #define FL_CHART_LCOL FL_LCOL
277 #define FL_CHART_ALIGN FL_ALIGN_BOTTOM
278 
285 Fl_Chart::Fl_Chart(int X, int Y, int W, int H,const char *L) :
286 Fl_Widget(X,Y,W,H,L) {
289  numb = 0;
290  maxnumb = 0;
292  autosize_ = 1;
293  min = max = 0;
295  textsize_ = 10;
298 }
299 
304  free(entries);
305 }
306 
311  numb = 0;
312  min = max = 0;
313  redraw();
314 }
315 
323 void Fl_Chart::add(double val, const char *str, unsigned col) {
324  /* Allocate more entries if required */
325  if (numb >= sizenumb) {
327  entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
328  }
329  // Shift entries as needed
330  if (numb >= maxnumb && maxnumb > 0) {
331  memmove(entries, entries + 1, sizeof(FL_CHART_ENTRY) * (numb - 1));
332  numb --;
333  }
334  entries[numb].val = float(val);
335  entries[numb].col = col;
336  if (str) {
337  strlcpy(entries[numb].str,str,FL_CHART_LABEL_MAX + 1);
338  } else {
339  entries[numb].str[0] = 0;
340  }
341  numb++;
342  redraw();
343 }
344 
353 void Fl_Chart::insert(int ind, double val, const char *str, unsigned col) {
354  int i;
355  if (ind < 1 || ind > numb+1) return;
356  /* Allocate more entries if required */
357  if (numb >= sizenumb) {
359  entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
360  }
361  // Shift entries as needed
362  for (i=numb; i >= ind; i--) entries[i] = entries[i-1];
363  if (numb < maxnumb || maxnumb == 0) numb++;
364  /* Fill in the new entry */
365  entries[ind-1].val = float(val);
366  entries[ind-1].col = col;
367  if (str) {
368  strlcpy(entries[ind-1].str,str,FL_CHART_LABEL_MAX+1);
369  } else {
370  entries[ind-1].str[0] = 0;
371  }
372  redraw();
373 }
374 
383 void Fl_Chart::replace(int ind,double val, const char *str, unsigned col) {
384  if (ind < 1 || ind > numb) return;
385  entries[ind-1].val = float(val);
386  entries[ind-1].col = col;
387  if (str) {
388  strlcpy(entries[ind-1].str,str,FL_CHART_LABEL_MAX+1);
389  } else {
390  entries[ind-1].str[0] = 0;
391  }
392  redraw();
393 }
394 
399 void Fl_Chart::bounds(double a, double b) {
400  this->min = a;
401  this->max = b;
402  redraw();
403 }
404 
411 void Fl_Chart::maxsize(int m) {
412  int i;
413  /* Fill in the new number */
414  if (m < 0) return;
415  maxnumb = m;
416  /* Shift entries if required */
417  if (numb > maxnumb) {
418  for (i = 0; i<maxnumb; i++)
419  entries[i] = entries[i+numb-maxnumb];
420  numb = maxnumb;
421  redraw();
422  }
423 }
424 
425 //
426 // End of "$Id$".
427 //
min
static int min(int i1, int i2)
Definition: Fl_Text_Buffer.cxx:63
Fl_Widget::draw_label
void draw_label() const
Definition: fl_labeltype.cxx:99
Fl_Chart::replace
void replace(int ind, double val, const char *str=0, unsigned col=0)
Definition: Fl_Chart.cxx:383
fl_font
void fl_font(Fl_Font face, Fl_Fontsize fsize)
Definition: fl_draw.H:509
fl_width
FL_EXPORT double fl_width(const char *txt)
Definition: fl_font.cxx:65
FL_ALIGN_LEFT
const Fl_Align FL_ALIGN_LEFT
Definition: Enumerations.H:839
Fl_Widget::y
int y() const
Definition: Fl_Widget.H:289
FL_LINE_CHART
#define FL_LINE_CHART
Definition: Fl_Chart.H:32
Fl.H
Fl_Widget::align
Fl_Align align() const
Definition: Fl_Widget.H:348
fl_line
void fl_line(int x, int y, int x1, int y1)
Definition: fl_draw.H:223
Fl_Widget::draw_box
void draw_box() const
Definition: fl_boxtype.cxx:442
Fl_Color
unsigned int Fl_Color
Definition: Enumerations.H:934
fl_begin_polygon
void fl_begin_polygon()
Definition: fl_draw.H:374
Fl::box_dx
static int box_dx(Fl_Boxtype)
Definition: fl_boxtype.cxx:360
Fl_Boxtype
Fl_Boxtype
Definition: Enumerations.H:603
fl_color
void fl_color(Fl_Color c)
Definition: fl_draw.H:52
free
void free()
H
static int H
Definition: Fl_Tooltip.cxx:76
fl_height
int fl_height()
Definition: fl_draw.H:527
Fl_Widget::x
int x() const
Definition: Fl_Widget.H:284
fl_draw
FL_EXPORT void fl_draw(const char *str, int x, int y)
Definition: fl_font.cxx:70
FL_CHART_ENTRY::val
float val
Definition: Fl_Chart.H:45
fl_begin_loop
void fl_begin_loop()
Definition: fl_draw.H:370
b
long b
Definition: jpegint.h:397
FL_CHART_LABEL_MAX
#define FL_CHART_LABEL_MAX
Definition: Fl_Chart.H:41
draw_piechart
static void draw_piechart(int x, int y, int w, int h, int numb, FL_CHART_ENTRY entries[], int special, Fl_Color textcolor)
Definition: Fl_Chart.cxx:169
Fl::box_dh
static int box_dh(Fl_Boxtype)
Definition: fl_boxtype.cxx:397
Fl_Widget::w
int w() const
Definition: Fl_Widget.H:294
FL_FOREGROUND_COLOR
const Fl_Color FL_FOREGROUND_COLOR
the default foreground color (0) used for labels and text
Definition: Enumerations.H:937
Fl::box_dw
static int box_dw(Fl_Boxtype)
Definition: fl_boxtype.cxx:391
math.h
FL_SPECIALPIE_CHART
#define FL_SPECIALPIE_CHART
Definition: Fl_Chart.H:36
Fl_Chart::sizenumb
int sizenumb
Definition: Fl_Chart.H:75
fl_end_loop
void fl_end_loop()
Definition: fl_draw.H:436
ARCINC
#define ARCINC
Definition: Fl_Chart.cxx:26
Fl::box_dy
static int box_dy(Fl_Boxtype)
Definition: fl_boxtype.cxx:385
Fl_Chart::max
double max
Definition: Fl_Chart.H:77
Fl_Chart::textfont_
Fl_Font textfont_
Definition: Fl_Chart.H:79
Fl_Chart::maxsize
int maxsize() const
Definition: Fl_Chart.H:115
fl_draw.H
utility header to pull drawing functions together
Fl_Chart::maxnumb
int maxnumb
Definition: Fl_Chart.H:74
FL_HELVETICA
const Fl_Font FL_HELVETICA
Helvetica (or Arial) normal (0)
Definition: Enumerations.H:879
Fl_Chart::autosize
uchar autosize() const
Definition: Fl_Chart.H:138
max
static int max(int i1, int i2)
Definition: Fl_Text_Buffer.cxx:58
Fl_Chart::numb
int numb
Definition: Fl_Chart.H:73
strlcpy
#define strlcpy
Definition: flstring.h:84
Fl_Chart::textsize_
Fl_Fontsize textsize_
Definition: Fl_Chart.H:80
Fl_Chart::clear
void clear()
Definition: Fl_Chart.cxx:310
fl_arc
void fl_arc(int x, int y, int w, int h, double a1, double a2)
Definition: fl_draw.H:304
FL_CHART_ENTRY::col
unsigned col
Definition: Fl_Chart.H:46
FL_BORDER_BOX
see figure 1
Definition: Enumerations.H:619
draw_linechart
static void draw_linechart(int type, int x, int y, int w, int h, int numb, FL_CHART_ENTRY entries[], double min, double max, int autosize, int maxnumb, Fl_Color textcolor)
Definition: Fl_Chart.cxx:119
draw_barchart
static void draw_barchart(int x, int y, int w, int h, int numb, FL_CHART_ENTRY entries[], double min, double max, int autosize, int maxnumb, Fl_Color textcolor)
Definition: Fl_Chart.cxx:33
fl_push_clip
void fl_push_clip(int x, int y, int w, int h)
Definition: fl_draw.H:82
FL_CHART_ENTRY
Definition: Fl_Chart.H:44
Fl_Widget::redraw
void redraw()
Definition: Fl.cxx:1782
Fl_Widget
Definition: Fl_Widget.H:101
fl_end_polygon
void fl_end_polygon()
Definition: fl_draw.H:440
FL_ALIGN_BOTTOM
const Fl_Align FL_ALIGN_BOTTOM
Definition: Enumerations.H:835
Fl_Widget::box
Fl_Boxtype box() const
Definition: Fl_Widget.H:363
Fl_Widget::h
int h() const
Definition: Fl_Widget.H:299
FL_HORBAR_CHART
#define FL_HORBAR_CHART
Definition: Fl_Chart.H:31
color
void color(int n)
Definition: gl2opengl.h:29
FL_CHART_MAX
#define FL_CHART_MAX
Definition: Fl_Chart.H:40
fl_pop_clip
void fl_pop_clip()
Definition: fl_draw.H:103
x
int x
Definition: test.c:73
Fl_Chart::Fl_Chart
Fl_Chart(int X, int Y, int W, int H, const char *L=0)
Definition: Fl_Chart.cxx:285
FL_ALIGN_TOP
const Fl_Align FL_ALIGN_TOP
Definition: Enumerations.H:833
Fl_Chart::textfont
Fl_Font textfont() const
Definition: Fl_Chart.H:120
Fl_Chart::textcolor
Fl_Color textcolor() const
Definition: Fl_Chart.H:130
Fl_Chart::min
double min
Definition: Fl_Chart.H:77
Fl_Chart::textcolor_
Fl_Color textcolor_
Definition: Fl_Chart.H:81
FL_CHART_ENTRY::str
char str[18+1]
Definition: Fl_Chart.H:47
y
int y
Definition: test.c:74
calloc
voidp calloc()
Fl_Chart::textsize
Fl_Fontsize textsize() const
Definition: Fl_Chart.H:125
Fl_Widget::type
uchar type() const
Definition: Fl_Widget.H:274
FL_FILLED_CHART
#define FL_FILLED_CHART
Definition: Fl_Chart.H:38
Y
static int Y
Definition: Fl_Tooltip.cxx:76
flstring.h
FL_SPIKE_CHART
#define FL_SPIKE_CHART
Definition: Fl_Chart.H:34
fl_vertex
void fl_vertex(double x, double y)
Definition: fl_draw.H:379
FL_ALIGN_RIGHT
const Fl_Align FL_ALIGN_RIGHT
Definition: Enumerations.H:841
Fl_Chart::add
void add(double val, const char *str=0, unsigned col=0)
Definition: Fl_Chart.cxx:323
FL_BAR_CHART
#define FL_BAR_CHART
Definition: Fl_Chart.H:30
draw_horbarchart
static void draw_horbarchart(int x, int y, int w, int h, int numb, FL_CHART_ENTRY entries[], double min, double max, int autosize, int maxnumb, Fl_Color textcolor)
Definition: Fl_Chart.cxx:73
Fl_Chart::~Fl_Chart
~Fl_Chart()
Definition: Fl_Chart.cxx:303
Fl_Chart::autosize_
uchar autosize_
Definition: Fl_Chart.H:78
fl_rectbound
void fl_rectbound(int x, int y, int w, int h, Fl_Color color)
Definition: fl_boxtype.cxx:266
Fl_Chart::draw
void draw()
Definition: Fl_Chart.cxx:224
fl_polygon
void fl_polygon(int x, int y, int x1, int y1, int x2, int y2)
Definition: fl_draw.H:244
Fl_Chart.H
Fl_Chart::bounds
void bounds(double *a, double *b) const
Definition: Fl_Chart.H:101
FL_PIE_CHART
#define FL_PIE_CHART
Definition: Fl_Chart.H:35
Fl_Chart::insert
void insert(int ind, double val, const char *str=0, unsigned col=0)
Definition: Fl_Chart.cxx:353
Fl_Chart::entries
FL_CHART_ENTRY * entries
Definition: Fl_Chart.H:76