Report Bar Chart Color by Axis label

Hi there,
Need some guidance, I'm populating a Bar chart in the report by a SQL Query, and I tried the Script in the bar chart in the Reports to change the color of the Segments of the Bar depending on the Value, But I would like Change the color depending on the Category label:

I want the bar "Starved" to be always yellow, "Blocked" Blue, "Cycling" Green, "InFault" Red and so on.
What will be the correct script to check that??, this is the script I tried to change the colours of the bars, depending on Value:

from java.awt import Color
a = 1
    #gets the underlying dataset in the chart
ds = chart.getPlot().getDataset()
print ds.columnCount
    # 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

I need to do something similar. Any help?

Reports are neither Vision nor Perspective. They are independent. There's just some overlap with JFreeCharts.

Gahh! I missed the first word in the title. Thanks. I've added the reporting tag.

1 Like

Came across this today and sharing the script I wrote for my situation. I have a stacked bar chart with all the state durations of stations within a line.

from java.awt import Color
plot = chart.getCategoryPlot()
lst_States = plot.dataset.getRowKeys()

# Set the color of each state in stacked bar chart
categoryRenderer = plot.getRenderer()
for i in range(len(lst_States)):
    state = lst_States[i]

    # Converts the RGB color for java.awt.Color
    rgbColor = gl.style.getColorOfState(state)
    finalColor = Color(rgbColor[0], rgbColor[1], rgbColor[2])

    # Update the color for the state
    categoryRenderer.setSeriesPaint(i, finalColor)