{
  "$schema": "https://vega.github.io/schema/vega/v4.json",
  "width": 600,
  "height": 360,
  "autosize": "fit",
  "data": [
    {
      "name": "tree",
      "url": "<%= dataset_url %>",
      "transform": [
        {
          "type": "stratify",
          "key": "<%= data_mapper.column_1 %>",
          "parentKey": "<%= data_mapper.column_3 %>"
        },
        {
          "type": "treemap",
          "field": "<%= data_mapper.column_4 %>",
          "sort": {
            "field": "value"
          },
          "round": true,
          "method": "binary",
          "ratio": "aspectRatio",
          "size": [
            {
              "signal": "width"
            },
            {
              "signal": "height"
            }
          ]
        }
      ]
    },
    {
      "name": "nodes",
      "source": "tree",
      "transform": [
        {
          "type": "filter",
          "expr": "datum.children"
        }
      ]
    },
    {
      "name": "leaves",
      "source": "tree",
      "transform": [
        {
          "type": "filter",
          "expr": "!datum.children"
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "color",
      "type": "ordinal",
      "domain": {
        "data": "nodes",
        "field": "<%= data_mapper.column_1 %>"
      },
      "range": [
        "#3182bd",
        "#6baed6",
        "#9ecae1",
        "#c6dbef",
        "#e6550d",
        "#fd8d3c",
        "#fdae6b",
        "#fdd0a2",
        "#31a354",
        "#74c476",
        "#a1d99b",
        "#c7e9c0",
        "#756bb1",
        "#9e9ac8",
        "#bcbddc",
        "#dadaeb",
        "#636363",
        "#969696",
        "#bdbdbd",
        "#d9d9d9"
      ]
    },
    {
      "name": "<%= data_mapper.column_4 %>",
      "type": "ordinal",
      "domain": [
        2,
        3
      ],
      "range": [
        10,
        6
      ]
    }
  ],
  "marks": [
    {
      "type": "rect",
      "from": {
        "data": "nodes"
      },
      "interactive": false,
      "encode": {
        "enter": {
          "fill": {
            "scale": "color",
            "field": "<%= data_mapper.column_1 %>"
          }
        },
        "update": {
          "x": {
            "field": "x0"
          },
          "y": {
            "field": "y0"
          },
          "x2": {
            "field": "x1"
          },
          "y2": {
            "field": "y1"
          }
        }
      }
    },
    {
      "type": "rect",
      "from": {
        "data": "leaves"
      },
      "encode": {
        "enter": {
          "stroke": {
            "value": "#fff"
          }
        },
        "update": {
          "x": {
            "field": "x0"
          },
          "y": {
            "field": "y0"
          },
          "x2": {
            "field": "x1"
          },
          "y2": {
            "field": "y1"
          },
          "fill": {
            "value": "transparent"
          }
        },
        "hover": {
          "tooltip": {
            "signal": "datum.['<%= data_mapper.column_1 %>'] + ': '+datum['<%= data_mapper.column_4 %>']"
          }
        }
      }
    },
    {
      "type": "text",
      "from": {
        "data": "leaves"
      },
      "interactive": false,
      "encode": {
        "enter": {
          "font": {
            "value": "Helvetica"
          },
          "align": {
            "value": "center"
          },
          "baseline": {
            "value": "top"
          },
          "fill": {
            "value": "#000"
          },
          "text": {
            "field": "<%= data_mapper.column_1 %>"
          },
          "fontSize": [
            {
              "test": "((datum.x1 - datum.x0)/datum['<%= data_mapper.column_1 %>'].length) > 2.5 ? true: false",
              "signal": "max(ceil((datum.x1 - datum.x0)/datum['<%= data_mapper.column_1 %>'].length), 6)"
            },
            {
              "value": 0
            }
          ]
        },
        "update": {
          "x": {
            "signal": "0.5 * (datum.x0 + datum.x1)"
          },
          "y": {
            "signal": "0.5 * (datum.y0 + datum.y1)"
          }
        }
      }
    }
  ]
}
