# Is there way to sort graphs in visualization?

**URL:** <https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425>\
**Category:** Kibana\
**Created:** [December 11, 2020, 3:10pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425 "2020-12-11T15:10:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhagen92](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jhagen92/32/80458_2.png) [@jhagen92](https://discuss.elastic.co/u/jhagen92)\
**Post date:** [December 11, 2020, 3:10pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/1 "2020-12-11T15:10:59Z")

</div>

Hi.

I have a heatmap in one visualization. I have 3 sources, so I split chart by column by source, which creates 3 heatmaps. Let's say source A, B, and C.

Currently, order is A,B,C. I can change using Order, but it only has ascending and descending. Is there way to sort them anyway I want to? For example, I want to see graph in order of B, A and C.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 11, 2020, 4:22pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/2 "2020-12-11T16:22:52Z")

</div>

If I understand correctly you can do this with a [scripted field](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html).

You would create a new numeric field based on your source (A, B, C). Assign new values like below.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/9/b9503a539d0f6c9703660f0f8f0b61737bb64f55.png)

Now in your visualization you would sort by `vis_order`.

---

<div class="post-metadata">

**Author:** ![jhagen92](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jhagen92/32/80458_2.png) [@jhagen92](https://discuss.elastic.co/u/jhagen92)\
**Post date:** [December 11, 2020, 6:26pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/3 "2020-12-11T18:26:22Z")

</div>

So I have to modify index pattern in order to sort them?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 11, 2020, 6:38pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/4 "2020-12-11T18:38:50Z")

</div>

It's one way to do this. You are adding to the index pattern on the fly and not really modifying it.

This new field only is calculated when the visualization loads. At runtime the values are generated.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/0/70210d6f26b9683b21246fa7117fea3a25cb2400.png)

---

<div class="post-metadata">

**Author:** ![jhagen92](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jhagen92/32/80458_2.png) [@jhagen92](https://discuss.elastic.co/u/jhagen92)\
**Post date:** [December 30, 2020, 6:34pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/5 "2020-12-30T18:34:13Z")

</div>

Hi,

Thanks for helping me. I added Scripted Field and added `vis_order`. I checked logs and source A,B, and C has `vis_order` with assigned number.

However, when I try to sort them in visualization, it doesn't work. For my pie chart, I go to `Bucket` -\> `Split chart` -\> `Order by` and tried different options. I found `Unique Count` but it doesn't work. Can you please help me to sort A,B,C using scripted field in visualization?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 30, 2020, 7:01pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/6 "2020-12-30T19:01:16Z")

</div>

This is due to the values of the scripted fields are being generated while the data is being inserted.

Say you have 100k records in this index. It would have to process all 100k records to add the scripted field first before able to do a sort on them. All that to say I was incorrect and you **can't** sort by a scripted field.

What you need to do now is to process this new field during ingestion using Logstash, Beats, or an Ingestion Pipeline to add this new field. That way it's calculated one time and you can sort on the finished field.

---

<div class="post-metadata">

**Author:** ![jhagen92](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jhagen92/32/80458_2.png) [@jhagen92](https://discuss.elastic.co/u/jhagen92)\
**Post date:** [December 30, 2020, 7:56pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/7 "2020-12-30T19:56:12Z")

</div>

So I need to add the field when I actually log them with other field in my java code right?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 30, 2020, 9:10pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/8 "2020-12-30T21:10:48Z")

</div>

Correct. Create a new field to sort on before the data is ingested.

---

<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:** [January 27, 2021, 9:10pm UTC](https://discuss.elastic.co/t/is-there-way-to-sort-graphs-in-visualization/258425/9 "2021-01-27T21:10:52Z")

</div>

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