How calculate integral of history tag

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.

Hi Chris,

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

2 Likes

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!

Do you have a sample dataset I can play with?

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

Dat8017_test_2023-11-02_0737.zip (218 KB)

1 Like

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. :slight_smile:

image

Hope this works, thanks

image001.jpg

Dat8017_test_2023-11-02_1041.zip (224 KB)

Chart.xlsx (10.5 KB)

With the help of one of @pturmel's scripts 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. :wink:

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

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

1 Like