我在 Vega 中制作了一个条形图,每个类别有两列。有谁知道如何设置每列的最小像素高度?
当值不等于 0 时,我希望它的最小像素高度为 2px,因为有时数据会发生变化,并且您只能看到很少的值,具体取决于它在范围上的渲染方式。
这是我当前的实现:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 700,
"height": 370,
"autosize": {"type": "fit", "contains": "padding"},
"padding": {"bottom": 2, "right": 9, "left": 5},
"config": {
"axis": {
"labelFont": "HelveticaNeueLTW01-55Roman",
"titleFont": "HelveticaNeueLTW01-55Roman"
}
},
"data": [
{
"name": "capital_flows",
"values": [
{"label": "2012", "value1": 90.75, "value2": 114.11},
{"label": "2013", "value1": 17.36, "value2": -17.54},
{"label": "2014", "value1": 7.36, "value2": -100.43},
{"label": "2015", "value1": 8.64, "value2": -0.07},
{"label": "2016", "value1": 3.36, "value2": -1.01},
{"label": "2017", "value1": 10.48, "value2": -1.29},
{"label": "2018", "value1": 6.32, "value2": -84.2},
{"label": "2019", "value1": 3.29, "value2": -3.06}
],
"transform": [
{"type": "formula", "expr": "split(datum.label, ' ')", "as": "label"}
]
},
{
"name": "parsed",
"source": ["capital_flows"],
"transform": [
{
"type": "aggregate",
"fields": ["value1", "value1", "value2", "value2"],
"ops": ["min", "max", "min", "max"],
"as": ["min_value1", "max_value1", "min_value2", "max_value2"]
},
{
"type": "formula",
"expr": "datum.min_value1 && datum.min_value2 == 0 ? 1 : datum.max_value1 > datum.max_value2 ? abs(datum.max_value1) : abs(datum.max_value2)",
"as": "mergedMax"
},
{
"type": "formula",
"expr": "datum.min_value1 && datum.min_value2 == 0 ? 1 : datum.min_value1 < datum.min_value2 ? abs(datum.min_value1) : abs(datum.min_value2)",
"as": "mergedMin"
},
{
"type": "formula",
"expr": "datum.mergedMax > datum.mergedMin ? datum.mergedMax : datum.mergedMin",
"as": "max"
},
{"type": "formula", "expr": "-(datum.max)", "as": "min"}
]
}
],
"scales": [
{
"name": "x",
"type": "band",
"range": "width",
"domain": {"data": "capital_flows", "field": "label"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": false,
"domain": {"data": "parsed", "field": "limit"},
"domainMin": {"signal": "pluck(data('parsed'), 'min')"},
"domainMax": {"signal": "pluck(data('parsed'), 'max')"}
}
],
"axes": [
{
"orient": "bottom",
"scale": "x",
"zindex": 1,
"labelFontSize": 14,
"labelColor": "#85868C",
"labelAlign": "center",
"labelLineHeight": 19.6,
"labelFontWeight": "normal",
"labelPadding": 8,
"labelOpacity": 1,
"domain": false,
"domainColor": "#e5e5e5",
"domainWidth": 1,
"ticks": false
},
{
"orient": "left",
"scale": "y",
"zindex": 0,
"labelFontSize": 14,
"labelColor": "#85868C",
"labelLineHeight": 19.6,
"labelFontWeight": "normal",
"labelPadding": 8,
"labelOpacity": 1,
"domain": false,
"domainColor": "#e5e5e5",
"grid": true,
"gridOpacity": 0.5,
"ticks": false,
"tickCount": 4,
"gridDash": {"signal": "datum.value == 0 ? [] : [4,4]"},
"encode": {
"labels": {
"update": {
"text": {
"signal": "if(abs(datum.value) >= 1e9, format(datum.value/1e9, '$,.1~f') + 'B', if(abs(datum.value) >= 1e6, format(datum.value/1e6, '$,.1~f') + 'M', if(abs(datum.value) >= 1e3, format(datum.value/1e3, '$,.1~f') + 'K', format(datum.value, '$,.2~f'))))"
}
}
}
}
}
],
"marks": [
{
"name": "inflow",
"type": "rect",
"from": {"data": "capital_flows"},
"encode": {
"enter": {
"xc": {"scale": "x", "field": "label", "band": 0.5},
"width": {"value": 16},
"y": {"scale": "y", "field": "value1"},
"y2": {"scale": "y", "value": 0},
"fill": {"value": "#68c487"},
"cornerRadiusTopLeft": {"signal": "datum.value1 > 0 ? '2' : '0'"},
"cornerRadiusTopRight": {"signal": "datum.value1 > 0 ? '2' : '0'"},
"cornerRadiusBottomLeft": {"signal": "datum.value1 > 0 ? '0' : '2'"},
"cornerRadiusBottomRight": {"signal": "datum.value1 > 0 ? '0' : '2'"}
}
}
},
{
"name": "outflow",
"type": "rect",
"from": {"data": "capital_flows"},
"encode": {
"enter": {
"xc": {"scale": "x", "field": "label", "band": 0.5, "offset": 18},
"width": {"value": 16},
"y": {"scale": "y", "field": "value2"},
"y2": {"scale": "y", "value": 0},
"fill": {"value": "#FFB066"},
"cornerRadiusTopLeft": {"signal": "datum.value2 > 0 ? '2' : '0'"},
"cornerRadiusTopRight": {"signal": "datum.value2 > 0 ? '2' : '0'"},
"cornerRadiusBottomLeft": {"signal": "datum.value2 > 0 ? '0' : '2'"},
"cornerRadiusBottomRight": {"signal": "datum.value2 > 0 ? '0' : '2'"}
}
}
}
]
}
您可以添加一个额外的转换,如下所示:
{"type": "formula", "expr": "datum.value2 < 0 && datum.value2 > -1 ? -1: datum.value2 > 0 && datum.value2 < 1 ? 1: datum.value2", "as": "value2"}
所以:
如果它在 -1 和 0 之间,那么它将是 -1
如果介于 0 和 1 之间,则为 1
还要在图表底部添加注释,因为它可能会产生误导:)