# 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:** 1\
**Showing post:** 8

<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)

```

---

_[View the full topic](https://forum.inductiveautomation.com/t/how-calculate-integral-of-history-tag/27741)._
