Reporting bar chart colors

This is an example to make your bar chart colors customizable using the JFree Chart API in JYTHON.
This is executed under the chart properties => edit script.

from java.awt import Color
a = 1
    #gets the underlying dataset in the chart
ds = chart.getPlot().getDataset()
#print ds.rowCount
    # gets the plot
plot = chart.getCategoryPlot()
    #gets the Barrenderer object
renderer = plot.getRenderer()
    #Loop Through dataset and check value and assign series colors
for row in range(ds.rowCount):
	print row
	value  = float(ds.getValue(row, 0))
	print value
	
	if value >= 100.0:
		print 'Row %d should be green'%row
		renderer.setSeriesPaint(row, Color.GREEN)
	if value <= 99.9 and value >= 88.0:
		print 'Row %d  should be yellow'%row
		renderer.setSeriesPaint(row,Color.YELLOW)
	if value <= 87.9:
		print 'Row %d should be red'%row
		renderer.setSeriesPaint(row,Color.RED)
	else:
		a = 0

This sets the series colors. It doesn’t set the color based on the value of the same series. I am trying to change the color based on the value of the bar in the same series. Any suggestion…thanks

[code] from org.jfree.chart.renderer.category import BarRenderer
from java.awt import Color

class myBarRenderer(BarRenderer):
	def getItemPaint(self, row, column):
		v = chart.getCategoryPlot().getDataset().getValue(row, column)
		if v < 70:
			return Color.blue
		else:
			return Color.red

plot = chart.getCategoryPlot()
plot.setRenderer(myBarRenderer())[/code]

[quote=“jpark”][code] from org.jfree.chart.renderer.category import BarRenderer
from java.awt import Color

class myBarRenderer(BarRenderer):
	def getItemPaint(self, row, column):
		v = chart.getCategoryPlot().getDataset().getValue(row, column)
		if v < 70:
			return Color.blue
		else:
			return Color.red

plot = chart.getCategoryPlot()
plot.setRenderer(myBarRenderer())[/code][/quote]

Thank you! It works great!

Hi,

It didn’t work for me in a bar chart script in report module. (ignition 7.9.9)
My code below in def configureChart(data,chart) :

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

class myBarRenderer(BarRenderer):
def getItemPaint(self, row, column):
			v = chart.getCategoryPlot().getDataset().getValue(row, column)
			if v < 0:
				return Color.green
			else:
				return Color.red
	
	plot = chart.getCategoryPlot()
	plot.setRenderer(myBarRenderer())

Error is :
File “function:configureChart”, line 13, in configureChart
java.lang.ClassCastException: java.lang.ClassCastException: org.jfree.chart.plot.XYPlot cannot be cast to org.jfree.chart.plot.CategoryPlot

Thanks