Formatting chart labels in reporting V7.9?

Hi all,

So, I will be the first to admit I know very little about java coding… I am trying to add formatted labels to a time series chart in a report, I found the code below to add the labels - which works - but I can’t figure out how to format them to one decimal place, they all show as 3 decimal places which I guess is the Java US standard… I know the format string can somehow be added to the parameters of the StandardXYItemLabelGenerator, but I can’t figure out how to do it… The help page from jFreechart assumes I know Java, so no help there. Can anyone help a brother out?

Code that adds the chart labels:
from org.jfree.chart.labels import StandardXYItemLabelGenerator
chart.getPlot().getRenderer().setItemLabelGenerator(StandardXYItemLabelGenerator())
chart.getPlot().getRenderer().setBaseItemLabelsVisible(True)

Thanks,
Dan

Sure thing.

The StandardXYItemLabelGenerator() has multiple constructors, that can be found here.

All you need to do is call the constructor that you need.

from org.jfree.chart.labels import StandardXYItemLabelGenerator
chart.getPlot().getRenderer().setItemLabelGenerator(StandardXYItemLabelGenerator('Your_Format_String'))
chart.getPlot().getRenderer().setBaseItemLabelsVisible(True)

Irose,

Thanks for the reply, but that is the problem - I can’t figure out how to add the format for one decimal place! I tried the code below, but it doesn’t work… What is the java format string for one decimal place?

from org.jfree.chart.labels import StandardXYItemLabelGenerator
chart.getPlot().getRenderer().setItemLabelGenerator(StandardXYItemLabelGenerator('#.#'))
chart.getPlot().getRenderer().setBaseItemLabelsVisible(True)

Okay, so it looks like you will need to also import the NumberFormat to do this. Give this a try.

from java.text import NumberFormat
from org.jfree.chart.labels import StandardXYItemLabelGenerator

#if you want the format to be different for the x and y values then you will need to define a yFormat as
#well.  When calling the constructor with 2 NumberFormat objets, the first one is for XValues and the
#second is for the YValues

xFormat = NumberFormat.getNumberInstance()
xFormat.setMaximumFractionDigits(1)

chart.getPlot().getRenderer().setItemLabelGenerator(StandardXYItemLabelGenerator('{0}',xFormat,xFormat))
chart.getPlot().getRenderer().setBaseItemLabelsVisible(True)

Irose,

I appreciate the help! So, when I added the formatting per your recommendation, the data labels are now all the name of the data series instead of the values…?

Chart Image with no formatting specified:
image

Chart image using the formats ( '{0}',xFormat,xFormat):
image

Any ideas?

Thanks in advance!
Dan

Yeah,

So I was only providing an example, to show a simple solution for what it would look like. Probably should have just not been lazy and actually included a detailed explanation as well. :sweat_smile:

So for this generator, the MessageFormat ArgumentIndex values correspond to the following:
0 = series name
1 = domain or X-Axis Value
2 = range or Y-Axis Value

The number between the ‘{}’ tells the generator which value/s to use in the label output.

You can include text that will be the same for every value as well, so your formatString could be:
“The {0} for {1} was {2}”. This would print out something like, The Attainment for Sep-2018 was 58.2. Assuming that you used only the formatString in the constructor.

Now obviously that’s not going to look very good on a chart but I wanted to include an example, because this would also be used if you want to include something like engineering units.

In your case it appears that you want to just show the range value so I would recommend the following code:

from java.text import NumberFormat
from org.jfree.chart.labels import StandardXYItemLabelGenerator
yFormat = NumberFormat.getNumberInstance()
yFormat.setMaximumFractionDigits(1)

#Grabbing the current DateFormat for the Domain Axis for continuities sake.
xFormat = chart.getPlot().getRenderer().getSeriesItemLabelGenerator(0).getXDateFormat()

chart.getPlot().getRenderer().setItemLabelGenerator(StandardXYItemLabelGenerator('{2}',xFormat,yFormat))
chart.getPlot().getRenderer().setBaseItemLabelsVisible(True)

Hopefully that should get you where you want to be.

If you want to learn more about how the MessageFormat works that can be found here.

It should be noted that while, the domain and range axis in Ignition are the x and y axis respectively, and this is generally what you will find in use for most jfree chart implementations outside of ignition, it is possible to change the orientation of the plot (i.e. define which of the axes are the range axis).

2 Likes

Irose,

Perfect, thank you for the assistance!

I'm facing something similar. I want to format the domain instead. My domain is displaying miliseconds (as shown below), and I want to remove that. Any help is more than welcome.

image