# Calculating Area under graph help

## Recommended Posts

Hi, i would like to display, as a variable, the area under a real time graph of Watts over a 24 Hour period. If thats the way you do it. (so i can have a record of total watts generated by my solar panel over a 24 hour period.

Then store this daily value and plot that value as a bar graph daily.

Any pointers

Thanks

##### Share on other sites

I've attached a sample that does this, however, I have it set for 1 minute sums instead of 1 day sums so you can see it working faster. All the work is done in the Event of the test channel (the input):

```if (test.Time % 60 &lt; test.Time % 60) // minute transition.  For daily, change all 60's to 86400
private endtime = floor(test.Time / 60) * 60
private total = sum(test[endtime,endtime-60]) * 60 // area is height (sum) * width (60 seconds)
total.time = test.Time
endif```

First, it looks to see if the minute turned over by taking the modulus (remainder) of the time of the last two points. If the remainder of the most recent point is less than the next most recent point, then it is the next minute. Change the 60 to 86400 for daily (or 3600 for hourly, etc).

If it is the turnover, we calc the time of the end of the minute by dividing the time by 60, throwing away the decimal, and multiplying by 60. Basically, rounding down to the minute.

Next, we calc the sum of test for the last minute by subsetting by time. You can subset by index (i.e. , ), or by the time of the data points. Here, since we don't really know exactly how many points make up the last minute (ok, its 60, but if you had comm failure or similar, it might be less), we let DAQFactory figure it out by subsetting by time. We then take the sum of that, which when multiplied by dt (60 seconds) gives us the area under graph for the last minute. Again, change all the 60's to 86400 for daily.

Next, we assign the time of the most recent data point to the time of our total.

Finally, we add it to our totals channel. The totals channel is just a test channel with timing of 0. So, we are actually never going to the "test" device, but instead, adding the value here from code. This allows us to take advantage of all the features of a channel. The one you'll definitely want to take advantage of, and I did not in this sample, is Persist. You should probably set the persist of the Totals channel to 3600 or something so that if you have to reboot your computer due to power failure or something, you will not lose the totals you accumulated.

On Page_0 of this sample, I have two graphs, one of Test (which I used a conversion to make into somewhat random data) and the other of Totals in bar graph form. Note that you have to play with the Bar Width parameter on the General page of the bar graph to get it to work right. I'm not sure how that number scales, but if the graph looks way off, with bars appearing off the grid, then this number is likely to be too big and you should start dividing it by 10 until it comes out right.

areacalcandgraph.ctl

##### Share on other sites

Wow Thanks for the speedy reply, i would never had got this in a million years, thanks again for all your support.

Andy

##### Share on other sites

Hi, thanks for the expression. I included it in my application however the results were not credable, I then set the test value at 5 so after 1 minute i should have a bar graph with a value of 30 but it was in the thousands. What i am effectifly trying to achive i a graphical Watt meter.

Thanks again

Andy

##### Share on other sites

I also think I forgot about the sample interval. If you have a reading of 5 and you read it once a second, the sum over the minute is going to be 300. If you have the same reading and you read it once every ten seconds, the sum will be 30. Since area is dxdt you have to multiply each interval by dt and sum it, so, technically, if you read every ten seconds, the sum is:

5*10 + 5*10 + 5*10 + 5*10 + 5*10 + 5*10

Since the interval is constant, you can pull the 10 out and just get:

(5+5+5+5+5+5) * 10

This will give you an area in units of watt seconds in a minute (assuming watts is your y axis).

So then there is a units issue. You have to decide your final units will be. I'm going to leave it to you to figure out how to make the conversion. Its basic dimensional analysis.

#### Archived

This topic is now archived and is closed to further replies.