How to set maximum degree scale in x-axis

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

{
  "$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?

Not sure if this will work, but can you try setting a scale domain, similar to this Stack Overflow answer?

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

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

1 Like

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

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

Fair, size is a standard param for all queries and aggregations. It's not in the cumulative_sum docs, but it is listed in other places such as as the aggregation top level page. Apologies for the confusion there!

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

Did you tried to put these parameters after

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

Yes,just look at my lastest code

{
  "$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

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.

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

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?

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

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

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]

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