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