User:Jason.nlw

From Semantic Name Authority Repository Cymru
Revision as of 11:57, 19 January 2023 by Jason.nlw (talk | contribs)
Jump to navigation Jump to search

<graph> {

 "version": 2,
 "width": 300,
 "height": 50,
 "data": [
   {
     // Data is retrieved from the Wikidat Query API (SPARQL).
     "name": "table",
     "url": "wikidatasparql:///?query=SELECT+%3Finstance+%3FinstanceLabel+%3Fvalue+%7B+%7B%0A++++SELECT+%3Finstance+%28COUNT%28DISTINCT+%3Fitem%29+as+%3Fvalue%29+%7B%0A++++++++++%7B%3Fitem+wdt%3AP131+wd%3AQ25+.+%7D%0A++UNION+%7B+%3Fitem+wdt%3AP131+%3Fpob+.+%3Fpob+wdt%3AP131%2A+wd%3AQ25+%7D%0A++++++++++%3Fitem+wdt%3AP31+%3Finstance.%7D%0A++++GROUP+BY+%3Finstance%0A++++HAVING+%28%3Fvalue+%3E+100%29%7D%0A+SERVICE+wikibase%3Alabel+%7B+bd%3AserviceParam+wikibase%3Alanguage+%22en%22.+%7D+%7D%0AORDER+BY+DESC%28%3Fvalue%29%0ALIMIT+15%0AOFFSET+0",
     "format": { "type": "json" },
     "transform": [
       // sort in descending order using value as the sort key
       {"type": "sort","by": "-value"},
       // To visualize, use "pie" transformation to add layout_start, layout_end, and layout_mid fields to each {label, value} object
       // These fields contain angles at which to start and stop drawing arcs. First element's start will be 0, and last element's end will be 360 degrees (in radians)
       {"type": "pie","field": "value"}
     ]
   }
 ],
 // Scales are like functions -- marks use them to convert a data value into a visual value, like x or y coordinate on the graph, or a color value.
 "scales": [
   {
     // This scale will be used to assign a color to each slice, using a palette of 10 colors
     "name": "color",
     "domain": {"data": "table","field": "instanceLabel"},
     "range": "category10",
     "type": "ordinal"
   }
 ],
 "marks": [
   {
     // This mark draws the actual pie chart from the data source
     // Each element is an arc between layout_start and layout_end angles (as calculated by the pie transformation)
     // drawn with a given radius, stroke, and fill.
     "from": {"data": "table"},
     "type": "arc",
     "properties": {
       "enter": {
         "fill": {"scale": "color","field": "instanceLabel"},
         // Use "width" signal divided by 2 for the outer radius
         "outerRadius": {"signal": "width", "mult": 0.5},
         "startAngle": {"field": "layout_start"},
         "endAngle": {"field": "layout_end"},
         "stroke": {"value": "white"},
         "strokeWidth": {"value": 1}
       }
     }
   },
   {
     // This mark draws labels around the pie chart after the pie chart has been drawn
     "type": "text",
     // Before drawing, we need to perform a number of calculations to figure out the exact location and orientation of the text
     "from": {
       "data": "table",
       "transform": [
         // For each data point (datum), each of these transformations will be ran in order.
         // Formula transformation evaluates the expression and assigns result to the datapoint
         // Size of the pie slice, in degrees:  sliceSize = (end - start) * 180 / Pi
         { "type": "formula", "field": "sliceSize", "expr": "(datum.layout_end - datum.layout_start)*180/PI" },
         // Draw text only if the slice of the arc is more than 2 degrees to avoid overcrowding
         { "type": "filter", "test": "datum.sliceSize > 2" },
         // Determine the side of the pie chart we are on - left or right.
         { "type": "formula", "field": "invert", "expr": "datum.layout_mid*180/PI < 180 ? 1 : -1" },
         // If on the left, the text should be right-aligned (go from the rim inward)
         { "type": "formula", "field": "align", "expr": "datum.invert > 0 ? 'left' : 'right'" },
         // At what angle should the text be drawn relative to the point on the circle
         { "type": "formula", "field": "angle", "expr": "(datum.layout_mid*180/PI)-90*datum.invert" },
         // Make font smaller for smaller pie slices
         { "type": "formula", "field": "fontSize", "expr": "datum.sliceSize > 20 ? 13 : (datum.sliceSize > 10 ? 11 : 10)" },
         // Make font bold for largest pie slices 
         { "type": "formula", "field": "fontWeight", "expr": "datum.sliceSize > 15 ? 'bold' : 'normal'" }
       ]
     },
     "properties": {
       "enter": {
         // Use the fields calculated in the transformation to draw category names
         "align": {"field": "align"},
         "angle": {"field": "angle"},
         "baseline": {"value": "middle"},
         "fill": {"value": "black"},
         "fontSize": {"field": "fontSize"},
         "fontWeight": {"field": "fontWeight"},
         "radius": {"signal": "width", "mult": 0.5, "offset": 10},
         "text": {"template": "\u007b{datum.value|number:',d' }\u007d"},
         "theta": {"field": "layout_mid"}
       }
     }
   }
 ],
 // Show legend, using the scale named "color" as the source.
 "legends": [{
   "title": "Type of place",
   "fill": "color",
    "properties": {
      "title": { "fontSize": {"value": 14} },
      "symbols": { "stroke": {"value": "transparent"} },
      "legend": {
        "strokeWidth": {"value": 1.5},
        // Set new position of the legend relative to the center of the graph, using graph's width & height
        "x": {"signal":"width", "mult":0.5, "offset":60},
        "y": {"signal":"height", "mult":-0.5}
      }
    }
 }]

} </graph>See it in Wikidata Query Service

<templatestyles src="Graph:Chart/styles.css"></templatestyles><graph>{"legends":[{"stroke":"color","title":"passengers per month","fill":"color"}],"scales":[{"type":"linear","name":"x","domain":{"data":"chart","field":"x"},"zero":false,"range":"width","nice":true},{"type":"linear","name":"y","domain":{"data":"chart","field":"y"},"zero":false,"range":"height","nice":true},{"domain":{"data":"chart","field":"series"},"type":"ordinal","name":"color","range":"category10"}],"version":2,"marks":[{"type":"group","marks":[{"properties":{"hover":{"stroke":{"value":"red"}},"update":{"stroke":{"scale":"color","field":"series"}},"enter":{"y":{"scale":"y","field":"y"},"x":{"scale":"x","field":"x"},"stroke":{"scale":"color","field":"series"},"strokeWidth":{"value":2.5}}},"type":"line"}],"from":{"data":"chart","transform":[{"groupby":["series"],"type":"facet"}]}}],"height":200,"axes":[{"type":"x","scale":"x","format":"d","properties":{"axis":{"strokeWidth":{"value":2},"stroke":{"value":"#54595d"}},"ticks":{"stroke":{"value":"#54595d"}},"title":{"fill":{"value":"#54595d"}},"labels":{"fill":{"value":"#54595d"}}},"grid":false},{"type":"y","scale":"y","format":"d","properties":{"title":{"fill":{"value":"#54595d"}},"grid":{"stroke":{"value":"#54595d"}},"ticks":{"stroke":{"value":"#54595d"}},"axis":{"strokeWidth":{"value":2},"stroke":{"value":"#54595d"}},"labels":{"fill":{"value":"#54595d"}}},"grid":false}],"data":[{"format":{"parse":{"y":"integer","x":"integer"},"type":"json"},"name":"chart","values":[{"y":6463,"series":"y1","x":1},{"y":6360,"series":"y1","x":2},{"y":7411,"series":"y1","x":3},{"y":6664,"series":"y1","x":4},{"y":6945,"series":"y1","x":5},{"y":6790,"series":"y1","x":6},{"y":7150,"series":"y1","x":7},{"y":8089,"series":"y1","x":8},{"y":7041,"series":"y1","x":9},{"y":7482,"series":"y1","x":10},{"y":7432,"series":"y1","x":11},{"y":7068,"series":"y1","x":12},{"y":6726,"series":"y2","x":1},{"y":6489,"series":"y2","x":2},{"y":7818,"series":"y2","x":3},{"y":6564,"series":"y2","x":4},{"y":6968,"series":"y2","x":5},{"y":7089,"series":"y2","x":6},{"y":7175,"series":"y2","x":7},{"y":7994,"series":"y2","x":8},{"y":7294,"series":"y2","x":9},{"y":7090,"series":"y2","x":10},{"y":7247,"series":"y2","x":11},{"y":6871,"series":"y2","x":12},{"y":6568,"series":"y3","x":1},{"y":5678,"series":"y3","x":2},{"y":2748,"series":"y3","x":3}]}],"width":600}</graph>


<graph mode="interactive"> {

 "version": 2,
 "width": 800,
 "height": 600,
 "padding": {"top":0, "bottom":0, "left":0, "right":0},
 "signals": [
   {
     "name": "hover", "init": null,
     "streams": [
       { "type": "symbol:mouseover",
         "expr": "datum._id" },
       { "type": "window:mouseup, symbol:mouseout", "expr": "null" }
     ]
   },
   {
     "name": "grab", "init": null,
     "streams": [
       { "type": "symbol:touchstart, symbol:mousedown",
         "expr": "datum._id" },
       { "type": "touchend, window:mouseup", "expr": "null" }
     ]
   },
   {
     "name": "active", "init": null,
     "streams": [
       { "type": "hover, grab", "expr": "{id: grab || hover}" },
       {
         "type": "symbol:touchmove, [symbol:mousedown, window:mouseup] > window:mousemove",
         "expr": "{x: eventX(), y: eventY(), id: grab, update: true}"
       }
     ]
   },
   {
     "name": "click",
     "init": null,
     "streams": [
       {
         "type": "symbol:dblclick",
         "expr": "open('https://www.wikidata.org/wiki/' + datum.item)"
       }
     ]
   }
 ],
 "data": [
   {
     "name": "rawData",
     "url":"wikidatasparql:///?query=SELECT%20%3Fitem%20%3FitemLabel%20%3FlinkTo%20%7B%20wd%3AQ5%20wdt%3AP279%2A%20%3Fitem%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP279%20%3FlinkTo%20%7D%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20%7D",
     "format": { "type": "json" }
   },
   {
     "name": "rawNodes",
     "source": "rawData",
     "transform": [
       {
         "type": "aggregate", 
         "groupby": ["item"]
       },
       {"type": "rank" },
       {"type": "formula", "field": "index", "expr": "datum.rank - 1" },
       {"type": "formula", "field": "name", "expr": "datum.values[0].itemLabel" }
     ]
   },
   {
     "name": "edges",
     "source": "rawData",
     "transform": [
       {"type": "filter", "test": "datum.linkTo"},
       {
         "type": "lookup",
         "on": "rawNodes",
         "onKey": "item",
         "keys": ["item", "linkTo"],
         "as": ["_target", "_source"]
       },
       {"type": "filter","test": "datum._source"},
       {"type": "formula", "field": "source", "expr": "datum._source.index" },
       {"type": "formula", "field": "target", "expr": "datum._target.index" }
     ]
   },
   {
     "name": "nodes",
     "source": "rawNodes",
     "transform": [
       {
         "type": "force",
         "links": "edges",
         "linkDistance": 7,
         "charge": -200,
         "linkStrength": 0.5,
         "bound": false,
         "interactive": false,
         "active": {"signal": "active"}
       }
     ]
   }, 
   {
     "name": "edges2",
     "source": "edges",
     "transform": [
       { "type": "lookup", "on": "nodes",
         "keys": ["source", "target"],
         "as":   ["_source", "_target"] },
       {
         "type": "formula",
         "field": "angle",
         "expr": "atan2(datum._target.layout_y - datum._source.layout_y, datum._target.layout_x - datum._source.layout_x) * 180 / PI"
       },
       { "type": "linkpath", "shape": "line" }
     ]
   }
 ],
 "marks": [
   {
     "type": "path",
     "from": {
       "data": "edges2"
     },
     "properties": {
       "enter": {
         "stroke": {"value": "black"}
       },
       "update": {
         "path": {"field": "layout_path"}
       }
     }
   },
   {
     "type": "symbol",
     "from": {"data": "nodes"},
     "properties": {
       "enter": {
         "fill": {"value": "#ccc"},
         "size": {"value": 70},
         "stroke": {"value": "black"}
       },
       "update": {
         "x": {"field": "layout_x"},
         "y": {"field": "layout_y"}
       }
     }
   },
   {
     "type": "text",
     "from": {"data": "edges2"},
     "properties": {
       "enter": {
         "fill": {"value": "black"},
         "text": {"value": "  ◀"},
         "fontSize": {"value": "8"},
         "baseline": {"value": "middle"}
       },
       "update": {
         "x": {"field": "_source.layout_x"},
         "y": {"field": "_source.layout_y"},
         "angle": {"field": "angle"}
       }
     }
   },
   {
     "type": "text",
     "from": {
       "data": "nodes"
     },
     "properties": {
       "enter": {
         "fill": {"value": "black"},
         "text": {"field": "name"},
         "fontSize": {"value": "10"},
         "baseline": {"value": "middle"}
       },
       "update": {
         "x": {"field": "layout_x", "offset": 10},
         "y": {"field": "layout_y"}
       }
     }
   }
 ]

}</graph>}