summaryrefslogtreecommitdiff
path: root/examples/chart-simple.cxx
blob: 6898ce0054c3f280076c27c407216dcb59083b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Demonstrate how to use Fl_Chart in fltk
//
//     Demonstrates rudimentary features of Fl_Chart.
//     Origin: http://seriss.com/people/erco/fltk/#Fl_Chart
//
// Copyright 2008 by Greg Ercolano.
// Copyright 1998-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Chart.H>
#include <FL/Fl_Choice.H>
#include <math.h>

// Globals
Fl_Window *G_win = 0;
Fl_Chart  *G_chart = 0;
Fl_Choice *G_choice = 0;

// Fl_Choice callback for changing chart type()
static void chart_type_cb(Fl_Widget *w, void*) {
    const Fl_Menu_Item *item = G_choice->mvalue();  // item picked
    G_chart->type( (uchar)item->argument() );       // apply change
    G_chart->redraw();
    // printf("Choice: '%s', argument=%ld\n", G_choice->text(), item->argument());
}

// main
int main() {
    G_win = new Fl_Window(1000, 510, "Chart Simple");
    // Fl_Chart with a sin() wave of data
    G_chart = new Fl_Chart(20, 20, G_win->w()-40, G_win->h()-80, "Chart");
    G_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);
        G_chart->add(val, val_str, (val<0)?FL_RED:FL_GREEN);
    }
    // Let user change chart type
    G_choice = new Fl_Choice(140,470,200,25,"Chart Type: ");
    G_choice->add("FL_BAR_CHART",        0, chart_type_cb, (void*)FL_BAR_CHART );
    G_choice->add("FL_HORBAR_CHART",     0, chart_type_cb, (void*)FL_HORBAR_CHART);
    G_choice->add("FL_LINE_CHART",       0, chart_type_cb, (void*)FL_LINE_CHART);
    G_choice->add("FL_FILL_CHART",       0, chart_type_cb, (void*)FL_FILL_CHART);
    G_choice->add("FL_SPIKE_CHART",      0, chart_type_cb, (void*)FL_SPIKE_CHART);
    G_choice->add("FL_PIE_CHART",        0, chart_type_cb, (void*)FL_PIE_CHART);
    G_choice->add("FL_SPECIALPIE_CHART", 0, chart_type_cb, (void*)FL_SPECIALPIE_CHART);
    G_choice->value(0);
    G_win->resizable(G_win);
    G_win->show();
    return(Fl::run());
}