Graph Test

From wikiluntti

Introduction

https://www.mediawiki.org/wiki/Extension:Graph/Guide

https://cod3v.info/index.php?title=Special:GraphSandbox

Installed ExternalData and WhiteListPages extensions. Not any help.

https://www.mediawiki.org/wiki/Extension:Graph#External_data

Methods

Graph

<graph> {

 "width": 600,
 "height": 150,
 "background": "#ffffff",
 "data": [
   {
     "name": "tree",
     "values": [
       {"A": "Luukalat","B": "Viuhkaeväiset","C": "Cladistia" },
       {"A": "Luukalat","B": "Viuhkaeväiset","C": "Rustokiillesuomuiset" },
       {"A": "Luukalat","B": "Viuhkaeväiset","C": "Luukiillesuomuiset" },
       {"A": "Luukalat","B": "Viuhkaeväiset","C": "Teleosteomorpa" },
       {"A": "Luukalat","B": "Varsieväiset","C": "Latimeriat" },
       {"A": "Luukalat","B": "Varsieväiset","C": "Keuhkokalat"},
       {"A": "Luukalat","B": "Varsieväiset","C": "Nelijalkaiset"},
       {"A": "Rustokalat","B": "Hait","C": ""},
       {"A": "Rustokalat","B": "Rauskut","C": ""},
       {"A": "Rustokalat","B": "Sillikuningaskalat","C": ""},
       {"A": "Leuattomat selkärankaiset","B": "Ympyräsuiset","C": "Nahkiaiset"},
       {"A": "Leuattomat selkärankaiset","B": "Alkukalat","C": "Pteraspidomorphi"},
       {"A": "Leuattomat selkärankaiset","B": "Alkukalat","C": "Thelodonti"},
       {"A": "Leuattomat selkärankaiset","B": "Alkukalat","C": "Cephalaspidomorphi"}
     ],
     "transform": [
       {
         "type": "treeify",
         "groupby": ["A","B"]
       },
       {
         "type": "hierarchy",
         "mode": "cluster",
         "nodesize": [20,100]
       },
       {
         "type": "formula",
         "field": "align",
         "expr": "'right'"
       },
       {
         "type": "formula",
         "field": "offset",
         "expr": "-10"
       }
     ]
   }
 ],
 "marks": [
   {
     "type": "path",
     "from": {
       "data": "tree",
       "transform": [
         {"type": "filter","test": "datum.parent"},
         {
           "type": "linkpath",
           "sourceX": "parent.layout_y",
           "sourceY": "parent.layout_x",
           "targetX": "layout_y",
           "targetY": "layout_x",
           "shape": "cornerX"
         }
       ]
     },
     "properties": {
       "enter": {
         "path": {"field": "layout_path"},
         "strokeWidth": {"value": 2},
         "stroke": {"value": "#000000"}
       }
     }
   },
   {
     "type": "text",
     "from": {
       "data": "tree",
       "transform": [
         {
           "type": "formula",
           "field": "ff_node_label",
           "expr": "if(datum.layout_depth == 0, 'Selkärankaiset', if(datum.layout_depth == 1, datum.A, if(datum.layout_depth == 2, datum.B, if(datum.layout_depth == 3, datum.C, ))))"
         }
       ]
     },
     "properties": {
       "enter": {
         "x": {"field": "layout_y"},
         "dx": {"field": "offset"},
         "y": {"field": "layout_x"},
         "dy": {"value": -8},
         "font": {"value": "Arial"},
         "fontSize": {"value": 16},
         "align": {"field": "align"},
         "baseline": {"value": "middle"},
         "fill": {"value": "#0000ff"},
         "text": {"field": "ff_node_label"}
       }
     }
   }
 ]

} </graph>

Line Chart

Tutorial: https://en.wikipedia.org/wiki/Template:Graph:Chart

<graph> Template:Graph:Chart </graph>


Line graphs

https://www.mediawiki.org/wiki/Extension:Graph/Demo

<graph>

{
  "version": 2,
  "width": 550,
  "height": 400,
  "data": [
    {
      "name": "tree",
      "url": "https://cod3v.info/index.php?title=Extension:Graph/data/flare-json",
      "format": {"type": "treejson"},
      "transform": [
        {"type": "hierarchy", "field": "size", "size": [400, 500]}
      ]
    }
  ],
  "marks": [
    {
      "type": "path",
      "from": {
        "data": "tree",
        "transform": [
          {
            "type": "filter", "test": "datum.parent"
          },
          {
            "type": "linkpath",
            "sourceX": "parent.layout_y",
            "sourceY": "parent.layout_x",
            "targetX": "layout_y",
            "targetY": "layout_x",
            "shape": "diagonalX"
          }
        ]
      },
      "properties": {
        "enter": {
          "path": {"field": "layout_path"},
          "stroke": {"value": "#ddd"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "tree"},
      "properties": {
        "enter": {
          "x": {"field": "layout_y", "offset": -10},
          "y": {"field": "layout_x"},
          "font": {"value": "Helvetica Neue"},
          "fontSize": {"value": 10},
          "align": {"value": "left"},
          "baseline": {"value": "middle"},
          "fill": {"value": "#000"},
          "text": {"field": "name"}
        }
      }
    }
  ]
}

</graph>


Horizontal Bar Chart

https://www.mediawiki.org/wiki/Extension:Graph/Demo/HorizontalBarGraphSample

<graph> {

 "version": 2, "width": 400, "height": 400,
 "padding": {"top": 10, "left": 30, "bottom": 30, "right": 10},
 "data": [
   {
     "name": "table",
     "values": [
       {"t": "a", "v": 28}, {"t": "b", "v": 55},
       {"t": "c", "v": 43}, {"t": "d", "v": 91},
       {"t": "e", "v": 81}, {"t": "f", "v": 53},
       {"t": "g", "v": 19}, {"t": "h", "v": 87},
       {"t": "i", "v": 52}, {"t": "j", "v": 48},
       {"t": "k", "v": 24}, {"t": "l", "v": 49},
       {"t": "m", "v": 87}, {"t": "n", "v": 66},
       {"t": "o", "v": 17}, {"t": "p", "v": 27},
       {"t": "q", "v": 68}, {"t": "r", "v": 16},
       {"t": "s", "v": 49}, {"t": "t", "v": 15}
     ]
   }
 ],
 "scales": [
   {
     "name": "x",
     "range": "width",
     "domain": {"data": "table", "field": "v"}
   },
   {
     "name": "y",
     "range": "height",
     "type": "ordinal",
     "domain": {"data": "table", "field": "t"}
   }
 ],
 "axes": [
   {"type": "x", "scale": "x"},
   {"type": "y", "scale": "y"}
 ],
 "marks": [
   {
     "type": "rect",
     "from": {"data": "table"},
     "properties": {
       "enter": {
         "y": {"scale": "y", "field": "t"},
         "height": {"scale": "y", "band": true, "offset": -1},
         "x": {"scale": "x", "value": 0},
         "x2": {"scale": "x", "field": "v"}
       },
       "update": {
         "fill": {"value": "steelblue"}
       },
       "hover": {
         "fill": {"value": "red"}
       }
     }
   }
 ]

} </graph>

Two graphs

https://www.mediawiki.org/wiki/Extension:Graph/Demo#Overlaying_two_types_of_data

<graph> {

 "width": 500,
 "height": 250,
 "padding": "auto",
 "data": [
   {
     "name": "table",
     "values": [
       {"year": 1875,"population": 1309},
       {"year": 1890,"population": 1558},
       {"year": 1910,"population": 4512},
       {"year": 1925,"population": 8180},
       {"year": 1933,"population": 15915},
       {"year": 1939,"population": 24824},
       {"year": 1946,"population": 28275},
       {"year": 1950,"population": 29189},
       {"year": 1964,"population": 29881},
       {"year": 1971,"population": 26007},
       {"year": 1981,"population": 24029},
       {"year": 1985,"population": 23340},
       {"year": 1989,"population": 22307},
       {"year": 1990,"population": 22087},
       {"year": 1991,"population": 22139},
       {"year": 1992,"population": 22105},
       {"year": 1993,"population": 22242},
       {"year": 1994,"population": 22801},
       {"year": 1995,"population": 24273},
       {"year": 1996,"population": 25640},
       {"year": 1997,"population": 27393},
       {"year": 1998,"population": 29505},
       {"year": 1999,"population": 32124},
       {"year": 2000,"population": 33791},
       {"year": 2001,"population": 35297},
       {"year": 2002,"population": 36179},
       {"year": 2003,"population": 36829},
       {"year": 2004,"population": 37493},
       {"year": 2005,"population": 38376},
       {"year": 2006,"population": 39008},
       {"year": 2007,"population": 39366},
       {"year": 2008,"population": 39821},
       {"year": 2009,"population": 40179},
       {"year": 2010,"population": 40511},
       {"year": 2011,"population": 40465},
       {"year": 2012,"population": 40905},
       {"year": 2013,"population": 41258},
       {"year": 2014,"population": 41777}
     ]
   },
   {
     "name": "annotation",
     "values": [
       {"start": 1933,"end": 1945,"text": "Nazi rule"},
       {
         "start": 1948,
         "end": 1989,
         "text": "GDR (East Germany)"
       }
     ]
   }
 ],
 "scales": [
   {
     "name": "x",
     "type": "linear",
     "range": "width",
     "zero": false,
     "domain": {"data": "table","field": "year"}
   },
   {
     "name": "y",
     "type": "linear",
     "range": "height",
     "nice": true,
     "domain": {"data": "table","field": "population"}
   },
   {
     "name": "color",
     "type": "ordinal",
     "domain": {"data": "annotation","field": "text"},
     "range": ["black","red"]
   }
 ],
 "axes": [
   {
     "type": "x",
     "scale": "x",
     "format": "d",
     "title": "Year",
     "ticks": 15
   },
   {
     "type": "y",
     "scale": "y",
     "title": "Population",
     "grid": true,
     "layer": "back"
   }
 ],
 "marks": [
   {
     "type": "rect",
     "from": {"data": "annotation"},
     "properties": {
       "enter": {
         "x": {"scale": "x","field": "start"},
         "x2": {"scale": "x","field": "end"},
         "y": {"value": 0},
         "y2": {"signal": "height"},
         "fill": {"scale": "color","field": "text"},
         "opacity": {"value": 0.2}
       }
     }
   },
   {
     "type": "line",
     "from": {"data": "table"},
     "properties": {
       "enter": {
         "interpolate": {"value": "monotone"},
         "x": {"scale": "x","field": "year"},
         "y": {"scale": "y","field": "population"},
         "stroke": {"value": "steelblue"},
         "strokeWidth": {"value": 3}
       }
     }
   },
   {
     "type": "symbol",
     "from": {"data": "table"},
     "properties": {
       "enter": {
         "x": {"scale": "x","field": "year"},
         "y": {"scale": "y","field": "population"},
         "stroke": {"value": "steelblue"},
         "fill": {"value": "white"},
         "size": {"value": 30}
       }
     }
   },
   {
     "type": "text",
     "from": {
       "data": "table",
       "transform": [
         {
           "type": "aggregate",
           "summarize": {"year": ["min","max"]}
         }
       ]
     },
     "properties": {
       "enter": {
         "x": {"signal": "width","mult": 0.5},
         "y": {"value": -10},
         "text": {
           "template": "Population of Falkensee from Template:Datum.min year to Template:Datum.max year"
         },
         "fill": {"value": "black"},
         "fontSize": {"value": 16},
         "align": {"value": "center"},
         "fontWeight": {"value": "bold"}
       }
     }
   }
 ],
 "legends": [
   {
     "fill": "color",
     "title": "Period",
     "properties": {
       "symbols": {
         "strokeWidth": {"value": 0},
         "shape": {"value": "square"},
         "opacity": {"value": 0.3}
       },
       "legend": {
         "x": {"value": 10},
         "y": {"value": 5},
         "fill": {"value": "white"}
       }
     }
   }
 ]

} </graph>

Bar chart from Vega Tutorials

https://vega.github.io/vega/tutorials/bar-chart/

<graph> {

 "$schema": "https://vega.github.io/schema/vega/v5.json",
 "version": 2,
 "width": 400,
 "height": 200,
 "padding": 5,
 "data": [
   {
     "name": "table",
     "values": [
       {"category": "A", "amount": 28},
       {"category": "B", "amount": 55},
       {"category": "C", "amount": 43},
       {"category": "D", "amount": 91},
       {"category": "E", "amount": 81},
       {"category": "F", "amount": 53},
       {"category": "G", "amount": 19},
       {"category": "H", "amount": 87}
     ]
   }
 ],
 "signals": [
   {
     "name": "tooltip",
     "value": {},
     "on": [
       {"events": "rect:mouseover", "update": "datum"},
       {"events": "rect:mouseout",  "update": "{}"}
     ]
   }
 ],
 "scales": [
   {
     "name": "xscale",
     "type": "band",
     "domain": {"data": "table", "field": "category"},
     "range": "width",
     "padding": 0.05,
     "round": true
   },
   {
     "name": "yscale",
     "domain": {"data": "table", "field": "amount"},
     "nice": true,
     "range": "height"
   }
 ],
 "axes": [
   { "orient": "bottom", "scale": "xscale" },
   { "orient": "left", "scale": "yscale" }
 ],
 "marks": [
   {
     "type": "rect",
     "from": {"data":"table"},
     "encode": {
       "enter": {
         "x": {"scale": "xscale", "field": "category"},
         "width": {"scale": "xscale", "band": 1},
         "y": {"scale": "yscale", "field": "amount"},
         "y2": {"scale": "yscale", "value": 0}
       },
       "update": {
         "fill": {"value": "steelblue"}
       },
       "hover": {
         "fill": {"value": "red"}
       }
     }
   },
   {
     "type": "text",
     "encode": {
       "enter": {
         "align": {"value": "center"},
         "baseline": {"value": "bottom"},
         "fill": {"value": "#333"}
       },
       "update": {
         "x": {"scale": "xscale", "signal": "tooltip.category", "band": 0.5},
         "y": {"scale": "yscale", "signal": "tooltip.amount", "offset": -2},
         "text": {"signal": "tooltip.amount"},
         "fillOpacity": [
           {"test": "isNaN(tooltip.amount)", "value": 0},
           {"value": 1}
         ]
       }
     }
   }
 ]

} </graph>

Radial Tree

https://www.mediawiki.org/wiki/Extension:Graph/Demo#Radial_tree

<graph> {

 "version": 2,
 "width": 600,
 "height": 600,
 "padding": 5,
 "data": [
   {
     "name": "tree",
     "url": "https://www.mediawiki.org/wiki/Extension:Graph/data/flare-json",
     "format": {"type": "treejson"},
     "transform": [
       { "type": "hierarchy", "sort": "name", "size": [6.28, 300], "orient": "radial", "output": {"x": "angle1", "y": "radius"} },
       { "type": "formula", "field": "angle",   "expr": "(datum.angle1 + rotation) % (2*PI)" },
       { "type": "formula", "field": "sign",    "expr": "datum.angle > 3.14 ? 1 : -1" },
       { "type": "formula", "field": "degrees", "expr": "!datum.radius ? 0 : (datum.angle + datum.sign*1.57) * 180/PI" },
       { "type": "formula", "field": "yy",      "expr": "300 + datum.radius * cos(datum.angle)" },
       { "type": "formula", "field": "xx",      "expr": "300 + datum.radius * -sin(datum.angle)" },
       { "type": "formula", "field": "align",   "expr": "!datum.radius ? 'center' : datum.sign < 0 ? 'left' : 'right'" }
     ]
   }
 ],
 "marks": [
   {
     "type": "path",
     "from": {
       "data": "tree",
       "transform": [
         {"type": "filter", "test": "datum.parent"},
         {
           "type": "linkpath",
           "sourceX": "parent.xx", "sourceY": "parent.yy",
           "targetX": "xx", "targetY": "yy",
           "shape": "line"
         }
       ]
     },
     "properties": {
       "update": {
         "path": {"field": "layout_path"},
         "stroke": {"value": "#ddd"}
       }
     }
   },
   {
     "type": "text",
     "from": {"data": "tree"},
     "properties": {
       "update": {
         "x": {"field": "xx"},
         "y": {"field": "yy"},
         "font": {"value": "Helvetica Neue"},
         "fontSize": {"value": 10},
         "align": {"field": "align"},
         "baseline": {"value": "middle"},
         "fill": {"value": "green"},
         "text": {"field": "name"},
         "angle": {"field": "degrees"}
       }
     }
   }
 ]

} </graph>

Cartesian Tree

https://www.mediawiki.org/wiki/Extension:Graph/Demo#Cartesian_tree

<graph mode=interactive> {

 "version": 2,
 "width": 550,
 "height": 400,
 "data": [
   {
     "name": "tree",
     "url": "https://www.mediawiki.org/wiki////Extension:Graph/data/flare-json",
     "format": {"type": "treejson"},
     "transform": [
       {"type": "hierarchy", "field": "size", "size": [400, 500]}
     ]
   }
 ],
 "marks": [
   {
     "type": "path",
     "from": {
       "data": "tree",
       "transform": [
         {
           "type": "filter", "test": "datum.parent"
         },
         {
           "type": "linkpath",
           "sourceX": "parent.layout_y",
           "sourceY": "parent.layout_x",
           "targetX": "layout_y",
           "targetY": "layout_x",
           "shape": "diagonalX"
         }
       ]
     },
     "properties": {
       "enter": {
         "path": {"field": "layout_path"},
         "stroke": {"value": "#ddd"}
       }
     }
   },
   {
     "type": "text",
     "from": {"data": "tree"},
     "properties": {
       "enter": {
         "x": {"field": "layout_y", "offset": -10},
         "y": {"field": "layout_x"},
         "font": {"value": "Helvetica Neue"},
         "fontSize": {"value": 10},
         "align": {"value": "left"},
         "baseline": {"value": "middle"},
         "fill": {"value": "#000"},
         "text": {"field": "name"}
       }
     }
   }
 ]

} </graph>

Interactive Map

https://www.mediawiki.org/wiki/Extension:Graph/Interactive_Graph_Tutorial

<graph mode=interactive> {

 // We want to use Vega 2, and specify image size
 "version": 2, "width": 300, "height": 80,
 // Set padding to the same value on all sides
 "padding": 12,
 // By default the background is transparent
 "background": "#edf1f7",

"signals": [

{

   "name": "handlePosition",
   "init": 200,
   "streams": [
     {
       "type": "mousemove[isDragging]",
       "expr": "eventX()"
     }
   ]
 },
 {
   "name": "isDragging",
   "init": false,
   "streams": [
     {"type": "@handle:mousedown","expr": "true"},
     {"type": "mouseup","expr": "false"}
   ]
 },

],

 "marks": [

{

   "name": "debugHandlePosition",
   "type": "text",
   "properties": {
     "enter": {
       "x": {"value": 250},
       "y": {"value": 14},
       "fill": {"value": "black"}
     },
     "update": {"text": {"signal": "handlePosition"}}
   }
 },

{

   "name": "debugIsDragging",
   "type": "text",
   "properties": {
     "enter": {
       "x": {"value": 250},
       "y": {"value": 0},
       "fill": {"value": "black"}
     },
     "update": {"text": {"signal": "isDragging"}}
   }
 },
   {
     // Draw a horizontal line
     "name": "scrollLine",
     "type": "rule",
     "properties": {
       "enter": {
         "x": {"value": 0},
         "y": {"value": 40},
         "x2": {"value": 300},
         "stroke": {"value": "#000"},
         "strokeWidth": {"value": 2}
       }
     }
   },
   {
     // Draw a triangle shape with a hover effect
     // naming objects allows us to reference them later
     "name": "handle",
     "type": "path",
     "properties": {
       "enter": {
         "x": {"value": 200},
         "y": {"value": 40},
         // path syntax is the same as SVG's path tag
         "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"},
         "stroke": {"value": "#880"},
         "strokeWidth": {"value": 2.5}
       },
       "update": { "x": {"signal": "handlePosition"} },
       // Change fill color of the object on mouse hover
       "hover": {"fill": {"value": "#f00"}}
     }
   }
 ],

"scales": [

   {
     "name": "yearsScale",
     "type": "linear",
     "zero": false,
     "domain": [1960, 2013],
     "range": "width"
   }
 ],

} </graph>