Heh heh, I have been trying to work off that example actually but not having any luck. The legend item is "fake" in the sense that I generate the "limit line" from a tag property.
This is my full script that gets called:
def gwConfigureChart(data, chart):
"""Adds a limit line to a report chart."""
from java.awt import Color
from java.awt import BasicStroke;
from org.jfree.chart.plot import ValueMarker
import math
plot = chart.getXYPlot()
dataset = plot.getDataset()
lowerBound = chart.getPlot().getRangeAxis().getLowerBound()
upperBound = chart.getPlot().getRangeAxis().getUpperBound()
Limit = data['tagPath1_ReportLimit']
ReportDataAboveLimit = data['tagPath1_ReportDataAboveLimit']
red = Color(255, 0, 0)
green = Color(0, 255, 0)
stroke = BasicStroke(2.0)
if Limit <= lowerBound:
chart.getPlot().getRangeAxis().setLowerBound(Limit - math.floor(Limit * 0.05))
if Limit >= upperBound:
chart.getPlot().getRangeAxis().setUpperBound(Limit + math.ceil(Limit * 0.05))
if ReportDataAboveLimit:
if data['tagPath1_MinValue'] > Limit:
markerLimit = ValueMarker(Limit, green, stroke)
else:
markerLimit = ValueMarker(Limit, red, stroke)
else:
if data['tagPath1_MaxValue'] > Limit:
markerLimit = ValueMarker(Limit, red, stroke)
else:
markerLimit = ValueMarker(Limit, green, stroke)
chart.getPlot().addRangeMarker(markerLimit)
plot.getRangeAxis().setLabel(data['tagPath1_ReportYAxisTitle'])
which generates this:
and the modified code:
def gwConfigureChart(data, chart):
"""Adds a limit line to a report chart."""
from java.awt import Color
from java.awt import BasicStroke;
from org.jfree.chart.plot import ValueMarker
import math
## modified
from org.jfree.chart.renderer.xy import XYLineAndShapeRenderer
from org.jfree.chart import LegendItem
legend = chart.getLegend()
plot = chart.getPlot()
renderer = plot.getRenderer()
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)
collection.add(legend)
return collection
dataset = plot.getDataset()
lowerBound = chart.getPlot().getRangeAxis().getLowerBound()
upperBound = chart.getPlot().getRangeAxis().getUpperBound()
Limit = data['tagPath1_ReportLimit']
ReportDataAboveLimit = data['tagPath1_ReportDataAboveLimit']
red = Color(255, 0, 0)
green = Color(0, 255, 0)
stroke = BasicStroke(2.0)
if Limit <= lowerBound:
chart.getPlot().getRangeAxis().setLowerBound(Limit - math.floor(Limit * 0.05))
if Limit >= upperBound:
chart.getPlot().getRangeAxis().setUpperBound(Limit + math.ceil(Limit * 0.05))
if ReportDataAboveLimit:
if data['tagPath1_MinValue'] > Limit:
markerLimit = ValueMarker(Limit, green, stroke)
else:
markerLimit = ValueMarker(Limit, red, stroke)
else:
if data['tagPath1_MaxValue'] > Limit:
markerLimit = ValueMarker(Limit, red, stroke)
else:
markerLimit = ValueMarker(Limit, green, stroke)
chart.getPlot().addRangeMarker(markerLimit)
plot.getRangeAxis().setLabel(data['tagPath1_ReportYAxisTitle'])
newRenderer = CustomRenderer(True, False)
newRenderer.setBaseSeriesVisibleInLegend(True)
legend.setSources([newRenderer])
which currently generates the same thing.
I don't really understand what a renderer is or how to use it appropriately [obviously].
Any help is appreciated.