On 07/20/13 16:22, David Topham wrote:
> I did all that (I believe), but still the chart does not display.
The widget /is/ being displayed, but there's no data being
assigned to the chart because you appear to have implemented
the initialization code as a callback, instead of in main().
Not all widgets use callbacks.. callbacks are usually invoked
when someone interacts with the widget, like pushing a button
on a button widget, or typing text into a text widget.
Fl_Chart is not really a widget one interacts with.. it's a
display-only widget, so in your case it seems like you want
the chart data to be assigned to the widget as soon as the
widget is created.
Since you have the widget being created in main(), you'll
want to assign the data to the chart in main() as well.
I would suggest take out all the code you've put into the Callback:
window, and instead put it into a New -> Code -> Code window attached
to main(), so that it's assigned to the widget right after the widget
is created.
To do that:
1) Click on the chart widget, open the properties, and under C++ -> Name:
type 'mychart'
2) Click on 'main()' in fluid, and choose: New -> Code -> Code
3) In the code dialog window, paste the chart initialization code,
referring to the chart widget as 'mychart'
mychart->bounds(-125.0, 125.0);
for ( double t=0; t<15; t+=0.5 )
{
double val = sin(t) * 125.0;
static char val_str[20];
sprintf(val_str, "%.0lf", val);
mychart->add(val, val_str, (val<0)?FL_RED:FL_GREEN);
}
Here's the chart.fl file that does this:
------------------------------------------------------ chart.fl
# data file for the Fltk User Interface Designer (fluid)
version 1.0300
header_name {.h}
code_name {.cxx}
decl {\#include <FL/Fl_Chart.H>} {public global
}
decl {\#include <math.h>} {selected private local
}
Function {} {open
} {
Fl_Window {} {
label {Chart Demo}
xywh {623 21 355 275} type Double visible
} {
Fl_Box mychart {
label Chart
xywh {25 25 305 222} align 5
class Fl_Chart
}
}
code {mychart->bounds(-125.0, 125.0);
for ( double t=0; t<15; t+=0.5 )
{
double val = sin(t) * 125.0;
static char val_str[20];
sprintf(val_str, "%.0lf", val);
mychart->add(val, val_str, (val<0)?FL_RED:FL_GREEN);
}} {}
}
------------------------------------------------------ end
-- OR --
If you want, you can put the initialization code into a function instead,
and call the function within the chart's 'Extra Code'.
So /instead/ of doing the above, using New->Code->Code being part of main(),
you would undo that mod, and do this instead:
1) Click above main() (ie. on one of your #includes)
2) Create a new function called InitializeChart():
2a) Choose: New -> Function/Method
..and fill out the form:
Name(args): InitializeChart(Fl_Chart *chart)
Return Type: void
..so that the chart widget is passed in as an argument called 'chart'.
2b) Assign code to the function:
New -> Code -> Code
..and in the code window dialog, paste the following (slightly different) code:
chart->bounds(-125.0, 125.0);
for ( double t=0; t<15; t+=0.5 )
{
double val = sin(t) * 125.0;
static char val_str[20];
sprintf(val_str, "%.0lf", val);
chart->add(val, val_str, (val<0)?FL_RED:FL_GREEN);
}
Note here, I'm referring to the chart as the argument to the function,
not the 'mychart' global name you assigned to the chart. This allows the
function to work with /any/ chart, not just the global one.
3) Call this new function from your chart widget's Extra Code:
3a) Click on your 'Fl_Chart' widget in fluid
3b) Open "Properties"
3c) Under C++ -> Extra Code:, type:
InitializeChart(mychart);
Now when the chart widget is created, it calls your new function InitializeChart()
as part of the widget's initialization, passing the instance 'mychart' as the argument
to your function, and the function will fill that instance of the chart with the data.
Functionally the same as my first suggestion above, but a bit more flexible
because you can use that function on /any/ instance of an Fl_Chart, not just
the global 'mychart' instance. Also, this let's you call the initialization
from something like a button callback or timer.
Here's a copy of the above as an .fl file:
------------------------------------------------------ chart2.fl
# data file for the Fltk User Interface Designer (fluid)
version 1.0300
header_name {.h}
code_name {.cxx}
decl {\#include <FL/Fl_Chart.H>} {public global
}
decl {\#include <math.h>} {private local
}
Function {InitializeChart(Fl_Chart *chart)} {
comment {A FUNCTION TO INITIALIZE AN Fl_Chart} open
} {
code {chart->bounds(-125.0, 125.0);
for ( double t=0; t<15; t+=0.5 )
{
double val = sin(t) * 125.0;
static char val_str[20];
sprintf(val_str, "%.0lf", val);
chart->add(val, val_str, (val<0)?FL_RED:FL_GREEN);
}} {}
}
Function {} {open
} {
Fl_Window {} {
label {Chart Demo} open
xywh {623 21 355 275} type Double visible
} {
Fl_Box mychart {
label Chart selected
xywh {25 25 305 222} align 5
code0 {InitializeChart(mychart);}
class Fl_Chart
}
}
}
------------------------------------------------------ end