# How to plot speed derived from time and distance using aggregation

**URL:** <https://discuss.elastic.co/t/how-to-plot-speed-derived-from-time-and-distance-using-aggregation/252861>\
**Category:** Kibana\
**Created:** [October 21, 2020, 3:04pm UTC](https://discuss.elastic.co/t/how-to-plot-speed-derived-from-time-and-distance-using-aggregation/252861 "2020-10-21T15:04:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NiclasK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niclask/32/47189_2.png) [@NiclasK](https://discuss.elastic.co/u/NiclasK)\
**Post date:** [October 21, 2020, 3:04pm UTC](https://discuss.elastic.co/t/how-to-plot-speed-derived-from-time-and-distance-using-aggregation/252861/1 "2020-10-21T15:04:43Z")

</div>

Hi,

I have an query that calculates average speed for different car makes based on the fields 'distance' and 'time' in travel documents in Elasticsearch:

```auto
POST /test_data/_search
{
	"size": 0,
	"aggs": {
		"speed_per_make": {
			"terms": {
				"field": "CarMake"
			},
			"aggs": {
				"sum_dist": {
					"sum": {
						"field": "DistanceCovered"
					}
				},
				"sum_time": {
					"sum": {
						"field": "DrivingDurationHours"
					}
				},
				"division": {
					"bucket_script": {
						"buckets_path": {
							"var_sum_dist": "sum_dist",
							"var_sum_time": "sum_time"
						},
						"script": "params.var_dist / params.var_time"
					}
				}
			}
		}
	}
}

```

For each car make it sums the distance and time, and then calculates the speed.  
How can I plot this in a vertical bar chart?

Thanks!

---

<div class="post-metadata">

**Author:** ![lukeelmers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukeelmers/32/35230_2.png) [@lukeelmers](https://discuss.elastic.co/u/lukeelmers)\
**Post date:** [October 21, 2020, 8:20pm UTC](https://discuss.elastic.co/t/how-to-plot-speed-derived-from-time-and-distance-using-aggregation/252861/2 "2020-10-21T20:20:34Z")

</div>

Kibana core visualizations [do not yet support bucket script aggregations](https://github.com/elastic/kibana/issues/4707), however you should be able to visualize this in TSVB if you are okay using a line chart instead of a bar chart.

Edit: adding a screengrab for clarity

 ![Screen Shot 2020-10-21 at 2.21.24 PM](https://us1.discourse-cdn.com/elastic/original/3X/9/9/990feb8a0303ba806098a95a78d32f8ad57288fb.png)

---

<div class="post-metadata">

**Author:** ![NiclasK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niclask/32/47189_2.png) [@NiclasK](https://discuss.elastic.co/u/NiclasK)\
**Post date:** [October 22, 2020, 9:22am UTC](https://discuss.elastic.co/t/how-to-plot-speed-derived-from-time-and-distance-using-aggregation/252861/3 "2020-10-22T09:22:01Z")

</div>

Thanks for the quick reply!

I got it working by using vega. I can now display the average speed as a bar chart.

I will try TSVB too.

---

<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:** [November 19, 2020, 9:22am UTC](https://discuss.elastic.co/t/how-to-plot-speed-derived-from-time-and-distance-using-aggregation/252861/4 "2020-11-19T09:22:05Z")

</div>

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