Pie Chart legend adaptability

Hello,

I’m working on Ignition 8.1.13 in the Perspective module.
I’m using a Pie Chart which have a large number of data (fruit varieties), so the legend is very large. Is there a way to make the legend adaptative in order to display only the data for which the “count” property is above 0 (in order not to have 20 data displayed on the legend while only 2 are actually used) ?

Thanks for your help,

Carla

Would you consider a script transform on the data binding to remove the entries with a count of zero? You could even sum all the items ≤ 1 as ‘Other’, for example.

1 Like

Thanks for your idea, I used it to find a solution, but instead of doing the binding on the “data” property, I did it directly on my custom property :

  1. Data retrieval from the tag history on a specific day

  2. Script transform to perform calculations on the dataset to retrieve a list without duplicates (list1)

  3. Another script transform, that initializes the “data” property to an empty list, and for each of the element which can appear in the Pie Chart, counts the number of occurences of this element in list1. If this number is above 0, it then create a new object and add it in the “data” array. Here is the code, where “value” is list1.

	self.props.data = []
	count_elem = 0
	species_list = ["E1","E2","E3","E4","E5","E6","E7","E8","E9","E10"]
	
	for i in range(len(species_list)) :
	
		count_elem = value.count(species_list[i])

		new_object = {}
	
		if count_elem != 0 : 
			new_object["specy"] = species_list[i]
			new_object["count"] = count_elem
			self.props.data.append(new_object)
	
	return value

It seems to work properly

1 Like

Hello m’am, this is the python loop police. You’re under arrest for unauthorized use of range(len()) syntax.

for i in  range(len(species_list)):
    species_list[i]
# This can be translated to
for species in species_list:
    species

I have a few remarks about what you just described:

Why another transform ? Why not do it in the same one ?
I thought list1 didn’t have duplicates, why would you count occurrences ? Am I missing something ?

I’m curious to see the full thing. If you’re okay with this, I’ll try and improve your script a bit.

PS: I think the singular of species is… species.

Oh no, please excuse me for this terrible crime, I won’t do it again!

Actually I wasn’t clear enough about that. The thing is that I have a database which stores (omong others) the number of the batch of fruits currently treated, the variety of the fruits in the batch, and also its species. But for practical reasons I use another software to transmit data from the PLC to Ignition in MQTT, ant it is only this soft which can communicate with the database and retrieve the fruit information. But as the MQTT frame is sent every 30s, there are a lot of duplicated info for a same fruit batch in the tag history. Then, I have to perform a script transform in order to have the data of the different batches only once. But two different batches can contain fruits with the same variety or species, and that’s why there can be several occurences of a same variety or species in list1 (called “species” in the script below). To do this, I wrote this script :

	lots = [] #list of the different lots produced during the specified time range
	species = [] #list of the different fruit species passed on the line during the specified time range
	row_index = 0 #index of the line in the dataset
	
	dataset_lots = value.getColumnAsList(1) #conversion of the dataset column to a list
	dataset_species = value.getColumnAsList(2) #conversion of the dataset column to a list
	
	for lot in dataset_lots :
		if lot!=0 and lot not in lots : 
			lots.append(lot) #addition of the lot number in the list
			species.append(dataset_species[row_index]) #addition of the species name in the list
		row_index = row_index+1
	
	return species

I can of course do it in the same, I just prefered to separate the different parts to make the steps clearer.

If I got this right, you could be removing duplicates and counting species at the same time:

dataset_lots = value.getColumnAsList(1) #conversion of the dataset column to a list
dataset_species = value.getColumnAsList(2) #conversion of the dataset column to a list

lots = []
species_count = {}
for lot, species in zip(dataset_lots, dataset_species):
	if lot != 0 and lot not in lots:
		lots.append(lot) #addition of the lot number in the list
		species_count[species] = species_count.get(species, 0) + 1

return [
	{
		'species': species,
		'count': count
	} for species, count in species_count.items()
]

It functionally equivalent and (I believe) only marginally more efficient, but I think it’s clearer.

Yes it works too, thanks !