Thanks, Mr. Kantor.

I did use Cartesian as the example. But I still not clear about some part of 
your code. We decided to build our own using openGL to draw graphs, and fltk to 
draw x and y axises.

I tried couple things, but nothing seems to work. Here is the simpler version 
of my code.


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

class Axis;
class X_Axis;

static const int MAX_LABEL_FORMAT = 16;
static const int MAX_LABEL_LENGTH = 32;
enum Ca_Damage {CA_DAMAGE_ALL=1, CA_DAMAGE_ADD=2};
X_Axis *xAxis;

////////////////////////////////////////////////////////////////////////

class Axis: public Fl_Box
{
protected:
    double min;
    double max;
    int minPosition;
    int maxPosition;
        int tickLength;
    virtual int MinPosition()=0;
    virtual int MaxPosition()=0;

public:
    Axis(int x,int y,int w,int h,const char *l=0): Fl_Box(x,y,w,h,l)
    { tickLength = 5; }
    double Minimum() const { return min; }
    double Maximum() const { return max; }
    void Minimum(double minimum) {      min = minimum; }
    void Maximum(double maximum) {      max = maximum; }

    void Axis::FindScientificValue(double val, int *mantisa, int *exponent)
    {
        for (*mantisa=(int)val, *exponent=0; *mantisa > 1; *mantisa =  
*mantisa/10, *exponent += 1)
        {
            if (*mantisa < 10)
                break;
        }
    }
};

////////////////////////////////////////////////////////////////////////

class X_Axis: public Axis
{
protected:
public:
    int MinPosition() { return minPosition; }
    int MaxPosition() { return maxPosition; }
    void draw();

public:
    X_Axis(int x, int y, int w, int h, const char * label=0) : Axis(x, y, w, h, 
label)
        {
                minPosition = x;
                maxPosition = x + w;
        }

    ~X_Axis() {};
};

////////////////////////////////////////////////////////////////////////

void X_Axis::draw()
{
    int i, mantisa, exponent, x0, x1, y0, y1, width1, height1, width2, height2, 
j=-2, loop;
    char labelFormat[MAX_LABEL_FORMAT];
    char label1[MAX_LABEL_LENGTH];
    char label2[MAX_LABEL_LENGTH];
    float tickValue;

    //Set line color and style, font for label
    fl_color(0,0,0);
    fl_line_style(FL_SOLID);
    fl_font(0, 8);

    //position of y0 and y1
    y0 = y() + 1;
    y1 = y() + 1 + tickLength;

    j = -2;             //tick starts from 0.01, 0.1, 1, 10, 100, ....

    //tick at every 10^i with 0.01 <= i <= 10^exponent. max is in log value. 
max = mantisa*10^exponent
    FindScientificValue(pow(10,max), &mantisa, &exponent);

    if (mantisa == 1)
        tickValue = (float)1 / (exponent + 2) * w();
    else
        tickValue = 1 / ((float)mantisa / 10 + exponent + 2) * w();

    for (i=0; i <= exponent+2; i++)
    {
        x0 = minPosition + (int)(tickValue * i);                                
                                                                        x1 = 
minPosition + (int)(tickValue * i);
                fl_line(x0, y0, x1, y1);                                        
                                                //draw tick

                if (i < 2)                                                      
                                                                //format label 
0.01 and 0.1
                        sprintf(labelFormat,"%.2f",pow(10,j));
                else
                        sprintf(labelFormat,"%.0f",pow(10,j));                  
                                //format label 1, 10, 100, etc.

                sprintf(label1, labelFormat,pow(10,j));
                fl_measure(label1, width1, height1);

                //draw label at each tick
                if (i == 0)                                                     
                                                                //display label 
at first tick
                        fl_draw(label1, x0, y1 + tickLength + (height1 / 2));
                else if (i == exponent+2)                                       
                                                //display the end of the label 
at the tick
                        fl_draw(label1, x0-width1, y1 + tickLength + (height1 / 
2));
                else                                                            
                                                                //display 
middle length of the label at the tick
                        fl_draw(label1, x0 - (width1 / 2), y1 + tickLength + 
(height1 / 2));

                j++;
        }
}

///////////////////////////////////////////////////////////////////////////

void start_cb( Fl_Widget* o, void*)
{
        xAxis->Maximum(log10(1000));
        xAxis->Minimum(0.1);
        xAxis->redraw();
}

///////////////////////////////////////////////////////////////////////////

int main(int argc, char **argv)
{
        Fl_Double_Window window(1000,750, "Drawing Graphs");
        window.begin();

        xAxis = new X_Axis(643, 217, 348, 20,"");
        xAxis->Maximum(log10(600));
        xAxis->Minimum(0.01);

        Fl_Button* start = new Fl_Button(12,370+320+5, 60, 25, "&Start");
        start->callback( (Fl_Callback*) start_cb );
        start->labelsize(13);

        window.end();
        window.show(argc, argv);

        return Fl::run();
}




_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to