User:Tomastvivlaren/graphDataImport

GraphDataImport
Description Convert defunct {{Graph: }} code in Wikipedia articles to JSON with a click of a button for use with the Chart extension.
Author User:Tomastvivlaren
Status
Release
First released May 21, 2025; 10 months ago (2025-05-21)
Updated July 12, 2025
    (9 months ago)
Source User:Tomastvivlaren/graphDataImport.js

GraphDataImport (source) is an easy-to-use userscript for converting legacy Graph extension markup into the modern Chart extension format for use in Wikipedia pages. The tool is available in the web browser and does not require programming skills.

It aims at assisting in the conversion of defunct graphs in Wikipedia articles to the newer Chart extension format. It extracts data from Wikipedia graphs – currently from wikicode that includes a Graph:Chart, Graph:PieChart, Graph:Lines or a Graph:Stacked block (or corresponding template in some other languages). Based on the data, it generates Tabular Data (.tab pages) and .chart definition pages (JSON files that can be stored in the Wikimedia Commons Data namespace), and also new wikicode for the Wikipedia article.

The script adds an "Import graph wikicode" button to .tab and .chart edit pages.

🔧 How it works

  1. Activate the userscript, as described below in § How to activate.
  2. Go to a Commons Data namespace, and edit or create a .tab or .chart page, such as Data:Chart Example Data.tab or Data:Example.Line.chart.
  3. Click the Import graph wikicode button.
  4. Review the generated tabular data (.tab) and chart definition files (.chart).
  5. Save and update the article wikicode.

🛠️ How to activate

  1. Go to your personal common.js page.
  2. Add this line:
    /* graphDataImport from [[User:Tomastvivlaren/graphDataImport]] for Chart extension. */
    mw.loader.load('//commons.wikimedia.org/w/index.php?title=User:Tomastvivlaren/graphDataImport.js&action=raw&ctype=text/javascript');
    
  3. Save the page.

🧪 How to use

  1. Edit or create a .tab page or a .chart page in the Data namespace – for example:
    Data:Chart Example Data.tab -> "Edit source",
    Data:Example.Line.chart -> "Edit source",
    • Or search for a suggested Data: page name at Special:Search-> "Create the page".
  2. You should see a new button: "Import graph wikicode". Click it.
  3. Paste wikicode that includes a {{Graph:... }} block, or corresponding in other languages. For example code, see:
    Graph:Chart,
    Graph:PieChart,
    Graph:Lines,
    Graph:Stacked,
    English Wikipedia articles with disabled graphs or other languages.
  4. Optionally: In the "Source" text field, state the permanent URL of the Wikipedia page version it was brought from, or other sources. And/or in the "Language code" text field, state the language that the titles and description are written in.
  5. Click "Convert to .tab" to generate and preview the converted tabular data.
  6. Click "Convert to .chart" to generate and preview the corresponding chart description page.
  7. Edit categories and other details. When it looks good: Click "Apply" to transfer the JSON code directly into the editing window (either the .tab page or the .chart page that you started from), where you later can preview the result or further edit it.
    NB! Do not save it if you started out by editing an example page, but only if you created a new.
  8. Click "Copy" to copy the other JSON code preview to the clipboard. Create or edit a suitable .chart or .tab page, and paste the code there.
  9. Click "Generate new wikicode" to suggest a replacement for the copied wikicode. Copy the new wikicode into the article edit window, preview and save. You may now click "Close".
  10. Consider updating the .tab page with newer data from some data source.
  11. Consider applying the .chart page to other language versions of the Wikipedia page.
  12. Consider adding translations to the .tab and .chart pages.

⚙️ Technical Details

Currently, the tool does not support graph wikicode containing tabletype=query (Wikidata queries) or type=stackedrect.

An {{Image frame}} will automatically be inserted in the new wikicode, if the {{Graph: ...}} block wasn’t already wrapped in another template in the pasted wikicode, The image frame allows the user to locally specify the chart width, alignment and capture.

By default, a select transform is added to the chart page whenever the original wikicode includes two or more yseries parameters, or a series parameter defining columns or curves. This transform allows the user to locally specify in the new wikicode which columns (curves) and rows (x axis values) to include in that specific wiki page.

An alternative application to generating charts, is to use the imported data to generate tables in Wikipedia articles via the {{Json2table}} template. It automatically displays centrally stored tabular data from Commons as tables.

🔗 See also

Category:Commons user scripts Category:Tabular data Category:MediaWiki Graph Extension Category:MediaWiki Chart Extension Category:Wikipedia tools
Category:Commons user scripts Category:MediaWiki Chart Extension Category:MediaWiki Graph Extension Category:Tabular data Category:Wikipedia tools