# Creating a visualization from a script query

**URL:** https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899
**Category:** Kibana
**Created:** [July 28, 2017, 5:13am UTC](https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899 "2017-07-28T05:13:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nikhil\_Utane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhil_utane/32/27547_2.png) [@Nikhil\_Utane](https://discuss.elastic.co/u/Nikhil_Utane)
#### Post date: [July 28, 2017, 5:13am UTC](https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899/1 "2017-07-28T05:13:34Z")

</div>

I need to create visualizations after doing some math on the indexed data.  
I went through this: [https://www.elastic.co/guide/en/elasticsearch/painless/5.5/painless-examples.html](https://www.elastic.co/guide/en/elasticsearch/painless/5.5/painless-examples.html)  
where it shows the usage using REST APIs.  
Now I want to do the same using Kibana.  
If we take the same example, how can I generate a bar chart where I can see the player name on X-axis and total number of goals scored on the Y axis?  
Thanks.

---

<div class="post-metadata">

### Author: ![Nikhil\_Utane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhil_utane/32/27547_2.png) [@Nikhil\_Utane](https://discuss.elastic.co/u/Nikhil_Utane)
#### Post date: [July 28, 2017, 5:21am UTC](https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899/2 "2017-07-28T05:21:26Z")

</div>

Ohh. Actually i was using the wrong Aggregation for X-axis due to which I wasn't getting the option to 'run' the visualization.  
With this configuration I was able to generate the chart.  
Y-axis: Aggregation: Max, Field: Goals.  
JSON Input:  
{  
"script": {  
"lang": "painless",  
"inline": "int total = 0; for (int i = 0; i \< doc['goals'].length; ++i) { total += doc['goals'][i]; } return total;"  
}  
}

X-axis: Aggregation Terms, Field: first.keyword

---

<div class="post-metadata">

### Author: ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)
#### Post date: [July 28, 2017, 5:21am UTC](https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899/3 "2017-07-28T05:21:57Z")

</div>

I would try (I am using ELK5.4)  
Create a query where you select those event you need. Then visualisations -\> vertical bar  
Change the Y-Xasis to Sum() and select the field that hold the goals for the users. Select bucket type X-Axsis -\> term aggr and select the users name.

Paul.

---

<div class="post-metadata">

### Author: ![Nikhil\_Utane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhil_utane/32/27547_2.png) [@Nikhil\_Utane](https://discuss.elastic.co/u/Nikhil_Utane)
#### Post date: [July 28, 2017, 6:16am UTC](https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899/4 "2017-07-28T06:16:06Z")

</div>

Yes, this works too and is the proper way for such a query. Thanks.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 25, 2017, 6:16am UTC](https://discuss.elastic.co/t/creating-a-visualization-from-a-script-query/94899/5 "2017-08-25T06:16:07Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
