Actual source code: ex3.c
petsc-3.6.1 2015-08-06
2: static char help[] = "Plots a simple line graph.\n";
4: #if defined(PETSC_APPLE_FRAMEWORK)
5: #import <PETSc/petscsys.h>
6: #import <PETSc/petscdraw.h>
7: #else
8: #include <petscsys.h>
9: #include <petscdraw.h>
10: #endif
14: int main(int argc,char **argv)
15: {
16: PetscDraw draw;
17: PetscDrawLG lg;
18: PetscDrawAxis axis;
19: PetscInt n = 20,i,x = 0,y = 0,width = 300,height = 300,nports = 1;
20: PetscBool flg;
21: const char *xlabel,*ylabel,*toplabel;
22: PetscReal xd,yd;
23: PetscDrawViewPorts *ports;
24: PetscErrorCode ierr;
26: xlabel = "X-axis Label";toplabel = "Top Label";ylabel = "Y-axis Label";
28: PetscInitialize(&argc,&argv,(char*)0,help);
29: PetscOptionsGetInt(NULL,"-width",&width,NULL);
30: PetscOptionsGetInt(NULL,"-height",&height,NULL);
31: PetscOptionsGetInt(NULL,"-n",&n,NULL);
32: PetscOptionsHasName(NULL,"-nolabels",&flg);
33: if (flg) {
34: xlabel = (char*)0; toplabel = (char*)0;
35: }
36: PetscDrawCreate(PETSC_COMM_SELF,0,"Title",x,y,width,height,&draw);
37: PetscDrawSetFromOptions(draw);
38: PetscOptionsGetInt(NULL,"-nports",&nports,NULL);
39: PetscDrawViewPortsCreate(draw,nports,&ports);
40: PetscDrawViewPortsSet(ports,0);
42: PetscDrawLGCreate(draw,1,&lg);
43: PetscDrawLGGetAxis(lg,&axis);
44: PetscDrawAxisSetColors(axis,PETSC_DRAW_BLACK,PETSC_DRAW_RED,PETSC_DRAW_BLUE);
45: PetscDrawAxisSetLabels(axis,toplabel,xlabel,ylabel);
47: for (i=0; i<n; i++) {
48: xd = (PetscReal)(i - 5); yd = xd*xd;
49: PetscDrawLGAddPoint(lg,&xd,&yd);
50: }
51: PetscDrawLGSetUseMarkers(lg,PETSC_TRUE);
52: PetscDrawLGDraw(lg);
53: PetscDrawString(draw,-3.,150.0,PETSC_DRAW_BLUE,"A legend");
54: PetscDrawFlush(draw);
56: PetscDrawViewPortsDestroy(ports);
57: PetscDrawLGDestroy(&lg);
58: PetscDrawDestroy(&draw);
59: PetscFinalize();
60: return 0;
61: }