Updated Fl_Chart to allocate entries dynamically. The previous "scrolling"

behavior can be restored by calling Fl_Chart::maxsize(), otherwise entries
can be added until you run out of memory.


git-svn-id: file:///fltk/svn/fltk/trunk@262 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Michael R Sweet 1999-02-01 20:15:00 +00:00
parent 468391363b
commit 6882323172
3 changed files with 78 additions and 68 deletions

View File

@ -1,5 +1,5 @@
// //
// "$Id: Fl_Chart.H,v 1.4 1999/01/07 19:16:52 mike Exp $" // "$Id: Fl_Chart.H,v 1.5 1999/02/01 20:14:58 mike Exp $"
// //
// Forms chart header file for the Fast Light Tool Kit (FLTK). // Forms chart header file for the Fast Light Tool Kit (FLTK).
// //
@ -53,7 +53,8 @@ struct FL_CHART_ENTRY {
class Fl_Chart : public Fl_Widget { class Fl_Chart : public Fl_Widget {
int numb; int numb;
int maxnumb; int maxnumb;
FL_CHART_ENTRY entries[FL_CHART_MAX+1]; int sizenumb;
FL_CHART_ENTRY *entries;
double min,max; double min,max;
uchar autosize_; uchar autosize_;
uchar textfont_,textsize_,textcolor_; uchar textfont_,textsize_,textcolor_;
@ -83,5 +84,5 @@ public:
#endif #endif
// //
// End of "$Id: Fl_Chart.H,v 1.4 1999/01/07 19:16:52 mike Exp $". // End of "$Id: Fl_Chart.H,v 1.5 1999/02/01 20:14:58 mike Exp $".
// //

View File

@ -17,8 +17,7 @@
</PRE> </PRE>
</UL> </UL>
<H3>Description</H3> <H3>Description</H3>
This widget displays simple charts and is provided for forms This widget displays simple charts and is provided for Forms compatibility.
compatibility.
<H3>Methods</H3> <H3>Methods</H3>
<CENTER> <CENTER>
<TABLE width=90%> <TABLE width=90%>
@ -81,8 +80,9 @@ char *label = NULL, uchar color = 0)</A></H4>
pos</TT>. Position 0 is the first data value. pos</TT>. Position 0 is the first data value.
<H4><A name=Fl_Chart.maxsize>int maxsize(void) const <H4><A name=Fl_Chart.maxsize>int maxsize(void) const
<BR> void maxsize(int n)</A></H4> <BR> void maxsize(int n)</A></H4>
The <TT>maxsize</TT> method gets or sets the maximum number of data The <TT>maxsize</TT> method gets or sets the maximum number of data
values for a chart. values for a chart. If you do not call this method then the chart will
be allowed to grow to any size depending on available memory.
<H4><A name=Fl_Chart.replace>void replace(int pos, double value, const <H4><A name=Fl_Chart.replace>void replace(int pos, double value, const
char *label = NULL, uchar color = 0)</A></H4> char *label = NULL, uchar color = 0)</A></H4>
The <TT>replace</TT> method replaces data value <TT>pos</TT> with <TT> The <TT>replace</TT> method replaces data value <TT>pos</TT> with <TT>

View File

@ -1,5 +1,5 @@
// //
// "$Id: Fl_Chart.cxx,v 1.4 1999/01/07 19:17:17 mike Exp $" // "$Id: Fl_Chart.cxx,v 1.5 1999/02/01 20:15:00 mike Exp $"
// //
// Forms-compatible chart widget for the Fast Light Tool Kit (FLTK). // Forms-compatible chart widget for the Fast Light Tool Kit (FLTK).
// //
@ -28,6 +28,7 @@
#include <FL/Fl_Chart.H> #include <FL/Fl_Chart.H>
#include <FL/fl_draw.H> #include <FL/fl_draw.H>
#include <string.h> #include <string.h>
#include <stdlib.h>
#define ARCINC (2.0*M_PI/360.0) #define ARCINC (2.0*M_PI/360.0)
@ -272,28 +273,34 @@ void Fl_Chart::draw() {
Fl_Chart::Fl_Chart(int x,int y,int w,int h,const char *l) : Fl_Chart::Fl_Chart(int x,int y,int w,int h,const char *l) :
Fl_Widget(x,y,w,h,l) { Fl_Widget(x,y,w,h,l) {
box(FL_BORDER_BOX); box(FL_BORDER_BOX);
align(FL_ALIGN_BOTTOM); align(FL_ALIGN_BOTTOM);
numb = 0; numb = 0;
maxnumb = FL_CHART_MAX; maxnumb = 0;
autosize_ = 1; sizenumb = FL_CHART_MAX;
min = max = 0; autosize_ = 1;
textfont_ = FL_HELVETICA; min = max = 0;
textsize_ = 10; textfont_ = FL_HELVETICA;
textcolor_ = FL_BLACK; textsize_ = 10;
textcolor_ = FL_BLACK;
entries = (FL_CHART_ENTRY *)calloc(sizeof(FL_CHART_ENTRY), FL_CHART_MAX + 1);
} }
void Fl_Chart::clear() { void Fl_Chart::clear() {
numb = 0; numb = 0;
redraw(); redraw();
} }
void Fl_Chart::add(double val, const char *str, uchar col) { void Fl_Chart::add(double val, const char *str, uchar col) {
int i; /* Allocate more entries if required */
/* Shift entries if required */ if (numb >= sizenumb) {
if (numb >= maxnumb) { sizenumb += FL_CHART_MAX;
for (i=0; i<numb-1; i++) entries[i] = entries[i+1]; entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
numb--; }
// Shift entries as needed
if (numb >= maxnumb && maxnumb > 0) {
memcpy(entries, entries + 1, sizeof(FL_CHART_ENTRY) * (numb - 1));
numb --;
} }
entries[numb].val = float(val); entries[numb].val = float(val);
entries[numb].col = col; entries[numb].col = col;
@ -308,59 +315,61 @@ void Fl_Chart::add(double val, const char *str, uchar col) {
} }
void Fl_Chart::insert(int index, double val, const char *str, uchar col) { void Fl_Chart::insert(int index, double val, const char *str, uchar col) {
int i; int i;
if (index < 1 || index > numb+1) return; if (index < 1 || index > numb+1) return;
/* Shift entries */ /* Allocate more entries if required */
for (i=numb; i >= index; i--) entries[i] = entries[i-1]; if (numb >= sizenumb) {
if (numb < maxnumb) numb++; sizenumb += FL_CHART_MAX;
/* Fill in the new entry */ entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
entries[index-1].val = float(val); }
entries[index-1].col = col; // Shift entries as needed
if (str) { for (i=numb; i >= index; i--) entries[i] = entries[i-1];
strncpy(entries[index-1].str,str,FL_CHART_LABEL_MAX+1); if (numb < maxnumb || maxnumb == 0) numb++;
entries[index-1].str[FL_CHART_LABEL_MAX] = 0; /* Fill in the new entry */
} else { entries[index-1].val = float(val);
entries[index-1].str[0] = 0; entries[index-1].col = col;
} if (str) {
redraw(); strncpy(entries[index-1].str,str,FL_CHART_LABEL_MAX+1);
entries[index-1].str[FL_CHART_LABEL_MAX] = 0;
} else {
entries[index-1].str[0] = 0;
}
redraw();
} }
void Fl_Chart::replace(int index,double val, const char *str, uchar col) { void Fl_Chart::replace(int index,double val, const char *str, uchar col) {
if (index < 1 || index > numb) return; if (index < 1 || index > numb) return;
entries[index-1].val = float(val); entries[index-1].val = float(val);
entries[index-1].col = col; entries[index-1].col = col;
if (str) { if (str) {
strncpy(entries[index-1].str,str,FL_CHART_LABEL_MAX+1); strncpy(entries[index-1].str,str,FL_CHART_LABEL_MAX+1);
entries[index-1].str[FL_CHART_LABEL_MAX] = 0; entries[index-1].str[FL_CHART_LABEL_MAX] = 0;
} else { } else {
entries[index-1].str[0] = 0; entries[index-1].str[0] = 0;
} }
redraw(); redraw();
} }
void Fl_Chart::bounds(double min, double max) { void Fl_Chart::bounds(double min, double max) {
this->min = min; this->min = min;
this->max = max; this->max = max;
redraw(); redraw();
} }
void Fl_Chart::maxsize(int m) { void Fl_Chart::maxsize(int m) {
int i; int i;
/* Fill in the new number */ /* Fill in the new number */
if (m < 0) return; if (m < 0) return;
if (m > FL_CHART_MAX) maxnumb = m;
maxnumb = FL_CHART_MAX; /* Shift entries if required */
else if (numb > maxnumb) {
maxnumb = m; for (i = 0; i<maxnumb; i++)
/* Shift entries if required */ entries[i] = entries[i+numb-maxnumb];
if (numb > maxnumb) { numb = maxnumb;
for (i = 0; i<maxnumb; i++) redraw();
entries[i] = entries[i+numb-maxnumb]; }
numb = maxnumb;
redraw();
}
} }
// //
// End of "$Id: Fl_Chart.cxx,v 1.4 1999/01/07 19:17:17 mike Exp $". // End of "$Id: Fl_Chart.cxx,v 1.5 1999/02/01 20:15:00 mike Exp $".
// //