Почему я не могу добавить легенду на свой график в Vega? - PullRequest
0 голосов
/ 13 ноября 2018

это мой код vega, и я пишу код, чтобы добавить легенду к моей круговой диаграмме, но он не может работать.Не могли бы вы мне помочь?

{
  "$schema": "https://vega.github.io/schema/vega/v4.json",
  "width": 300,
  "height": 300,
  "autosize": "none",     //put in my data
  "signals": [
    {
      "name": "startAngle",
      "value": 0,
      "bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
    },
    {
      "name": "endAngle",
      "value": 6.29,
      "bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
    },
    {
      "name": "padAngle",
      "value": 0,
      "bind": {"input": "range", "min": 0, "max": 0.1}
    },
    {
      "name": "innerRadius",
      "value": 0,
      "bind": {"input": "range", "min": 0, "max": 90, "step": 1}
    },
    {
      "name": "cornerRadius",
      "value": 0,
      "bind": {"input": "range", "min": 0, "max": 10, "step": 0.5}
    },
    {"name": "sort", "value": false, "bind": {"input": "checkbox"}}
  ],   //set the control bar
  "data": [
    {
      "name": "table",
      "values": [
        {"id": "BenCarsonNumber", "field": 564553},
        {"id": "BernieSanders", "field": 11958297},
        {"id": "CarlyFiorina", "field": 15191},
        {"id": "ChrisChristie", "field": 24353},
        {"id": "DonaldTrump", "field": 13301237},
        {"id": "HillaryClinton", "field": 15691617},
        {"id": "JebBush", "field": 94411},
        {"id": "JohnKasich", "field": 4159528},
        {"id": "MarcoRubio", "field": 3320810},
        {"id": "Malley", "field": 752},
        {"id": "MikeHuckabee", "field": 3345},
        {"id": "NoPreference", "field": 8152},
        {"id": "RandPaul", "field": 8479},
        {"id": "RickSantorum", "field": 1782},
        {"id": "TedCruz", "field": 7602299},
        {"id": "Uncommitted", "field": 43}       //this is my data
      ],
      "transform": [    //try to make a pie chart
        {
          "type": "pie",
          "field": "field",
          "startAngle": {"signal": "startAngle"},
          "endAngle": {"signal": "endAngle"},
          "sort": {"signal": "sort"}
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "table", "field": "id"},
      "range": {"scheme": "category20"}
    }
  ],
  "legends": [     //there is my legend code
    {
      "stroke": "color",
      "title": "Origin",
      "encode": {
        "symbols": {
          "update": {
            "fill": {"value": ""},
            "strokeWidth": {"value": 2},
            "size": {"value": 64}
          }
        }
      }
    }
  ],
  "marks": [
    {
      "type": "arc",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "fill": {"scale": "color", "field": "id"},
          "x": {"signal": "width / 2"},
          "y": {"signal": "height / 2"}
        },
        "update": {
          "startAngle": {"field": "startAngle"},
          "endAngle": {"field": "endAngle"},
          "padAngle": {"signal": "padAngle"},
          "innerRadius": {"signal": "innerRadius"},
          "outerRadius": {"signal": "width / 2"},
          "cornerRadius": {"signal": "cornerRadius"}
        }
      }
    }
  ]
}

все работает отлично, ожидайте, что для диаграммы нет легенды.или если есть лучший способ написать такую ​​круговую диаграмму?но я не могу предложить что-то новое.Пожалуйста, помогите мне!Большое спасибо за помощь!

1 Ответ

0 голосов
/ 19 ноября 2018

Вам нужно обновить раздел legends на "orient": "top-right".

Вот полная версия Vega Editor

...
  "legends": [
    { ....
     "orient": "top-right",
      ....
    }
  ],
...

enter image description here

...