# Pie Chart Plugin Example?

**URL:** <https://community.grafana.com/t/pie-chart-plugin-example/9068>\
**Category:** Time Series Panel\
**Tags:** mssql\
**Created:** [July 23, 2018, 2:42pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068 "2018-07-23T14:42:49Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 23, 2018, 2:42pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/1 "2018-07-23T14:42:50Z")

</div>

Hi all.

I’m using Grafana 5.2.1 and Microsoft SQL server. The pie chart plugin 1.3.3? is installed on my instance of Grafana, and I can open a new pie chart on the dashboard.

I can’t figure out how do make a pie chart work. I understand it’s a time series, but don’t know how to get it running. I have a bunch of counts (int) that I want to display, but was hoping someone had an example of a functioning pie chart that I could model mine after.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [July 23, 2018, 2:53pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/2 "2018-07-23T14:53:18Z")

</div>

It depends on if you have a time column or not. If you don’t have a time column you need to fake it and could use a query similar to [comment](https://github.com/grafana/piechart-panel/issues/142#issuecomment-406971415)

- you’ll need to convert this to MSSQL syntax.

If you have a time column please refer to [Using Microsoft SQL Server in Grafana documentation](http://docs.grafana.org/features/datasources/mssql/#time-series-queries).

Marcus

---

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 24, 2018, 12:45pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/3 "2018-07-24T12:45:47Z")

</div>

Thanks for the links! The issue I’m having is that I’m trying to do counts of items with specific attributes. I don’t have a table set up already of the metrics I want to measure, I’m trying to count them out from an existing table. Is setting up a table the easiest thing to do?

Thanks!

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [July 24, 2018, 1:30pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/4 "2018-07-24T13:30:53Z")

</div>

I dont understand what you mean. Can you provide an example of the data you have and query you would use to count of items with specific attributes?

---

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 25, 2018, 12:26pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/5 "2018-07-25T12:26:41Z")

</div>

No worries. I have a table with information on processes that were run on a certain server. I want to count by status tag so I have a visualization of how many processes failed, how many were completed, etc.  
It’s not letting me past in my code, so take the following screenshot I guess (apologies in advance for probably bad style, I’ve never written SQL before):

 ![code](https://us1.discourse-cdn.com/grafana/original/2X/4/4c01e1a97ff712b3dc08b9bd27725825b88a2061.PNG)  
Anyways, this produces a table like the following:  
 ![exampleTable](https://us1.discourse-cdn.com/grafana/original/2X/9/96945ad9f70ecf046c7fbcaea69c2578af3b3a88.PNG)  
I figured for visualization in MS SQL Server it would be easier to make a temp table, but the important point is counting by attribute “statusid”.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [July 25, 2018, 1:10pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/6 "2018-07-25T13:10:21Z")

</div>

If you want to use that in Grafana you must put that in a [stored procedure](http://docs.grafana.org/features/datasources/mssql/#stored-procedure-support)

However, I think using one query instead of looping would be more optimized.

```auto
SELECT
  GETUTCDATE() AS time,
  COUNT(*) AS measurement,
  CASE statusId
    WHEN 0 THEN 'pending'
    WHEN 1 THEN 'running'
    ...
    ELSE 'something else'
  END AS outcome
FROM
  BPASession
GROUP BY
  statusid

```

That should work fine for pie chart panel. Give it a go and let me know the outcome.

---

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 25, 2018, 1:23pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/7 "2018-07-25T13:23:03Z")

</div>

Yep, that works great, and that code is certainly much nicer than what I had.

I do wonder why there are no zero values though, is there a way to change that?  
 ![graphvalues](https://us1.discourse-cdn.com/grafana/original/2X/7/7e8de50d5160a6112a53b2c2828caae44349decb.PNG)

Thanks so much for your help!!

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [July 25, 2018, 2:30pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/8 "2018-07-25T14:30:40Z")

</div>

When i tested zero values are shown in legend. Please verify the response using the query inspector to see if rows with zero values are included.

---

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 25, 2018, 2:48pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/9 "2018-07-25T14:48:19Z")

</div>

Here’s the values from the query inspector:  
{  
“xhrStatus”: “complete”,  
“request”: {  
“method”: “POST”,  
“url”: “api/tsdb/query”,  
“data”: {  
“from”: “1532508508175”,  
“to”: “1532530108175”,  
“queries”: [  
{  
“refId”: “A”,  
“intervalMs”: 7200000,  
“maxDataPoints”: 3,  
“datasourceId”: 1,  
“rawSql”: “SELECT\r\n GETUTCDATE() AS time,\r\n COUNT(_) AS measurement,\r\n CASE statusId\r\n WHEN 0 THEN ‘Pending’\r\n WHEN 1 THEN ‘Running’\r\n WHEN 2 THEN ‘Terminated’\r\n WHEN 3 THEN ‘Stopped’\r\n WHEN 4 THEN ‘Completed’\r\n WHEN 5 THEN ‘Debugging’\r\n WHEN 6 THEN ‘Archived’\r\n ELSE ‘Stopping’\r\n END AS outcome\r\nFROM\r\n BPASession\r\nGROUP BY\r\n statusid\r\n",  
“format”: “time\_series”  
}  
]  
}  
},  
“response”: {  
“results”: {  
“A”: {  
“refId”: “A”,  
“meta”: {  
“rowCount”: 0,  
“sql”: "SELECT\r\n GETUTCDATE() AS time,\r\n COUNT(_) AS measurement,\r\n CASE statusId\r\n WHEN 0 THEN ‘Pending’\r\n WHEN 1 THEN ‘Running’\r\n WHEN 2 THEN ‘Terminated’\r\n WHEN 3 THEN ‘Stopped’\r\n WHEN 4 THEN ‘Completed’\r\n WHEN 5 THEN ‘Debugging’\r\n WHEN 6 THEN ‘Archived’\r\n ELSE ‘Stopping’\r\n END AS outcome\r\nFROM\r\n BPASession\r\nGROUP BY\r\n statusid\r\n”  
},  
“series”: [  
{  
“name”: “Terminated”,  
“points”: [  
[  
106,  
1532530108420  
]  
]  
},  
{  
“name”: “Stopped”,  
“points”: [  
[  
47,  
1532530108420  
]  
]  
},  
{  
“name”: “Completed”,  
“points”: [  
[  
768,  
1532530108420  
]  
]  
},  
{  
“name”: “Debugging”,  
“points”: [  
[  
473,  
1532530108420  
]  
]  
}  
],  
“tables”: null  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [July 25, 2018, 2:51pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/10 "2018-07-25T14:51:36Z")

</div>

Guess you don’t have any data with statusid=0 then?

Or make sure that you specify what to count, i.e. COUNT(\*) or COUNT(1)

---

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 25, 2018, 2:54pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/11 "2018-07-25T14:54:25Z")

</div>

> [@anon34853223](#):
>
> Guess you don’t have any data with statusid=0 then?

I mean, there are 0 items with statusid = 0, 1, 6 or 7. It’s not super important, but I would prefer to at least put them on the key so I know there are other statuses, even if they aren’t being used at the moment, if that makes sense?

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [July 25, 2018, 2:58pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/12 "2018-07-25T14:58:02Z")

</div>

If you would have a related table holding all available statuses with statusid and name it’s easy to accomplish what you want since you can select from related table and left join with BPASession - then you’ll get all statuses if they don’t have a row in the BPASession table. With this you wouldn’t need the case when logic since you can just select the measurement name from the related table.

---

<div class="post-metadata">

**Author:** ![adrianap](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adrianap](https://community.grafana.com/u/adrianap)\
**Post date:** [July 25, 2018, 2:59pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/13 "2018-07-25T14:59:13Z")

</div>

Ok, gotcha. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![anis](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anis](https://community.grafana.com/u/anis)\
**Post date:** [February 21, 2019, 4:52pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/14 "2019-02-21T16:52:33Z")

</div>

I am a young developer (beginner), I really need help. My problem is similar to Adrianap’s initial issue, I think you can help me … please 🙂

I have a table named **project** with a **user\_id** column. My table looks like Adrianap’s table (screenshot). **I would like to represent in a pie chart the number of projects realized by each user**.

For example, user 1 has made 20 projects, users 2 and 3 have made 10, and user 4 has made 0. Thus, my graph will be completed by half by the user1’s id, a quarter by the id of user2, and the last quarter by the id of user3.

I’m blocked since this morning… Thanks for helping

---

<div class="post-metadata">

**Author:** ![anon34853223](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon34853223](https://community.grafana.com/u/anon34853223)\
**Post date:** [February 21, 2019, 11:03pm UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/15 "2019-02-21T23:03:02Z")

</div>

Would help if you include the mssql queries you’ve tried so far.

Something like this maybe could work without knowing the exact schema of your table?

```sql
SELECT
  GETUTCDATE() as time,
  user_id as metric,
  COUNT(1) as value
FROM
  project
GROUP BY user_id

```

Refer to Using MSSQL in Grafana documentation for further reference (posted earlier in this topic) and/or MSSQL tutorials regarding queries with group by/aggregate functions (count in above example).

Good luck

---

<div class="post-metadata">

**Author:** ![anis](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anis](https://community.grafana.com/u/anis)\
**Post date:** [February 22, 2019, 9:11am UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/16 "2019-02-22T09:11:29Z")

</div>

Thanks for helping… I replaced GETUTCDATE by UTC\_DATE (it didn’t work before this replacement).  
My current error is : _Column metric must be of type CHAR, VARCHAR, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT. metric column name: metric type: INT but datatype is \*uint32_  
In my database, user\_id is _int(10) unsigned NOT NULL,_

Do you know what have I done wrong ?

I want to add that I use MySQL

---

<div class="post-metadata">

**Author:** ![anis](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anis](https://community.grafana.com/u/anis)\
**Post date:** [February 22, 2019, 11:15am UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/17 "2019-02-22T11:15:00Z")

</div>

I finally found the answer 🙂

SELECT  
UTC\_DATE() as time,  
CAST(user\_id as CHAR) as metric,  
COUNT(\*) as value  
FROM  
project  
GROUP BY user\_id;

---

<div class="post-metadata">

**Author:** ![amagonawin](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@amagonawin](https://community.grafana.com/u/amagonawin)\
**Post date:** [April 23, 2020, 11:10am UTC](https://community.grafana.com/t/pie-chart-plugin-example/9068/18 "2020-04-23T11:10:36Z")

</div>

Hello there i have the same problem ,i dont have any time column in my data

Select CAST(nn.ComputerID AS NVARCHAR(36))[Computer ID], client.ComputerName , count(\*) as [number Software] From oems\_client\_nn\_software as nn  
Left Join oems\_client as client on client.ComputerID = nn.ComputerID  
GROUP BY nn.ComputerID, client.ComputerName

could someone show me how i need to turn this , to use it in a piechart?

Thanks
