Legend not updating with Pareto Bar Chart via ConfigureChart function

Hi everyone. I have an enquiry on the legend not updating with a pareto bar chart (using the Configure Chart function) in a Vision window.

I have created a Downtime Pareto chart of issues related to the downtime of an asset. The data source is populated with varying issue types. The problem with using the standard bar graph was that the color of each issue series did not remain the same when the downtime duration of issues changed, specifically in a pareto format. As a result, I was able to utilize the ConfigureChart function to fix the color of each issue series. But the legend does not update accordingly.

The following is the Downtime Pareto chart along with the dataset. When comparing the dataset with the chart, you should notice that the legend is not updated to the fixed colors and is defaulting to its randomly generated colors.

image

I have done some research and testing with updating/overriding the legend via the ConfigureChart but I am not experiencing much success. There are a lot of examples of updating and overriding legends for XY Scatter Plots which I believe does not work exactly the same for Category or Bar graphs.

Has anyone been able to do something similar to the following example? Otherwise, is my approach in fixing the colors of the graph incorrect?

Any help on this matter is greatly appreciated.

Code sample of the fixing of series colors is below:

def configureChart(self, chart):
	"""
	Provides an opportunity to perform further chart configuration via
	scripting. Doesn't return anything.

	Arguments:
		self: A reference to the component that is invoking this function.
		chart: A JFreeChart object. Refer to the JFreeChart documentation for
		       API details.
	"""

	import java.awt
	from java.awt import Color
	from org.jfree.chart.renderer.category import BarRenderer
	from org.jfree.chart.renderer.category import StandardBarPainter
	from org.jfree.chart import LegendItem

	class CustomRenderer(BarRenderer):
		def getItemPaint(self, row, column):
			v = chart.getCategoryPlot().getDataset().getRowKey(row)

			if v == "Issue 1":
				return Color(128,128,128)
			elif v == "Issue 2":
				return Color(255,0,255)
			elif v == "Issue 3":
				return Color(255,140,0)
			elif v == "Issue 4":
				return Color(255,255,0)
			elif v == "Issue 5":
				return Color(0,255,0)
			elif v == "Issue 6":
				return Color(0,255,255)
			elif v == "Issue 7":
				return Color(0,0,255)
			elif v == "Issue 8":
				return Color(255,0,0)
			elif v == "Issue 9":
				return Color(213,213,213)
			elif v == "Issue 10":
				return Color(255,138,138)
			elif v == "Issue 11":
				return Color(255,202,138)
			elif v == "Issue 12":
				return Color(255,255,138)
			elif v == "Issue 13":
				return Color(138,255,138)
			elif v == "Issue 14":
				return Color(138,255,255)
			elif v == "Issue 15":
				return Color(138,138,255)
			elif v == "Issue 16":
				return Color(255,138,255)
			elif v == "Issue 17":
				return Color(43,43,43)
			elif v == "Issue 18":
				return Color(172,0,0)
			elif v == "Issue 19":
				return Color(172,95,0)

	chart.getCategoryPlot().setRenderer(CustomRenderer())
	chart.getPlot().getRenderer().setBarPainter(StandardBarPainter())
	chart.getPlot().getRenderer().setShadowVisible(0)

The bar chart component should have getBarColor extension function, so you could simplify your code quite a bit by moving it from configureChart to there. Using your code as a template, the simplified extension function code would look something like this:

#def getBarColor(self, series, category, [...]):
	v = self.data.getValueAt(category, series)
	if v == "Issue 1":
		return system.gui.color(128,128,128)
	elif v == "Issue 2":
		return system.gui.color(255,0,255)
	elif v == "Issue 3":
		return system.gui.color(255,140,0)
	elif v == "Issue 4":
		return system.gui.color(255,255,0)
	elif v == "Issue 5":
		return system.gui.color(0,255,0)
	elif v == "Issue 6":
		return system.gui.color(0,255,255)
	elif v == "Issue 7":
		return system.gui.color(0,0,255)
	elif v == "Issue 8":
		return system.gui.color(255,0,0)
	elif v == "Issue 9":
		return system.gui.color(213,213,213)
	elif v == "Issue 10":
		return system.gui.color(255,138,138)
	elif v == "Issue 11":
		return system.gui.color(255,202,138)
	elif v == "Issue 12":
		return system.gui.color(255,255,138)
	elif v == "Issue 13":
		return system.gui.color(138,255,138)
	elif v == "Issue 14":
		return system.gui.color(138,255,255)
	elif v == "Issue 15":
		return system.gui.color(138,138,255)
	elif v == "Issue 16":
		return system.gui.color(255,138,255)
	elif v == "Issue 17":
		return system.gui.color(43,43,43)
	elif v == "Issue 18":
		return system.gui.color(172,0,0)
	elif v == "Issue 19":
		return system.gui.color(172,95,0)

As far as the legend is concerned, I'm not sure how to best make that work. My inclination is to set the chart's legend property to false, and to overlay a template canvas to build a custom legend using the strings in the type column of the chart's data property.

...but sitting here contemplating this, another possible approach would be to create a template with a color box and a label, and use a template repeater at the bottom of the chart to quickly create a legend using the type column list.

2 Likes

Thanks for the feedback on the simplified code. I will take up on your advise to clean the script a bit. I had completed the code once upon a time so I'm not too sure why I didn't do it in the getBarColor function in the first place ...

Regarding the legend, I have come to the conclusion that using the configureChart to update the inbuilt legend is probably not going to work. I am definitely interested in your second idea, creating a simple table like version of a legend. The issues list is fixed and relatively common across all assets and so, it shouldn't need to be updated often. I'll report back if I manage to create a modified legend.

Thanks again for your response, greatly appreciated!

Just arrived to this post looking for that, thanks.
I case still anyone is stacked in this problem, just wanted to point out that category is for rows and series for columns, except when you set "extract order" by columns in your bar chart.

1 Like