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).
//
@ -53,7 +53,8 @@ struct FL_CHART_ENTRY {
class Fl_Chart : public Fl_Widget {
int numb;
int maxnumb;
FL_CHART_ENTRY entries[FL_CHART_MAX+1];
int sizenumb;
FL_CHART_ENTRY *entries;
double min,max;
uchar autosize_;
uchar textfont_,textsize_,textcolor_;
@ -83,5 +84,5 @@ public:
#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>
</UL>
<H3>Description</H3>
This widget displays simple charts and is provided for forms
compatibility.
This widget displays simple charts and is provided for Forms compatibility.
<H3>Methods</H3>
<CENTER>
<TABLE width=90%>
@ -81,8 +80,9 @@ char *label = NULL, uchar color = 0)</A></H4>
pos</TT>. Position 0 is the first data value.
<H4><A name=Fl_Chart.maxsize>int maxsize(void) const
<BR> void maxsize(int n)</A></H4>
The <TT>maxsize</TT> method gets or sets the maximum number of data
values for a chart.
The <TT>maxsize</TT> method gets or sets the maximum number of data
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
char *label = NULL, uchar color = 0)</A></H4>
The <TT>replace</TT> method replaces data value <TT>pos</TT> with <TT>
@ -116,4 +116,4 @@ proportionate slice in the circle.</DD>
</DL>
The second form of <TT>type()</TT> sets the chart type to <TT>t</TT>.
<CENTER><IMG src=./charts.gif width=80%></CENTER>
</BODY></HTML>
</BODY></HTML>

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).
//
@ -28,6 +28,7 @@
#include <FL/Fl_Chart.H>
#include <FL/fl_draw.H>
#include <string.h>
#include <stdlib.h>
#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_Widget(x,y,w,h,l) {
box(FL_BORDER_BOX);
align(FL_ALIGN_BOTTOM);
numb = 0;
maxnumb = FL_CHART_MAX;
autosize_ = 1;
min = max = 0;
textfont_ = FL_HELVETICA;
textsize_ = 10;
textcolor_ = FL_BLACK;
box(FL_BORDER_BOX);
align(FL_ALIGN_BOTTOM);
numb = 0;
maxnumb = 0;
sizenumb = FL_CHART_MAX;
autosize_ = 1;
min = max = 0;
textfont_ = FL_HELVETICA;
textsize_ = 10;
textcolor_ = FL_BLACK;
entries = (FL_CHART_ENTRY *)calloc(sizeof(FL_CHART_ENTRY), FL_CHART_MAX + 1);
}
void Fl_Chart::clear() {
numb = 0;
redraw();
numb = 0;
redraw();
}
void Fl_Chart::add(double val, const char *str, uchar col) {
int i;
/* Shift entries if required */
if (numb >= maxnumb) {
for (i=0; i<numb-1; i++) entries[i] = entries[i+1];
numb--;
/* Allocate more entries if required */
if (numb >= sizenumb) {
sizenumb += FL_CHART_MAX;
entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
}
// 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].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) {
int i;
if (index < 1 || index > numb+1) return;
/* Shift entries */
for (i=numb; i >= index; i--) entries[i] = entries[i-1];
if (numb < maxnumb) numb++;
/* Fill in the new entry */
entries[index-1].val = float(val);
entries[index-1].col = col;
if (str) {
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();
int i;
if (index < 1 || index > numb+1) return;
/* Allocate more entries if required */
if (numb >= sizenumb) {
sizenumb += FL_CHART_MAX;
entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
}
// Shift entries as needed
for (i=numb; i >= index; i--) entries[i] = entries[i-1];
if (numb < maxnumb || maxnumb == 0) numb++;
/* Fill in the new entry */
entries[index-1].val = float(val);
entries[index-1].col = col;
if (str) {
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) {
if (index < 1 || index > numb) return;
entries[index-1].val = float(val);
entries[index-1].col = col;
if (str) {
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();
if (index < 1 || index > numb) return;
entries[index-1].val = float(val);
entries[index-1].col = col;
if (str) {
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::bounds(double min, double max) {
this->min = min;
this->max = max;
redraw();
this->min = min;
this->max = max;
redraw();
}
void Fl_Chart::maxsize(int m) {
int i;
/* Fill in the new number */
if (m < 0) return;
if (m > FL_CHART_MAX)
maxnumb = FL_CHART_MAX;
else
maxnumb = m;
/* Shift entries if required */
if (numb > maxnumb) {
for (i = 0; i<maxnumb; i++)
entries[i] = entries[i+numb-maxnumb];
numb = maxnumb;
redraw();
}
int i;
/* Fill in the new number */
if (m < 0) return;
maxnumb = m;
/* Shift entries if required */
if (numb > maxnumb) {
for (i = 0; i<maxnumb; i++)
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 $".
//