扩展:图表

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page Extension:Graph and the translation is 35% complete.

Outdated translations are marked like this.
Other languages:
English • ‎français • ‎occitan • ‎中文 • ‎日本語 • ‎한국어
MediaWiki扩展手册
OOjs UI icon advanced.svg
Graph
发布状态: 稳定版
实现 Tag, ContentHandler
描述 Data-driven graphs
作者 Yuri Astrakhan (Yurik 讨论), Dan Andreescu, Frédéric Bolduc
许可协议 MIT License
下载
使用的钩子
ParserFirstCallInit
翻译Graph扩展如果在translatewiki.net可用
检查使用和版本矩阵。
问题 开放的工作 · 报告错误

<graph>標籤將JSON格式的數據轉換、呈現為可視的Vega圖表,例如:長條圖、圓餅圖、時間線、直方圖...(更多範例)。

基本信息

圖表擴展允許基於Vega的圖表被加入至Wiki頁面中。圖表可以是靜態或互動式的。

插入圖表最簡單的方法就是使用預先製作的模板,如利用{{Graph:Chart}}製作長條圖。 這些模板隱藏了所有Vega複雜的部份。 開發者可藉由圖表沙盒開發圖表。 (除了JSON格式,圖表沙盒也支援維基語法)

外部链接

安装

  • 如果使用Vagrant ,请通过vagrant roles enable graph --provision安装
手动安装
  • 需要JsonConfig 擴展
  • 下载文件,并将其放置在您extensions/文件夹中的Graph目录内。
  • 将下列代码放置在您的LocalSettings.php的底部:
    wfLoadExtension( 'Graph' );
    
  • Yes 完成 – 在您的wiki上导航至Special:Version,以验证扩展已成功安装。

致使用MediaWiki 1.24或更早版本的用户:

上面的说明介绍的是安装此扩展的新方法,它使用wfLoadExtension() 如果您需要在早期版本(MediaWiki 1.24和更早版本)中安装此扩展,而不是wfLoadExtension( 'Graph' );,您需要使用:

require_once "$IP/extensions/Graph/Graph.php";

圖表範例

參考範例頁面 獲得更多範例及使用技巧。

Historical Fertility Rates
Historical Fertility Rates

外部数据

HTTPS無法為圖表取得數據。 但是可使用自定義的維基傳輸安全協議wikiraw:wikiapi:

Graphoid服務和圖表擴展利用GraphAllowedDomains設定解析這些協議。

$wgGraphAllowedDomains = [
    # 下方列出所有允許外部數據訪問的網域:(http和https開頭)
    # 允許訪問的網域,子網域也允許訪問。
    # 自定義的協議如'wikiraw'利用$gad來確認使用哪種協議。
    # 這樣一來wikiraw://en.wikipedia.org/Page 將會對https://en.wikipedia.org/w/api.php?action=query&titles=Page&... 請求API。
    'https' => [ 'wikipedia.org', 'wikimedia.org', ... ],
    'http' => [ 'wmflabs.org', ... ],
    
    # 下方列出所有wikirawupload:協議允許訪問的網域:
    # wikirawupload:協議要求完全吻合,不允許子網域的訪問。
    'wikirawupload' => [ 'upload.wikimedia.org' ],
    
    # Wikidata Sparql 可供查詢的網域:
    'wikidatasparql' => [ 'query.wikidata.org' ],
];

已知的錯誤和限制

內部事務

When parsing, the Graph extension expands all template parameters/expressions, and stores the complete graph definitions in the graph_specs page property, using graph hashes for IDs. This means you can easily locate a wiki's graphs with either Special:PageWithProps or the action API.

The graph extension adds HTML to the page where graphs should go, a <div> containing an <img> tag. Sample:

<div class="mw-wiki-graph-container">
  <img class="mw-wiki-graph-img"
    src="/api/rest_v1/page/graph/png/Extension%3AGraph%2FDemo/1686336/d243aa924db2f82926e0f317217fcce591fe8cd4.png" />
</div>

The <img> tag's href attribute points to the #Graphoid service, which provides a static image of the graph.

If the Graph extension is configured for interactivity, and if the client browser has JavaScript enabled and is not a legacy browser to which ResourceLoader does not ship JavaScript, then the client browser renders graphs on the fly. The Graph extension adds an ext.graph ResourceLoader JavaScript module to the page that includes the Vega library, and puts the JSON of graph definitions into a JavaScript mediaWiki.config variable named wgGraphSpecs. Once the client has loaded this module, the Vega JavaScript library populates each <div> with an HTML canvas and draws the graph in it, replacing the static image. As of 11月 2015, Wikimedia wikis only enable this interactive rendering for edit preview.

You can configure the Graph extension to always use just the <img> tag, and not add the Vega libraries or graph definition JSON. This mode will not work during editing, since page_props do not get updated until after the save. Once saved, the Graphoid service will be able to access the new graph definition via the action API.

Security Features

<graph> can be configured to disallow referencing untrusted data sources (e.g. Wikimedia only allows data from the Wikimedia sites). It can also send extra headers whenever accessing external data, e.g. Treat-as-Untrusted header that MediaWiki uses to prevent CSRF attacks.

License

Vega library is distributed under a modified BSD license acceptable under for us to use.

This appears to be a copy of the BSD license, with some minor (acceptable) modifications. It's not a problem for us to use it, although ideally they would not make changes like this to the license. It's better if people do not make these changes to their license, to avoid confusion (like this) about whether the license is safe for open-source use.

—Stephen LaPorte

Configuration

wgGraphIsTrusted

If false (default), passes Treat-as-Untrusted:1 header with all graph data requests

wgGraphImgServiceUrl

A format string to form a backend service request URL for the <img> tag. For example:

"//graphoid.wikimedia.org/%1\$s/v1/png/%2\$s/%3\$s/%4\$s.png"

Would produce this URL:

//graphoid.wikimedia.org/mediawiki.org/v1/png/Extension:Graph/0/be66c7016b9de3188ef6a585950f10dc83239837.png
/{domain}/v1/png/{title}/{revid}/{hash}.png

Parameters will be supplied in this order: 1=server, 2=title, 3=revid, 4=graph-hash-id. All parameters will be escaped with rawurlencode(). If the value is false (default), no <noscript> urls will be generated

wgGraphUrlBlacklist variable has been removed in 787d64a11 (7 Dec 2015), wgGraphDataDomains has been removed in e0813f85a (28 Jan 2016). Use a wgGraphAllowedDomains instead.

wgGraphUserServiceAlways variable has been removed in b735f63ff4b (30 Sep 2015).

Enabling Graph namespace

To store graph definitions as standalone pages in their own namespace, configure JsonConfig .

// See https://www.mediawiki.org/wiki/Extension:JsonConfig
$wgJsonConfigModels['graph.jsonconfig'] = 'graph\Content';
$wgJsonConfigs['graph.jsonconfig'] = array(
	'namespace' => <PICK-A-NS-NUMBER>,
	'nsName' => 'Graph',
	'isLocal' => true,
);

Graphoid service

This section should reference/sync-up with wikitech:Graphoid

Graphoid (git repo) is a node.js service that converts a graph definition into a static PNG image using the same Vega library code that runs in advanced browsers. The reason Graphoid was initially developed was to provide a static image so we wouldn't have to bundle Vega and d3 resource loader modules with every page response. See phab:T211881 for more details. The service is available on the Wikimedia cluster at graphoid.wikimedia.org.

You can install it yourself:

$ git clone https://gerrit.wikimedia.org/r/mediawiki/services/graphoid
$ cd graphoid
$ npm install
$ npm start

The service URLs contain the domain of the page (for example mediawiki.org), service version (v1), the title of the page with the graph (PageTitle), revision ID of the page (12345, but could be 0 for current), and a hash ID of the graph itself (also used in HTML page to identify graph definition), for example:

http://localhost:6927/mediawiki.org/v1/PageTitle/12345/a64b022a8fa5b7fc5e40a2c95cd0a114b2ae1174.png
http://graphoid.wikimedia.org/mediawiki.org/v1/png/Extension:Graph/0/be66c7016b9de3188ef6a585950f10dc83239837.png

You configure the Graph extension to use the Graphoid service in LocalSettings.php with a line like

$wgGraphImgServiceUrl = "//localhost:6927/%1\$s/v1/%2\$s/%3\$s/%4\$s.png";
$wgGraphImgServiceUrl = "//graphoid.wikimedia.org/%1\$s/v1/png/%2\$s/%3\$s/%4\$s.png";

Configure graphoid services

Graph extension workflow

You can further configure the service via its config file.

VisualEditor module

Since summer 2015, the Graph extension also comes with a module (ext.graph.VisualEditor) which enables graph editing within VisualEditor.

This module was a result of a Google Summer of Code 2015 project. See phab:T89287 for more details.

Ve-graph-showcase.png

This module allows users to see graphs within VisualEditor, as opposed to alien extension nodes. Furthermore, users can open up a dialog to edit a graph's type, data and padding. The UI also offers a way to edit a graph's raw JSON specification within VE without having to switch to the classic wikitext editor, in case more advanced users want to tweak settings not supported by the UI.

This first step serves as a stepping stone for many possibilities with graph editing within VisualEditor, and there are a lot of ways in which the module can be improved and expanded.



See also

  • Extension:GraphViz — for displaying graphs of sets of vertices connected by edges (i.e. not charts, like this extension)
  • Plotly — The open source JavaScript graphing library (with 3d charting capabilities)
  • D3 — Data-Driven Documents