On 3/18/21 2:14 PM, sabbir shubho wrote:
I am trying to plot two or more function like first order equation , unit function etc.. in single plot using fltk library. I have already seen the draw an X example but it did not help me much.
Appreciate any help on how to plot function in fltk
Plotting a graph is just drawing points between the samples of
your function.
The "draw an X" example should show you all the FLTK related
code to do this.
Just add the loop needed to iterate through the values of
your function for each
data point, and draw a line from the last point to the current
point, scaling and
translating the raw data from your function to integer screen
coordinates within
the x/y/w/h rectangle, as shown by the "draw an X" code.
On 3/18/21 3:20 PM, sabbir shubho wrote:
I am trying to plot the sine wave function for example. In the loop, I save the value of the sine function in an array that is of type float. The draw x example shows how to draw a line between two points which are pixel value or absolute coordinate. I am looking to draw an axis on the screen and have my function plotted on the screen as normally we do in Matlab or python.
I am attaching a sample picture that I want to draw in fltk.
FLTK is a simple graphics toolkit providing bare bones drawing stuff, and doesn't often have high level tools like math plotting.
That said, there is a widget called Fl_Chart that can plot bargraphs and line graphs, but it's very rudimentary. I don't think FLTK has a good example showing its various uses.. it could use one. From what I can find, it supports at least these types of graphs.. not sure where the example code that made this screenshot though:
If I can find some time, I'll try to provide a variations of my cheat sheet's Fl_Graph example that shows how plot line graphs for data, too. It might be as easy as just changing Fl_Graph::type().
Short of that, you'd either have to draw all that yourself (the graticule markings, labels, text heading), or use a third party FLTK library that provides such high level graphing features.. just googling around, there seems to be Fl_ChartEx, Multiplot, ..and I imagine there's several others.
I think most people would simply do it themselves, as it's not that hard to draw lines and text in FLTK. And now that FLTK supports SVG, one should be able to plot really nice looking stuff with antialiased lines, filled gradients, and whatnot. Getting text mixed with SVG might be a little tricky though. An interesting FLTK widget to see would be Fl_Chart refitted to use SVG that handles all that.
I'd wait to see other folk's replies though, as there may be things I'm not aware of.. in my work I don't often find the need to graph things.
--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/06bbdc20-71f7-0216-9f52-2c1bd4e116c2%40seriss.com.
That said, there is a widget called Fl_Chart that can plot bargraphs and line graphs, but it's very rudimentary. I don't think FLTK has a good example showing its various uses.. it could use one.
[..]
If I can find some time, I'll try to provide a variations of my cheat sheet's Fl_Graph example that shows how plot line graphs for data, too. It might be as easy as just changing Fl_Graph::type().


As I said, pretty rudimentary; you'll surely want to draw your own, especially if you want an axis labels, tick marks, grids, legends, self-avoiding text, etc.
But sniff around on the web; I know there's math folks who've made FLTK graphing/plotting library tools.
I just went on the fltk.org "Links" page under the "Software -> Programming Tools" subsection, and found a few more plotting tools. Have to warn you, because it's so old, there's many links there that have long gone stale. In those days there weren't long term dumping grounds like github, and sourceforge was still new then. So if you see something that you like but the link is dead, try archive.org and sometimes you can find a tar ball.
For links that are still alive, here's one called OctPlot:
..and here's one called "Gluplot", which describes itself as a clone of the "Gnuplot" tool, but is a library too:
![]()
There are probably many others folks have written, try searching the forums for "graphing" and "plotting".
I have used Roman Kantor's Cartesian library.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/e08ddc0c-53ff-495d-bd1d-23fc0c853892n%40googlegroups.com.
Can you help me with how to add axis and scale in that graph?
Looks like you've got it drawing stuff, so that's good,
you're now used to how
to draw stuff in FLTK.
But adding tick marks and axes to your app the way it's
written now will be very
hard, because there's lots of magic numbers everywhere for
scale and offset,
and those magic numbers would have to be duplicated and
finagled around to
get the axes tick marks/labels to match up with your data.
I'd suggest doing a second pass, designing your GRAPH class to
be more
generalized, so that any number of arrays of data can be
passed in,
allowing each data array to be assigned colors, shapes for
data points,
and have it automatically calculate the min/max of the data on
entry
so that these values can be drawn along the axes, no matter
what the
range of values is.
There's a few things you can do to your code to make it
easier how to
draw the axes and tick marks.
First, start by naming all the visual elements in your graph,
e.g.

In general replace all the 'magic numbers' used throughout the
code with
the meaningful names you've chosen, and then use these names
throughout
your code, so the variables are consistent.
And choose names for non-visual elements too.
For instance an X and Y scale values would be useful, so you
can easily change
the drawing scale of the equation, and the tick marks adjust
accordingly.
The code that draws the equation has to use the same scale
that draws the
tick marks and axes, so use these values consistently. And
drawing the graph
needs to be within an area that allows for the tick marks and
axes to be drawn
around it. So you could call that region the 'margin', and the
data's xywh are
all drawn within that margin.
Then break up the drawing tasks into separate methods within
the class.
e.g. draw_data() to draw the actual arrays of data in their
chosen colors, and
e.g. draw_axes() to draw the axes and tick marks in the chosen
font/font size/colors.
Basically design the widget in such a way that it operates
how you'd want it to
work for any kind of data you want to throw at it. Maybe not
just sine waves,
but even random data from the rand() function.
Try to keep it generalized, and by doing that, it makes it
easier to keep the
tick marks in sync with the graph, and allows things to be
changed easily
(such as the scale, or colors, adding more data, etc)
For instance:
#define MY_RED 0xff0000
#define MY_BLUE 0x0000ff
#define SINE_SCALE 100
#define COS_SCALE 100
[..]
GRAPH->add_data(sine_array, MY_RED,
GRAPH_FILLED_CIRCLE, SINE_SCALE);
GRAPH->add_data(cosine_array, MY_BLUE,
GRAPH_FILLED_SQUARE, COS_SCALE);
With that, each call to add_data() just appends another array
to the class,
and calculates the min + max range of the data which will help
drawing the
tick marks.
And you'd probably want to separately control the overall
scale and number
of tick marks drawn along the X and Y axes separately. As you
might want
to scale the data down more so that it doesn't exactly draw to
the edges
of the graph, but leaves some extra white space.
So in short, break the visual problem up into parts and name
all the parts,
and in the code, break up drawing the elements into parts, and
name those
methods accordingly.
It's complex as a whole, but I think you'll find the large
problem breaks up
into simpler pieces that are each easy to program, and will
fit together nicely
when done, if you've designed well for it.
Another thing is try to design the app to be a normal widget,
so the app
can just call the normal application loop, instead of having
to use WAIT
and emulating the timing loop for drawing yourself.
If you want the data to draw up slowly, you can use an FLTK
timer to
step through drawing the data, so the widget redraws itself
each tick
of the timer, just drawing a little bit more from the array
each time.
On 4/8/21 4:18 PM, 'Daniel G.' via fltk.general wrote:
Just FYI, there is also the MathGL project, which implements an FLTK based widget to plot mathematical functions.
http://mathgl.sourceforge.net/doc_en/Main.html
Wow, that's pretty impressive.. lots of pics of various graph
screenshots here:
http://mathgl.sourceforge.net/doc_en/Pictures.html
..and in particular I suppose what the OP is looking for
specifically:
http://mathgl.sourceforge.net/doc_en/axis-sample.html#axis-sample
I started off (years ago) with the Cartesian library, but needed some of my own special stuff and found it hard to integrate it there (one of the new features is a possibility to zoom in). Inspired by It, I ended up rewriting my own from scratch. I happen to have taken it up again not long ago and am right in the middle of turning it into a library. Still work to do but it almost meets my own needs now. Screenshot attached. Anyone interested, drop me a note.