{
  "$schema": "https://vega.github.io/schema/vega/v4.json",
  "width": 600,
  "height": 360,
  "autosize": "fit",
  "signals": [
    {"name": "zero", "value": 0},
    {"name": "two_PI", "value": 6.29},
    {"name": "sort", "value": true},
    {"name": "minDimension", "update": "width > height ? height : width"}
  ],
  "data": [
    {
      "name": "table",
      "url": "<%= dataset_url %>",
      "transform": [
        {
          "type": "pie",
          "field": "<%= data_mapper.column_1 %>",
          "startAngle": {"signal": "zero"},
          "endAngle": {"signal": "two_PI"},
          "sort": {"signal": "sort"}
        },
        {
          "type": "joinaggregate",
          "fields": ["<%= data_mapper.column_1 %>"],
          "ops": ["sum"],
          "as": ["total"]
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "text_to_color",
      "type": "ordinal",
      "domain": {"data": "table", "field": "<%= data_mapper.column_2 %>"},
      "range": "category"
    },
    {
      "name": "normalize_radius",
      "type": "linear",
      "domain": {"data": "table", "field": "<%= data_mapper.column_1 %>"},
      "range": {"signal": "[0, height/2]"}
    }
  ],
  "marks": [
    {
      "name": "arc segment",
      "type": "arc",
      "from": {"data": "table"},
      "encode": {
        "update": {
          "fill": {"scale": "text_to_color", "field": "<%= data_mapper.column_2 %>"},
          "x": {"signal": "width / 2"},
          "y": {"signal": "height / 2"},
          "startAngle": {"field": "startAngle"},
          "endAngle": {"field": "endAngle"},
          "outerRadius": {"field": "<%= data_mapper.column_1 %>", "scale": "normalize_radius"},
          "innerRadius": {"value": 0},
          "stroke": {"value": "#ffffff"},
          "opacity": {"value": 0.9},
          "tooltip": {"signal": "datum"}
        }
      }
    },
    {
      "name": "text labels",
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "update": {
          "font": {"value": "Roboto"},
          "fill": {"value": "#800040"},
          "opacity": {"signal": "datum.endAngle < 1 ? 0: 1"},
          "fontSize": {"value": 12},
          "x": {"signal": "width / 2"},
          "y": {"signal": "height / 2"},
          "radius": {"field": "<%= data_mapper.column_1 %>", "scale": "normalize_radius", "offset": {"value": 10}},
          "theta": {"signal": "(datum['startAngle'] + datum['endAngle']) / 2"},
          "text": {
            "signal": "datum['<%= data_mapper.column_2 %>'] + ', ' + format(datum['<%= data_mapper.column_1 %>']/datum['total'], '.0%')"
          },
          "baseline": {"value": "middle"},
          "align": {
            "signal": "(datum.startAngle + datum.endAngle)/2 > 0 && (datum.startAngle + datum.endAngle)/2 < 3.14 ? 'left' : 'right' "
          }
        }
      }
    }
  ]
}