Forum Discussion
Deneb: Multi-layer card
- 3 years ago
Hi Salle,
I assume that you want the text to display the mean value? If so, you need to add a text encoding channel property and put the field in there. So, your snippet of JSON that didn't match Vega-Lite's construct for what I think you want to do should look like this:
"encoding": { "text": { "field": "Diameter Mean" }, "x": {"value": 0}, "y": {"value": 10} }My output now looks as follows:
Revised spec:
{ "data": {"name": "dataset"}, "title": "Information", "background": "lightblue", "height": 400, "width": 200, "layer": [ { "mark": { "type": "text", "align": "left", "baseline": "top", "font": "sans-serif", "fontSize": 24, "dx": 10, "dy": 0, "xOffset": 0, "yOffset": 0 }, "encoding": { "text": { "field": "Diameter Mean" }, "x": {"value": 0}, "y": {"value": 10} } }, { "mark": { "text": "Mean", "type": "text", "align": "left", "baseline": "top", "font": "sans-serif", "fontSize": 16, "dx": 10, "dy": 0, "xOffset": 0, "yOffset": 0 }, "encoding": { "x": {"value": 0}, "y": {"value": 30} } }, { "mark": { "text": "Diameter Median", "type": "text", "baseline": "top", "align": "left", "font": "sans-serif", "fontSize": 16, "dx": 10, "dy": 0, "xOffset": 0, "yOffset": 0 }, "encoding": { "x": {"value": 0}, "y": {"value": 80} } }, { "mark": { "text": "Diameter Max", "type": "text", "baseline": "top", "align": "left", "font": "sans-serif", "fontSize": 16, "dx": 10, "dy": 0, "xOffset": 0, "yOffset": 0 }, "encoding": { "x": {"value": 0}, "y": {"value": 130} } } ] }Regards,
Daniel
Hi Salle,
This may be jumping ahead of your learning journey, but possibly something for you to think about to avoid too much repetition.
If you're just adding measures and your dataset is effectively one row, you can use a fold transform (which is the same as an unpivot in Power Query) and specify your measure names, to get a simpler list of name/value pairs that you can encode just using two marks.
So, if my dataset looks like this...
I can add the following transform...
"transform": [
{
"fold": [
"Diameter Mean",
"Diameter Median",
"Diameter Max"
],
"as": ["measure", "value"]
}
],
...and the resulting dataset (now named as data_0 by Vega-Lite) will look like this:
You'll notice that this is now 3 rows, with additional measure and value columns. You'll get as many rows as entries you specify in the fold operation (as long as they are in your dataset).
The plan here is that I can encode the y channel to measure, which will position vertically by this field, and then I can add a text mark that encodes the text channel to the value column, and another that encodes text to the measure column.
Here's what this spec might look like as a whole:
{
"data": {"name": "dataset"},
"title": "Information",
"background": "lightblue",
"transform": [
{
"fold": [
"Diameter Mean",
"Diameter Median",
"Diameter Max"
],
"as": ["measure", "value"]
}
],
"encoding": {
"y": {
"field": "measure",
"axis": null
}
},
"layer": [
{
"mark": {
"type": "text",
"align": "left",
"baseline": "bottom",
"fontSize": 24
},
"encoding": {
"text": {"field": "value"}
}
},
{
"mark": {
"type": "text",
"align": "left",
"baseline": "top",
"fontSize": 16
},
"encoding": {
"text": {"field": "measure"}
}
}
]
}
And here's the output:
Hopefully this might help you reduce the amount of maintenance you could potentially have with this one. Good luck!
Daniel
EDIT: if you want the y-encoding to sort in the order you type the measures in the fold transform, just add "sort": null to enforce this order (ref). The y-encoding from above would now look as follows:
"encoding": {
"y": {
"field": "measure",
"axis": null,
"sort": null
}
},dm-p:
Daniel, thank you for all the time and effort you put in to guide me through some of the basic elements in Deneb! I really appreciate all the support and tips!
I will certainly look into your suggestion with using fold, I did not know it existed.
But I am starting to learn that anything is possible with Deneb 😉
Cheers/Carl