Actual source code: ex12.c


  2: static char help[] = "Makes a simple bar graph.\n";

  4: #include <petscsys.h>
  5: #include <petscdraw.h>

  7: int main(int argc, char **argv)
  8: {
  9:   PetscDraw         draw;
 10:   PetscDrawBar      bar;
 11:   PetscDrawAxis     axis;
 12:   int               color = PETSC_DRAW_ROTATE;
 13:   const char       *xlabel, *ylabel, *toplabel;
 14:   const PetscReal   values[]  = {.3, .5, .05, .11};
 15:   const char *const labels[]  = {"A", "B", "C", "D", NULL};
 16:   PetscReal         limits[2] = {0, 0.55};
 17:   PetscInt          nlimits   = 2;
 18:   PetscBool         nolabels, setlimits;

 20:   xlabel   = "X-axis Label";
 21:   toplabel = "Top Label";
 22:   ylabel   = "Y-axis Label";

 24:   PetscFunctionBeginUser;
 25:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 26:   PetscCall(PetscOptionsHasName(NULL, NULL, "-nolabels", &nolabels));
 27:   if (nolabels) {
 28:     xlabel   = NULL;
 29:     ylabel   = NULL;
 30:     toplabel = NULL;
 31:   }
 32:   PetscCall(PetscOptionsGetRealArray(NULL, NULL, "-limits", limits, &nlimits, &setlimits));

 34:   PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "Title", PETSC_DECIDE, PETSC_DECIDE, 400, 300, &draw));
 35:   PetscCall(PetscDrawSetFromOptions(draw));
 36:   PetscCall(PetscDrawBarCreate(draw, &bar));

 38:   PetscCall(PetscDrawBarGetAxis(bar, &axis));
 39:   PetscCall(PetscDrawAxisSetColors(axis, PETSC_DRAW_BLACK, PETSC_DRAW_RED, PETSC_DRAW_BLUE));
 40:   PetscCall(PetscDrawAxisSetLabels(axis, toplabel, xlabel, ylabel));
 41:   PetscCall(PetscDrawBarSetColor(bar, color));
 42:   PetscCall(PetscDrawBarSetFromOptions(bar));

 44:   if (setlimits) PetscCall(PetscDrawBarSetLimits(bar, limits[0], limits[1]));
 45:   PetscCall(PetscDrawBarSetData(bar, 4, values, labels));
 46:   PetscCall(PetscDrawBarDraw(bar));
 47:   PetscCall(PetscDrawBarSave(bar));

 49:   PetscCall(PetscDrawBarDestroy(&bar));
 50:   PetscCall(PetscDrawDestroy(&draw));
 51:   PetscCall(PetscFinalize());
 52:   return 0;
 53: }

 55: /*TEST

 57:    build:
 58:      requires: x

 60:    test:
 61:      output_file: output/ex1_1.out

 63: TEST*/