Шаблон:Graph

From Традиция
Jump to navigation Jump to search

{{Graph}} — шаблон, позволяющий внедрять в страницы визуализации, отрисованные программой Vega.

Визуализация описывается особым языком, являющимся подмножеством JSON.

Особенностью реализации является то, что визуализация отрисовывается и на стороне сервера, и на стороне клиента. Первое созраняет визуализацию в средах без JavaScript, например, при экспорте в PDF, второе делает визуализации интерактивными в браузере.

Параметры[edit source]

  1. или vega — описание визуализации. Все | должны быть заменены на {{!}}, }} на } }, = на {{=}} (вместо последнего можно использовать именованный параметр vega),
  • ширина (необязательный) — ширина в пикселях,
  • высота (необязательный) — высота в пикселях.

Примеры[edit source]

Викитекст Вывод
{{graph|width=400|vega={
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A basic bar chart example, with value labels shown upon mouse hover.",
  "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": "datum === tooltip", "value": 0},
            {"value": 1}
          ]
        }
      }
    }
  ]
}
}}

ABCDEFGH0102030405060708090100

{{graph|width=400|vega={
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A plot of 2016 annual global precipitation data from the NOAA Climate Forecast System (CFS) v2.",
  "width": 600,
  "height": 300,
  "autosize": "fit-x",

  "signals": [
    {
      "name": "projection", "value": "naturalEarth1",
      "bind": {"input": "select", "options": [
        "azimuthalEqualArea", "equalEarth", "equirectangular",
        "naturalEarth1", "mollweide", "orthographic"
      ]}
    },
    {
      "name": "scale", "value": 110,
      "bind": {"input": "range", "min": 50, "max": 400, "step": 5}
    },
    {
      "name": "rotate0", "value": 0,
      "bind": {"input": "range", "min": -180, "max": 180, "step": 1}
    },
    {
      "name": "rotate1", "value": 0,
      "bind": {"input": "range", "min": 0, "max": 360, "step": 1}
    },
    {
      "name": "rotate2", "value": 0,
      "bind": {"input": "range", "min": 0, "max": 360, "step": 1}
    },
    {
      "name": "opacity", "value": 0.5,
      "bind": {"input": "range", "min": 0, "max": 1, "step": 0.01}
    },
    {
      "name": "levels", "value": 6,
      "bind": {"input": "range", "min": 2, "max": 12, "step": 1}
    },
    {
      "name": "stop", "value": 3000
    },
    {
      "name": "step",
      "update": "stop / levels"
    }
  ],

  "data": [
    {
      "name": "precipitation",
      "url": "https:{{filepath:Annual-precip.json}}"
    },
    {
      "name": "contours",
      "source": "precipitation",
      "transform": [
        {
          "type": "isocontour",
          "thresholds": {"signal": "sequence(step, stop, step)"}
        }
      ]
    },
    {
      "name": "world",
      "url": "https:{{filepath:World-110m.json}}",
      "format": {"type": "topojson", "feature": "countries"}
    }
  ],

  "projections": [
    {
      "name": "projection",
      "type": {"signal": "projection"},
      "scale": {"signal": "scale"},
      "rotate": {"signal": "[rotate0, rotate1, rotate2]"},
      "translate": {"signal": "[width/2, height/2]"}
    }
  ],

  "scales": [
    {
      "name": "color",
      "type": "quantize",
      "domain": {"signal": "[0, stop]"},
      "range": {"scheme": "bluepurple", "count": {"signal": "levels"} }
    }
  ],

  "marks": [
    {
      "type": "shape",
      "clip": true,
      "from": {"data": "world"},
      "encode": {
        "update": {
          "strokeWidth": {"value": 1},
          "stroke": {"value": "#eee"},
          "fill": {"value": "#ddd"}
        }
      },
      "transform": [
        {
          "type": "geoshape",
          "projection": "projection"
        }
      ]
    },
    {
      "type": "shape",
      "clip": true,
      "from": { "data": "contours"},
      "encode": {
        "update": {
          "fill": {"scale": "color", "field": "contour.value"},
          "fillOpacity": {"signal": "opacity"}
        }
      },
      "transform": [
        {
          "type": "geoshape",
          "field": "datum.contour",
          "projection": "projection"
        }
      ]
    }
  ],

  "legends": [
    {
      "title": "Annual Precipitation (mm)",
      "fill": "color",
      "orient": "bottom",
      "offset": 5,
      "type": "gradient",
      "gradientLength": 300,
      "gradientThickness": 12,
      "titlePadding": 10,
      "titleOrient": "left",
      "titleAnchor": "end",
      "direction": "horizontal"
    }
  ],

  "config": {
    "legend": {"layout": {"anchor": "middle"} }
  }
}
}}

5001,0001,5002,0002,500Annual Precipita…


{{graph|width=400|height=350|vega={
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "Interactive map of U.S. airport connections in 2008.",
  "width": 900,
  "height": 560,
  "padding": {"top":  25, "left": 0, "right":  0,  "bottom": 0 },
  "autosize": "none",

  "signals": [
    {
      "name":  "scale",  "value":  1200,
      "bind": {  "input":  "range",  "min" :  500,  "max":  3000 }
    },
    {
      "name":  "translateX", "value":  450,
      "bind": { "input": "range",  "min":  -500,  "max":  1200     }
    },
    {
      "name": "translateY",  "value": 260,
      "bind": { "input": "range", "min": -300,  "max": 700  }
    },
    {
      "name": "shape", "value": "line",
      "bind": { "input": "radio", "options": [ "line", "curve" ] }
    },
    {
      "name": "hover",
      "value": null,
      "on": [
        { "events": "@cell:mouseover", "update": "datum" },
        { "events": "@cell:mouseout", "update": "null"  }
      ]
    },
    {
      "name": "title",
      "value": "U.S. Airports, 2008",
      "update":  "hover ? hover.name + ' (' + hover.iata + ')' : 'U.S. Airports, 2008'"
    },
    {
      "name": "cell_stroke",
      "value": null,
      "on": [
        { "events": "dblclick", "update": "cell_stroke ? null : 'brown'"},
        { "events": "mousedown!", "update": "cell_stroke" }
      ]
    }
  ],

  "data": [
    {
      "name": "states",
      "url": "https:{{filepath:Us-10m.json}}",
      "format": { "type": "topojson", "feature": "states"},
      "transform": [
        {
          "type": "geopath",
          "projection": "projection"
        }
      ]
    },
    {
      "name": "traffic",
      "url": "https:{{filepath:Flights-airport.csv}}",
      "format": {"type": "csv", "parse": "auto"},
      "transform": [
        {
          "type": "aggregate",
          "groupby": ["origin"],
          "fields": ["count"], "ops": ["sum"], "as": ["flights"]
        }
      ]
    },
    {
      "name": "airports",
      "url": "https:{{filepath:Airports.csv}}",
      "format": {"type": "csv","parse": "auto"
      },
      "transform": [
        {
          "type": "lookup",
          "from": "traffic", "key": "origin",
          "fields": ["iata"], "as": ["traffic"]
        },
        {
          "type": "filter",
          "expr": "datum.traffic!=null"
        },
        {
          "type": "geopoint",
          "projection": "projection",
          "fields": ["longitude", "latitude"]
        },
        {
          "type": "filter",
          "expr": "!(datum.x==null {{!}}{{!}} datum.y==null)"
        },
        {
          "type": "voronoi", "x": "x", "y": "y"
        },
        {
          "type": "collect", "sort": {
            "field": "traffic.flights",
            "order": "descending"
          }
        }
      ]
    },
    {
      "name": "routes",
      "url": https:{{filepath:Flights-airport.csv}}",
      "format": {"type": "csv", "parse": "auto"},
      "transform": [
        {
          "type": "filter",
          "expr": "!(!hover {{!}}{{!}} hover.iata != datum.origin)"
        },
        {
          "type": "lookup",
          "from": "airports", "key": "iata",
          "fields": ["origin", "destination"], "as": ["source", "target"]
        },
        {
          "type": "filter",
          "expr": "!(!datum.source {{!}}{{!}} !datum.target)"
        },
        {
          "type": "linkpath",
          "shape": {"signal": "shape"}
        }
      ]
    }
  ],

  "projections": [
    {
      "name": "projection",
      "type": "albersUsa",
      "scale": {"signal": "scale"},
      "translate": [{"signal": "translateX"}, {"signal": "translateY"}]
    }
  ],

  "scales": [
    {
      "name": "size",
      "type": "linear",
      "domain": {"data": "traffic", "field": "flights"},
      "range": [16, 1000]
    }
  ],

  "marks": [
    {
      "type": "path",
      "from": {"data": "states"},
      "encode": {
        "enter": {
          "fill": {"value": "#dedede"},
          "stroke": {"value": "white"}
        },
        "update": {
          "path": {"field": "path"}
        }
      }
    },
    {
      "type": "symbol",
      "from": {"data": "airports"},
      "encode": {
        "enter": {
          "size": {"scale": "size", "field": "traffic.flights"},
          "fill": {"value": "steelblue"},
          "fillOpacity": {"value": 0.8},
          "stroke": {"value": "white"},
          "strokeWidth": {"value": 1.5}
        },
        "update": {
          "x": {"field": "x"},
          "y": {"field": "y"}
        }
      }
    },
    {
      "type": "path",
      "name": "cell",
      "from": {"data": "airports"},
      "encode": {
        "enter": {
          "fill": {"value": "transparent"},
          "strokeWidth": {"value": 0.35}
        },
        "update": {
          "path": {"field": "path"},
          "stroke": {"signal": "cell_stroke"}
        }
      }
    },
    {
      "type": "path",
      "interactive": false,
      "from": {"data": "routes"},
      "encode": {
        "enter": {
          "path": {"field": "path" },
          "stroke": {"value": "black"},
          "strokeOpacity": {"value": 0.35}
        }
      }
    },
    {
      "type": "text",
      "interactive": false,
      "encode": {
        "enter": {
          "x": {"value": 895},
          "y": {"value": 0 },
          "fill": { "value": "black" },
          "fontSize": { "value": 20},
          "align": {"value": "right"}
        },
        "update": {
          "text": {"signal": "title" }
        }
      }
    }
  ]
}
}}

U.S. Airports, 2008

Системные требования[edit source]

Системаные требования и настройки описаны в статье graph.

Ссылки[edit source]