Extension:Graph/Guide/fa

This page is for the community to write best practices using extension. See also the 

نمودارها را جدا از مقالات نگه دارید
Even though graphs can be stored inside the article, it is a far better approach to store them as separate templates, e.g. Template:Graph:&lt;yourgraphname&gt;, and can be inserted with

نمودارها را قابل سفارشی‌سازی کنید
Even though you can embed graph settings directly in the graph, it is generally a bad design to do so. Graphs should be made as flexible templates that can be reused in multiple articles with different sets of data and styling. For example, even though graph's width and height could be hardcoded:

it should be made as template parameters with default values, allowing for easy customization:

Keep data separate from the graph

 * Just like styling above, data itself should not be stored within the graph. Lets consider a page with a large table that can benefit from a graph, e.g. the list of most expensive paintings. The page contains a fairly complex table with data:

The simple approach to build a graph would be to extract the needed data, and place the graph with that data next to the table. Even though this would work, this approach is not very good because there is now two copies of the same data, and both need to be updated when something changes. Also, if the graph is inside the text, the article becomes even longer and filled with graph code and data - fewer people will be able to edit it. If the graph is placed in a separate template, the editors are more likely to forget to update it when changing the data.

The proper solution, despite being a bit more difficult at first, will provide a much more future-proof and easier to manage path, and can be reused for all such lists.


 * 1) Set up a separate page with the raw data, preferably in JSON format. The structure of that file is determined by the data needed by the list.
 * 2) Create a Lua module with a function that converts that raw data into a well formatted wiki table.
 * 3) In the same Lua module create another function that extracts data needed for the graph, and outputs it as JSON-formatted data.
 * 4) Create the graph template that can consume the above data and plot it
 * 5) Lastly, insert these two snippets into the list page:

inserts well-formatted table with data:

inserts graph with the same data:

External data
Graph data can be embedded inside the graph definition, or provided via a  link. Unlike the standard Vega, the graph tag requires all external links to use one of the custom wiki protocols:


 * - Gets tabular dataset from Commons. Should be used with  (or "meta" or "fields"). The data given to the graph is already localized to the wiki language.

For example, this dataset would be given to the graph in this format:


 * - Gets map dataset from Commons. Should be used with  (can also use "meta" to get the same information as for tabular). The data given to the graph is already localized to the wiki language.
 * - Gets File:Einstein_1921.jpg image from Commons for an image mark. It is always recommended to add  (and/or height) to make the image the right size and reduce server load.
 * OBSOLETE, use wikifile: instead  - Get an image for the graph, e.g. from commons. This tag specifies any content from the uploads.* domain, without query params.  Use filepath magic keyword to get file path and optional size of the image.
 * - Get raw content of a wiki page, where the path is the title of the page. The domain is optional, and if specified can be any Wikimedia host such as en.wikipedia.org, commons.wikimedia.org, ...
 * - Get data from the MediaWiki API. The domain is optional.
 * - Call to RESTbase API. The domain is optional.
 * or  - Get geoshapes (as topojson) of the given regions, using Wikidata IDs, or a URL-encoded query (in SPARQL), with "id" column specifying geoshapes to get.
 * - Makes a link to the map snapshot service, so that a static map can be drawn for a given location/zoom/size. Parameters are converted it into a snapshot image request for Kartotherian - https://maps.wikimedia.org/img/{style},{zoom},{lat},{lon},{width}x{height}@2x.png

Examples of charts with external data
To see working example of graph json which uses external data it is possible to use Template:Graph:Lines and Template:Graph:Stacked template's debug= parameter. For example below template invocation

will produce following graph consistent json

Reminder about nested template parameters
Templates require you to separate nested template parameters by at least one character, otherwise the graph with simply not display and you will get a syntax error. Be careful!

Using double curly braces in the graphs
Vega 2 supports template parameters, such as, but MediaWiki will attempt to find "indexDate" template and transclude it. To avoid this, use \u007b instead of the first curly brace, \u007d instead of the last closing curly brace, and the | instead of the pipe symbol " ":  until you get the one you want. Remove first and last quote, and copy/paste to the Vega editor above.


 * In Google Chrome, this will copy the string directly onto the clipboard :


 * You can also use browser's debugger to inspect the graph you are interested in (even if it is not showing) - you should see something like . Copy the hash value, hit escape (in Chrome), and use this oneliner to show the JSON (replacing the shown hash value with your own). Remove first and last quote, and copy/paste to the Vega editor above.