如何在Vega的标签中添加“%”等符号?

时间:2017-06-29 13:16:12

标签: javascript data-visualization vega

我有以下代码:

{
  "$schema": "https://vega.github.io/schema/vega/v3.0.json",
  "width": 700,
  "height": 100,
  "padding": 5,
  "data": [
    {
      "name": "table",
      "values": [
        {"category": "MOYENNE","position": 0,"value": 91},
        {"category": "MOYENNE","position": 1,"value": 9}
      ]
    }
  ],
  "scales": [
    {
      "name": "yscale",
      "type": "band",
      "domain": {"data": "table","field": "category"},
      "range": "height"
    },
    {
      "name": "xscale",
      "type": "linear",
      "domain": {"data": "table","field": "value"},
      "range": "width",
      "round": true,
      "zero": true,
      "nice": true
    },
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "table","field": "position"},
      "range": ["#C8E6C9", "#FF8A65"]
    }
  ],
  "axes": [
    {
      "orient": "left",
      "scale": "yscale",
      "tickSize": 0,
      "labelPadding": 4,
      "zindex": 1
    }
  ],
  "marks": [
    {
      "type": "group",
      "from": {
        "facet": {
          "data": "table",
          "name": "facet",
          "groupby": "category"
        }
      },
      "encode": {
        "enter": {"y": {"scale": "yscale","field": "category"}}
      },
      "signals": [{"name": "height","update": "bandwidth('yscale')"}],
      "scales": [
        {
          "name": "pos",
          "type": "band",
          "range": "height",
          "domain": {"data": "facet","field": "position"}
        }
      ],
      "marks": [
        {
          "name": "bars",
          "from": {"data": "facet"},
          "type": "rect",
          "encode": {
            "enter": {
              "y": {"scale": "pos","field": "position"},
              "height": {"scale": "pos","band": 1},
              "x": {"scale": "xscale","field": "value"},
              "x2": {"scale": "xscale","value": 0},
              "fill": {"scale": "color","field": "position"}
            }
          }
        },
        {
          "type": "text",
          "from": {"data": "bars"},
          "encode": {
            "enter": {
              "x": {"field": "x2","offset": 15},
              "y": {
                "field": "y",
                "offset": {"field": "height","mult": 0.5}
              },
              "fill": {"value": "black"},
              "align": {"value": "right"},
              "baseline": {"value": "middle"},
              "text": {"field": "datum.value"}
            }
          }
        }
      ]
    }
  ]
}

我只想在我的标签中加上91%和9%。

我认为这是在最后一行:“text”:{“field”:“datum.value”},但我不能只做以下操作:“text”:{“field”:“datum。值“+”%“}或类似的东西......

任何人都可以帮助我吗?非常感谢您提前:))

2 个答案:

答案 0 :(得分:1)

我不完全确定这是最佳做法,但它确实有效,并且基于format的Vega表达式文档。

{
  "$schema": "https://vega.github.io/schema/vega/v3.0.json",
  "width": 700,
  "height": 100,
  "padding": 5,
  "data": [
    {
      "name": "table",
      "values": [
        {"category": "MOYENNE","position": 0,"value": 91},
        {"category": "MOYENNE","position": 1,"value": 9}
      ],
      "transform": [
        {
          "type": "formula",
          "expr": "format(datum.value/100,'0.0p')",
          "as": "percentvalue"
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "yscale",
      "type": "band",
      "domain": {"data": "table","field": "category"},
      "range": "height"
    },
    {
      "name": "xscale",
      "type": "linear",
      "domain": {"data": "table","field": "value"},
      "range": "width",
      "round": true,
      "zero": true,
      "nice": true
    },
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "table","field": "position"},
      "range": ["#C8E6C9", "#FF8A65"]
    }
  ],
  "axes": [
    {
      "orient": "left",
      "scale": "yscale",
      "tickSize": 0,
      "labelPadding": 4,
      "zindex": 1
    }
  ],
  "marks": [
    {
      "type": "group",
      "from": {
        "facet": {
          "data": "table",
          "name": "facet",
          "groupby": "category"
        }
      },
      "encode": {
        "enter": {"y": {"scale": "yscale","field": "category"}}
      },
      "signals": [{"name": "height","update": "bandwidth('yscale')"}],
      "scales": [
        {
          "name": "pos",
          "type": "band",
          "range": "height",
          "domain": {"data": "facet","field": "position"}
        }
      ],
      "marks": [
        {
          "name": "bars",
          "from": {"data": "facet"},
          "type": "rect",
          "encode": {
            "enter": {
              "y": {"scale": "pos","field": "position"},
              "height": {"scale": "pos","band": 1},
              "x": {"scale": "xscale","field": "value"},
              "x2": {"scale": "xscale","value": 0},
              "fill": {"scale": "color","field": "position"}
            }
          }
        },
        {
          "type": "text",
          "from": {"data": "bars"},
          "encode": {
            "enter": {
              "x": {"field": "x2","offset": 15},
              "y": {
                "field": "y",
                "offset": {"field": "height","mult": 0.5}
              },
              "fill": {"value": "black"},
              "align": {"value": "right"},
              "baseline": {"value": "middle"},
              "text": {"field": "datum.percentvalue"}
            }
          }
        }
      ]
    }
  ]
}

screenshot of percent values on chart

答案 1 :(得分:0)

//Function 
public int[] getSumOfEvensAndOdds(int[] array) {
    // Student code begins here
    int[] res = {0,0};
    int len = array.length;

    if(array == null){
        return res;
    }

    //logic
    for(int i = 0; i < len; i++){
        if(array[i] % 2 == 0){
            res[0] += array[i];
        } else if(array[i] % 2 != 0){
            res[1] += array[i];
        } else{
            res[0] += 0;
            res[1] += 0;
        }
    }

    return res;

}

enter image description here