Difference between revisions of "Template:Graph:Lines"

From blackwiki
Jump to navigation Jump to search
imported>Yurik
imported>Yurik
Line 27: Line 27:
 
     },
 
     },
 
     "transform": [
 
     "transform": [
    // Convert xField parameter into a field "_xfield"
+
      // Convert xField parameter into a field "_xfield"
    {"type": "formula", "field": "_xfield", "expr":  
+
      {"type": "formula", "field": "_xfield", "expr":
 
{{#switch: {{{type|year}}}
 
{{#switch: {{{type|year}}}
 
| year= "datetime(datum.{{{xField|year}}}, 0, 1)"
 
| year= "datetime(datum.{{{xField|year}}}, 0, 1)"
Line 34: Line 34:
 
}}
 
}}
 
       },
 
       },
       {"type": "fold", "fields": [{{{series|}}}]}
+
       {"type": "sort", "by": ["_xfield"]},
 +
{{#if:{{{series|}}}
 +
|    {"type": "fold", "fields": [{{{series}}}]}
 +
}}
 
     ]
 
     ]
 
   },
 
   },
{{#ifeq: {{{tabletype|tab}}} | tab |
+
{{#if:{{{series|}}}|{{#ifeq: {{{tabletype|tab}}} | tab |
 
   {
 
   {
 
     "name": "labels",
 
     "name": "labels",
 
     "url": "tabular:///{{{table}}}",
 
     "url": "tabular:///{{{table}}}",
     "format": {"type": "json", "property": "schema.fields"}
+
     "format": {"type": "json", "property": "fields"}
 
   }
 
   }
}}
+
}}}}
 
   ],
 
   ],
 
   "scales": [
 
   "scales": [
Line 62: Line 65:
 
       "type": "linear",
 
       "type": "linear",
 
       "range": "height",
 
       "range": "height",
       "domain": {"data": "chart", "field": "value"},
+
       "domain": {"data": "chart", "field": "{{{value|value}}}"},
 
{{#if:{{{yZero|}}}|"zero": {{{yZero}}},}}
 
{{#if:{{{yZero|}}}|"zero": {{{yZero}}},}}
 
{{#if:{{{yMin|}}}|"domainMin": {{{yMin}}},}}
 
{{#if:{{{yMin|}}}|"domainMin": {{{yMin}}},}}
Line 70: Line 73:
 
       "name": "color",
 
       "name": "color",
 
       "type": "ordinal",
 
       "type": "ordinal",
       "domain": {"data": "chart", "field": "key"},
+
       "domain": {"data": "chart", "field": "{{{group|key}}}"},
 
       "range": "category10"
 
       "range": "category10"
 
     },
 
     },
 +
{{#if:{{{series|}}}|
 
     {
 
     {
 
       "name": "labels",
 
       "name": "labels",
Line 83: Line 87:
 
}}
 
}}
 
     }
 
     }
 +
}}
 
   ],
 
   ],
 
{{#if:{{{legend|}}}|
 
{{#if:{{{legend|}}}|
Line 88: Line 93:
 
     "fill": "color",
 
     "fill": "color",
 
     "stroke": "color",
 
     "stroke": "color",
    "title": "{{{legend|}}}",
+
{{#ifeq: {{{legend|}}}|-|| "title": "{{{legend|}}}", }}
    "properties": {
+
{{#if:{{{series|}}}
      "labels": { "text": {"scale": "labels", "field": "data"} }
+
"properties": { "labels": { "text": {"scale": "labels", "field": "data"} } }
    }
+
}}
 
   }],
 
   }],
 
}}
 
}}
 
   "axes": [
 
   "axes": [
     {"scale": "x", "type": "x", "ticks": 7
+
     {"scale": "x", "type": "x", "tickSizeEnd": 0, "ticks": 7
 
{{#if:{{{xAxis|}}}|, "title": "{{{xAxis|}}}"}}
 
{{#if:{{{xAxis|}}}|, "title": "{{{xAxis|}}}"}}
 
{{#if:{{{xGrid|}}}|, "grid": true}}
 
{{#if:{{{xGrid|}}}|, "grid": true}}
 
},
 
},
     {"scale": "y", "type": "y"
+
     {"scale": "y", "type": "y", "tickSizeEnd": 0
 
{{#if:{{{yAxis|}}}|, "title": "{{{yAxis|}}}"}}
 
{{#if:{{{yAxis|}}}|, "title": "{{{yAxis|}}}"}}
 
{{#if:{{{yGrid|}}}|, "grid": true}}
 
{{#if:{{{yGrid|}}}|, "grid": true}}
Line 106: Line 111:
  
 
   "marks": [
 
   "marks": [
     // Group data by the "key", and draw lines, one line per key
+
     // Group data by the group parameter or "key", and draw lines, one line per group
 
     {
 
     {
 
       "type": "group",
 
       "type": "group",
 
       "from": {
 
       "from": {
 
         "data": "chart",
 
         "data": "chart",
         "transform": [{"groupby": ["key"], "type": "facet"}]
+
         "transform": [{"type": "facet", "groupby": ["{{{group|key}}}"]}]
 
       },
 
       },
 
       "marks": [
 
       "marks": [
Line 118: Line 123:
 
           "properties": {
 
           "properties": {
 
             "enter": {
 
             "enter": {
               "y": {"scale": "y", "field": "value"},
+
               "y": {"scale": "y", "field": "{{{value|value}}}"},
 
               "x": {"scale": "x", "field": "_xfield"},
 
               "x": {"scale": "x", "field": "_xfield"},
               "stroke": {"scale": "color", "field": "key"},
+
               "stroke": {"scale": "color", "field": "{{{group|key}}}"},
 
               "interpolate": {"value": "monotone"},
 
               "interpolate": {"value": "monotone"},
 
               "strokeWidth": {"value": 2.5}
 
               "strokeWidth": {"value": 2.5}

Revision as of 20:20, 5 January 2017

Template documentation[view] [edit] [history] [purge]

This graph's main version resides at Template:Graph:Lines. Please make or suggest all the changes there, and copy it everywhere else (until the copying is automated)

This graph draws one or more independent numeric data series as lines. The data must be stored on Commons' Data namespace or come from Wikidata Query Service.

{{Graph:Lines
| table=bls.gov/US Women's weekly earnings as a percent of men's by age, annual averages.tab
| type=year | xField=year
| series="age_16_24", "age_25_34", "age_35_44", "age_45_54"
| title=Gender pay gap in the United States
| yZero=false | xAxis=Year | yAxis=Percentage | yMax=100 | yGrid=y | width=500 | legend=Age group
}}
{{Graph:Lines
| tabletype=query | table=

SELECT ?decade (COUNT(?decade) AS ?count) WHERE {
  ?item wdt:P31 wd:Q3305213 .
  ?item wdt:P571 ?inception .
  BIND( year(?inception) as ?year ). 
  BIND( ROUND(?year/10)*10 as ?decade ) .
  FILTER( ?year > 1400)
} GROUP BY ?decade ORDER BY ?decade

| series="count" | type=year | xField=decade
| title=Paintings by decade

}}
<graph>

{

 //
 // ATTENTION: This code is maintained at https://www.mediawiki.org/wiki/Template:Graph:Lines
 //            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_talk:Graph:Lines
 //
 "version": 2,
 "width": 500,
 "height": 200,
 "data": [{
   "name": "chart",

"url": "tabular:///bls.gov/US Women's weekly earnings as a percent of men's by age, annual averages.tab",

   "format": {"type": "json"

, "property": "data"

   },
   "transform": [
     // Convert xField parameter into a field "_xfield"
     {"type": "formula", "field": "_xfield", "expr":

"datetime(datum.year, 0, 1)"

     },
     {"type": "sort", "by": ["_xfield"]},

{"type": "fold", "fields": ["age_16_24", "age_25_34", "age_35_44", "age_45_54"]}

   ]
 },

{

   "name": "labels",
   "url": "tabular:///bls.gov/US Women's weekly earnings as a percent of men's by age, annual averages.tab",
   "format": {"type": "json", "property": "fields"}
 }
 ],
 "scales": [
   {
     "name": "x",
     "type": "time",
     "domain": {"data": "chart", "field": "_xfield"},
     "range": "width",


   },
   {
     "name": "y",
     "type": "linear",
     "range": "height",
     "domain": {"data": "chart", "field": "value"},

"zero": false,

"domainMax": 100,

   },
   {
     "name": "color",
     "type": "ordinal",
     "domain": {"data": "chart", "field": "key"},
     "range": "category10"
   },

{

     "name": "labels",
     "type": "ordinal",

"domain": {"data": "labels", "field": "name"},

     "range": {"data": "labels", "field": "title"},
   }
 ],

"legends": [{

   "fill": "color",
   "stroke": "color",

"title": "Age group", "properties": { "labels": { "text": {"scale": "labels", "field": "data"} } }

 }],
 "axes": [
   {"scale": "x", "type": "x", "tickSizeEnd": 0, "ticks": 7

, "title": "Year"

},

   {"scale": "y", "type": "y", "tickSizeEnd": 0

, "title": "Percentage" , "grid": true }

 ],
 "marks": [
   // Group data by the group parameter or "key", and draw lines, one line per group
   {
     "type": "group",
     "from": {
       "data": "chart",
       "transform": [{"type": "facet", "groupby": ["key"]}]
     },
     "marks": [
       {
         "type": "line",
         "properties": {
           "enter": {
             "y": {"scale": "y", "field": "value"},
             "x": {"scale": "x", "field": "_xfield"},
             "stroke": {"scale": "color", "field": "key"},
             "interpolate": {"value": "monotone"},
             "strokeWidth": {"value": 2.5}
           }
         }
       }
     ],
   }

// Draw title at the top of the graph , {

     "type": "text",
     "properties": {
       "enter": {
         "x": {"signal": "width", "mult": 0.5, "offset": 30},
         "y": {"value": -15},
         "text": {"value": "Gender pay gap in the United States"},
         "fontWeight": {"value": "bold"},
         "align": {"value": "center"},
         "baseline": {"value": "bottom"},
         "fill": {"value": "#000"}
       }
     }
   }
 ]

} </graph> See or edit raw graph data.

<graph>

{

 //
 // ATTENTION: This code is maintained at https://www.mediawiki.org/wiki/Template:Graph:Lines
 //            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_talk:Graph:Lines
 //
 "version": 2,
 "width": 300,
 "height": 200,
 "data": [{
   "name": "chart",

"url": "wikidatasparql:///?query=SELECT%20%3Fdecade%20%28COUNT%28%3Fdecade%29%20AS%20%3Fcount%29%20WHERE%20%7B%0A%20%20%3Fitem%20wdt%3AP31%20wd%3AQ3305213%20.%0A%20%20%3Fitem%20wdt%3AP571%20%3Finception%20.%0A%20%20BIND%28%20year%28%3Finception%29%20as%20%3Fyear%20%29.%20%0A%20%20BIND%28%20ROUND%28%3Fyear%2F10%29%2A10%20as%20%3Fdecade%20%29%20.%0A%20%20FILTER%28%20%3Fyear%20%3E%201400%29%0A%7D%20GROUP%20BY%20%3Fdecade%20ORDER%20BY%20%3Fdecade",

   "format": {"type": "json"


   },
   "transform": [
     // Convert xField parameter into a field "_xfield"
     {"type": "formula", "field": "_xfield", "expr":

"datetime(datum.decade, 0, 1)"

     },
     {"type": "sort", "by": ["_xfield"]},

{"type": "fold", "fields": ["count"]}

   ]
 },
 ],
 "scales": [
   {
     "name": "x",
     "type": "time",
     "domain": {"data": "chart", "field": "_xfield"},
     "range": "width",


   },
   {
     "name": "y",
     "type": "linear",
     "range": "height",
     "domain": {"data": "chart", "field": "value"},


   },
   {
     "name": "color",
     "type": "ordinal",
     "domain": {"data": "chart", "field": "key"},
     "range": "category10"
   },

{

     "name": "labels",
     "type": "ordinal",

"domain": ["count"],

     "range": ["count"],
   }
 ],
 "axes": [
   {"scale": "x", "type": "x", "tickSizeEnd": 0, "ticks": 7


},

   {"scale": "y", "type": "y", "tickSizeEnd": 0


}

 ],
 "marks": [
   // Group data by the group parameter or "key", and draw lines, one line per group
   {
     "type": "group",
     "from": {
       "data": "chart",
       "transform": [{"type": "facet", "groupby": ["key"]}]
     },
     "marks": [
       {
         "type": "line",
         "properties": {
           "enter": {
             "y": {"scale": "y", "field": "value"},
             "x": {"scale": "x", "field": "_xfield"},
             "stroke": {"scale": "color", "field": "key"},
             "interpolate": {"value": "monotone"},
             "strokeWidth": {"value": 2.5}
           }
         }
       }
     ],
   }

// Draw title at the top of the graph , {

     "type": "text",
     "properties": {
       "enter": {
         "x": {"signal": "width", "mult": 0.5, "offset": 30},
         "y": {"value": -15},
         "text": {"value": "Paintings by decade"},
         "fontWeight": {"value": "bold"},
         "align": {"value": "center"},
         "baseline": {"value": "bottom"},
         "fill": {"value": "#000"}
       }
     }
   }
 ]

} </graph> See source Wikidata query.

Wikidata query produces three columns - year, population size, and country name. This graph shows one line per country.
{{Graph:Lines
| tabletype=query | table=

#Population of countries sharing a border with Germany
SELECT ?year ?population ?countryLabel WHERE {
  {
    SELECT ?country ?year (AVG(?populations) AS ?population) WHERE {
      {
        SELECT ?country (YEAR(?date) AS ?year) ?populations WHERE {
          ?country wdt:P47 wd:Q183.
          ?country p:P1082 ?populationStatement.
          ?populationStatement ps:P1082 ?populations.
          ?populationStatement pq:P585 ?date.
        }  FILTER( ?year >= 1900)
      }
    }
    GROUP BY ?country ?year
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "{{CONTENTLANGUAGE}}". }
}
| type=year | xField=year | group=countryLabel | value=population
| title=Population of countries bordering Germany
| legend=- | xGrid=y | yGrid=y | yTicks=7 | width=600

}}
<graph>

{

 //
 // ATTENTION: This code is maintained at https://www.mediawiki.org/wiki/Template:Graph:Lines
 //            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_talk:Graph:Lines
 //
 "version": 2,
 "width": 600,
 "height": 200,
 "data": [{
   "name": "chart",

"url": "wikidatasparql:///?query=%23Population%20of%20countries%20sharing%20a%20border%20with%20Germany%2C%20fixed%0ASELECT%20%3Fyear%20%3Fpopulation%20%3FcountryLabel%20WHERE%20%7B%0A%20%20%7B%0A%20%20%20%20SELECT%20%3Fcountry%20%3Fyear%20%28AVG%28%3Fpopulations%29%20AS%20%3Fpopulation%29%20WHERE%20%7B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20SELECT%20%3Fcountry%20%28YEAR%28%3Fdate%29%20AS%20%3Fyear%29%20%3Fpopulations%20WHERE%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3Fcountry%20wdt%3AP47%20wd%3AQ183.%0A%20%20%20%20%20%20%20%20%20%20%3Fcountry%20p%3AP1082%20%3FpopulationStatement.%0A%20%20%20%20%20%20%20%20%20%20%3FpopulationStatement%20ps%3AP1082%20%3Fpopulations.%0A%20%20%20%20%20%20%20%20%20%20%3FpopulationStatement%20pq%3AP585%20%3Fdate.%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%20%20%20FILTER%28%20%3Fyear%20%3E%3D%201900%29%0A%20%20%20%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fcountry%20%3Fyear%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22.%20%7D%0A%7D",

   "format": {"type": "json"


   },
   "transform": [
     // Convert xField parameter into a field "_xfield"
     {"type": "formula", "field": "_xfield", "expr":

"datetime(datum.year, 0, 1)"

     },
     {"type": "sort", "by": ["_xfield"]},
   ]
 },
 ],
 "scales": [
   {
     "name": "x",
     "type": "time",
     "domain": {"data": "chart", "field": "_xfield"},
     "range": "width",


   },
   {
     "name": "y",
     "type": "linear",
     "range": "height",
     "domain": {"data": "chart", "field": "population"},


   },
   {
     "name": "color",
     "type": "ordinal",
     "domain": {"data": "chart", "field": "countryLabel"},
     "range": "category10"
   },
 ],

"legends": [{

   "fill": "color",
   "stroke": "color",


 }],
 "axes": [
   {"scale": "x", "type": "x", "tickSizeEnd": 0, "ticks": 7

, "grid": true },

   {"scale": "y", "type": "y", "tickSizeEnd": 0

, "grid": true }

 ],
 "marks": [
   // Group data by the group parameter or "key", and draw lines, one line per group
   {
     "type": "group",
     "from": {
       "data": "chart",
       "transform": [{"type": "facet", "groupby": ["countryLabel"]}]
     },
     "marks": [
       {
         "type": "line",
         "properties": {
           "enter": {
             "y": {"scale": "y", "field": "population"},
             "x": {"scale": "x", "field": "_xfield"},
             "stroke": {"scale": "color", "field": "countryLabel"},
             "interpolate": {"value": "monotone"},
             "strokeWidth": {"value": 2.5}
           }
         }
       }
     ],
   }

// Draw title at the top of the graph , {

     "type": "text",
     "properties": {
       "enter": {
         "x": {"signal": "width", "mult": 0.5, "offset": 30},
         "y": {"value": -15},
         "text": {"value": "Population of countries bordering Germany"},
         "fontWeight": {"value": "bold"},
         "align": {"value": "center"},
         "baseline": {"value": "bottom"},
         "fill": {"value": "#000"}
       }
     }
   }
 ]

} </graph> See source Wikidata query.

Lua error in Module:TNT at line 124: mw.text.jsonEncode: Cannot use type 'boolean' as a table key.

<graph>
{
  //
  // ATTENTION: This code is maintained at https://www.mediawiki.org/wiki/Template:Graph:Lines
  //            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_talk:Graph:Lines
  //

  "version": 2,
  "width": 300,
  "height": 200,

  "data": [{
    "name": "chart",

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

    },
    "transform": [
      // Convert xField parameter into a field "_xfield"
      {"type": "formula", "field": "_xfield", "expr":
"datetime(datum.year, 0, 1)"
      },
      {"type": "sort", "by": ["_xfield"]},

    ]
  },

  ],
  "scales": [
    {
      "name": "x",
      "type": "time",
      "domain": {"data": "chart", "field": "_xfield"},
      "range": "width",



    },
    {
      "name": "y",
      "type": "linear",
      "range": "height",
      "domain": {"data": "chart", "field": "value"},



    },
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "chart", "field": "key"},
      "range": "category10"
    },

  ],

  "axes": [
    {"scale": "x", "type": "x", "tickSizeEnd": 0, "ticks": 7


},
    {"scale": "y", "type": "y", "tickSizeEnd": 0


}
  ],

  "marks": [
    // Group data by the group parameter or "key", and draw lines, one line per group
    {
      "type": "group",
      "from": {
        "data": "chart",
        "transform": [{"type": "facet", "groupby": ["key"]}]
      },
      "marks": [
        {
          "type": "line",
          "properties": {
            "enter": {
              "y": {"scale": "y", "field": "value"},
              "x": {"scale": "x", "field": "_xfield"},
              "stroke": {"scale": "color", "field": "key"},
              "interpolate": {"value": "monotone"},
              "strokeWidth": {"value": 2.5}
            }
          }
        }
      ],
    }


  ]
}
</graph>