Dynamic Bar Graph Segment Colors and using SQL query to populate the graph

I am populating the bar graph using the sql query in the report viewer. I am successfully able to populate the bar graph however I would like the bars colour based on condition such as (Please see Bar Chart 1).

Now looking through the forum I found some pointers but the examples were associated to when the bar chart is used in the vision screen. Unlike the bar chart properties in vision screen where one can easily use getBarColor Extention to dynically colour the bars based on the condition, I couldn’t find something similar in the report viewer window.

Here is how I want the bar screen to look (Please Note: This bar screen was populated in the vision window and I used getBarColor extention and code used is below this graph)
BAR CHART 1 - Inserted in Vision Window

’from java.avt import Color
if value >= 4.3:
return Color.RED
if value > 4 and value < 4.3:
return Color.GREEN
else:
return defaulColor’

Now, in the report viewer when I am populating bar graph using sql query, it is noticed that they are all one colou**r. **
BAR CHART 2 - Created in Report Viewer

I am guessing in order to add dynamic colour to the bars I need to use the ‘configurechart’ definition in the report viewer found under the configuration. Please see below the screen shots

I believe the code will go here but I don’t know how to properly do this.

In a nut shell, could you please provide assistance as to how I can fill the bar’s in the bar chart dynamically based on condition in the report viewer window.

Bumping the thread! Any help would be highly appreciated.

I don’t know how to do this, but I might can give some feedback. Make sure your code is indented so it is inside the

def configureChart(chart)

Then you probably want to do something like

if data['VAL'] >= 4.3:

        chart.setColor.GREEN

else:
        chart.setColor.RED

I don’t know the correct syntax for the chart.setColor, or even if that is correct, but it might help you a little bit. check the JFreeChart API

Thank you for the direction. I will try to build up on your code and hopefully I get it to work. Thanks!

I contacted Ignition Technical support and they were able to provide me with the solution. I am posting it here incase anyone else needs to use it in future. Thank you again.

In the report properties, scroll down to enable script and add this script in there. Please pay attention to tables otherwise it may not work.

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

    class CustomRenderer(BarRenderer):
    def getItemPaint(self, row, column):
	v = chart.getCategoryPlot().getDataset().getValue(row, column)
	if v >= 4.3:
		return Color.red
	if v > 4 and v < 4.3:
		return Color.green
	else:
		return Color.blue

     chart.getCategoryPlot().setRenderer(CustomRenderer())
1 Like