# How to set maximum degree scale in x-axis

**URL:** https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390
**Category:** Kibana
**Tags:** vega
**Created:** [February 1, 2023, 6:44am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390 "2023-02-01T06:44:13Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![justseeyouagain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justseeyouagain/32/116592_2.png) [@justseeyouagain](https://discuss.elastic.co/u/justseeyouagain)
#### Post date: [February 1, 2023, 6:44am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/1 "2023-02-01T06:44:13Z")

</div>

Hello,  
I am using vega-lite to finish my graph, but meet something confusion that how to set maximum degree scale in x-axis, just look at my pic

 ![download](https://us1.discourse-cdn.com/elastic/original/3X/7/5/756d69d261cf1f322cf5b0a41c611d6a3e9f1c90.png)

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "data": {
    "url": {
      "index": "projectindex-*",
      "body": {
        "query": {"match_all": {}},
        "size": 0,
        "aggs": {
          "group_by_date": {
            "date_histogram": {
              "field": "timestamp",
              "interval": "day"
            },
            "aggs": {
              "cumulative_docs": {
                "cumulative_sum": {"buckets_path": "_count"}
              }
            }
          }
        }
      }
    },
    "format": {
      "property": "aggregations.group_by_date.buckets"
    }
  },
  "layer": [
    {
    "mark": {
        "type": "line",
        "point": {"filled": false, "fill": "black"}
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "middle",
        "fontSize": 15
      },
      "encoding": {
        "text": {"field": "cumulative_docs.value", "type": "quantitative"}
      }
    }
  ],
  "encoding": {
    "x": {
      "axis": {"title": "day"},
      "field": "key_as_string",
      "type": "nominal"
    },
    "y": {
      "aggregate": "y",
      "axis": {"title": "projects"},
      "field": "cumulative_docs.value",
      "type": "quantitative",
      "stack": "normalize"
    }
  }
}

```

My x-axis data is too large, I tried to cut my data from ES DSL, but it will impact my data integrity，because cumulative\_sum and this aggs not size paramter, it I wanna adjust my size of x-axis in near 30 days, how can I fix my code?

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [February 1, 2023, 9:51am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/2 "2023-02-01T09:51:05Z")

</div>

Not sure if this will work, but can you try s[etting a scale domain, similar to this Stack Overflow answer](https://stackoverflow.com/questions/55134128/setting-a-maximum-axis-value-in-vega-lite)?

---

<div class="post-metadata">

### Author: ![justseeyouagain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justseeyouagain/32/116592_2.png) [@justseeyouagain](https://discuss.elastic.co/u/justseeyouagain)
#### Post date: [February 1, 2023, 1:51pm UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/3 "2023-02-01T13:51:39Z")

</div>

thanks, I tried this solution long time ago, this solution can self-define a scale domain, but my circumstance needs dynamic x-axis, for example I wanna display the data within 30 days from now, so I read most of vega-lite official document and not find any useful params

---

<div class="post-metadata">

### Author: ![Stratoula\_Kalafateli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stratoula_kalafateli/32/70923_2.png) [@Stratoula\_Kalafateli](https://discuss.elastic.co/u/Stratoula_Kalafateli)
#### Post date: [February 1, 2023, 1:59pm UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/4 "2023-02-01T13:59:34Z")

</div>

Hey, you can set a variable in vega spec `%timefield%: date_field` so now vega filters the data with the time span that is selected on the timepicker.

Check the documentation here [Vega | Kibana Guide [8.6] | Elastic](https://www.elastic.co/guide/en/kibana/current/vega.html)

---

<div class="post-metadata">

### Author: ![justseeyouagain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justseeyouagain/32/116592_2.png) [@justseeyouagain](https://discuss.elastic.co/u/justseeyouagain)
#### Post date: [February 2, 2023, 2:25am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/6 "2023-02-02T02:25:50Z")

</div>

I found it works, but if I use this solution and `cumulative_sum` will aggregations base on the result of query, it will lost the day which not contain

---

<div class="post-metadata">

### Author: ![justseeyouagain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justseeyouagain/32/116592_2.png) [@justseeyouagain](https://discuss.elastic.co/u/justseeyouagain)
#### Post date: [February 2, 2023, 2:31am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/7 "2023-02-02T02:31:50Z")

</div>

So the best idea is cut of the result after using `cumulative_sum` like params `size`, but I not found this param in `cumulative_sum` offcial doc, and the second way is to set the x-axis maximum degree scale lenght in vega-lite, how pity I'm not found it too

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [February 2, 2023, 10:25am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/8 "2023-02-02T10:25:46Z")

</div>

Fair, `size` is a standard param for all queries and aggregations. It's not in the [`cumulative_sum`](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/search-aggregations-pipeline-cumulative-sum-aggregation.html) docs, but it is listed in other places such as as the [`aggregation top level page`](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/search-aggregations.html#return-only-agg-results). Apologies for the confusion there!

Glad you've found a couple of solutions for your issue!

---

<div class="post-metadata">

### Author: ![plus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plus/32/97297_2.png) [@plus](https://discuss.elastic.co/u/plus)
#### Post date: [February 2, 2023, 11:39am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/9 "2023-02-02T11:39:55Z")

</div>

Did you tried to put these parameters after

```auto
> url:{
> "%context%": true,
> "%timefield%": "timestamp"

```

---

<div class="post-metadata">

### Author: ![justseeyouagain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justseeyouagain/32/116592_2.png) [@justseeyouagain](https://discuss.elastic.co/u/justseeyouagain)
#### Post date: [February 3, 2023, 5:04am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/10 "2023-02-03T05:04:02Z")

</div>

Yes，just look at my lastest code

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "data": {
    "url": {
      %context%: true,
      %timefield%: timestamp,
      "index": "projectindex-*",
      "body": {
        "size": 0,
        "aggs": {
          "group_by_date": {
            "date_histogram": {
              "field": "timestamp",
              "interval": {%autointerval%: 10},
              extended_bounds: {
                    min: "now-1d",
                    max: "now"
                  }
            },
            "aggs": {
              "cumulative_docs": {
                "cumulative_sum": {"buckets_path": "_count"}
              }
            }
          }
        }
      }
    },
    "format": {
      "property": "aggregations.group_by_date.buckets"
    }
  },
  "layer": [
    {
    "mark": {
        "type": "line",
        "point": {"filled": false, "fill": "black"}
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "middle",
        "fontSize": 15
      },
      "encoding": {
        "text": {"field": "cumulative_docs.value", "type": "quantitative"}
      }
    }
  ],
  "encoding": {
    "x": {
      "axis": {"title": "date"},
      "field": "key_as_string",
      "type": "nominal"
    },
    "y": {
      "aggregate": "y",
      "axis": {"title": "projects"},
      "field": "cumulative_docs.value",
      "type": "quantitative",
      "stack": "normalize"
    }
  }
}

```

It generate this pic

 ![download](https://us1.discourse-cdn.com/elastic/original/3X/5/e/5eacb1d41772aa42530b88c3a0e6d49dc6834fb8.png)

Now I wanna using `extended_bounds` control its date range and `"interval": {%autointerval%: 10}` control its spot count in x-axis, but it doesn't work..

But I have other question, if `%timefield%` works, whether it effect the result of aggregation in `cumulative_sum`, `cumulative_sum` in my project need aggregate 3 month data even more but display only near 30 days , so if `date_histogram` aggregation just filter near 30 days data, I fear that the finnal result is fault.

this pic is filter datetime before, you can see the highest y-axis and the lastest time is about 1000.

 ![download (1)](https://us1.discourse-cdn.com/elastic/original/3X/f/e/fe3fcae05cb39d685a71e4cebfa9b3086d2617a8.png)

this pic is filter datetime after, the highest y-axis is become 200 about,

 ![download (2)](https://us1.discourse-cdn.com/elastic/original/3X/8/9/89c19056d18df44b849de3ae37ca20dcbefd44fb.png)

so its finnal result is effected, I wanna solve this problem that if I display only 30 days in x-axis,  
how can I make the finnal result right?

---

<div class="post-metadata">

### Author: ![plus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plus/32/97297_2.png) [@plus](https://discuss.elastic.co/u/plus)
#### Post date: [February 3, 2023, 9:20am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/11 "2023-02-03T09:20:38Z")

</div>

Hello, I not able to understand your last sentence: ""how can I make the finnal result right?"" could you explain it to try to help you? Thanks

---

<div class="post-metadata">

### Author: ![justseeyouagain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justseeyouagain/32/116592_2.png) [@justseeyouagain](https://discuss.elastic.co/u/justseeyouagain)
#### Post date: [February 4, 2023, 3:22am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/12 "2023-02-04T03:22:00Z")

</div>

Thanks your help,

if I filter data in the stage of `query` before `cumulative_sum`, I will get Wrong result finnaly, Why? Look at this table below

 ![download](https://us1.discourse-cdn.com/elastic/original/3X/8/7/87afb0f34ae50c64d2d1504193c4d3f1a86f10b6.png)

If I use `cumulative_sum` in `number` field with full data, it will return `[1，4，9，16]`,

but if I filter data from 2022/10/12 to 2022/10/13 in `query` stage and use `cumulative_sum`, it will return `[5，12]`

In a word, I dont want this return `[1，4，9，16]` or `[5，12]`, I want `[9，16]` which is `right anwser`.

But now something embarrassed me that how should I make this code only return `[9，16]`

---

<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: [March 4, 2023, 3:22am UTC](https://discuss.elastic.co/t/how-to-set-maximum-degree-scale-in-x-axis/324390/13 "2023-03-04T03:22:29Z")

</div>

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