# Vega transform-stack not calculating correctly

**URL:** <https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604>\
**Category:** Kibana\
**Created:** [October 1, 2018, 7:33pm UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604 "2018-10-01T19:33:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thekm1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thekm1/32/35926_2.png) [@thekm1](https://discuss.elastic.co/u/thekm1)\
**Post date:** [October 1, 2018, 7:33pm UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/1 "2018-10-01T19:33:07Z")

</div>

Hi There, I got a problem,

I am trying to create a chart with VEGA. I want in the x axis the _time_ and in the y axis the number of _doc\_count_. And ofcourse for coolnes i wanted the data in y-axis stacked by the _continent\_code_.  
The stacking creates a y0 and y1 for me, this works fine. But this y0 should start for a new point on the x from 0, but it is having an offset from the last point on x.  
Let me show this in image1:

 ![image1](https://us1.discourse-cdn.com/elastic/original/3X/e/3/e38de9b631e2c1f1ea8c1d4444065b81be33ddf4.png)  
On the 6 elm in the table array there is a new date so the y0 should be 0 but it is taking the value from table[5].y1  
And this follows that my chart looks a bit stupid. Let me show you that in image 2: ![image2](https://us1.discourse-cdn.com/elastic/original/3X/0/1/01e9dd60402fb40099a76fb6a53ae985d5ebe407.png)

And this is my code :

```
{
  "$schema": "https://vega.github.io/schema/vega/v3.json",
  "padding": 5,
  "data": [
    {
      "name": "table",
      "url": {
        "%context%": true,
        "%timefield%": "@timestamp",
        "index": "demo_logs_server*",
        "body": {
          "size": 0,
          "aggs": {
            "logs_time_country": {
              "composite": {
                "size": 50,
                "sources": [
                  {
                    "time": {
                      "date_histogram": {
                        "field": "@timestamp",
                        "interval": {"%autointerval%":200},
                        "format": "dd-MM"
                      }
                    }
                  },
                  {
                    "country": {
                      "terms": {
                        "field": "geoip.continent_code.keyword"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      },
      "format": {
        "property": "aggregations.logs_time_country.buckets"
      },
      "transform": [
        {
          "type": "stack",
          "groupby": ["time"],
          "sort": {"field":"country"},
          "field": "doc_count"
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "point",
      "range": "width",
      "domain": {"data": "table", "field": "key.time"}
    },
    {
      "name": "y",
      "type": "linear",
      "range": "height",
      "nice": true, "zero": true,
      "domain": {"data": "table", "field": "y1"}
    },
    {
      "name": "color",
      "type": "ordinal",
      "range": "category",
      "domain": {"data": "table", "field": "key.country"}
    }
  ],
  "axes": [
    {"orient": "bottom", "scale": "x", "zindex": 1},
    {"orient": "left", "scale": "y", "zindex": 1}
  ],
  "marks": [
    {
      "type": "group",
      "from": {
        "facet": {
          "name": "series",
          "data": "table",
          "groupby": "key.country"
        }
      },
      "marks": [
        {
          "type": "area",
          "from": {"data": "series"},
          "encode": {
            "enter": {
              "interpolate": {"value": "linear"},
              "x": {"scale": "x", "field": "key.time"},
              "y": {"scale": "y", "field": "y0"},
              "y2": {"scale": "y", "field": "y1"},
              "fill": {"scale": "color", "field": "key.country"}
            },
            "update": {
              "fillOpacity": {"value": 1}
            },
            "hover": {
              "fillOpacity": {"value": 0.5}
            }
          }
        }
      ]
    }
  ]
}

```

I think the mistake is somewhere in the transform-stack cause this is the part which is calculating the y0 and y1.

Will appriciate any kind of help  
Thanks folks

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [October 1, 2018, 8:46pm UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/2 "2018-10-01T20:46:45Z")

</div>

Hey @thekm1, can you share the data returned from Elasticsearch which the vega visualization uses in a github gist?

---

<div class="post-metadata">

**Author:** ![thekm1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thekm1/32/35926_2.png) [@thekm1](https://discuss.elastic.co/u/thekm1)\
**Post date:** [October 2, 2018, 5:53am UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/3 "2018-10-02T05:53:19Z")

</div>

Hi Brandon, Thanks for answering.  
I just took away the sort attribute from transform-stack  
now its like

```
  "transform": [
    {
      "type": "stack",
      "groupby": ["time"],
      "field": "doc_count"
    }
  ]

```

This made a better graph.. got till the stacking problem, but don't have have the blue areas that's crossing the graph as they like...  
see image :

 ![41](https://us1.discourse-cdn.com/elastic/original/3X/1/e/1efc54866fcddaf5fb0f6e32ff13ee49e550ff7d.png)  
And i reduced the size of the composite aggr from 50 to 33. this gives me a shorter response that i can publish in gist.  
my new aggr:

```
"body": {
  "size": 0,
  "aggs": {
    "logs_time_country": {
      "composite": {
        "size": 33,
        "sources": [
          {
            "time": {
              "date_histogram": {
                "field": "@timestamp",
                "interval": {"%autointerval%":200},
                "format": "dd-MM"
              }
            }
          },
          {
            "country": {
              "terms": {
                "field": "geoip.continent_code.keyword"
              }
            }
          }
        ]
      }
    }
  }
}

```

And here is the response from elastic search that you asked for 🙂

```
"[
  {
    "key": {
      "time": "31-03",
      "country": "AF"
    },
    "doc_count": 1,
    "y0": 0,
    "y1": 1
  },
  {
    "key": {
      "time": "31-03",
      "country": "AS"
    },
    "doc_count": 18,
    "y0": 1,
    "y1": 19
  },
  {
    "key": {
      "time": "31-03",
      "country": "EU"
    },
    "doc_count": 40,
    "y0": 19,
    "y1": 59
  },
  {
    "key": {
      "time": "31-03",
      "country": "NA"
    },
    "doc_count": 183,
    "y0": 59,
    "y1": 242
  },
  {
    "key": {
      "time": "31-03",
      "country": "OC"
    },
    "doc_count": 4,
    "y0": 242,
    "y1": 246
  },
  {
    "key": {
      "time": "31-03",
      "country": "SA"
    },
    "doc_count": 9,
    "y0": 246,
    "y1": 255
  },
  {
    "key": {
      "time": "01-04",
      "country": "AF"
    },
    "doc_count": 14,
    "y0": 255,
    "y1": 269
  },
  {
    "key": {
      "time": "01-04",
      "country": "AS"
    },
    "doc_count": 993,
    "y0": 269,
    "y1": 1262
  },
  {
    "key": {
      "time": "01-04",
      "country": "EU"
    },
    "doc_count": 679,
    "y0": 1262,
    "y1": 1941
  },
  {
    "key": {
      "time": "01-04",
      "country": "NA"
    },
    "doc_count": 1329,
    "y0": 1941,
    "y1": 3270
  },
  {
    "key": {
      "time": "01-04",
      "country": "OC"
    },
    "doc_count": 59,
    "y0": 3270,
    "y1": 3329
  },
  {
    "key": {
      "time": "01-04",
      "country": "SA"
    },
    "doc_count": 26,
    "y0": 3329,
    "y1": 3355
  },
  {
    "key": {
      "time": "01-04",
      "country": "AF"
    },
    "doc_count": 23,
    "y0": 3355,
    "y1": 3378
  },
  {
    "key": {
      "time": "01-04",
      "country": "AS"
    },
    "doc_count": 566,
    "y0": 3378,
    "y1": 3944
  },
  {
    "key": {
      "time": "01-04",
      "country": "EU"
    },
    "doc_count": 1099,
    "y0": 3944,
    "y1": 5043
  },
  {
    "key": {
      "time": "01-04",
      "country": "NA"
    },
    "doc_count": 2248,
    "y0": 5043,
    "y1": 7291
  },
  {
    "key": {
      "time": "01-04",
      "country": "OC"
    },
    "doc_count": 24,
    "y0": 7291,
    "y1": 7315
  },
  {
    "key": {
      "time": "01-04",
      "country": "SA"
    },
    "doc_count": 59,
    "y0": 7315,
    "y1": 7374
  },
  {
    "key": {
      "time": "02-04",
      "country": "AF"
    },
    "doc_count": 15,
    "y0": 7374,
    "y1": 7389
  },
  {
    "key": {
      "time": "02-04",
      "country": "AS"
    },
    "doc_count": 638,
    "y0": 7389,
    "y1": 8027
  },
  {
    "key": {
      "time": "02-04",
      "country": "EU"
    },
    "doc_count": 565,
    "y0": 8027,
    "y1": 8592
  },
  {
    "key": {
      "time": "02-04",
      "country": "NA"
    },
    "doc_count": 1151,
    "y0": 8592,
    "y1": 9743
  },
  {
    "key": {
      "time": "02-04",
      "country": "OC"
    },
    "doc_count": 57,
    "y0": 9743,
    "y1": 9800
  },
  {
    "key": {
      "time": "02-04",
      "country": "SA"
    },
    "doc_count": 21,
    "y0": 9800,
    "y1": 9821
  },
  {
    "key": {
      "time": "02-04",
      "country": "AF"
    },
    "doc_count": 20,
    "y0": 9821,
    "y1": 9841
  },
  {
    "key": {
      "time": "02-04",
      "country": "AS"
    },
    "doc_count": 1517,
    "y0": 9841,
    "y1": 11358
  },
  {
    "key": {
      "time": "02-04",
      "country": "EU"
    },
    "doc_count": 1925,
    "y0": 11358,
    "y1": 13283
  },
  {
    "key": {
      "time": "02-04",
      "country": "NA"
    },
    "doc_count": 1482,
    "y0": 13283,
    "y1": 14765
  },
  {
    "key": {
      "time": "02-04",
      "country": "OC"
    },
    "doc_count": 56,
    "y0": 14765,
    "y1": 14821
  },
  {
    "key": {
      "time": "02-04",
      "country": "SA"
    },
    "doc_count": 39,
    "y0": 14821,
    "y1": 14860
  },
  {
    "key": {
      "time": "03-04",
      "country": "AF"
    },
    "doc_count": 61,
    "y0": 14860,
    "y1": 14921
  },
  {
    "key": {
      "time": "03-04",
      "country": "AS"
    },
    "doc_count": 1814,
    "y0": 14921,
    "y1": 16735
  },
  {
    "key": {
      "time": "03-04",
      "country": "EU"
    },
    "doc_count": 2490,
    "y0": 16735,
    "y1": 19225
  }
]"

```

Thanks a lot for helping  
regards thekm1

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [October 2, 2018, 10:58pm UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/4 "2018-10-02T22:58:09Z")

</div>

@nyuriks - can u plz help ?

Cheers  
Rashmi

---

<div class="post-metadata">

**Author:** ![thekm1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thekm1/32/35926_2.png) [@thekm1](https://discuss.elastic.co/u/thekm1)\
**Post date:** [October 3, 2018, 4:45am UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/5 "2018-10-03T04:45:43Z")

</div>

Thankyou rashmi

I got it. It was a mistake that i made in the transform options. As i declared the field _groupby_ and _sort_ i hadn't give the accessing of the array. cause the time and country that i given before is in the key array. so need to access the array.  
So i think my problem is solved.

Thankyou to everybody for the support. =)  
The result :

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

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [October 3, 2018, 4:41pm UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/6 "2018-10-03T16:41:24Z")

</div>

Great . Thanks for posting the solution you arrived at.

Cheers  
Rashmi

---

<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:** [October 31, 2018, 4:41pm UTC](https://discuss.elastic.co/t/vega-transform-stack-not-calculating-correctly/150604/7 "2018-10-31T16:41:30Z")

</div>

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