1π
β
In your graph, each part is a trapezium with height = 1.
You just need to loop through the graphValue
array and calculate the area of each trapezium and add it to the total area under curve.
Formula for area of trapezium is:
Area =
0.5 * (a + b) * h
wherea
&b
are two parallel sides of the trapezium andh
is the distance between them.
The code below can solve your problem.
var graphValue = [98,12,19,64,85,91,56,181,171,90];
var area = 0; // Initially set area to zero
var height = 1; // distance between parallel sides of the trapezium
for(var i=1; i<graphValue.length; i++) {
area += 0.5 * (graphValue[i] + graphValue[i-1]) * height;
// here a = graphValue[i]
// b = graphValue[i-1]
}
console.log(area); // 773 in this example
Source:stackexchange.com