KGJS includes many economics-specific features, including linear supply, linear demand, and equilbrium. Let’s take a look at how to generate a linear demand curve, which is similar to a line:
Note that naming the EconLinearDemand
curve is optional, but will be helpful later on when designating multiple equilibria. The demand curve here is defined by an x-intercept and an invSlope (the same as the slope, but different notation). Similarly, the linear supply curve should be defined instead using the y-intercept and slope:
Let’s combine the demand and supply curves into an equilibrium object:
As you can see, the EconLinearEquilibrium
object automatically calculates an equilibrium point when you add the attribute equilibrium
. We can label the droplines however we wish; here, we’ve chosen label as Q0 and P0.
Of course, we will often want to see what happens when we shift supply or demand and how the new equilibrium is achieved. Let’s see how to do this with a change in demand:
Notice above that we add the params.demandShift
to the x-intercept of demand, but we subtract the params.supplyShift
from the y-intercept of supply. This is because as we drag the supply curve to the right, we actually want the y-intercept to decrease.
We could also add a feature to show consumer and producer surplus on an equilibrium graph like this. By default, the color of the surplus will be the color of the curve it is associated with (blue for demand, orange for supply) but you can use the fill
command to change this, as we do for producer surplus below:
We can add several layers of complexity to the graphs above to illustrate the properties of supply, demand, and equilibrium. Play with the graph below to see some of these features in action:
Some important things to notice about the graph above:
EconLinearEquilibrium
objects, one to represent the original equilibrium and one to represent the new equilibrium. Before the user has shifted the supply or demand curves, we ‘hide’ the new equilibrium point with the show: (calcs.demandChanged || calcs.supplyChanged)
line. This tells the equilibrium point to only show up on the graph when the supply or demand has been changed. Check out the calcs to see how this works mechanically. We’ve squared the terms so that the shift is always positive, and made it >.04 so that the two points don’t have to be exactly equilvalent to show up as such.a
and with different values. We created these points so that we could generate the arrows pointing from the original demand/supply to the new demand/supply. We have arbitrarily selected the y-values 4 and 8 for the demand and supply, respectively, where the arrows will be show up. The arrows begin at the a-coordinates of the old demand and end at the a-coordinates of the new demand, and same for supply. Also note that the trim
attribute of the arrow tells the arrow to stop slightly short of the line itself so as not to overlap with the line. Trim should generally be about 0.1, and always less than 0.5.