Date: 2013-07-02T14:38:27+00:00

Actual source code: ex3.c
petsc-3.4.2 2013-07-02

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

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

      9: int main(int argc,char **argv)
     10: {
     11:   PetscDraw          draw;
     12:   PetscDrawLG        lg;
     13:   PetscDrawAxis      axis;
     14:   PetscInt           n = 20,i,x = 0,y = 0,width = 300,height = 300,nports = 1;
     15:   PetscBool          flg;
     16:   const char         *xlabel,*ylabel,*toplabel;
     17:   PetscReal          xd,yd;
     18:   PetscDrawViewPorts *ports;
     19:   PetscErrorCode     ierr;

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

     23:   PetscInitialize(&argc,&argv,(char*)0,help);
     24:   PetscOptionsGetInt(NULL,"-width",&width,NULL);
     25:   PetscOptionsGetInt(NULL,"-height",&height,NULL);
     26:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
     27:   PetscOptionsHasName(NULL,"-nolabels",&flg);
     28:   if (flg) {
     29:     xlabel = (char*)0; toplabel = (char*)0;
     30:   }
     31:   PetscDrawCreate(PETSC_COMM_SELF,0,"Title",x,y,width,height,&draw);
     32:   PetscDrawSetFromOptions(draw);
     33:   PetscOptionsGetInt(NULL,"-nports",&nports,NULL);
     34:   PetscDrawViewPortsCreate(draw,nports,&ports);
     35:   PetscDrawViewPortsSet(ports,0);

     37:   PetscDrawLGCreate(draw,1,&lg);
     38:   PetscDrawLGGetAxis(lg,&axis);
     39:   PetscDrawAxisSetColors(axis,PETSC_DRAW_BLACK,PETSC_DRAW_RED,PETSC_DRAW_BLUE);
     40:   PetscDrawAxisSetLabels(axis,toplabel,xlabel,ylabel);

     42:   for (i=0; i<n; i++) {
     43:     xd   = (PetscReal)(i - 5); yd = xd*xd;
     44:     PetscDrawLGAddPoint(lg,&xd,&yd);
     45:   }
     46:   PetscDrawLGIndicateDataPoints(lg);
     47:   PetscDrawLGDraw(lg);
     48:   PetscDrawString(draw,-3.,150.0,PETSC_DRAW_BLUE,"A legend");
     49:   PetscDrawFlush(draw);

     51:   PetscDrawViewPortsDestroy(ports);
     52:   PetscDrawLGDestroy(&lg);
     53:   PetscDrawDestroy(&draw);
     54:   PetscFinalize();
     55:   return 0;
     56: }