Jump to content

Template:Graph:Stacked/core

From Wikipedia, the free encyclopedia
<graph>
{
  //
  // ATTENTION: This code is maintained at https://www.mediawiki.org/wiki/Template:Graph:Stacked
  //            Please do not modify it anywhere else, as it may get copied and override your changes.
  //            Suggestions can be made at https://www.mediawiki.org/wiki/Template:Graph:Stacked
  //
  // Template translation is in https://commons.wikimedia.org/wiki/Data:Original/Template:Graphs.tab
  //

  "version": 2,
  "width": 550,
  "height": 250,
  // Parameter to controls how width/height should be interpreted.
  // See Vega 2.x "padding" https://vega.github.io/vega/docs/porting-guide/#layout
  // and Vega x.x "autosize" https://vega.github.io/vega-lite/docs/size.html#autosize
  // and CSS attribute "box-sizing" https://developer.mozilla.org/docs/Web/CSS/box-sizing
  // for references


  "data": [

    {
      "name": "chart",

"url": "tabular:///{{{table}}}",
      "format": {"type": "json"
, "property": "data"

      },
    "transform": [



      // Convert xField parameter into a field "_xfield"
      {"type": "formula", "field": "_xfield", "expr":
"datum.{{{xField}}}"
      },
      {"type": "sort", "by": ["_xfield"]},

      // For non-bar graph, ensure that all missing values still present as zeros to avoid area breaks
      // We might need to allow other, non-0 values to be imputed.
{
        "type": "impute",
        "groupby": ["key"],
        "orderby": ["_xfield"],
        "field": "value",
        "method": "value",
        "value": 0
      },
    ]
  },



    {
      "name": "stats",
      "source": "chart",
      "transform": [{
        "type": "aggregate",
        "groupby": ["_xfield"],
        "summarize": [{"field": "value", "ops": ["sum"]}]
      }]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "{{{type}}}",
      "domain": {"data": "chart", "field": "_xfield"},

      "range": "width",
      

      
    },
    {
      "name": "y",
      "type": "linear",
      "range": "height",
      "domain": {"data": "stats", "field": "sum_value"},
      
      
      
      
    },
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "chart", "field": "key"},
      "range": 
"category10", 

    },

  ],

  "axes": [
    {
      "scale": "x", "type": "x", "tickSizeEnd": 0,
      "ticks": 7,
      
      
      "properties": { 
        "labels": { 
          
          
        } 
      },
    },
    {
      "scale": "y", "type": "y", "tickSizeEnd": 0,
      // When drawing "type=year" (or "type=ordinal") bars with the given width, shift y axis left by half the width
      
      
      
      
      "properties": { 
        "labels": { 
          
          
        } 
      },
    }
  ],

  "marks": [
    // Group data by the "key", and draw lines, one line per key
    {
"type": "group",
      "from": {
        "data": "chart",
        "transform": [
          {"type": "stack", "groupby": ["_xfield"], "sortby": ["key"], "field": "value"},
          {"type": "facet", "groupby": ["key"]}
        ]
      },
      "marks": [
        {
          "type": "area",
          "properties": {
            "hover": {
              "fill": {"value": "red"}
            },
            "update": {
              "fill": {"scale": "color", "field": "key"}
            },
            "enter": {
              "x": {"scale": "x", "field": "_xfield"},
              "y": {"scale": "y", "field": "layout_start"},
              "y2": {"scale": "y", "field": "layout_end"},
              "fill": {"scale": "color", "field": "key"},
              "interpolate": {"value": "monotone"}
            }
          }
        }
      ],
    },



  ]
}
</graph>