Histogramme grouped by modalities and sum of modalities kibana

Hello, I want to reproduce an analysis like that.

I want to reproduce an analysis with the vega or vega-lite kibana plugin. I would like to associate the total of the sites each day. Here is my vega code. If possible associate an interactive legend with a multiple selection.

Here is the code I want to transform

##############################################################

{

  "$schema": "https://vega.github.io/schema/vega/v4.json",

  "width": 300,

  "height": 240,

  "padding": 5,

 "data": [

{

"name": "table",

"values": [

{"jour":"A", "site":0, "nb_passage":0.1},

{"jour":"A", "site":1, "nb_passage":0.6},

{"jour":"A", "site":2, "nb_passage":0.9},

{"jour":"A", "site":3, "nb_passage":0.4},

{"jour":"B", "site":0, "nb_passage":0.7},

{"jour":"B", "site":1, "nb_passage":0.2},

{"jour":"B", "site":2, "nb_passage":1.1},

{"jour":"B", "site":3, "nb_passage":0.8},

{"jour":"C", "site":0, "nb_passage":0.6},

{"jour":"C", "site":1, "nb_passage":0.1},

{"jour":"C", "site":2, "nb_passage":0.2},

{"jour":"C", "site":3, "nb_passage":0.7}

],

"transform": [

{ "type": "joinaggregate",

"fields": [ "np_passage"],

"ops": [ "sum"],

"as": [ "m"]

},

{"type":"formula", "expr": "datum.nb_passage", "as":"nb_passage"},

{"type":"formula", "expr": "datum.jour", "as": "jour"}

]

}

],

"scales": [

{

"name": "xscale",

"type": "band",

"domain": {"data": "table", "field": "jour"},

"range": "width",

"padding": 0.2

},

{

"name": "yscale",

"type": "linear",

"domain": {"data": "table", "field": "nb_passage"},

"range": "height",

"round": true,

"zero": true,

"nice": true

},

{

"name": "color",

"type": "ordinal",

"domain": {"data": "table", "field": "site"},

"range": {"scheme": "category20"}

}

],

"axes": [

{"orient": "left", "scale": "yscale", "tickSize": 6, "labelPadding": 4, "zindex": 1},

{"orient": "bottom", "scale": "xscale"}

],

"marks": [

{

"type": "group",

"clip": true,

"foreground" : false,

"from": {

"facet": {

"data": "table",

"name": "facet",

"groupby": ["jour"]

}

},

"encode": {

"enter": {

"x": {"scale": "xscale", "field": "jour"}

}

},

"signals": [

{"name": "width", "update": "bandwidth('xscale')"}

],

"scales": [

{

"name": "pos",

"type": "band",

"range": "width",

"domain": {"data": "facet", "field": "site"}

}

],

"marks": [

{

"name": "bars",

"from": {"data": "facet"},

"type": "rect",

"encode": {

"enter": {

"x": {"scale": "pos", "field": "site"},

"width": {"scale": "pos", "band": 1},

"y": {"scale": "yscale", "field": "nb_passage"},

"y2": {"scale": "yscale", "value": 0},

"fill": {"scale": "color", "field": "site"}

}

}

},

{

"type": "text",

"from": {"data": "bars"},

"encode": {

"enter": {

"y": {"field": "y", "offset": -4},

"x": {"field": "x", "offset": {"field": "width", "mult": 0.3}},

"fill": [

{"value": "black"}

],

"align": {"value": "left"},

"baseline": {"value": "middle"},

"text": {"field": "datum.nb_passage"},

"angle":{"value": -55},

"fontSize": {"value": 10}

}

}

}

]

}

]

}

Thank you

I'm not sure I understand your question. What have you tried, and what is not working?

Excuse me for my english.

I want to create a histogram which displays the quantitative variables "nb_passage" of "sites" 0, 1, 2, 3 combined with the total amount for each "jour" modality: "A", "B" and "C".

Okay, I now see what you are trying to do. I see that you have tried doing this in Vega. Can you be a bit more specific about the part you need help with?

Thank you for your interest on topic. First, I would like to make a dynamic aggregate which calculates the sum of the sites then combine the sum of "nb_passage" and "nb_passage" for each site. how to do it without adding by hand? I mean by dynamic computes fields. After, make a dynamic legend. do you have an idea? Thank you.

It is hard to help you without knowing:

  • What have you tried?
  • What data do you have to work with?

Or any other details that might be relevant.

Hi,

What I tried, you got it by my code posted for the topic.
The data can be found in the posted vega script.
My difficulty is to calculate the sum of the "nb-passage" grouped by the variable "day".

At the end, I want to have a histogram which combines for each "day", the "nb_passage" by "site" and the sum of "nb_passage" for all "site".

here is my vega script that I had to do,

##############################################################

{

"$schema": "https://vega.github.io/schema/vega/v4.json",

"width": 300,

"height": 240,

"padding": 5,

"data": [

{

"name": "table",

"values": [

{"jour":"A", "site":0, "nb_passage":0.1},

{"jour":"A", "site":1, "nb_passage":0.6},

{"jour":"A", "site":2, "nb_passage":0.9},

{"jour":"A", "site":3, "nb_passage":0.4},

{"jour":"B", "site":0, "nb_passage":0.7},

{"jour":"B", "site":1, "nb_passage":0.2},

{"jour":"B", "site":2, "nb_passage":1.1},

{"jour":"B", "site":3, "nb_passage":0.8},

{"jour":"C", "site":0, "nb_passage":0.6},

{"jour":"C", "site":1, "nb_passage":0.1},

{"jour":"C", "site":2, "nb_passage":0.2},

{"jour":"C", "site":3, "nb_passage":0.7}

],

"transform": [

{ "type": "joinaggregate",

"fields": [ "np_passage"],

"ops": [ "sum"],

"as": [ "m"]

},

{"type":"formula", "expr": "datum.nb_passage", "as":"nb_passage"},

{"type":"formula", "expr": "datum.jour", "as": "jour"}

]

}

],

"scales": [

{

"name": "xscale",

"type": "band",

"domain": {"data": "table", "field": "jour"},

"range": "width",

"padding": 0.2

},

{

"name": "yscale",

"type": "linear",

"domain": {"data": "table", "field": "nb_passage"},

"range": "height",

"round": true,

"zero": true,

"nice": true

},

{

"name": "color",

"type": "ordinal",

"domain": {"data": "table", "field": "site"},

"range": {"scheme": "category20"}

}

],

"axes": [

{"orient": "left", "scale": "yscale", "tickSize": 6, "labelPadding": 4, "zindex": 1},

{"orient": "bottom", "scale": "xscale"}

],

"marks": [

{

"type": "group",

"clip": true,

"foreground" : false,

"from": {

"facet": {

"data": "table",

"name": "facet",

"groupby": ["jour"]

}

},

"encode": {

"enter": {

"x": {"scale": "xscale", "field": "jour"}

}

},

"signals": [

{"name": "width", "update": "bandwidth('xscale')"}

],

"scales": [

{

"name": "pos",

"type": "band",

"range": "width",

"domain": {"data": "facet", "field": "site"}

}

],

"marks": [

{

"name": "bars",

"from": {"data": "facet"},

"type": "rect",

"encode": {

"enter": {

"x": {"scale": "pos", "field": "site"},

"width": {"scale": "pos", "band": 1},

"y": {"scale": "yscale", "field": "nb_passage"},

"y2": {"scale": "yscale", "value": 0},

"fill": {"scale": "color", "field": "site"}

}

}

},

{

"type": "text",

"from": {"data": "bars"},

"encode": {

"enter": {

"y": {"field": "y", "offset": -4},

"x": {"field": "x", "offset": {"field": "width", "mult": 0.3}},

"fill": [

{"value": "black"}

],

"align": {"value": "left"},

"baseline": {"value": "middle"},

"text": {"field": "datum.nb_passage"},

"angle":{"value": -55},

"fontSize": {"value": 10}

}

}

}

]

}

]

}

################################################################
In this script you have the data I want to work on.

"data": [

{

"name": "table",

"values": [

{"jour":"A", "site":0, "nb_passage":0.1},

{"jour":"A", "site":1, "nb_passage":0.6},

{"jour":"A", "site":2, "nb_passage":0.9},

{"jour":"A", "site":3, "nb_passage":0.4},

{"jour":"B", "site":0, "nb_passage":0.7},

{"jour":"B", "site":1, "nb_passage":0.2},

{"jour":"B", "site":2, "nb_passage":1.1},

{"jour":"B", "site":3, "nb_passage":0.8},

{"jour":"C", "site":0, "nb_passage":0.6},

{"jour":"C", "site":1, "nb_passage":0.1},

{"jour":"C", "site":2, "nb_passage":0.2},

{"jour":"C", "site":3, "nb_passage":0.7}

],

The best I can do is point out this blog post with a bunch of Vega examples: https://www.elastic.co/blog/custom-vega-visualizations-in-kibana

Thanks for the link, I have already explored this link. The difficulty lies in the transformation of the data to have the histogram

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