# Easy Chart Auto range margin crossing zero

**URL:** <https://forum.inductiveautomation.com/t/easy-chart-auto-range-margin-crossing-zero/98394>\
**Category:** Ignition\
**Tags:** ignition81, vision\
**Created:** [November 8, 2024, 3:45pm UTC](https://forum.inductiveautomation.com/t/easy-chart-auto-range-margin-crossing-zero/98394 "2024-11-08T15:45:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [November 8, 2024, 3:45pm UTC](https://forum.inductiveautomation.com/t/easy-chart-auto-range-margin-crossing-zero/98394/1 "2024-11-08T15:45:43Z")

</div>

Version 8.1.44 Vision

I have an easy chart that uses historical pens. I want the y axis to be auto ranging around 5%, but even when configuring this, if my minimum values are 0 or near 0 the 5% margin does not appear because it doesn't cross 0 to show negative range values.

I already have a custom method on my easy chart that determines min and max values over my historical range so I can easily figure out the lowest datapoint value.

Is there a way, from this custom method, that I can I disable auto range to set my lower bound and turn back on auto range?  
I can't seem to get a reference to the chart from the method so this fails:

```python
	# Set your start and end dates
	s = self.startDate
	e = self.endDate
	
	# Convert tagPens to a Python dataset
	newTagPens = self.tagPens
	tagPens = system.dataset.toPyDataSet(self.tagPens)
	
	# Loop through the tag pens and retrieve min, max values from the tag history
	for i in range(len(tagPens)):
	    pen = tagPens[i]
	    tagPath = pen["TAG_PATH"]
	    
	    minValue = None
	    maxValue = None
	    avgValue = None
	    
	    try:
	        historyValues = system.tag.queryTagHistory(
	            paths=[tagPath, tagPath, tagPath], 
	            startDate=s, 
	            endDate=e, 
	            returnSize=1, 
	            columnNames=['%sMin', '%sMax', '%sAvg'], 
	            aggregationModes=['Minimum', 'Maximum', 'Average']
	        )
	        if historyValues.getRowCount():
	            minValue = historyValues.getValueAt(0, 1)
	            maxValue = historyValues.getValueAt(0, 2)
	            avgValue = historyValues.getValueAt(0, 3)
	    except:
	        pass
	    
	    # Only adjust if minValue is valid and less than 5
	    if minValue is not None and minValue < 5:
	    	lowerBound = minValue - (maxValue - minValue) * 0.05
	    	chart = self
	    	chart.setAutoRange(False) # Temporarily disable auto-range to set custom bounds
	    	chart.getYAxes().get(0).setLowerBound(lowerBound) # Set lower bound for Y-axis
	    	chart.setAutoRange(True) # Re-enable auto-range for the upper bound

```

---

<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 8, 2024, 5:24pm UTC](https://forum.inductiveautomation.com/t/easy-chart-auto-range-margin-crossing-zero/98394/2 "2024-11-08T17:24:06Z")

</div>

Oy!

Doing a history query in a chart event? When the chart already has all the data? Ewww! (UI stalls much?)

I would recommend, in the `configureChart` method, replacing the Y axis (or axes) with a customized [ValueAxis](https://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/axis/ValueAxis.html) instance, that has the `getUpperBound()`, `getRange()`, and `getLowerBound()` overridden.

When constructing that instance, pass the plot object that applies, so that those functions can scan the plot's already-retrieved data to find the min or max (or both) you need at that moment.

This approach works even if the chart data is updated without the chart calling `configureChart` again, which commonly happens with Classic Charts, but ought to happen with EasyCharts, too (for performance).
