1 2 static char help[] = "Plots a simple line graph.\n"; 3 4 #if defined(PETSC_APPLE_FRAMEWORK) 5 #import <PETSc/petscsys.h> 6 #import <PETSc/petscdraw.h> 7 #else 8 9 #include <petscsys.h> 10 #include <petscdraw.h> 11 #endif 12 13 int main(int argc, char **argv) { 14 PetscDraw draw; 15 PetscDrawLG lg; 16 PetscDrawAxis axis; 17 PetscInt n = 15, i, x = 0, y = 0, width = 400, height = 300, nports = 1; 18 PetscBool useports, flg; 19 const char *xlabel, *ylabel, *toplabel, *legend; 20 PetscReal xd, yd; 21 PetscDrawViewPorts *ports = NULL; 22 23 toplabel = "Top Label"; 24 xlabel = "X-axis Label"; 25 ylabel = "Y-axis Label"; 26 legend = "Legend"; 27 28 PetscFunctionBeginUser; 29 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 30 PetscCall(PetscOptionsGetInt(NULL, NULL, "-x", &x, NULL)); 31 PetscCall(PetscOptionsGetInt(NULL, NULL, "-y", &y, NULL)); 32 PetscCall(PetscOptionsGetInt(NULL, NULL, "-width", &width, NULL)); 33 PetscCall(PetscOptionsGetInt(NULL, NULL, "-height", &height, NULL)); 34 PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL)); 35 PetscCall(PetscOptionsGetInt(NULL, NULL, "-nports", &nports, &useports)); 36 PetscCall(PetscOptionsHasName(NULL, NULL, "-nolegend", &flg)); 37 if (flg) legend = NULL; 38 PetscCall(PetscOptionsHasName(NULL, NULL, "-notoplabel", &flg)); 39 if (flg) toplabel = NULL; 40 PetscCall(PetscOptionsHasName(NULL, NULL, "-noxlabel", &flg)); 41 if (flg) xlabel = NULL; 42 PetscCall(PetscOptionsHasName(NULL, NULL, "-noylabel", &flg)); 43 if (flg) ylabel = NULL; 44 PetscCall(PetscOptionsHasName(NULL, NULL, "-nolabels", &flg)); 45 if (flg) { 46 toplabel = NULL; 47 xlabel = NULL; 48 ylabel = NULL; 49 } 50 51 PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, 0, "Title", x, y, width, height, &draw)); 52 PetscCall(PetscDrawSetFromOptions(draw)); 53 if (useports) { 54 PetscCall(PetscDrawViewPortsCreate(draw, nports, &ports)); 55 PetscCall(PetscDrawViewPortsSet(ports, 0)); 56 } 57 PetscCall(PetscDrawLGCreate(draw, 1, &lg)); 58 PetscCall(PetscDrawLGSetUseMarkers(lg, PETSC_TRUE)); 59 PetscCall(PetscDrawLGGetAxis(lg, &axis)); 60 PetscCall(PetscDrawAxisSetColors(axis, PETSC_DRAW_BLACK, PETSC_DRAW_RED, PETSC_DRAW_BLUE)); 61 PetscCall(PetscDrawAxisSetLabels(axis, toplabel, xlabel, ylabel)); 62 PetscCall(PetscDrawLGSetLegend(lg, &legend)); 63 PetscCall(PetscDrawLGSetFromOptions(lg)); 64 65 for (i = 0; i <= n; i++) { 66 xd = (PetscReal)(i - 5); 67 yd = xd * xd; 68 PetscCall(PetscDrawLGAddPoint(lg, &xd, &yd)); 69 } 70 PetscCall(PetscDrawLGDraw(lg)); 71 PetscCall(PetscDrawLGSave(lg)); 72 73 PetscCall(PetscDrawViewPortsDestroy(ports)); 74 PetscCall(PetscDrawLGDestroy(&lg)); 75 PetscCall(PetscDrawDestroy(&draw)); 76 PetscCall(PetscFinalize()); 77 return 0; 78 } 79 80 /*TEST 81 82 build: 83 requires: x 84 85 test: 86 output_file: output/ex1_1.out 87 88 TEST*/ 89