How to align 0 value in multiple y axis layered chart in Vega-lite? - vega-lite

In Vega-lite, I want to align y-axis values, so that zeroes are in the same place and negative values go below x-axis. Is it possible to do in Vega-lite? I can't hardcode the domain limits, as the data passed to this chart is dynamic, thus can be different every time.
My code:
{
"$schema":"https://vega.github.io/schema/vega-lite/v5.json",
"width":300,
"data":{
"values":[
{
"key":"Type A",
"sum1":"1000",
"sum2":"2000",
"ratio":"15"
},
{
"key":"Type B",
"sum1":"1500",
"sum2":"1500",
"ratio":"-5"
},
{
"key":"Type C",
"sum1":"2000",
"sum2":"1000",
"ratio":"7"
}
]
},
"layer":[
{
"mark":{
"type":"bar",
"size":15,
"xOffset":0
},
"encoding":{
"y":{
"field":"sum1",
"type":"quantitative",
"axis":{
"title":null,
"ticks":true,
"orient":"left"
}
},
"x":{
"field":"key",
"type":"nominal",
"axis":{
"title":null,
"labelAngle":-45,
"labelLimit":1000
}
},
"color":{
"datum":"Sum1"
}
}
},
{
"mark":{
"type":"bar",
"size":15,
"xOffset":15
},
"encoding":{
"y":{
"field":"sum2",
"type":"quantitative",
"axis":{
"domain":false,
"title":null,
"labels":false,
"ticks":false
}
},
"x":{
"field":"key",
"type":"nominal",
"axis":{
"title":null,
"labelAngle":-45,
"labelLimit":1000
}
},
"color":{
"datum":"Sum2"
}
}
},
{
"mark":{
"type":"line",
"interpolate":"monotone",
"clip":true
},
"encoding":{
"y":{
"type":"quantitative",
"field":"ratio",
"axis":{
"title":null,
"ticks":true,
"orient":"right"
}
},
"x":{
"field":"key",
"type":"nominal",
"axis":{
"title":null,
"labelAngle":-45,
"labelLimit":1000
}
},
"color":{
"datum":"Ratio"
}
}
}
],
"resolve":{
"scale":{
"y":"independent"
}
},
"config":{
"legend":{
"orient":"bottom",
"columns":3,
"labelLimit":500,
"layout":{
"bottom":{
"anchor":"middle"
}
}
}
}
}
Open the Chart in the Vega Editor

Related

Change Canvas Page Colour and Chart Background Colour in JSON Themes for Power BI Template

I want to prepare a default theme and have written the JSON file below for powerbi dashboards. But I don't know how to:
Change the background colour for the canvas to white?
Change the background colour of the page (Canvas colour) to white?
Change the border colour of all visuals to black
We will then push this file to be the default theme for all of our dashboards.
{
"name": "Bardz",
"dataColors": [
"#173583",
"#7DCCE0",
"#26BCD7",
],
"minimum": "#C7F0FA",
"center": "#7CD7E8",
"maximum": "#3EAEFD",
"neutral": "#F58220",
"foreground": "#000000",
"textClasses": {
"label": {
"fontFace": "'Segoe UI', wf_segoe-ui_normal, helvetica, arial, sans-serif",
"fontSize": 9,
"color": "#040404"
},
"callout": {
"fontFace": "'Segoe UI Semibold', wf_segoe-ui_semibold, helvetica, arial, sans-serif",
"fontSize": 15
},
"title": {
"fontFace": "'Segoe UI Light', wf_segoe-ui_light, helvetica, arial, sans-serif",
"fontSize": 10
},
"header": { "fontSize": 11 }
},
"visualStyles": {
"*": {
"*": {
"background": [
{ "color": { "solid": { "color": "#E1E1E1" } }, "transparency": 5 }
],
"border": [
{
"color": { "solid": { "color": "#000000" } },
"show": true,
"radius": 1
}
],
"outspacePane": [
{
"backgroundColor": { "solid": { "color": "#FFFEFE" } },
"transparency": 15,
"foregroundColor": { "solid": { "color": "#000000" } }
}
],
"filterCard": [
{
"$id": "Applied",
"backgroundColor": { "solid": { "color": "#E4E4E4" } },
"transparency": 18
},
{
"$id": "Available",
"backgroundColor": { "solid": { "color": "#F2F2F2" } },
"transparency": 13
}
],
"visualHeader": [
{
"transparency": 35,
"foreground": { "solid": { "color": "#FFFFFF" } },
"border": { "solid": { "color": "#000000" } }
}
],
"visualTooltip": [
{
"titleFontColor": { "solid": { "color": "#454545" } },
"valueFontColor": { "solid": { "color": "#000000" } },
"background": { "solid": { "color": "#C7C5C5" } }
}
],
"visualHeaderTooltip": [
{
"titleFontColor": { "solid": { "color": "#454545" } },
"background": { "solid": { "color": "#C7C5C5" } }
}
]
}
},
"scatterChart": {
"*": {
"fillPoint": [
{
"show": true
}
],
"bubbles": [
{
"bubbleSize": -10
}
],
"background": [
{ "color": { "solid": { "color": "#FFFFFF" } }, "transparency": 100 }
]
}
},
"page": {
"*": {
"background": [
{ "color": { "solid": { "color": "#F9F9F9" } }, "transparency": 89 }
],
"outspace": [
{ "color": { "solid": { "color": "#FAFAFA" } }, "transparency": 14 }
]
}
}
}
}

Elasticsearch: get the max and min value form a specific object of a maps of objects

I has this mapping for the índex on elastic, i was try to get the max value of a day for a specific sensor, but my query get the value of all the sensors.
"sensor": {
"type": "nested",
"properties": {
"type": {
"type": "integer"
},
"name": {
"type": "keyword"
},
"number": {
"type": "integer"
},
"values": {
"type": "nested",
"properties": {
"type": {
"type": "text"
},
"value": {
"type": "float"
},
"unit": {
"type": "text"
},
"Lmin": {
"type": "float"
},
"Lmax": {
"type": "float"
}
}
}
}
An this is the map of objects,
I need only the max and the min value of the las day from the sensor number 13, i try it but ever i get the max of all sensors.
{"query": {
"nested": {
"path": "sensor",
"query": {
"nested": {
"path": "sensor.values",
"query": {
"bool": {
"must": [
{
"match": {
"sensor.values.type": "TEMPERATURE"
}
}
]
}
}
}
}
}
},
"aggs": {
"agg_by_type": {
"nested": {
"path": "sensor.values"
},
"aggs": {
"max_value": {
"max": {
"field": "sensor.values.value"
}
}
}
}
}
}
I'm new in elasticsearch, can someone help whit this please?, thanks.
You need to also add the nested filter in the aggregation part to only aggregate the relevant nested documents, i.e. the ones related to TEMPERATURE, like this:
{
"query": {
"nested": {
"path": "sensor",
"query": {
"nested": {
"path": "sensor.values",
"query": {
"bool": {
"must": [
{
"match": {
"sensor.values.type": "TEMPERATURE"
}
}
]
}
}
}
}
}
},
"aggs": {
"agg_by_type": {
"nested": {
"path": "sensor.values"
},
"aggs": {
"temperature_only": {
"filter": {
"match": {
"sensor.values.type": "TEMPERATURE"
}
},
"aggs": {
"max_value": {
"max": {
"field": "sensor.values.value"
}
}
}
}
}
}
}
}
After a few days of work in another projects, i back to try make this query and finally i can do it, now i can get the data per day, hours, and by type of sensor, thanks for your help.
This is my code if somebody are trying the same.
{
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"match": {
"mac": "34:ab:95:8f:84:c0"
}
}
],
"filter": [
{
"range": {
"timestamp": {
"gte": "2021-08-10",
"lt": "2021-08-25"
}
}
}
]
}
},
{
"nested": {
"path": "sensor",
"query": {
"bool": {
"must": [
{
"match": {
"sensor.type": 1
}
}
],
"should": [
{
"nested": {
"path": "sensor.values",
"query": {
"bool": {
"must": [
{
"match": {
"sensor.values.type": "HUMIDITY"
}
},
{
"match": {
"sensor.values.type": "TEMPERATURE"
}
}
]
}
}
}
}
]
}
}
}
}
]
}
},
"aggs": {
"values_per_day": {
"date_histogram": {
"field": "timestamp",
"fixed_interval": "1d",
"format" : "yyyy-MM-dd HH:mm:ss"
},
"aggs": {
"agg_type": {
"nested": {
"path": "sensor"
},
"aggs": {
"type_only": {
"filter": {
"match": {
"sensor.type": 1
}
},
"aggs": {
"agg_by_type": {
"nested": {
"path": "sensor.values"
},
"aggs": {
"temperature_only": {
"filter": {
"match": {
"sensor.values.type": "TEMPERATURE"
}
},
"aggs": {
"max_value": {
"max": {
"field": "sensor.values.value"
}
},
"min_value": {
"min": {
"field": "sensor.values.value"
}
}
}
},
"humedity_only": {
"filter": {
"match": {
"sensor.values.type": "HUMIDITY"
}
},
"aggs": {
"max_value": {
"max": {
"field": "sensor.values.value"
}
},
"min_value": {
"min": {
"field": "sensor.values.value"
}
}
}
}
}
}
}
}
}
}
}
}
},
"from": 0,
"sort": [
{
"timestamp": {
"order": "desc"
}
}
]
}

Rescale Chart in Vega-Lite

We have this dataset which we want to visualize with Vega/Vega-Lite
{
"$schema":"https://vega.github.io/schema/vega-lite/v4.13.json",
"description":"Testing",
"width":"container",
"height":350,
"autosize":{
"type":"fit",
"contains":"padding"
},
"layer":[
{
"data":{
"values":[
{
"stepid":"4444",
"stepname":"Name1",
"serialnumber":"SN3444444",
"lowval":45000,
"highval":45500,
"resultdecimal":45466
},
{
"stepid":"4444",
"stepname":"Name1",
"serialnumber":"SN3444445",
"lowval":45000,
"highval":45500,
"resultdecimal":45433
},
{
"stepid":"4444",
"stepname":"Name1",
"serialnumber":"SN3444446",
"lowval":45000,
"highval":45500,
"resultdecimal":45400
},
{
"stepid":"4444",
"stepname":"Name1",
"serialnumber":"SN3444447",
"lowval":45000,
"highval":45500,
"resultdecimal":45422
},
{
"stepid":"4444",
"stepname":"Name1",
"serialnumber":"SN3444448",
"lowval":45000,
"highval":45500,
"resultdecimal":45403
},
{
"stepid":"4444",
"stepname":"Name1",
"serialnumber":"SN3444449",
"lowval":45000,
"highval":45500,
"resultdecimal":45422
}
]
},
"layer":[
{
"mark":{
"type":"line",
"strokeWidth":3,
"point":{
"size":45,
"filled":true
},
"interpolate":"monotone"
},
"encoding":{
"x":{
"field":"serialnumber",
"type":"ordinal",
"axis":{
"labelAngle":-70,
"title":"Selected Tests",
"titleFontSize":10
}
},
"y":{
"field":"resultdecimal",
"type":"quantitative",
"axis":{
"title":"Teststeps in selected Tests",
"titleFontSize":10
}
},
"tooltip":[
{
"field":"serialnumber",
"type":"ordinal"
},
{
"field":"resultdecimal",
"type":"quantitative"
}
]
}
},
{
"mark":"rule",
"encoding":{
"y":{
"field":"highval"
},
"stroke":{
"value":"#FF0000"
}
}
},
{
"mark":"rule",
"encoding":{
"y":{
"field":"lowval"
},
"stroke":{
"value":"#FF0000"
}
}
}
]
}
]
}
Everything works fine but the scaling on the y-axis is not too optimal
Is it possible to rescale y-axis so it is more scaled-into-view based on lowval and highval e.g. like this one?
Furthermore is it possible to write the "lowval" and "highval" values on top of the lines?
You can control the y-axis domain using the scale.domain property:
"y": {
"field": "resultdecimal",
"type": "quantitative",
"axis": {
"title": "Teststeps in selected Tests",
"titleFontSize": 10
},
"scale": {
"domain": [45000, 45500]
}
},
Adding that to your chart, results in this (view in editor):

Can I export a charts from web page to PDF format?

There are the charts from the web page that made with canvas and I'd like to export to the PDF format file. Can I do like that ?
Thank you for suggestion.
You can with charting libraries like ZingChart. Simply right-click the chart to display the context menu. Select the Download PDF menu item to export the chart as a PDF. Try it on the below chart.
zingchart.THEME="classic";
var myConfig = {
"background-color":"#ecf2f6",
"graphset":[
{
"type":"bar",
"background-color":"#fff",
"border-color":"#dae5ec",
"border-width":"1px",
"height":"30%",
"width":"96%",
"x":"2%",
"y":"3%",
"title":{
"margin-top":"7px",
"margin-left":"9px",
"font-family":"Arial",
"text":"DEPARTMENT PERFORMANCE",
"background-color":"none",
"shadow":0,
"text-align":"left",
"font-size":"11px",
"font-weight":"bold",
"font-color":"#707d94"
},
"scale-y":{
"values":"0:300:100",
"max-ticks":4,
"max-items":4,
"line-color":"none",
"tick":{
"visible":false
},
"item":{
"font-color":"#8391a5",
"font-family":"Arial",
"font-size":"10px",
"padding-right":"5px"
},
"guide":{
"rules":[
{
"rule":"%i == 0",
"line-width":0
},
{
"rule":"%i > 0",
"line-style":"solid",
"line-width":"1px",
"line-color":"#d2dae2",
"alpha":0.4
}
]
}
},
"scale-x":{
"items-overlap":true,
"max-items":9999,
"values":["Apparel","Drug","Footwear","Gift Card","Home","Jewelry","Garden","Other"],
"offset-y":"1px",
"line-color":"#d2dae2",
"item":{
"font-color":"#8391a5",
"font-family":"Arial",
"font-size":"11px",
"padding-top":"2px"
},
"tick":{
"visible":false,
"line-color":"#d2dae2"
},
"guide":{
"visible":false
}
},
"plotarea":{
"margin":"45px 20px 38px 45px"
},
"plot":{
"bar-width":"33px",
"hover-state":{
"visible":false
},
"animation":{
"delay":500,
"effect":"ANIMATION_SLIDE_BOTTOM"
},
"tooltip":{
"font-color":"#fff",
"font-family":"Arial",
"font-size":"11px",
"border-radius":"6px",
"shadow":false,
"padding":"5px 10px",
"background-color":"#707e94"
}
},
"series":[
{
"values":[150,165,173,201,185,195,162,125],
"styles":[
{
"background-color":"#4dbac0"
},
{
"background-color":"#25a6f7"
},
{
"background-color":"#ad6bae"
},
{
"background-color":"#707d94"
},
{
"background-color":"#f3950d"
},
{
"background-color":"#e62163"
},
{
"background-color":"#4e74c0"
},
{
"background-color":"#9dc012"
}
]
}
]
},
{
"type":"hbar",
"background-color":"#fff",
"border-color":"#dae5ec",
"border-width":"1px",
"x":"2%",
"y":"35.2%",
"height":"63%",
"width":"30%",
"title":{
"margin-top":"7px",
"margin-left":"9px",
"text":"BRAND PERFORMANCE",
"background-color":"none",
"shadow":0,
"text-align":"left",
"font-family":"Arial",
"font-size":"11px",
"font-color":"#707d94"
},
"scale-y":{
"line-color":"none",
"tick":{
"visible":false
},
"item":{
"visible":false
},
"guide":{
"visible":false
}
},
"scale-x":{
"values":["Kenmore","Craftsman","DieHard","Land's End","Laclyn Smith","Joe Boxer"],
"line-color":"none",
"tick":{
"visible":false
},
"item":{
"width":200,
"text-align":"left",
"offset-x":206,
"offset-y":-12,
"font-color":"#8391a5",
"font-family":"Arial",
"font-size":"11px",
"padding-bottom":"8px"
},
"guide":{
"visible":false
}
},
"plot":{
"bars-overlap":"100%",
"bar-width":"12px",
"thousands-separator":",",
"tooltip":{
"font-color":"#ffffff",
"background-color":"#707e94",
"font-family":"Arial",
"font-size":"11px",
"border-radius":"6px",
"shadow":false,
"padding":"5px 10px"
},
"hover-state":{
"background-color":"#707e94"
},
"animation":{
"delay":500,
"effect":"ANIMATION_EXPAND_LEFT"
}
},
"plotarea":{
"margin":"50px 15px 10px 15px"
},
"series":[
{
"values":[103902,112352,121823,154092,182023,263523],
"z-index":2,
"styles":[
{
"background-color":"#4dbac0"
},
{
"background-color":"#4dbac0"
},
{
"background-color":"#4dbac0"
},
{
"background-color":"#4dbac0"
},
{
"background-color":"#4dbac0"
},
{
"background-color":"#4dbac0"
}
],
"tooltip-text":"$%node-value"
},
{
"max-trackers":0,
"values":[300000,300000,300000,300000,300000,300000],
"data-rvalues":[103902,112352,121823,154092,182023,263523],
"background-color":"#d9e4eb",
"z-index":1,
"value-box":{
"visible":true,
"offset-y":"-12px",
"offset-x":"-54px",
"text-align":"right",
"font-color":"#8391a5",
"font-family":"Arial",
"font-size":"11px",
"text":"$%data-rvalues",
"padding-bottom":"8px"
}
}
]
},
{
"type":"line",
"background-color":"#fff",
"border-color":"#dae5ec",
"border-width":"1px",
"width":"64%",
"height":"63%",
"x":"34%",
"y":"35.2%",
"title":{
"margin-top":"7px",
"margin-left":"12px",
"text":"TODAY'S SALES",
"background-color":"none",
"shadow":0,
"text-align":"left",
"font-family":"Arial",
"font-size":"11px",
"font-color":"#707d94"
},
"plot":{
"animation":{
"delay":500,
"effect":"ANIMATION_SLIDE_LEFT"
}
},
"plotarea":{
"margin":"50px 25px 70px 46px"
},
"scale-y":{
"values":"0:100:25",
"line-color":"none",
"guide":{
"line-style":"solid",
"line-color":"#d2dae2",
"line-width":"1px",
"alpha":0.5
},
"tick":{
"visible":false
},
"item":{
"font-color":"#8391a5",
"font-family":"Arial",
"font-size":"10px",
"padding-right":"5px"
}
},
"scale-x":{
"line-color":"#d2dae2",
"line-width":"2px",
"values":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],
"tick":{
"line-color":"#d2dae2",
"line-width":"1px"
},
"guide":{
"visible":false
},
"item":{
"font-color":"#8391a5",
"font-family":"Arial",
"font-size":"10px",
"padding-top":"5px"
}
},
"legend":{
"layout":"x4",
"background-color":"none",
"shadow":0,
"margin":"auto auto 15 auto",
"border-width":0,
"item":{
"font-color":"#707d94",
"font-family":"Arial",
"padding":"0px",
"margin":"0px",
"font-size":"9px"
},
"marker":{
"show-line":"true",
"type":"match",
"font-family":"Arial",
"font-size":"10px",
"size":4,
"line-width":2,
"padding":"3px"
}
},
"crosshair-x":{
"lineWidth":1,
"line-color":"#707d94",
"plotLabel":{
"shadow":false,
"font-color":"#000",
"font-family":"Arial",
"font-size":"10px",
"padding":"5px 10px",
"border-radius":"5px",
"alpha":1
},
"scale-label":{
"font-color":"#ffffff",
"background-color":"#707d94",
"font-family":"Arial",
"font-size":"10px",
"padding":"5px 10px",
"border-radius":"5px"
}
},
"tooltip":{
"visible":false
},
"series":[
{
"values":[69,68,54,48,70,74,98,70,72,68,49,69],
"text":"Kenmore",
"line-color":"#4dbac0",
"line-width":"2px",
"shadow":0,
"marker":{
"background-color":"#fff",
"size":3,
"border-width":1,
"border-color":"#36a2a8",
"shadow":0
},
"palette":0
},
{
"values":[51,53,47,60,48,52,75,52,55,47,60,48],
"text":"Craftsman",
"line-width":"2px",
"line-color":"#25a6f7",
"shadow":0,
"marker":{
"background-color":"#fff",
"size":3,
"border-width":1,
"border-color":"#1993e0",
"shadow":0
},
"palette":1,
"visible":1
},
{
"values":[42,43,30,50,31,48,55,46,48,32,50,38],
"text":"DieHard",
"line-color":"#ad6bae",
"line-width":"2px",
"shadow":0,
"marker":{
"background-color":"#fff",
"size":3,
"border-width":1,
"border-color":"#975098",
"shadow":0
},
"palette":2,
"visible":1
},
{
"values":[25,15,26,21,24,26,33,25,15,25,22,24],
"text":"Land's End",
"line-color":"#f3950d",
"line-width":"2px",
"shadow":0,
"marker":{
"background-color":"#fff",
"size":3,
"border-width":1,
"border-color":"#d37e04",
"shadow":0
},
"palette":3
}
]
}
]
};
zingchart.render({
id : 'myChart',
data : myConfig,
height: 500,
width: '100%'
});
.zc-ref {
display: none;
}
<head>
<script src= "https://cdn.zingchart.com/zingchart.min.js"></script>
<script> zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/";
ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9","ee6b7db5b51705a13dc2339db3edaf6d"];</script></head>
<body>
<div id='myChart'><a class="zc-ref" href="https://www.zingchart.com/">Powered by ZingChart</a></div>
</body>
There's more information about chart export here: https://www.zingchart.com/docs/integrations/phantomjs-chart-export/
Disclaimer: I'm on the ZingChart team. Let me know if you have further questions.

ElasticSearch combine conditional statements 'and' with 'or'

Purpose of the query below is to return n results for each criteria i.e. it must match partnersites 16 and match 'venueTown' or partnersites 16 and match 'venueName'. Currently it returns the results where each field must contain the same string. In my case fields: name, venueName and venueTown must contain manchester, but I want separate results for each pair {(partnersites, venueName), (partnersites, venueTown)}.
{
"size": 0,
"_source": ["groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "media"],
"query": {
"bool": {
"must": [{
"match": {
"partnersites": {
"query": "16"
}
}
}, {
"match": {
"name": "manchester"
}
}, {
"match": {
"venueName": "manchester"
}
}, {
"match": {
"venueTown": "manchester"
}
}, {
"match": {
"venueTown": "manchester"
}
}]
}
},
"aggs": {
"distinct_names": {
"terms": {
"field": "name.keyword",
"size": 10
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"size": 1,
"_source": ["groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "media"]
}
}
}
},
"distinct_venues": {
"terms": {
"field": "venueName.keyword",
"size": 10
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"size": 1,
"_source": ["groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "media"]
}
}
}
},
"distinct_towns": {
"terms": {
"field": "venueTown.keyword",
"size": 10
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"size": 1,
"_source": ["groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "media"]
}
}
}
}
}
}
Try this:
{
"size": 0,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"partnersites": "16"
}
},
{
"match_phrase_prefix": {
"name": "mancheste"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"partnersites": "16"
}
},
{
"match_phrase_prefix": {
"venueName": "mancheste"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"partnersites": "16"
}
},
{
"match_phrase_prefix": {
"venueTown": "mancheste"
}
}
]
}
}
]
}
}
]
}
},
"aggs": {
"distinct_names": {
"terms": {
"field": "groupedName.keyword",
"size": 30
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"size": 1,
"_source": [
"groupedName",
"groupedDisplayName",
"groupedUrl",
"eventCode",
"venueName",
"venueTown",
"venueId",
"media"
]
}
}
}
}
}
}