Plotlib

X Example

The following example is similar, but shows plotting within an application-created window:


#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <Xm/Xm.h>
#include <Xm/AtomMgr.h>
#include <Xm/DialogS.h>
#include <Xm/DrawingA.h>
#include <Xm/Protocols.h>
#include "plotlib.h"

extern void win_input_handler(Widget w, XtPointer client_data,
    XEvent *xevent, Boolean *boolean);
extern void destroy_window(Widget w, XtPointer client_data,
    XtPointer callback_data);

static int active_windows = 0;


main(int argc, char *argv[])
{
    XtAppContext app_context;
    Widget topLevel, dialog, canvas;
    PlotWindow *window;
    Atom atom;
    HistogramPlot *histogramplot;
    int i,j;
    static float data[10000];
    static float bins[21] = { 0 };

	/* data */

    for (i=0;i < 10000;i++) {
	float x1 = ((double)random() / INT_MAX) * 10;
	float x2 = ((double)random() / INT_MAX) * 10;
	data[i] = x1+x2;
	bins[(int)(x1+x2+0.5)]++;
    }

	/* init X */

    topLevel = XtVaAppInitialize(&app_context, "Plotlib", NULL, 0,
	&argc, argv, NULL, XmNmappedWhenManaged, False, NULL);

	/* create frame */

    dialog = XtVaCreatePopupShell("dialog",
        xmDialogShellWidgetClass, topLevel,
        XmNheight, 450, XmNwidth, 650,
        XmNtitle, "Plots",
        XmNiconName, "Plots",
        XmNdeleteResponse, XmDO_NOTHING,
        NULL);
    XtRealizeWidget(dialog);
    canvas = XtVaCreateManagedWidget("canvas",
	xmDrawingAreaWidgetClass, dialog,
	XmNwidth, 650, XmNheight, 450, 0);

	/* create plot window, describing graph */

    window = new PlotWindow(canvas);
    if (!window || window->failed) {
	if (!window)
	    (void)fprintf(stderr, "Insufficient memory\n");
	return 1;
    }
    window->set_autoscale();
    window->set_axes_color(BLACK);
    window->set_window_height_and_width(450,650);
    window->set_axes_labels("Sum","Count");
    window->set_title("Histogram Test");
    window->set_font_type("helvetica");
    window->finish();

    atom = XmInternAtom(XtDisplay(dialog), "WM_DELETE_WINDOW",
	False);
    XmAddWMProtocolCallback(dialog, atom, destroy_window,
	(XtPointer)window);
    XtAddEventHandler(canvas, ExposureMask | StructureNotifyMask,
        (Boolean)False, win_input_handler, (XtPointer)window);
    active_windows++;

	/* create plot */

    histogramplot = new HistogramPlot(window, bins, 21, RED, 0);
    if (!histogramplot || histogramplot->failed) {
	if (!histogramplot) (void)fprintf(stderr, "Insufficient memory\n");
	return 1;
    }

	/* create second plot */

    histogramplot = new HistogramPlot(window, data, 10000, BLACK, 50);
    if (!histogramplot || histogramplot->failed) {
	if (!histogramplot) (void)fprintf(stderr, "Insufficient memory\n");
	return 1;
    }

	/* enter X event loop */

    XtRealizeWidget(topLevel);
    XtAppMainLoop(app_context);
}


void win_input_handler(Widget, XtPointer client_data,
    XEvent *xevent, Boolean *)
{
    PlotWindow *window;

    window = (PlotWindow *)client_data;

    if (xevent->type == Expose)
        PlotWindow::handle_expose_event(window, xevent);
    else if (xevent->type == ConfigureNotify)
        PlotWindow::handle_resize_event(window, xevent);
}


void destroy_window(Widget w, XtPointer client_data, XtPointer)
{
    PlotWindow *window;

    window = (PlotWindow *)client_data;

    delete window;
    XtDestroyWidget(w);
    if (!--active_windows) exit(0);
}