Easy char X trace Decimal limit

Hi Guys,

image

In easy chart when i select x trace it showing it showing 3 4 digit after decimal.
but i want to show only 2 digit after decimal.
Is it any way to fix it?

If you look in the extension functions for the easy chart, there is one specifically for customizing the xTrace labels. You could use python to round the value to two decimal places.

1 Like

Here is code

def getXTraceLabel(self, chart, penName, yValue):
    # Format the yValue with 2 decimal places
    formatted_y_value = "{:.2f}".format(yValue)

    # Return the formatted value as the x-trace label
    return str(formatted_y_value)

Can just be this:

def getXTraceLabel(self, chart, penName, yValue):
    # Format the yValue with 2 decimal places
    return "{:.2f}".format(yValue)

No need for the string conversion, it’s already a string.

1 Like

one question date format

Is it correct

 date_obj = datetime.fromtimestamp(yValue)

    # Format the datetime object as a desired date format
    formatted_date = date_obj.strftime("%Y-%m-%d %H:%M:%S")  
return str(formatted_date)

I would use system.date.format()

1 Like

tried like this system.date.format(yValue, "yyyy-MM-dd HH:mm:ss")

but getting error

i only want to change the x trace date format

any code idea you have?

image

You can also limit your decimals to two places using this setting:
image

Yes ,thanks i want this to be implemented for normal xy chart also. that y

I have a solution to this question here:

2 Likes

For reference, your code in the configureChart extension function would look like this:

def configureChart(self, chart):
	from java.text import SimpleDateFormat
	from org.jfree.chart.axis import DateAxis
	dateFormat = SimpleDateFormat("yyyy-MM-dd")
	timeFormat = SimpleDateFormat("HH:mm:ss")
	dateAxis = DateAxis()
	plot = chart.getPlot()
	plot.setDomainAxis(dateAxis)
	plotDateField = plot.getClass().getDeclaredField('dateFormat')
	plotTimeField = plot.getClass().getDeclaredField('timeFormat')
	plotDateField.setAccessible(True)
	plotTimeField.setAccessible(True)
	plotDateField.set(plot,dateFormat)
	plotTimeField.set(plot,timeFormat)
	plotDateField.setAccessible(False)
	plotDateField.setAccessible(False)

Good work @justinedwards.jle , I would only say that I would set the Accessibility back to False after mucking about with reflection.

2 Likes

After using this script i am getting same space in both left and right side of chart

Not necessary. .setAccessible() doesn't change the class, just makes that reference to the field or method (the instance right there) ignore the private/protected/package visibility.

3 Likes

I had no such artifact in my testing, so I would assume there is something else going on. Also, your date didn't change and it definitely should have.

no i tired with test data also
same thing happening
image

see in x axis config also


give zero for lower and upper bound

when i disable the script it space is removing

Well, the script in the configureChart is overwriting the dateAxis with a brand new one, so any configuration you need to do will need to be done there.

So, change the script to this:

def configureChart(self, chart):
	from java.text import SimpleDateFormat
	from org.jfree.chart.axis import DateAxis
	dateFormat = SimpleDateFormat("yyyy-MM-dd")
	timeFormat = SimpleDateFormat("HH:mm:ss")
	dateAxis = DateAxis()
	dateAxis.lowerMargin = 0
	dateAxis.upperMargin = 0
	plot = chart.getPlot()
	plot.setDomainAxis(dateAxis)
	plotDateField = plot.getClass().getDeclaredField('dateFormat')
	plotTimeField = plot.getClass().getDeclaredField('timeFormat')
	plotDateField.setAccessible(True)
	plotTimeField.setAccessible(True)
	plotDateField.set(plot,dateFormat)
	plotTimeField.set(plot,timeFormat)
1 Like

Check your console for errors

yes getting this error
NameError: global name 'dateFormat' is not defined

Show your actual code. I suspect you have made a copy paste error.

correct copy paste issue. cleared now thanks

1 Like