xref: /petsc/src/sys/classes/draw/tests/ex9.c (revision a32e9c995d3c9cc14233efbb30d372fdb63ce962)
1 
2 static char help[] = "Makes a simple histogram.\n";
3 
4 #include <petscsys.h>
5 #include <petscdraw.h>
6 
7 int main(int argc, char **argv)
8 {
9   PetscDraw     draw;
10   PetscDrawHG   hist;
11   PetscDrawAxis axis;
12   int           n = 20, i, x = 0, y = 0, width = 400, height = 300, bins = 8;
13   PetscInt      w = 400, h = 300, nn = 20, b = 8, c = PETSC_DRAW_GREEN;
14   int           color = PETSC_DRAW_GREEN;
15   const char   *xlabel, *ylabel, *toplabel;
16   PetscReal     xd;
17   PetscBool     flg;
18 
19   xlabel   = "X-axis Label";
20   toplabel = "Top Label";
21   ylabel   = "Y-axis Label";
22 
23   PetscFunctionBeginUser;
24   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
25   PetscCall(PetscOptionsGetInt(NULL, NULL, "-width", &w, NULL));
26   PetscCall(PetscOptionsGetInt(NULL, NULL, "-height", &h, NULL));
27   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &nn, NULL));
28   PetscCall(PetscOptionsGetInt(NULL, NULL, "-bins", &b, NULL));
29   PetscCall(PetscOptionsGetInt(NULL, NULL, "-color", &c, NULL));
30   PetscCall(PetscOptionsHasName(NULL, NULL, "-nolabels", &flg));
31   width  = (int)w;
32   height = (int)h;
33   n      = (int)nn;
34   bins   = (int)b;
35   color  = (int)c;
36   if (flg) {
37     xlabel   = NULL;
38     ylabel   = NULL;
39     toplabel = NULL;
40   }
41 
42   PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, 0, "Title", x, y, width, height, &draw));
43   PetscCall(PetscDrawSetFromOptions(draw));
44   PetscCall(PetscDrawHGCreate(draw, bins, &hist));
45   PetscCall(PetscDrawHGSetColor(hist, color));
46   PetscCall(PetscDrawHGGetAxis(hist, &axis));
47   PetscCall(PetscDrawAxisSetColors(axis, PETSC_DRAW_BLACK, PETSC_DRAW_RED, PETSC_DRAW_BLUE));
48   PetscCall(PetscDrawAxisSetLabels(axis, toplabel, xlabel, ylabel));
49   /* PetscCall(PetscDrawHGSetFromOptions(hist)); */
50 
51   for (i = 0; i < n; i++) {
52     xd = (PetscReal)(i - 5);
53     PetscCall(PetscDrawHGAddValue(hist, xd * xd));
54   }
55   PetscCall(PetscDrawHGDraw(hist));
56   PetscCall(PetscDrawHGSave(hist));
57 
58   PetscCall(PetscDrawHGDestroy(&hist));
59   PetscCall(PetscDrawDestroy(&draw));
60   PetscCall(PetscFinalize());
61   return 0;
62 }
63 
64 /*TEST
65 
66    build:
67      requires: x
68 
69    test:
70      output_file: output/ex1_1.out
71 
72 TEST*/
73