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
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.
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())
def getBarColor(self, series, category, value, defaultColor):
from java.awt import Color
dataset = self.parent.parent.parent.dataset
color1 = Color(73,186,183) #green
color2 = Color(255,0,0) #Red
rowcount = dataset.getRowCount()
for i in range(rowcount):
status = dataset.getValueAt(i, "status")
if status == 'Completed' :
return color1
if status != 'Progress' :
return color2
I'm using this script and in for loop if I print i which is giving me zero alone
I have a custom property in which it contains 3 columns and 30 rows.
Why its always showing zero in the for loop and it is not working because of this, Please support on this.
It seems that you are not understanding how this extension function works. There is no need to iterate, the function does that automatically with category being the row and series being the column. (Note that series is zero indexed and ignores the label column). That said, there is no need to do a self.data.getValueAt(category, series + 1) call because that is already done with the value variable. You can just use the value variable.
Also, this seems off to me:
My expectation would be: `dataset = self.data"
Putting it all together, I imagine that your script should look more like this:
#def getBarColor(self, series, category, value, defaultColor):
from java.awt import Color
dataset = self.data
color1 = Color(73,186,183) #green
color2 = Color(255,0,0) #Red
status = dataset.getValueAt(category, "status")
if status == 'Completed':
return color1
elif status != 'Progress':
return color2
else:
return defaultColor
Also note that the extension function won't directly bark at you if there is an error in your code. You will have to open up the console to see if there is a problem (ctrl+shift+c).