Wikipedia:Wikipedia Signpost/Templates/Pageview hack
{{#tag:graph|
{
//
// ATTENTION: This is forked from Template:Graph:PageViews, which (as of November 2022) didn't allow you to specify specific date ranges that weren't a reference to the current time. It is intended to be a usable fix. If you are reading this ten years from now because the template's still in use, well, we messed up.
"version": 2,
"width": {{{width|800}}},
"height": {{{height|200}}},
// The data for this graph comes from the PageView API. The request is made for N days back up to now.
"data": [
{
"name": "pageviews",
"url": "wikirest://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/user/{{URLENCODE:{{{2}}}|PATH}}/daily/{{formatnum:{{#time:Ymd|{{{start|now}}}}}|R}}00/{{formatnum:{{#time:Ymd|{{{start|now}}} + {{{1|30}}} days}}|R}}00",
"format": {
"type": "json",
"property": "items"
},
// The response is parsed here, converting date strings of form "20160223" into date 2016-02-23
"transform": [
{ "type": "formula", "field": "year", "expr": "parseInt(substring(datum.timestamp,0,4))" },
{ "type": "formula", "field": "month", "expr": "parseInt(substring(datum.timestamp,4,6))" },
{ "type": "formula", "field": "day", "expr": "parseInt(substring(datum.timestamp,6,8))" },
{ "type": "formula", "field": "date", "expr": "datetime(datum.year,datum.month-1,datum.day)" }
]
}
],
"scales": [
// The dates are scaled to the "x" axis - the width of the graph
{
"name": "x",
"type": "time",
"range": "width",
"domain": {"data": "pageviews","field": "date"}
},
// The pageviews are scaled to the "y" axis - the height of the graph
// Optional scale parameter can change "linear" to other scales like log
// Optional max parameter can fix the upper bound of the graph
{
"name": "y",
"type": "{{{scale|linear}}}",
"range": "height",
"domain": {"data": "pageviews","field": "views"},
"clamp": true,
{{ #if: {{{max|}}}
| "domainMax": {{{max}}},
}}
"nice": true
}
],
// Simple axis with horizontal grid lines
"axes": [
{
"type": "x",
"scale": "x",
"ticks": 5,
"properties": {
"ticks": {"stroke": {"value": "#666666"} },
"labels": {"fill": {"value": "#666666"} },
"axis": {"stroke": {"value": "#666666"}, "strokeWidth": {"value": 2} }
}
},
{
"type": "y",
"scale": "y",
"ticks": 5,
"grid": true,
"properties": {
"ticks": {"stroke": {"value": "#666666"} },
"labels": {"fill": {"value": "#666666"} },
"grid": {"stroke": {"value": "#666666"}, "strokeWidth": {"value": 2} },
"axis": {"stroke": {"value": "#666666"}, "strokeWidth": {"value": 2} }
}
}
],
// The graph is drawn with two elements a thick line at the top, and a semi-transparent area below
"marks": [
{
"type": "line",
"from": {"data": "pageviews"},
"properties": {
"enter": {
"x": {"scale": "x","field": "date"},
"y": {"scale": "y","field": "views"},
"stroke": {"value": "{{{color|#00f}}}"},
"strokeWidth": {"value": 3},
"interpolate": {"value": "{{{interpolate|monotone}}}"}
}
}
},
{
"type": "area",
"from": {"data": "pageviews"},
"properties": {
"enter": {
"x": {"scale": "x","field": "date"},
"y": {"scale": "y","value": 0},
"y2": {"scale": "y","field": "views"},
"fill": {"value": "{{{color|#00f}}}"},
"fillOpacity": {"value": 0.35},
"interpolate": {"value": "{{{interpolate|monotone}}}"}
}
}
}
]
}
}}