Vega分面条形图按字段a升序排序及对应坐标轴配置问题
Vega分面可视化问题解决方案
问题1:分面按字段"a"升序排序
- 解决思路:分面的排序由
trellis数据集的顺序决定,直接在该数据集的转换规则中新增排序步骤即可,不需要额外配置scale。 - 修改位置:找到
data数组下名为trellis的数据集,在aggregate(groupby为["a"])的转换步骤后添加排序转换:
{ "type": "sort", "field": "a", "order": "ascending" }
问题2:展示字段"a"对应的坐标轴
- 解决思路:新增一个对应分面维度的band scale,再绑定到新的坐标轴上即可,轴可放在左右两侧(因当前分面为纵向排列,若要放在上下侧需要调整分面为横向排列)。
- 第一步:在根节点的
scales数组中新增分面维度的scale:
{ "name": "trellisScale", "type": "band", "domain": {"data": "trellis", "field": "a", "sort": true}, "range": [0, {"signal": "trellisExtent[1]"}], "paddingOuter": 0 }
- 第二步:在根节点的
axes数组中新增对应的坐标轴:
{ "orient": "right", "scale": "trellisScale", "title": "a", "domain": true, "ticks": true, "labelAlign": "left" }
修改后完整代码
{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A nested bar chart example, with bars grouped by category.", "width": 300, "padding": 5, "autosize": "pad", "signals": [ { "name": "rangeStep", "value": 20, "bind": {"input": "range", "min": 5, "max": 50, "step": 1} }, { "name": "innerPadding", "value": 0.1, "bind": {"input": "range", "min": 0, "max": 0.7, "step": 0.01} }, { "name": "outerPadding", "value": 0.2, "bind": {"input": "range", "min": 0, "max": 0.4, "step": 0.01} }, { "name": "height", "update": "trellisExtent[1]" } ], "data": [ { "name": "tuples", "values": [ {"a": 1, "b": "b", "c": 4.4}, {"a": 0, "b": "c", "c": 5.1}, {"a": 0, "b": "a", "c": 6.3}, {"a": 0, "b": "a", "c": 4.2}, {"a": 0, "b": "b", "c": 6.8}, {"a": 2, "b": "b", "c": 3.5}, {"a": 2, "b": "c", "c": 6.2} ], "transform": [ { "type": "aggregate", "groupby": ["a", "b"], "fields": ["c"], "ops": ["average"], "as": ["c"] } ] }, { "name": "trellis", "source": "tuples", "transform": [ { "type": "aggregate", "groupby": ["a"] }, { "type": "sort", "field": "a", "order": "ascending" }, { "type": "formula", "as": "span", "expr": "rangeStep * bandspace(datum.count, innerPadding, outerPadding)" }, { "type": "stack", "field": "span" }, { "type": "extent", "field": "y1", "signal": "trellisExtent" } ] } ], "scales": [ { "name": "xscale", "domain": {"data": "tuples", "field": "c"}, "nice": true, "zero": true, "round": true, "range": "width" }, { "name": "color", "type": "ordinal", "range": "category", "domain": {"data": "trellis", "field": "a"} }, { "name": "trellisScale", "type": "band", "domain": {"data": "trellis", "field": "a", "sort": true}, "range": [0, {"signal": "trellisExtent[1]"}], "paddingOuter": 0 } ], "axes": [ { "orient": "bottom", "scale": "xscale", "domain": true }, { "orient": "right", "scale": "trellisScale", "title": "a", "domain": true, "ticks": true, "labelAlign": "left" } ], "marks": [ { "type": "group", "from": { "data": "trellis", "facet": { "name": "faceted_tuples", "data": "tuples", "groupby": "a" } }, "encode": { "enter": { "x": {"value": 0}, "width": {"signal": "width"} }, "update": { "y": {"field": "y0"}, "y2": {"field": "y1"} } }, "scales": [ { "name": "yscale", "type": "band", "paddingInner": {"signal": "innerPadding"}, "paddingOuter": {"signal": "outerPadding"}, "round": true, "domain": {"data": "faceted_tuples", "field": "b"}, "range": {"step": {"signal": "rangeStep"}} } ], "axes": [ { "orient": "left", "scale": "yscale", "ticks": false, "domain": false, "labelPadding": 4 } ], "marks": [ { "type": "rect", "from": {"data": "faceted_tuples"}, "encode": { "enter": { "x": {"value": 0}, "x2": {"scale": "xscale", "field": "c"}, "fill": {"scale": "color", "field": "a"}, "strokeWidth": {"value": 2} }, "update": { "y": {"scale": "yscale", "field": "b"}, "height": {"scale": "yscale", "band": 1}, "stroke": {"value": null}, "zindex": {"value": 0} }, "hover": { "stroke": {"value": "firebrick"}, "zindex": {"value": 1} } } } ] } ] }
内容的提问来源于stack exchange,提问作者Christian
相关产品推荐
相关产品推荐

