In a report I am building, I am using a timeseries chart and trying to update the label that is used in the legend.
I am doing this via script because the data that the timeseries chart uses is dynamic, I am basing my solution on this code from another forum post here
Here is the code I am using to attempt this:
# Taken from https://forum.inductiveautomation.com/t/dynamic-report-chart-legend-not-hiding-unused-pens/44565/2
from org.jfree.chart.renderer.xy import XYLineAndShapeRenderer
from org.jfree.chart import LegendItem
plot = chart.getPlot()
renderer = plot.getRenderer()
legend = chart.getLegend()
legenditems = plot.getLegendItems()
dataset = plot.getDataset()
xlist = range(dataset.getItemCount(0))
includedLabels=[]
for col in range(dataset.getSeriesCount()):
ylist = [dataset.getY(col, row) for row in range(dataset.getItemCount(col))]
try:
if not all(x is None for x in ylist):
label = legenditems.get(col).getLabel()
for tank in data['cs_enabledTanks']:
if label == tank['name']:
label = tank
includedLabels.append(label)
except TypeError:
pass
class CustomRenderer(XYLineAndShapeRenderer):
def getLegendItem(self, dataset, series):
original = renderer.getLegendItem(dataset, series)
new = LegendItem(
original.getLabel(),
original.getDescription(),
original.getToolTipText(),
"", #urlText
original.getShape(),
original.getLinePaint(), #set fill paint to line paint
original.getOutlineStroke(),
original.getOutlinePaint()
)
new.setSeriesIndex(series)
new.setDatasetIndex(dataset)
return new
def getLegendItems(self):
collection = XYLineAndShapeRenderer.getLegendItems(self)
for ds in range(plot.getDatasetCount()):
for series in range(plot.getSeriesCount()):
legend = self.getLegendItem(ds, series)
for i in includedLabels:
if legend.getLabel() == i['name']:
# legend.setLabel(i['alias'])
# setLabel() doesn't exist, I want to change this label to an alias
collection.add(legend)
return collection
newRenderer = CustomRenderer(True, False)
newRenderer.setBaseSeriesVisibleInLegend(True)
legend.setSources([newRenderer])
My problem is that there doesn’t seem to be a setLabel() method on the LegendItem as seen here.
Does anyone have an idea of how to change the label of a LegendItem through the script and not via the Pen Name in the Chart Options GUI in the property inspector?