Template:Pie chart/doc
{{Documentation subpage}}
{{High-use}}
{{lua|Module:Piechart}}
{{Uses TemplateStyles|Template:Pie chart/styles.css}}
This template draws pie charts with HTML, CSS, and a Lua module. It can accept data formatted as:
- Enumerated values:
|value1=123 |value2=456 (classic) or - A simple array:
|1=[{"value":123, "value":456}] (easy to re-order)
__TOC__
Usage
This template offers several different ways to draw pie charts. It can accept data as enumerated parameters (like many templates do). It can also accept data as an array. When placing the pie chart, the template can "float" it to either side of the body text (how image thumbnails are positioned). It can also place the chart as body content (how most tables are positioned).
=Thumb (floating) =
By default, the template generates charts as floating thumbnails.
==Enumerated parameters==
{{Pie chart
| radius = 100
| caption = Default colors
| label1 = {{#invoke:Piechart|color|1}} | value1 = 7.7
| label2 = {{#invoke:Piechart|color|2}} | value2 = 7.7
| label3 = {{#invoke:Piechart|color|3}} | value3 = 7.7
| label4 = {{#invoke:Piechart|color|4}} | value4 = 7.7
| label5 = {{#invoke:Piechart|color|5}} | value5 = 7.7
| label6 = {{#invoke:Piechart|color|6}} | value6 = 7.7
| label7 = {{#invoke:Piechart|color|7}} | value7 = 7.7
| label8 = {{#invoke:Piechart|color|8}} | value8 = 7.7
| label9 = {{#invoke:Piechart|color|9}} | value9 = 7.7
| label10 = {{#invoke:Piechart|color|10}} | value10 = 7.7
| label11 = {{#invoke:Piechart|color|11}} | value11 = 7.7
| label12 = {{#invoke:Piechart|color|12}} | value12 = 7.7
| other-label = other: {{#invoke:Piechart|color|-1}}
| other = y
}}
The labels, values, and colors of slices may be specified. All the parameters for six slices are included in the example below. To add more slices, copy the code for an existing line and change the digit at the end of each parameter's name. Order the values by size, from largest to smallest. This will order the chart's slices in a clockwise direction, with the largest slice at 12 o'clock.
{{Pie chart
| thumb =
| radius =
| caption=
| footer =
| label1 = | value1 = | color1 =
| label2 = | value2 = | color2 =
| label3 = | value3 = | color3 =
| label4 = | value4 = | color4 =
| label5 = | value5 = | color5 =
| other =
| other-color =
| other-label =
}}
==Enumerated values==
- {{para|other}}: If any value is given (even "0" or "no"), an "Other" item will appear in the legend, corresponding to the final slice that makes the values add up to 100.If the total of all values is greater than 100%, no final slice will be added, and "other" will not be added to the legend even if it is specified, as it would not correspond to any aspect of the visible chart.
- {{para|other-color}}: Specify a color to override the default white for the "Other" slice.Web colors can be specified by name or hex value.
- {{para|other-label}}: Give a name to override the default text from "Other" to any string.
- {{para|labelN}}: This string of text appears in the legend entry for each slice. Omitting it will cause a legend to be automatically generated. Use "$v" in label to show formatted values in the legend.
- {{para|valueN}}: This should be the percentage that the slice represents or an actual value. Use {{para|autoscale|true}} to automatically scale values to a percentage of the total. Values that add up to more than 100 will be scaled down automatically.
- {{para|colorN}}: Each slice can have a CSS color code or name. If omitted, the default color palette will be used. The default color palette is designed with accessibility and printing in mind.
Even though you could use as many values and labels as you want, at some point values will be too small to be readable. Values less then 1% don't make much sense. You could change the radius, but ultimately you should just use a wikitable instead. Pie chart should typically only have 2-10 values and the "Other" value.
==Common parameters==
- {{para|thumb}}: By default the chart is floated to the
right
of the body content, as with image files. To make the chart appear on the left side specify {{para|thumb|left}}. To place the chart as body content (similar to how tables are placed by default), specify {{para|thumb|none}}. - {{para|radius}} You can specify a desired radius in pixels. Do not include a "px" suffix. The chart defaults to {{para|radius|100}}.
- {{para|caption}}: The caption is a string of wikitext displayed above the legend.
- {{para|footer}}: The footer is a string of wikitext displayed below the legend.
- {{para|autoscale|true}}:Instead of
true
, you can useyes
,on
, or1
. This means that the sum of values will be automatically scaled to 100%. So for values: 5, 15, the slices will be "5 (25%)" and "15 (75%)". If the total is over 100, this defaults to true. - {{para|legend|true}}: A legend will be added. Defaults to true if thumb is used.
- {{para|direction}}: When the chart is positioned as body content, this parameter changes the placement of the legend. Possible values are:
row
: the order is a list, chart (default for thumb=none);row-reverse
: the reverse order, i.e., chart, list;column
: column arrangement (vertical);column-reverse
: column arrangement, reversed (chart on top; default for thumb=right/left).- {{para|width}}: In most cases, you probably don't want to use this. It changes the width of the box. Normally, the box will be automatically scaled (for thumb=right or thumb=left, to pie radius; for thumb=none, to full available width).
==Array of values==
The template can also accept a JSON array of data, labels, and colors in the first unnamed parameter or {{para|1}}. An array is more strict in how the data must be formatted, but offers extra flexibility because you can easily rearrange the rows. This can simplify charts where the values will be frequently updated. It also simplifies the process of syncing charts across multiple articles or wikis.
- {{para|1|[]}}: This parameter holds an array of all values for a chart. Each value can have a label and color.
- just value:
{"value": 1},
- value and a label:
{"label": "drinks", "value": 1},
- label with formatted value placeholder:
{"label": "drinks: $v", "value": 1},
or{"label": "drinks – $v", "value": 1},
- full (also custom color): {"label": "drinks: $v", "value": 1, "color":"#ccf"},
- To specify other:
- you just add braces to the end of the array of values
{}
(this will mean the value is to be calculated automatically); - you can add a label
{"label": "other citizens"}
; - you can add a custom color
{"label": "other citizens", "color": "silver"}
.
In the example below, the same lines of text are used to create charts arranged from most to least and from least to most. Only the order has changed:
scope=col style=text-align:left|Most to least
!scope=col style=text-align:right|Least to most |
---|
{{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"}, ] | autoscale = true | legend = true | thumb = left | radius = 100 | caption= Large to smallReferences should work fine in captions | footer = }} |{{Pie chart |[ {"label": "drinks: $v", "value": 1, "color":"#ccf"}, {"label": "cookies: $v", "value": 2, "color":"goldenrod"}, {"label": "sandwiches: $v", "value": 3, "color":"wheat"}, {"label": "sweets: $v", "value": 5, "color":"darkred"}, ] | autoscale = true | legend = true | thumb = right | radius = 100 | caption= Small to largeReferences should work fine in captions | footer = }} |
{{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"}, ] | autoscale = true | legend = true | thumb = left | radius = 100 | caption= Large to smallReferences should work fine in captions | footer = }} | {{Pie chart |[ {"label": "drinks: $v", "value": 1, "color":"#ccf"}, {"label": "cookies: $v", "value": 2, "color":"goldenrod"}, {"label": "sandwiches: $v", "value": 3, "color":"wheat"}, {"label": "sweets: $v", "value": 5, "color":"darkred"}, ] | autoscale = true | legend = true | thumb = right | radius = 100 | caption= Small to largeReferences should work fine in captions | footer = }} |
=Body content=
Pie charts can also be positioned as body content. This is how most table are positioned in articles.
== Minimal ==
The smallest possible definition of a graph that makes sense is below.
{{Pie chart| [ {"value":25}, {} ] |thumb=none}}
{{Pie chart| [ {"value":25}, {} ] |thumb=none}}
== Labels with formatted value ==
You can add labels to values as shown below.
{{Pie chart
| [
{"value":25, "label": "town $v"},
{"label": "city $v"},
]
|thumb=none
|legend=true
}}
{{Pie chart
| [
{"value":25, "label": "town $v"},
{"label": "city $v"},
]
|thumb=none
|legend=true
}}
Note that $v
is replaced with a formatted value. In most cases, it's the best choice as it automatically adapts to different contexts. It's a magic value.
See how $v
adapts when large values are auto-scaled:
{{Pie chart
| [
{"value":2500, "label": "town $v"},
{"value":7500, "label": "city $v"},
]
|thumb=none
|legend=true
|autoscale=true
}}
{{Pie chart
| [
{"value":2500, "label": "town $v"},
{"value":7500, "label": "city $v"},
]
|thumb=none
|legend=true
|autoscale=true
}}
== Labels and advanced formatting templates ==
As shown above, a typical label: "Abc: $v" will result in:
- "Abc: 23%" — when values are percentages (and sum up to 100)
- "Abc: 123,456,789 (23%)" — when values are auto-scaled
class="wikitable"
! Name !! Symbol !! No Scaling !! Auto-Scale | |||
Value | "Abc: $v" | "Abc: 23%" | "Abc: 450 (23%)" |
Data | "Abc: $d" | (don't use) | "Abc: 450" |
Percent | "Abc: $p" | (don't use) | "Abc: 23%" |
==Legend position==
When drawing the chart as body content, the legend is positioned to the left of the chart by default. You can change the display position using the {{para|direction}} parameter. Possible values include:
class="wikitable" style="padding=3em;"
|+ Using "direction" to position the legend !scope=col|Direction !scope=col|Example |
scope=row style=text-align:left|{{para|direction|row}} (default){{pb}}The legend is to the left of the chart.
|{{Pie chart|direction=row |[ {"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} }} |
---|
scope=row style=text-align:left|{{para|direction|row-reverse}} ( reverse order){{pb}}The legend is to right of the chart.
|{{Pie chart|direction=row-reverse |[ {"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} }} |
scope=row style=text-align:left|{{para|direction|column}} (vertical){{pb}}The legend is above the chart.
|{{Pie chart|direction=column| [ {"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} }} |
scope=row style=text-align:left|{{para|direction|column-reverse}} (vertical, reversed){{pb}}The legend is below the chart.
||{{Pie chart|direction=column-reverse| [ {"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} }} |
More examples
The following code generates the pie chart shown at right. Note that the default chart size and colors are used.
{{Pie chart
|value1 = 1
|value2 = 2
|value3 = 3
|value4 = 4
|value5 = 5
|value6 = 6
|autoscale = true
}}
{{Pie chart
|value1 = 1
|value2 = 2
|value3 = 3
|value4 = 4
|value5 = 5
|value6 = 6
|autoscale = true
}}
Here's a more real-world example with custom colors.
{{Pie chart
| caption= Religion in the Czech Republic in 2001.
| label1 = Atheists and agnostics
| value1 = 59
| color1 = darkgreen
| label2 = Catholics
| value2 = 26.8
| color2 = brown
| label3 = Protestants
| value3 = 2.5
| color3 = #08f
| other = yes
| other-color = silver
}}
{{Pie chart
| caption= Religion in the Czech Republic in 2001.
| label1 = Atheists and agnostics
| value1 = 59
| color1 = darkgreen
| label2 = Catholics
| value2 = 26.8
| color2 = brown
| label3 = Protestants
| value3 = 2.5
| color3 = #08f
| other = yes
| other-color = silver
}}
Limitations
- Some browsers might have problems with anti-aliasing borders properly, so the lines might be a bit jagged.
- Due to how the graphing is implemented, it is not possible to save a copy of the chart using the browser's "Save Image" function (however, a screenshot can be taken).
- The default color palette is limited to 12 colors. It will loop, but a wikitable is more readable than a pie chart with too many slices.
Notes
TemplateData
{{template data header}}
"params": {
"1": {
"suggested": true,
"label": "Chart items",
"description": "Array of chart items, each being a set of properties for a slice: value, label, and color. For most items, only \"value\" is required (for the last item, even value can be omitted). Colors can be omitted and will be assigned automatically. Instead of this parameter, you can also use \"value1\", \"label1\", \"color1\"...",
"example": "{\"label\": \"sweets: $v\", \"value\": 5, \"color\": \"darkred\"}",
"type": "string"
},
"thumb": {
"suggested": true,
"description": "Specifies whether the chart is floated to the left or right, similar to an image.",
"type": "line",
"suggestedvalues": [
"right",
"left",
"none"
],
"default": "right"
},
"radius": {
"description": "Specifies the radius of the pie chart in pixels. If omitted, the default is 100.",
"type": "number"
},
"width": {
"description": "Specifies the box size in pixels. Remove this parameter to allow the size to adjust automatically (based on labels and graph size).",
"type": "number"
},
"autoscale": {
"suggested": true,
"description": "If set to true, the sum of values will be automatically scaled to 100%. Autoscale is always enabled when the sum of values exceeds 100.",
"type": "line",
"suggestedvalues": ["true", "false"],
"default": "false"
},
"legend": {
"suggested": true,
"description": "If set to true, a legend will be added. Defaults to true if the thumb parameter is used.",
"type": "line",
"suggestedvalues": ["true", "false"],
"default": "true"
},
"direction": {
"description": "Determines the placement of the legend. The chart and legend can be arranged in a column or a row.",
"type": "line",
"suggestedvalues": ["row", "row-reverse", "column", "column-reverse"],
"default": "column-reverse"
},
"caption": {
"description": "Specifies the chart's caption, which appears before the legend. Only use this parameter if the legend is enabled. The caption can contain references.",
"type": "line"
},
"footer": {
"description": "Specifies the chart's footer, which appears after the legend. Only use this parameter if the legend is enabled.",
"type": "line"
},
"value1": { "description": "Value for the first item (used instead of the chart items array)." },
"label1": { "description": "Label for the first item (used instead of the chart items array)." },
"color1": { "description": "Color for the first item/slice (used instead of the chart items array)." },
"value2": {},
"value3": {},
"value4": {},
"value5": {},
"value6": {},
"value7": {},
"value8": {},
"value9": {},
"value10": {},
"label2": {},
"label3": {},
"label4": {},
"label5": {},
"label6": {},
"label7": {},
"label8": {},
"label9": {},
"label10": {},
"color2": {},
"color3": {},
"color4": {},
"color5": {},
"color6": {},
"color7": {},
"color8": {},
"color9": {},
"color10": {},
"other-label": {},
"other-color": {},
"other": {}
},
"description": "Template to create pie charts (proportionally divided circles). Note that values are approximate and may be automatically scaled to 100%.",
"format": "block",
"paramOrder": [
"1",
"thumb",
"legend",
"direction",
"autoscale",
"caption",
"footer",
"radius",
"width",
"value1", "label1", "color1",
"value2", "label2", "color2",
"value3", "label3", "color3",
"value4", "label4", "color4",
"value5", "label5", "color5",
"value6", "label6", "color6",
"value7", "label7", "color7",
"value8", "label8", "color8",
"value9", "label9", "color9",
"value10", "label10", "color10",
"other-label", "other-color", "other"
]
}
See also
- {{tl|Brick chart}}
- {{tl|Composition bar}}
- Module:Chart
{{Graph, chart and plot templates}}
|
Category:Chart, diagram and graph formatting and function templates
}}