User:Yair rand/Wiki readership map

{ "version": 2, "width": 1250, "height": 670, "padding": 12, "background": "#edf1f7", "data": [ {     "name": "rawmap", "url": "wikiraw:///Extension:Graph/Demo/RawData:WorldMap-iso2-json", "format": {"type": "topojson","feature": "countries"} },   {      "name": "map", "source":"rawmap", "transform": [ {         "type": "geopath", "value": "data", "scale": 180, "center": [-180,125], "translate": [0,0], "projection": "equirectangular" }     ]    },    {      "description":"The zoom effect is faked for borders. We put a 5x-zoom map behind the regular, and toggle hiding", "name": "bigmap", "source":"rawmap", "transform": [ {         "type": "geopath", "value": "data", "scale": 900, "center": [-180,125], "translate": [0,0], "projection": "equirectangular" }     ]    },    {      "name": "raw_reader_data", "url": "tabular:///Pageviews-by-country-monthly-2020-01.tab", "format":{ "":"wiki_db,country,count", "type":"json","property":"data" },     "transform":[ {         "description":"Optional filter: Show only sister projects. Change 1 to 0 to activate.", "type":"filter", "test":"1||!(test(/wiki$/,datum.wiki_db) && indexof(['commonswiki','metawiki','mediawikiwiki','wikidatawiki'],datum.wiki_db) == -1)" }     ]    },    {      "name": "by_wiki", "source":"raw_reader_data", "transform": [ {         "type": "aggregate", "groupby": ["wiki_db"], "summarize":{"count":"sum"} },       {          "type":"sort", "by":["-sum_count"] },       {          "type":"rank", "field":"sum-count" }     ]    },    {      "name": "top_wikis", "description":"List 48 largest wikis, for the legend", "source":"by_wiki", "transform": [ {         "type":"filter", "_test":"indexof(['enwiki','frwiki','dewiki','jawiki','eswiki'],datum.wiki_db)!=-1", "test":"datum.rank < 48" }     ]    },    {      "name": "by_country", "source":"raw_reader_data", "transform": [ {         "type": "aggregate", "groupby": ["country"], "summarize":{"count":"sum"} }     ]    },    {      "name":"country_coords", "url":"wikidatasparql:///?query=SELECT%20%3Fcode%20%3Fcoords%20WHERE%20%7B%0A%20%20%3Fq%20wdt%3AP297%20%3Fcode%3B%0A%20%20%20%20%20wdt%3AP625%20%3Fcoords%20.%0A%7D", "transform": [ {         "type": "geo", "projection": "equirectangular", "scale": 180, "center": [-180,125], "translate": [0,0], "lon": "coords[0]", "lat": "coords[1]" }     ]    },    {      "name": "all_pies", "source":"raw_reader_data", "transform": [ {         "description":"Wiki totals", "type":"lookup", "on":"by_wiki", "onKey":"wiki_db", "keys":["wiki_db"], "as":["db_count"] },       {            "":"wiki_db,country,count", "type": "facet", "groupby": ["country"], "transform":[ {             "description": "Each pie chart starts with slices from bigger wikis", "type":"sort", "by":"-db_count.sum_count" },           {"type":"pie","field":"count"}, {             "type":"filter", "description":"For performance...", "test":"datum.count > 100" }         ]        },        {          "description": "Add country location...", "type":"lookup", "on":"country_coords", "onKey":"code", "keys":["country"], "as":["coords_data"] },       {          "description":"Country totals, for the pie radius and top-bar text", "type":"lookup", "on":"by_country", "onKey":"country", "keys":["country"], "as":["big"] },       {          "type":"filter", "test":"datum.coords_data" },       {          "description": "Show smaller pies on top", "type":"sort", "by":"-big.sum_count" }     ]    }  ],

"signals": [ {     "name": "tooltip", "init": {}, "streams": [ {         "type": "arc:mouseover", "expr": "{'count':format(',',datum.count),wiki_db:datum.wiki_db,tc:format('.1%',datum.count/parent.big.sum_count),'pp':format('.1%',datum.count/datum.db_count.sum_count),'country':datum.country}" },       {          "type": "@legendItem:mouseover", "expr": "{'wiki_db':datum.wiki_db,'count':format(',',datum.sum_count)}", "_expr": "{'wiki_db':'viwiki'}", "_":",'wiki_db_':datum.label" },       {          "type": "@legendItemPiece:mouseover", "expr": "{'wiki_db':parent.wiki_db,'count':format(',',parent.sum_count)}" },       {"type": "arc:mouseout", "expr": "{}"}, {"type": "@legendItem:mouseout", "expr": "{}"} ]   },    {      "name": "zoom", "init": 1, "streams": [ {"type": "wheel", "expr": "if(event.deltaY>0,1/czoom,0.2/czoom)"} ]   },    {      "name": "czoom", "init": 1, "streams": { "type":"zoom", "expr": "czoom * zoom" }   },    {      "name": "circleMax", "init": 50, "streams": { "type":"czoom", "expr": "if( czoom == 1, 50, 200 )" }   },    {      "name": "point", "init": 0, "streams": [{ "type": "mousedown", "expr": "{x: eventX, y: eventY}" }]   },    {      "name": "delta", "init": 0, "streams": [ {         "type": "[mousedown, window:mouseup] > window:mousemove", "expr": "{x: point.x - eventX, y: point.y - eventY}" }     ]    },    {      "name": "xAnchor", "init": 0, "streams": [{ "type": "mousemove", "expr": "eventX", "scale": {"name":"scx", "invert":true} }]   },    {      "name": "yAnchor", "init": 0, "streams": [{ "type": "mousemove", "expr": "eventY", "scale": {"name":"scy", "invert":true} }]   },    {      "name": "xs", "streams": [{ "type": "mousedown, mouseup, wheel", "expr": "{min: xMin, max: xMax}" }]   },    {      "name": "ys", "streams": [{ "type": "mousedown, mouseup, wheel", "expr": "{min: yMin, max: yMax}" }]   },    {      "name": "xMin", "init": 0, "streams": [ {"type": "delta", "expr": "xs.min + (xs.max-xs.min)*delta.x/width"}, {"type": "zoom", "expr": "(xs.min-xAnchor)*zoom + xAnchor"} ]   },    {      "name": "xMax", "init": 1250, "streams": [ {"type": "delta", "expr": "xs.max + (xs.max-xs.min)*delta.x/width"}, {"type": "zoom", "expr": "(xs.max-xAnchor)*zoom + xAnchor"} ]   },    {      "name": "yMin", "init": 0, "streams": [ {"type": "delta", "expr": "ys.min + (ys.max-ys.min)*delta.y/height"}, {"type": "zoom", "expr": "(ys.min-yAnchor)*zoom + yAnchor"} ]   },    {      "name": "yMax", "init": 670, "streams": [ {"type": "delta", "expr": "ys.max + (ys.max-ys.min)*delta.y/height"}, {"type": "zoom", "expr": "(ys.max-yAnchor)*zoom + yAnchor"} ]   }  ],  "scales":[ {     "name": "color", "type": "ordinal", "_domain": {"data": "raw_reader_data", "field": "wiki_db"}, "_sort":{"field":"wiki_db"}, "range": "category20", "_range":["orange","blue","red","green"] },   {      "name": "pie_size", "type": "sqrt", "domain": {"data": "by_country", "field": "sum_count"}, "_domain": [0,4000000], "range": [0,50], "rangeMax": { "signal": "circleMax" } },   {      "name": "legendY", "type": "ordinal", "domain": { "data": "top_wikis", "field": "wiki_db","sort":{"field":"sum_count","op":"median"} },     "reverse":true, "range": [0,700] },   {      "name": "scx", "type": "linear", "range": "width", "zero": false, "domainMin": {"signal": "xMin"}, "domainMax": {"signal": "xMax"} },   {      "name": "scy", "type": "linear", "range": "height", "zero": false, "reverse":true, "domainMin": {"signal": "yMin"}, "domainMax": {"signal": "yMax"} } ],  "marks": [ {     "name": "map", "type": "path", "from": {"data": "map"}, "properties": { "update": { "x":{"value":"0","scale":"scx"}, "y":{"value":"0","scale":"scy"}, "fill":[{"test":"czoom == 1", "value": "grey"},{"value":"transparent"}], "_fill": {"value": "grey"} },       "enter":{ "path": {"field": "layout_path"} }     }    },    {      "name": "map", "type": "path", "from": {"data": "bigmap"}, "properties": { "update": { "x":{"value":"0","scale":"scx"}, "y":{"value":"0","scale":"scy"}, "fill":[{"test":"czoom == 0.2", "value": "grey"},{"value":"transparent"}], "_fill": {"value": "grey"} },       "enter":{ "path": {"field": "layout_path"} }     }    },    {      "name": "circle5", "type": "group", "from": { "data": "all_pies" },     "properties": { "update": { "x": {"field": "coords_data.layout_x","scale":"scx"}, "y": {"field": "coords_data.layout_y","scale":"scy"} }     },      "marks":[ { "type": "arc", "properties": { "enter": { "startAngle": {"field": "layout_start"}, "endAngle": {"field": "layout_end"}, "stroke": {"value": "#fff"}, "fill": {"field": "wiki_db","scale":"color"} },         "hover":{ "strokeWidth":{"value":1} },         "update":{ "outerRadius": {"field": {"parent":"big.sum_count"},"scale":"pie_size"}, "strokeWidth":[ { "test": "tooltip.wiki_db && tooltip.wiki_db == datum.wiki_db && datum.count == 1", "value": 10 },             { "test": "tooltip.wiki_db && tooltip.wiki_db == datum.wiki_db", "value": 0.5 },             {"value":0.25} ],           "opacity": [ { "test": "tooltip.wiki_db && tooltip.wiki_db != datum.wiki_db", "value": 0.1 },             {"value": 1} ]         }        }      } ]    },    {      "description": "Legend outline box", "type":"group", "properties":{ "enter":{ "x":{"value":25}, "y":{"value":0}, "fill":{"value":"white"}, "width":{"value":110}, "height":{"value":700} }     },      "marks":[ {         "type":"text", "properties":{ "enter":{ "text": {"value":"Wikis:"}, "fontWeight":{"value":"bold"}, "fill":{"value":"black"}, "x":{"value":5}, "y":{"value":15} }         }        }      ]    },    {      "type":"group", "description":"legend contents", "from": { "data": "top_wikis" },     "name":"legendItem", "properties":{ "enter": { "width":{"value":100}, "height":{"value":14}, "x":{"value":30}, "y": {"scale":"legendY","field": "wiki_db","offset":3}, "fill":{"value":"white"} }     },      "marks":[ {         "type": "rect","name":"legendItemPiece", "properties": { "enter": { "fill": {"field":{"parent": "wiki_db"},"scale":"color"}, "height":{"value":10}, "width":{"value":10}, "x":{"value":0}, "y": {"value":2} },           "update":{ "opacity": [ {                 "test": "tooltip.wiki_db && tooltip.wiki_db != parent.wiki_db", "value": 0.25 },               {"value": 1} ]           }          }        },        {          "type": "text","name":"legendItemPiece", "properties": { "enter": { "text": {"field":{"parent":"wiki_db"}}, "baseline":{"value":"top"}, "fill":{"value":"black"}, "_fill":{"scale":"color","field": "wiki_db"}, "fontSize":{"value":10}, "x":{"value":13}, "y2": {"value":1} },         "update":{ "opacity": [ {                 "test": "tooltip.wiki_db && tooltip.wiki_db != parent.wiki_db", "value": 0.25 },               {"value": 1} ]           }          }        }      ]    },    {      "type":"group", "description": "Text box at the top with stats, displayed when hovering an item.", "properties":{ "enter":{ "x":{"value":155}, "y":{"value":20}, "height":{"value":16}, "fill":{"value":"#FFF"}, "opacity": {"value": 0.8} },       "update":{ "width": [ {             "test": "!tooltip.wiki_db", "value": 0 },           {"value":400} ]       }      },      "marks":[ {         "type":"text", "properties":{ "enter":{ "baseline":{"value":"top"}, "fill":{"value":"black"}, "x":{"value":2}, "y":{"value":1} },           "update":{ "text": [ {"test":"tooltip.tc","template": "\u007b{tooltip.wiki_db}}: \u007b{tooltip.count}},000 views, \u007b{tooltip.tc}} of country (\u007b{tooltip.country}}), \u007b{tooltip.pp}} of \u007b{tooltip.wiki_db}}"}, {"template": "\u007b{tooltip.wiki_db}}: \u007b{tooltip.count}},000 views (all countries)"} ],             "opacity": [ {                 "test": "!tooltip.wiki_db", "value": 0 },               {"value": 1} ]           }          }        }      ]    }  ] }