Populate Pie Chart from table data

Hello!

I´m trying to populate a Pie Chart from the data of a column

I´m getting htis error

but the data is arriving well

the code I´m using is something like:

def runAction(self, event):

    # Retrieve the query results from the table component or any other source
    tab_container = self.parent.getChild("TabContainer") if self.parent.getChild("TabContainer") else None

    if tab_container:
      
        table_component = tab_container.getChild("TablaInventario")
        table_data = table_component.props.data if table_component else None

  
        if table_data and table_data.getRowCount() > 0:
            nivel_data = {}

            # Iterate through the rows to count occurrences of 'Nivel'
            for row in range(table_data.getRowCount()):
                nivel = table_data.getValueAt(row, "Nivel")

                # Count occurrences of each 'Nivel'
                if nivel in nivel_data:
                    nivel_data[nivel] += 1
                else:
                    nivel_data[nivel] = 1

            # Format the pie chart data with 'count' and 'flavor'
            pie_chart_nivel_data = [{"count": v, "flavor": str(k)} for k, v in nivel_data.items()]

       
            pie_chart_nivel = tab_container.getChild("ColumnContainer_0").getChild("PieChart_Nivel")

            if pie_chart_nivel:
                pie_chart_nivel.props.data = pie_chart_nivel_data
            else:
                print("PieChart_Nivel not found.")

        else:
            print("No data available in the table to update the pie chart.")
    else:
        print("TabContainer not found.")

Any ideas why this could happening welcome

I think the Pie Chart component is "smart" and looks for categories and values and assigns them automatically to the slices. The problem is that both 'flavor' and 'count' are numeric.
Try adding an 'x' or even a space in front of the flavor variables. e.g. x6.0 or 6.0 to see if that's the problem.

Why have you got "flavor 8.0". Is this your eighth attempt to make something that tastes nice?

1 Like

Wow! that works, thanks Transistor, no way I can find this by myself.

 pie_chart_nivel_data = [{"count": v, "flavor": "x" + str(k)} for k, v in nivel_data.items()]
            

only works with the x

now I should find the way to take the x off

the flavor thing is because it was the original structure on pie charts, didn´t change it...maybe I should.

Try
pie_chart_nivel_data = [{"count": v, "flavor": "[]" + str(k)} for k, v in nivel_data.items()]

The "[]" puts an empty AM Charts format string in front of the numeric value and that seems to fool the chart into treating the whole thing as a string.

Formatting Strings
All SVG textual labels displayed on the chart pass via TextFormatter before making onto screen. The formatter looks for special placeholder codes to replace either with real data or apply visual formatting.

https://www.amcharts.com/docs/v4/concepts/formatters/formatting-strings/

4 Likes

Yeah, that works, thank you Transistor!