Extension:Graph/Guide

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


 * Keep graphs separate from the articles
 * 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: , and can be inserted with


 * Make graphs customizable
 * 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.

inserts well-formatted table with data:
 * 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 graph with the same data:

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! "values": // will not render "values": // will render, notice the whitespace in the closing braces
 * Reminder about nested template parameters

Debugging

 * For now, the best place to develop and debug is at http://vega.github.io/vega-editor/?mode=vega
 * To get the graph JSON from a template, click edit / page preview, and use browser's debugger to inspect the graph (even if it is not showing) - you should see the &lt;div class="mw-graph ... data-graph-id="834d6921e69e66a00b40d8606e39e414145f8288" .... Copy the hash value, hit escape (in Chrome), and use this oneliner to show the JSON. Remove first and last quote, ad copy/paste to the Vega editor above.