Thank you this worked!
The reporting timeseries chart can now support a dynamic dataset.
A short how to for the next guy:
- Define the maximum amount of pens your chart will have.
- Edit the chart-script according to the code below.
- Populate your chart like you normally do.
Just make sure that your pen labels for the chart are the same as your dataset(datasource) column labels. This is used to match which labels to hide. See screenshot below:
Code for hiding legend items not used:
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:
sum(ylist)
includedLabels.append(legenditems.get(col).getLabel())
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)
if legend.getLabel() in includedLabels:
collection.add(legend)
return collection
newRenderer = CustomRenderer(True, False)
newRenderer.setBaseSeriesVisibleInLegend(True)
legend.setSources([newRenderer])