Shading

Page Contents

Shading Under a Function

You can add shading to a function following drawFunction ( ), or to a step function, following a drawStep ( ). All you have to do is indicate the range that you want shaded:

   var wGraph = new mathyma.Graph(400,200);
   wGraph.setXrange(-3,3);
   wGraph.drawFunction("sin(Math.PI*x)");
   wGraph.addShade(-0.5,1.5);
   wGraph.printInWindow();

Shaded Step Function

The following slightly more complicated example demonstrates a number of features including how to draw shade under a step function. Notice how you can draw graphs of your own functions. Note also how the first step graph is named "wDraw1", this is a cDrawing object. This allows us to add shade and a label to it even after we have drawn the next two step functions.

   function binom(iVal, iNum, iProb) {
      var wRes = Math.pow(1 - iProb, iNum);
      for (var ix = 1; ix <= iVal; ix++) {
         wRes *= ((iNum - (ix - 1))/ix)*(iProb/(1 - iProb));
      }
      return wRes;
   }

   var wGraph = new mathymaGraph(380,300);
   wGraph.setYstretch(40);
   wGraph.setXrange(-0.5,20.5);
   wGraph.setXgrid(5);

   wDraw1 = wGraph.drawStep("binom(x+0.5,20,0.1)",1);

   wGraph.drawStep("binom(x+0.5,20,0.5)",1);
   wGraph.addShade(-0.5,20.5);
   wGraph.addLabel("b (x,20,0.5)",9,0.2);

   wGraph.drawStep("binom(x+0.5,20,0.9)",1);
   wGraph.addShade(-0.5,20.5);
   wGraph.addLabel("b (x,20,0.8)",15.5,0.3);

   wDraw1.addLabel("b (x,20,0.2)",0.5,0.3);
   wDraw1.addShade(-0.5,20.5);

   wGraph.printInWindow();