# Bind data from Named Query to Pie Chart

**URL:** <https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [May 27, 2024, 11:18am UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946 "2024-05-27T11:18:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 27, 2024, 11:18am UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/1 "2024-05-27T11:18:05Z")

</div>

Hello!

I´ve got a Named Query with 2 columns binded to the data property of a Pie Chart.

It show the Pie Chart with the data for a second but later dissapear

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/e/ae7bd574f8158f8b1b5eca5bfd5e98a317930177.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/8/38ded83fa4752bffa7d4754e4a9edc832fc9a969.png)

Should I add a Transform Script to the Named Query to keep it stable?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 27, 2024, 11:47am UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/2 "2024-05-27T11:47:22Z")

</div>

If the table was using a dataset you could use a simple expression function [columnRearrange | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/advanced/columnRearrange) to return just the two columns of interest.

In a quick test with the default table data the Pie Chart component seems to use the first column as labels and the first numeric column as values. This might not be correct.

It looks like you are using JSON so you'll need a script transform.

 ![Pie data transform](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/2/523040bcf5834fd1cf9557b8f2b4077c99cb18a4.png)

> **Script**
>
> ```python
> def transform(self, value, quality, timestamp):
> pieData = []
> for row in value:
> pieData.append({
> "city": row['city'],
> "population": row['population']
> })
> return pieData
> 
> ```

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 27, 2024, 1:55pm UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/3 "2024-05-27T13:55:09Z")

</div>

Thanks Transistor,  
I just added the code, and I was able to put the data on the pie

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/8/d87854b3c1954c056946c4b70cc05914a11864f3.png)

```python
def transform(self, value, quality, timestamp):

    pieData = []

    for row in value:
        pieData.append({
            "label": str(row['RIESGO']), 
            "value": float(row['TOTAL'])  
        })

    return pieData

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/8/7892ab824d6071e3e0b03cde977ee66d24ca6d6d.png)

but the graph is not showing data,  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/1/c1155ca4372b3d643fe36457c3ed21ad09ecb763.png)

no errors show on props.data, just java errors in the console

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 27, 2024, 2:15pm UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/4 "2024-05-27T14:15:23Z")

</div>

At the bottom of the docs, [https://docs.inductiveautomation.com/docs/8.1/appendix/components/perspective-components/perspective-chart-palette/perspective-pie-chart#example](https://docs.inductiveautomation.com/docs/8.1/appendix/components/perspective-components/perspective-chart-palette/perspective-pie-chart#example),

> Value types can vary as **you can pass both "50" and 50 as counts** and the Pie Chart will still be able to render the data correctly.  
> ...  
> The query used must return two columns in any order where each column represents **a string and a numeric value** to be rendered by the chart.

The Pie Chart is trying to be smart. It sees that both `label` and `value` are numeric so it gets confused and doesn't know which is the label. Try this:

```python
def transform(self, value, quality, timestamp):
    pieData = []
    for row in value:
        pieData.append({
            "label": "RIESGO " + str(row['RIESGO'])), 
            "value": row['TOTAL']  
        })

    return pieData

```

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 27, 2024, 2:20pm UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/5 "2024-05-27T14:20:18Z")

</div>

Wow!, it works! thanks a lot Transistor.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/9/79a640bda3743a974177c3a5504ff2e8925810f3.png)

```python
def transform(self, value, quality, timestamp):

    pieData = []

    for row in value:
        pieData.append({
            "label": "RIESGO " + str(row['RIESGO']), # Convert RIESGO to string
            "value": float(row['TOTAL']) # Ensure TOTAL is a number
        })

    return pieData
    

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 27, 2024, 2:26pm UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/6 "2024-05-27T14:26:11Z")

</div>

The `str()` is required. I fixed my post.  
The `float()` is not required, I think. The doc suggests that the chart will take numbers or numeric strings and convert them.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 27, 2024, 2:37pm UTC](https://forum.inductiveautomation.com/t/bind-data-from-named-query-to-pie-chart/90946/7 "2024-05-27T14:37:30Z")

</div>

Thanks

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/a/eae86ed0bb66bd6b9925c26ba222901f56f66874.png)
