Bar chart Color based on different values

Hello everybody,

i am working on a simple bar chart and i would like to color the bars based on several target values.

Following example:

As you can see i would like to import machine based target values into the barchart, so the formatting can be made by these values. It’s possible for me to provide the target values as a dataset (Machine || target) in Ignition, but i am not shure how to use them in the scripting.

I’m very new into developing Ingition applications, so i hope someone can help me out :slight_smile:

You can set the colors of the series from a script like this:

from java.awt import Color
event.source.parent.getComponent('Bar Chart').seriesColors = [Color.decode("#00FFFF"), Color.decode("#FF00FF")]

I don’t know how to set the color on individual bars though :confused:

But perhaps you can hack your way around by having a “good” and “bad” series in your dataset and using stacked bars. Then you don’t even need dynamic colors, but you do need a script to write data to the “good” or “bad” column depending on the values.

2 Likes

@Sanderd17
Thank you for your help :slight_smile:
It was possible to solve the problem by using a staked bar chart wird “good/bad” values (in the SQL Statement) for the same measure. Now it is possible for me to show some nice prepared values :slight_smile:

On the BarChart component, put the following in the getBarColor extension function.

	from java.awt import Color

	data = self.data
	categoryName = data.getValueAt(category,"Label")

	red = Color.RED
	green = Color(113,174,70)
	
	if categoryName == "M1":
		if value < 6:
			return red
		else:
			return green
	elif categoryName == "M2":
		if value < 7:
			return red
		else:
			return green
	elif categoryName == "M3":
		if value < 3:
			return red
		else:
			return green
	elif categoryName == "M4":
		if value < 4:
			return red
		else:
			return green
	return defaultColor

6 Likes