# How calculate integral of history tag

**URL:** <https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741>\
**Category:** Ignition\
**Created:** [August 24, 2019, 7:16am UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741 "2019-08-24T07:16:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris.roys](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@chris.roys](https://forum.inductiveautomation.com/u/chris.roys)\
**Post date:** [August 24, 2019, 7:16am UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/1 "2019-08-24T07:16:17Z")

</div>

Hello  
I want to have integral of a history tag in selected range. The history only log data when I have change in value so multiply it by time not valid option here.  
appreciate for any idea.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 24, 2019, 11:24am UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/2 "2019-08-24T11:24:38Z")

</div>

Hi Chris,

Did you see this topic? (Note: The link in that post is dead. Use the one in the bottom post.)

> [@Chart Help](http://forum.inductiveautomation.com/t/chart-help/9303/2):
>
> Time for a paradigm shift. Dah-da-da-daahh! (Imagine Jeff Dunham’s “Melvin, the Superhero Guy”) wink First, a window to play with. [Integral\_2014-11-07\_1314.proj](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/ac01bdd388a88ed210b3220d8491a9f97cf49c59.proj) (41.8 KB) Ok, the best you’ll be able to do is an approximation. The only thing we have are a set of discrete data points. Strictly speaking, we have no idea what is happening between the points. Graphing these points will give us a straight line between each one. The more samples you have the finer the details, and the closer the a…

---

<div class="post-metadata">

**Author:** ![Shane\_Lamp](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Shane\_Lamp](https://forum.inductiveautomation.com/u/Shane_Lamp)\
**Post date:** [November 1, 2023, 7:31pm UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/3 "2023-11-01T19:31:03Z")

</div>

Jordan,  
I am new to Ignition. I am trying to use your [Integral\_2014-11-07\_1314.proj] for a current project I am into. I am collecting current reading throughout the plant and need to present kw-hr. I do not have kwhr meters. I am taking 4-20mA into input modules and then calculating kw and presenting this over time. I need a simple code to get me kw-hr. Basically just need area under the curve of plotted data from a table. So how I designed this is : I select start time and end time and plot the KW over time. How can I simply write the expression to give me kw-hr this. I cant seem to get your example to work for me; it is very impressive what you did there!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 2, 2023, 9:52am UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/4 "2023-11-02T09:52:35Z")

</div>

Do you have a sample dataset I can play with?

---

<div class="post-metadata">

**Author:** ![Shane\_Lamp](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Shane\_Lamp](https://forum.inductiveautomation.com/u/Shane_Lamp)\
**Post date:** [November 2, 2023, 11:42am UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/5 "2023-11-02T11:42:29Z")

</div>

Yes sir,

You can see window (phase 2 total example). I would like to output area under the curve for selected start and stop times of data table from chart.

![image001.jpg](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/2/923a9ac9527ff419a73d6ca60dcd9a6293904dca.jpeg)

[Dat8017\_test\_2023-11-02\_0737.zip](https://forum.inductiveautomation.com/uploads/short-url/jqjqmkM1V2Mc4PUP5YHNSw6dCsA.zip) (218 KB)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 2, 2023, 1:20pm UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/6 "2023-11-02T13:20:42Z")

</div>

The easy chart doesn't cache the data, so it's blank for me. Use the 'Save Button' on the chart to export the data. 🙂

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/c/8cecd6cd2b2d2155a9b91c5ae45f09ce68604276.png)

---

<div class="post-metadata">

**Author:** ![Shane\_Lamp](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Shane\_Lamp](https://forum.inductiveautomation.com/u/Shane_Lamp)\
**Post date:** [November 2, 2023, 2:42pm UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/7 "2023-11-02T14:42:59Z")

</div>

Hope this works, thanks

![image001.jpg](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/2/923a9ac9527ff419a73d6ca60dcd9a6293904dca.jpeg)

[Dat8017\_test\_2023-11-02\_1041.zip](https://forum.inductiveautomation.com/uploads/short-url/1UI7eu4BEuUcKCt9Q0f79tivHsA.zip) (224 KB)

[Chart.xlsx](https://forum.inductiveautomation.com/uploads/short-url/lQNXcpc3qwFxk3ri04oynADGQ7F.xlsx) (10.5 KB)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 2, 2023, 8:46pm UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/8 "2023-11-02T20:46:07Z")

</div>

With the help of one of [@pturmel's scripts](https://forum.inductiveautomation.com/t/easy-chart-accessing-the-datasets-being-rendered/8709/9) to find the underlying chart, something like this in the propertyChange script of the Easy Chart.

For testing only. Break some of this out into project scripts for use in production. 😉

```python
def findJFChart(src): 
    """IA's EasyChart and XYPlot components have their JFreeChart objects at different positions in the Container/Component hierarchy. 
    This helper function scans the hierarchy, given a container or component as a starting point, and returns a tuple containing 
    the parent JFreeChartPanel and the targeted JFreeChart. Note: The hierarchy is searched depth-first! """ 
    try: 
        return (src, src.chart) 
    except: 
        pass 
    try: 
        for x in range(src.componentCount): 
            try: 
                return findJFChart(src.getComponent(x)) 
            except: pass 
    except: 
        pass 
        raise ValueError, "Unable to Find a JFreeChart Instance"
        
if event.propertyName == 'startDate' or event.propertyName == 'endDate':
	component = event.source
	
	src, chart = findJFChart(component)
	
	dataIn = system.dataset.toPyDataSet(chart.getXYPlot().dataset.getDataSetForSeries(0))
	startTime = dataIn[0][0]
	kWseconds = 0.0
	for prevRow, row in zip(dataIn, dataIn[1:]):
		if row[1] == 0:
			break
		b1 = prevRow[1]
		b2 = row[1]
		h = system.date.secondsBetween(prevRow[0], row[0])
		kWseconds += (b1+b2)/2*h	
	totalSeconds = system.date.secondsBetween(startTime, prevRow[0])
	kWh = kWseconds / totalSeconds
	
	# Write value to some other component
	#event.source.parent.getComponent('Label').text = str(kWh)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 2, 2023, 9:22pm UTC](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741/9 "2023-11-02T21:22:02Z")

</div>

> [@JordanCClark](#):
>
> to find the underlying chart

Heh. That one goes back to the FactoryPMI days. (:
