<jupyter_start><jupyter_text>Blue Food Visualizing protein supply and how the practices generating that protein supply affect the ocean using a heirarchical relationship.Note that this is a parameterized widget; the specification passed to the API will not be renderable without the geostore identifier being inserted.  *Author: <NAME>Created: 08 24 2021Environment: jupyterlab*  Style - Vega chart- Chart type: [Sunburst](https://vega.github.io/vega/examples/sunburst/)- Value: Protein Supply (g/capita/day)  Data - Data: [ocn_calcs_015_blue_food_protein_supply](https://resourcewatch.carto.com/u/wri-rw/dataset/ocn_calcs_015_blue_food_protein_supply)- Resource Watch: [explore page](https://resourcewatch.org/data/explore/9e1b3cad-db6f-44b0-b6fb-048df7b6c680)- Source: [FAO Food Balance Sheet](http://www.fao.org/faostat/en/data/FBS)  Preparation  Vega<jupyter_code>import json
from vega import Vega
from IPython.display import display
def Vega(spec):
    bundle = {}
    bundle['application/vnd.vega.v5+json'] = spec
    display(bundle, raw=True)
widget_width = 600
widget_height = 600<jupyter_output><empty_output><jupyter_text>Visualization  Queries  Testing ``` gid_0 = 'JPN' ``` used as stand-in for parameterized ```geostore_id={{geostore_id}}``` in production version ```sqlSELECT alias.iso as gid_0, data.area, year, item as id, parent, size, value as protein, analysis_category, product FROM     (SELECT * FROM foo_061_rw1_blue_food_supply_edit) data INNER JOIN ow_aliasing_countries AS alias ON alias.alias = data.area WHERE iso='JPN' ORDER BY analysis_category ASC, id ASC```  Parameterization ```sqlSELECT gadm.gid_0 as gid_0, data.area, year, item as id, parent, size, value as protein, analysis_category, product FROM     (SELECT * FROM foo_061_rw1_blue_food_supply_edit) data LEFT OUTER JOIN ow_aliasing_countries AS alias ON alias.alias = data.area LEFT OUTER JOIN gadm36_0 gadm ON alias.iso = gadm.gid_0 WHERE gadm.{{geostore_env}} ILIKE '{{geostore_id}}' ORDER BY analysis_category ASC, id ASC```<jupyter_code>spec=json.loads("""{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "padding": 5,
  "autosize": "pad",
  "signals": [
    {
      "name": "year",
      "value": 2018,
      "bind": {"input": "range", "min": 1961, "max": 2018, "step": 1}
    }
  ],
  "data": [
    {
      "name": "table",
      "url": "https://wri-rw.carto.com/api/v2/sql?q= SELECT gadm.gid_0 as gid_0, data.area, year, item as id, parent, size, value as protein, analysis_category, product FROM (SELECT * FROM ocn_calcs_015_blue_food_protein_supply) data LEFT OUTER JOIN ow_aliasing_countries AS alias ON alias.alias = data.area LEFT OUTER JOIN gadm36_0 gadm ON alias.iso = gadm.gid_0 WHERE gadm.{{geostore_env}} ILIKE '{{geostore_id}}' ORDER BY analysis_category ASC, id ASC, year DESC",
      "format": {"type": "json", "property": "rows"},
      "transform": [
        {"type": "filter", "expr": "datum.year==year"},
        {"type": "stratify", "key": "id", "parentKey": "parent"},
        {
          "type": "partition",
          "field": "size",
          "sort": {"field": ["analysis_category"]},
          "size": [{"signal": "2 * PI"}, {"signal": "width/4"}],
          "as": ["a0", "r0", "a1", "r1", "depth", "children"],
          "padding": 0
        },
        {"type": "formula", "expr": "split(datum.id,'_')[0]", "as": "label"},
        {"type": "formula", "expr": "datum.protein ? format((datum.protein), '.1f') + ' g/capita/day' : ''", "as": "protein"}
      ]
    }
  ],
  "scales": [
    {
      "name": "legend",
      "type": "ordinal",
      "domain": ["Total food supply", "Pressure-generating, land-sourced foods","Ocean-sourced foods",  "Other land-sourced foods"],
      "range": ["#f3b229","#f5c93a","#2670a5","#e8d59a"]
    },
    {
      "name": "blues",
      "type": "linear",
      "domain": {"data": "table", "field": "depth"},
      "range": ["#2670a5", "#3d7fae", "#538fb7", "#699fc0"],
      "domainMin": 1,
      "reverse": false
    },
    {
      "name": "greys",
      "type": "ordinal",
      "domain": {"data": "table", "field": "depth"},
      "range": ["#f2e2b2", "#f7e9be", "#fcf0ca","#e8d59a"]
    },
    {
      "name": "oranges",
      "type": "linear",
      "domain": {"data": "table", "field": "depth"},
      "range": ["#f3b229", "#f4c141", "#f5c93a", "#f6d544", "#f6e04e"],
      "domainMin": 1,
      "reverse": false

    },
    {"name": "opacity",
    "type": "linear",
    "domain": {"data": "table", "field": "depth"},
    "domainMin": 1,
    "reverse": true,
    "range": [0.85,1]}
  ],
  "marks": [
    {
      "type": "arc",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {"signal": "width/3"},
          "y": {"signal": "height/2"},
          "zindex": {"value": 1},
          "opacity": [{"test": "test(/Secondary/, datum.product)", "value": 0.5 },{"value": 1}],
          "fill": [
            {
              "scale": {
                "signal": 
                "(datum.analysis_category === 'Other Land-Sourced Foods' ? 'greys': datum.analysis_category === 'Ocean-Sourced Foods' ? 'blues' : 'oranges')"
              },
              "field": "depth"
            }
          ]
        },
        "update": {
          "startAngle": {"field": "a0"},
          "endAngle": {"field": "a1"},
          "innerRadius": {"field": "r0"},
          "outerRadius": {"field": "r1"},
          "stroke": {"value": "white"},
          "strokeWidth": {"value": 0.5},
          "zindex": {"value": 1}
        },
        "hover": {
          "stroke": {"value": "red"},
          "strokeWidth": {"value": 2},
          "zindex": {"value": 0}
        }
      }
    }
  ],
  "legends": [
    {
      "title": ["Sources of Protein"],
      "orient": "none",
      "legendX": {"signal" : "width*.65"},
      "legendY": {"signal" : "height*.4"},
      "type": "symbol",
      "fill": "legend",
      "titleFontSize": {"signal": "width/40"},
      "titleFont": "Lato",
      "labelFontSize": {"signal": "width/50"},
      "labelFont": "Lato",
      "clipHeight": 16,
      "encode": {
        "labels": {
          "interactive": true,
          "enter": {
            "tooltip": {
              "signal": "datum.label"
            }
          },
          "update": {
            "fill": {"value": "grey"}
          },
          "hover": {
            "fill": {"value": "firebrick"}
          }
        }
      }
    }
  ],
    "interaction_config": [
    {
      "name": "tooltip",
      "config": {
        "fields": [
          {
            "column": "label",
            "property": "Commodity",
            "type": "text",
            "format": ""
          },
          {
            "column": "protein",
            "property": "Contribution to protein supply",
            "type": "text",
            "format": ""
          }
        ]
      }
    }
  ]
}
""")
vega_view=dict(spec)
vega_view['legends'][0]['labelFont'] = 'Arial'
vega_view['legends'][0]['titleFont'] = 'Arial'
vega_view['height'] = widget_height
vega_view['width'] = widget_width
vega_view['data'][0]['url']= vega_view['data'][0]['url'].replace('{{geostore_env}}','geostore_prod')
vega_view['data'][0]['url'] = vega_view['data'][0]['url'].replace('{{geostore_id}}','f653d0a434168104f4bdcdf8c712d079')
Vega(vega_view)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19606503620713212, 'fraction_numerical': 0.027599398824976088, 'mean_word_length': 2.7731958762886597, 'pattern_counts': {'":': 153, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '4420', 'license': 'MIT', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2503, 'n_words': 555, 'path': 'widgets/ocn_calcs_015_blue_food_protein_supply/blue_food_visulization.ipynb', 'repo_name': 'resource-watch/ocean-watch-data', 'size': '7325'}