Jump to content

Module:Piechart/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
give the link up top
Migration from the old template
Line 2: Line 2:
<!-- Add categories where indicated at the bottom of this page and interwikis at Wikidata -->
<!-- Add categories where indicated at the bottom of this page and interwikis at Wikidata -->


Smooth piechart module. Accessed via [[Template:Piechart]].
Smooth pie chart module. Accessed via [[Template:Pie chart]].


== Usage ==
== Usage ==
Line 8: Line 8:
Draws charts in HTML with an accessible legend (optional). A list of all features is in the "TODO" section of the main `p.pie` function.
Draws charts in HTML with an accessible legend (optional). A list of all features is in the "TODO" section of the main `p.pie` function.


Most of the time you should use with a helper template that adds required CSS: {{t|Piechart}}.
Most of the time you should use with a helper template that adds required CSS: {{t|Pie chart}}.


== Examples ==
== Examples ==
Line 16: Line 16:
Note that you don't need to provide the second value as it's calculated (assuming they sum up to 100).
Note that you don't need to provide the second value as it's calculated (assuming they sum up to 100).
<syntaxhighlight lang="wikitext">
<syntaxhighlight lang="wikitext">
{{Piechart| [ {"value":33.3}, {} ] }}
{{Pie chart| [ {"value":33.3}, {} ] |thumb=none}}
</syntaxhighlight>
</syntaxhighlight>
{{Piechart| [ {"value":33.3}, {} ] }}
{{Pie chart| [ {"value":33.3}, {} ] |thumb=none}}


=== Labels and Legend ===
=== Labels and Legend ===
Here we add some custom labels. Also note that we add a meta option to add legend on the side.
Here we add some custom labels. Also note that we add a meta option to add legend on the side.
<syntaxhighlight lang="wikitext">
<syntaxhighlight lang="wikitext">
{{Piechart| [
{{Pie chart| [
{"label": "women: $v", "value": 33.3},
{"label": "women: $v", "value": 33.3},
{"label": "men: $v"}
{"label": "men: $v"}
]
]
|thumb=none
| meta = {"legend":true}
|meta = {"legend":true}
}}
}}
</syntaxhighlight>
</syntaxhighlight>
{{Piechart| [
{{Pie chart| [
{"label": "women: $v", "value": 33.3},
{"label": "women: $v", "value": 33.3},
{"label": "men: $v"}
{"label": "men: $v"}
]
]
|thumb=none
| meta = {"legend":true}
|meta = {"legend":true}
}}
}}


Line 41: Line 43:


<syntaxhighlight lang="wikitext">
<syntaxhighlight lang="wikitext">
{{Piechart| [
{{Pie chart| [
{"label": "women: $v", "value": 750},
{"label": "women: $v", "value": 750},
{"label": "men: $v", "value": 250}
{"label": "men: $v", "value": 250}
]
]
|thumb=none
| meta = {"legend":true}
|meta = {"legend":true}
}}
}}
</syntaxhighlight>
</syntaxhighlight>
{{Piechart| [
{{Pie chart| [
{"label": "women: $v", "value": 750},
{"label": "women: $v", "value": 750},
{"label": "men: $v", "value": 250}
{"label": "men: $v", "value": 250}
]
]
|thumb=none
| meta = {"legend":true}
|meta = {"legend":true}
}}
}}


Line 58: Line 62:
The module allows displaying multiple values, not just 2.
The module allows displaying multiple values, not just 2.
<syntaxhighlight lang="wikitext">
<syntaxhighlight lang="wikitext">
{{Piechart| [
{{Pie chart| [
{"label": "sweets: $v", "value": 5, "color":"darkred"},
{"label": "sweets: $v", "value": 5, "color":"darkred"},
{"label": "sandwiches: $v", "value": 3, "color":"wheat"},
{"label": "sandwiches: $v", "value": 3, "color":"wheat"},
Line 64: Line 68:
{"label": "drinks: $v", "value": 1, "color":"#ccf"}
{"label": "drinks: $v", "value": 1, "color":"#ccf"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true}
|meta={"autoscale":true, "legend":true}
}}
}}
</syntaxhighlight>
</syntaxhighlight>
{{Piechart| [
{{Pie chart| [
{"label": "sweets: $v", "value": 5, "color":"darkred"},
{"label": "sweets: $v", "value": 5, "color":"darkred"},
{"label": "sandwiches: $v", "value": 3, "color":"wheat"},
{"label": "sandwiches: $v", "value": 3, "color":"wheat"},
Line 73: Line 78:
{"label": "drinks: $v", "value": 1, "color":"#ccf"}
{"label": "drinks: $v", "value": 1, "color":"#ccf"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true}
|meta={"autoscale":true, "legend":true}
}}
}}
Line 79: Line 85:


=== Links ===
=== Links ===
{{Piechart| [
{{Pie chart| [
{"label": "[[candy|sweets]]: $v", "value": 5, "color":"darkred"},
{"label": "[[candy|sweets]]: $v", "value": 5, "color":"darkred"},
{"label": "[[sandwich]]es: $v", "value": 3, "color":"wheat"},
{"label": "[[sandwich]]es: $v", "value": 3, "color":"wheat"},
Line 85: Line 91:
{"label": "[[drink]]s: $v", "value": 1, "color":"#ccf"}
{"label": "[[drink]]s: $v", "value": 1, "color":"#ccf"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true}
|meta={"autoscale":true, "legend":true}
}}
}}
Line 96: Line 103:
* column-reverse – column layout, reversed (chart on top).
* column-reverse – column layout, reversed (chart on top).
<syntaxhighlight lang="wikitext">
<syntaxhighlight lang="wikitext">
{{Piechart| [
{{Pie chart| [
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
Line 102: Line 109:
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true, "direction":"row-reverse"}
|meta={"autoscale":true, "legend":true, "direction":"row-reverse"}
}}
}}
Line 110: Line 118:
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
'''row''' (default ''direction'')
'''row''' (default ''direction'')
{{Piechart| [
{{Pie chart| [
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
Line 116: Line 124:
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true, "direction":"row"}
|meta={"autoscale":true, "legend":true, "direction":"row"}
}}
}}
Line 121: Line 130:
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
'''row-reverse'''
'''row-reverse'''
{{Piechart| [
{{Pie chart| [
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
Line 127: Line 136:
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true, "direction":"row-reverse"}
|meta={"autoscale":true, "legend":true, "direction":"row-reverse"}
}}
}}
Line 134: Line 144:
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
'''column'''
'''column'''
{{Piechart| [
{{Pie chart| [
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
Line 140: Line 150:
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true, "direction":"column"}
|meta={"autoscale":true, "legend":true, "direction":"column"}
}}
}}
Line 145: Line 156:
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
<div style="border:1px solid darkgreen; padding: 0 .5em .5em">
'''column-reverse'''
'''column-reverse'''
{{Piechart| [
{{Pie chart| [
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "cookies: $v", "value": 2, "color":"goldenrod"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
{"label": "drinks: $v", "value": 1, "color":"#ccf"},
Line 151: Line 162:
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
{"label": "sandwiches: $v", "value": 3, "color":"wheat"}
]
]
|thumb=none
|meta={"autoscale":true, "legend":true, "direction":"column-reverse"}
|meta={"autoscale":true, "legend":true, "direction":"column-reverse"}
}}
}}
Line 159: Line 171:


== Direct functions ==
== Direct functions ==
In case you want to use without the {{t|Piechart}} template, you can use this main functions:
In case you want to use without the {{t|Pie chart}} template, you can use this main functions:
* <code><nowiki>{{</nowiki>#invoke:Piechart|''pie''<nowiki>|json_data|meta=json_options}}</nowiki></code>
* <code><nowiki>{{</nowiki>#invoke:Piechart|''pie''<nowiki>|json_data|meta=json_options}}</nowiki></code>
* <code><nowiki>{{</nowiki>#invoke:Piechart|''color''<nowiki>|number}}</nowiki></code>
* <code><nowiki>{{</nowiki>#invoke:Piechart|''color''<nowiki>|number}}</nowiki></code>
Line 165: Line 177:
Note that direct calls to the ''pie'' function require adding CSS:
Note that direct calls to the ''pie'' function require adding CSS:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
<templatestyles src="Piechart/style.css"/>
<templatestyles src="Pie chart/style.css"/>
{{#invoke:Piechart|pie| [ {"value":33.3}, {} ] }}
{{#invoke:Piechart|pie| [ {"value":33.3}, {} ] }}
</syntaxhighlight>
</syntaxhighlight>
Line 190: Line 202:


== Feature requests ==
== Feature requests ==
For feature requests and bugs write to me, the author of the piecharte module: [[User:Nux|Maciej Nux]].
For feature requests and bugs write to me, the author of the piechart module: [[User:Nux|Maciej Nux]].


== See also ==
== See also ==

Revision as of 02:21, 27 February 2025


Smooth pie chart module. Accessed via Template:Pie chart.

Usage

Draws charts in HTML with an accessible legend (optional). A list of all features is in the "TODO" section of the main `p.pie` function.

Most of the time you should use with a helper template that adds required CSS: {{Pie chart}}.

Examples

Minimalistic

Note that you don't need to provide the second value as it's calculated (assuming they sum up to 100).

{{Pie chart| [ {"value":33.3},  {} ] |thumb=none}}

Labels and Legend

Here we add some custom labels. Also note that we add a meta option to add legend on the side.

{{Pie chart| [
 {"label": "women: $v", "value": 33.3},
 {"label": "men: $v"}
]
|thumb=none
|meta = {"legend":true}
}}
  1. women: 33.3%
  2. men: 66.7%

Automatic Scaling

In cases where you don't have calculated percentages, you can use automatic scaling. Just provide both values in this case.

{{Pie chart| [
 {"label": "women: $v", "value": 750},
 {"label": "men: $v", "value": 250}
]
|thumb=none
|meta = {"legend":true}
}}
  1. women: 750 (75.0%)
  2. men: 250 (25.0%)

Multiple Values

The module allows displaying multiple values, not just 2.

{{Pie chart| [
 {"label": "sweets: $v", "value": 5, "color":"darkred"},
 {"label": "sandwiches: $v", "value": 3, "color":"wheat"},
 {"label": "cookies: $v", "value": 2, "color":"goldenrod"},
 {"label": "drinks: $v", "value": 1, "color":"#ccf"}
]
|thumb=none
|meta={"autoscale":true, "legend":true}
}}
  1. sweets: 5 (45.5%)
  2. sandwiches: 3 (27.3%)
  3. cookies: 2 (18.2%)
  4. drinks: 1 (9.09%)

Note that in this case, it was necessary to provide the additional option "autoscale":true. This is necessary when the sum is less than 100.

  1. sweets: 5 (45.5%)
  2. sandwiches: 3 (27.3%)
  3. cookies: 2 (18.2%)
  4. drinks: 1 (9.09%)

Legend and Its Position

The legend is added using the meta property legend as shown. However, you can also change the order using direction. Possible values include:

  • row (default) – order is list, chart;
  • row-reverse – reverse order, i.e., chart, list;
  • column – column layout (vertical).
  • column-reverse – column layout, reversed (chart on top).
{{Pie chart| [
 {"label": "cookies: $v", "value": 2, "color":"goldenrod"},
 {"label": "drinks: $v", "value": 1, "color":"#ccf"},
 {"label": "sweets: $v", "value": 5, "color":"darkred"},
 {"label": "sandwiches: $v", "value": 3, "color":"wheat"}
]
|thumb=none
|meta={"autoscale":true, "legend":true, "direction":"row-reverse"}
}}

row (default direction)

  1. cookies: 2 (18.2%)
  2. drinks: 1 (9.09%)
  3. sweets: 5 (45.5%)
  4. sandwiches: 3 (27.3%)

row-reverse

  1. cookies: 2 (18.2%)
  2. drinks: 1 (9.09%)
  3. sweets: 5 (45.5%)
  4. sandwiches: 3 (27.3%)

column

  1. cookies: 2 (18.2%)
  2. drinks: 1 (9.09%)
  3. sweets: 5 (45.5%)
  4. sandwiches: 3 (27.3%)

column-reverse

  1. cookies: 2 (18.2%)
  2. drinks: 1 (9.09%)
  3. sweets: 5 (45.5%)
  4. sandwiches: 3 (27.3%)

Green frames added for clarity in examples. They are not normally added.

Direct functions

In case you want to use without the {{Pie chart}} template, you can use this main functions:

  • {{#invoke:Piechart|pie|json_data|meta=json_options}}
  • {{#invoke:Piechart|color|number}}

Note that direct calls to the pie function require adding CSS:

<templatestyles src="Pie chart/style.css"/>
{{#invoke:Piechart|pie| [ {"value":33.3},  {} ] }}

Example of json_data:

[
		{ "label": "pie: $v", "color": "wheat", "value": 40 },
		{ "label": "cheese pizza $v", "color": "#fc0", "value": 20 },
		{ "label": "mixed pizza: $v", "color": "#f60", "value": 20 },
		{ "label": "raw pizza $v", "color": "#f30" }
]
  • Note that the last value is missing. The last value is optional as long as the values are intended to sum up to 100 (as in 100%).
  • Notice $v label, this is a formatted number (see `function prepareLabel`).
  • Colors are hex or names. Default palette is in shades of green.

Example of meta=json_options:

|meta = {"size":200, "autoscale":false, "legend":true}

All meta options are optional (see `function p.setupOptions`).

Feature requests

For feature requests and bugs write to me, the author of the piechart module: Maciej Nux.

See also

  • {{Pie chart}}, also pie, but with classic template parameters (enumerated); that chart is shown on the right (like a thumbnail image).