# Chart Help

I have posted about this several times. This link and context is the closest thing that I can find that might help me achieve this result. What I need to do is this:

I would like to be able to calculate the integral or ‘area under the curve’ for a given tags history on a chart or easy chart.
This is the clearest that I can state “what I need to achieve”
Below is the closest solution I could find. I need the example to work in ignition or to be able to accomplish the same result in ignition.

matplotlib.org/examples/showcase … _demo.html

“”"
Plot demonstrating the integral as the area under a curve.

Although this is a simple example, it demonstrates some important tweaks:

* A simple line plot with custom color and line width.
* A shaded region created using a Polygon patch.
* A text label with mathtext rendering.
* figtext calls to label the x- and y-axes.
* Use of axis spines to hide the top and right spines.
* Custom tick placement and labels.


“”"
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def func(x):
return (x - 3) * (x - 5) * (x - 7) + 85

a, b = 2, 9 # integral limits
x = np.linspace(0, 10)
y = func(x)

fig, ax = plt.subplots()
plt.plot(x, y, ‘r’, linewidth=2)
plt.ylim(ymin=0)

ix = np.linspace(a, b)
iy = func(ix)
verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]
poly = Polygon(verts, facecolor=‘0.9’, edgecolor=‘0.5’)

plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
horizontalalignment=‘center’, fontsize=20)

plt.figtext(0.9, 0.05, ‘$x$’)
plt.figtext(0.1, 0.9, ‘$y$’)

ax.spines[‘right’].set_visible(False)
ax.spines[‘top’].set_visible(False)
ax.xaxis.set_ticks_position(‘bottom’)

ax.set_xticks((a, b))
ax.set_xticklabels((’$a$’, ‘$b$’))
ax.set_yticks([])

plt.show()

Out of this large community and skilled experts at IA I know this is achievable.

Time for a paradigm shift. Dah-da-da-daahh! (Imagine Jeff Dunham’s “Melvin, the Superhero Guy”)

First, a window to play with.
Integral_2014-11-07_1314.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 approximation to a curve it will be, but it’s still a bunch of straight lines.

Here’s a random dataset:

And the area we want to find:

Now, let’s zoom in on a section. If you break each area between points, you can see that each one is a trapezoid. So, adding up all the trapezoids will give the area under the “curve”

Here’s the script placed inside the Property change event in Table 1. (Not optimized for anything except illustrating the process )

dataIn=system.dataset.toPyDataSet(event.source.parent.getComponent('Table').data)
data=[]
x=0
for row in dataIn:
#  print x, row[2]
x+=1
data.append([x,row[2]])
event.source.parent.getComponent('Table 1').data=system.dataset.toDataSet(headers,data) # The Visually friendly Table...

runningSum=0
for point in range(len(data)-1):
b1 = float(data[point][1])
b2 = float(data[point+1][1])
h = float(data[point+1][0])-float(data[point][0])
sectionArea=(b1+b2)/2*h
runningSum+=sectionArea
print b1,b2,sectionArea, runningSum
event.source.parent.getComponent('Numeric Label').value=runningSum


SerializationException: Error deserializing element “”

Ignition v7.6.4 (b2013112117)
Java: Oracle Corporation 1.7.0_67

Unable to open window. I tried to open after import. Maybe its my version? Looks good on the forum…

Maybe it’s because the Ignition version. JordanCClark used 7.7.1 to make the proj file. I think that you can’t import this file in 7.6.4. Moreover, the error seems to be the invokeLater option added in 7.7.x on event’s scripts.

If that’s the case I can try it at home on the playground. I have not updated my production system yet.
That Java dependency has made other admins leery of updating recently. Java does not have a good reputation within my company.
It has been a very difficult struggle to get buy in for Ignition because of this.
Anyway thanks for the window to play with.Jordan!!

Sorry, Tim. I’d been on 7.2.x for so long that I forget that my Windows aren’t necessarily universal anymore!

Here’s one that should work a little better for you.
Integral_7.6.6_2014-11-10_1312.proj (38.3 KB)

1 Like

It sure does work. Its going to take me a while to study it so I can figure out how to use it with actual tag history. Thanks Jordan. Wish I had a small percentage of your skill.

1 Like

Is there an updated version of the sample project? I need to integrate an area under a curve on an easy chart. Thanks!

test_Integral_2019-01-16_1543.proj (28.8 KB)

2 Likes