Jump to content

Template:Pie chart/sandbox/doc

From Wikipedia, the free encyclopedia

This is a template that draws pie charts with HTML, CSS, and some Lua magic (module).

You can supply values both as a simple array: [{"value":123, "value":456}] and as enumerated values: |value1=123 |value2=456. See below for details.

Usage

[edit]

The labels, values, and colors of slices may be specified. All the parameters for six slices are included below; to include more slices, copy the code for one of the others, changing the digit at the end of each parameter name. When making a pie chart, ensure that the segments are ordered by size (largest to smallest) and in a clockwise direction.

Minimalistic

[edit]

The smallest possible definition of a graph that makes sense is below.

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

Labels with formatted value

[edit]

You can add labels to values as shown below.

  1. town 25.0%
  2. city 75.0%
{{Pie chart
| [ 
 {"value":25, "label": "town $v"},
 {"label": "city $v"},
]
|thumb=none
|legend=true
}}

Note that $v is replaced with a formatted value.

Array of values

[edit]

You might have noticed that the first parameter is an array. It's a JSON array with some extra flexibility. You can still use enumerated parameters (see below), but an array of values can be better because you can easily rearrange them.

Large to small[Usage 1]
  1. sweets: 5 (45.5%)
  2. sandwiches: 3 (27.3%)
  3. cookies: 2 (18.2%)
  4. drinks: 1 (9.09%)
Small to large[Usage 2]
  1. drinks: 1 (9.09%)
  2. cookies: 2 (18.2%)
  3. sandwiches: 3 (27.3%)
  4. sweets: 5 (45.5%)
{{Pie chart/sandbox
|[
 {"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  = none
| radius = 100
| caption= Large to small<ref group="Usage">References should work fine in captions</ref>
| footer = 
}}
{{Pie chart/sandbox
|[
 {"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  = none
| radius = 100
| caption= Small to large<ref group="Usage">References should work fine in captions</ref>
| footer = 
}}

Notes:

  1. ^ References should work fine in captions
  2. ^ References should work fine in captions

Enumerated values

[edit]
{{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 =
}}

Parameters

[edit]
Default colors
  1. #1b7837 (7%)
  2. #7fbf7b (7%)
  3. #d9f0d3 (7%)
  4. #762a83 (7%)
  5. #af8dc3 (7%)
  6. #e7d4e8 (7%)
  7. #d73027 (7%)
  8. #fc8d59 (7%)
  9. #fee090 (7%)
  10. #4575b4 (7%)
  11. #91bfdb (7%)
  12. #e0f3f8 (7%)
  13. other: #fff (16.0%)

Common parameters

[edit]
  • |thumb= specifies which side of the page the chart is floated to and defaults to right, as with image files. To make the chart appear on the left side of the page, specify |thumb=left.
  • |radius= specifies the radius of the pie chart in pixels. Do not include a "px" suffix. If omitted, it will default to 100.
  • |caption= specifies a string of wikitext that appears before the legend.
  • |footer= specifies a string of wikitext that appears after the legend.
  • |autoscale=true[Parameters 1] 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%)". Defaults to true if the sum of values is over 100.
  • |legend=true[Parameters 1] means that a legend will be added. Defaults to true if thumb is used.
  • |direction= can be used to change 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).
  • |width= changes the width of the box. In most cases, you probably don't want to use this. Normally, the box will be automatically scaled (for thumb=right, to pie radius; for thumb=none, to full available width).

Notes:

  1. ^ a b Instead of true, you can use yes, on, or 1.

Array of values

[edit]
  • |1=[] specifies an array of values. Each value can also 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"}.

Enumerated values

[edit]
  • |other=, if set equal to any visible string (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 is already over 100, then the "Other" percentage will be negative.)
  • |other-color= can be used to override the default white color of the "Other" slice.
  • |other-label= can be used to override the default text from "Other" to any other string.
  • Each |labelN= is a string of text that appears in the legend entry for a slice. Omitting it will cause a legend to be automatically generated. Use "$v" in label to show formatted values in the legend.
  • Each |valueN= is the percentage that the slice represents or an actual value. Use |autoscale=true to automatically scale values to percantage of the sum of values. Note that values that sum up to more then 100 will be scaled down automatically.
  • Each |colorN= is a CSS color code or name. If omitted, the default color palette will be used. Note that default color palette is designed with Accessibility in mind and should work well in print too.

Note that even though you could use as many values and labels as you want, at some point values will to 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.

Limitations

[edit]
  • 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).
  • Currently the default color palette is limited to 12 colors. It will loop around, but you should probably use a wikitable with pie chart beside it instead of using too many values.

More examples

[edit]

The following code generates the pie chart shown at right. Note that the default chart size and colors are used.

  1. 1 (4.76%)
  2. 2 (9.52%)
  3. 3 (14.3%)
  4. 4 (19.0%)
  5. 5 (23.8%)
  6. 6 (28.6%)
{{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.

  1. Atheists and agnostics (59%)
  2. Catholics (26.8%)
  3. Protestants (2.5%)
  4. Other (11.7%)
{{Pie chart
| caption= [[Religion in the Czech Republic]] in 2001.
| label1 = [[Atheist]]s and [[agnostic]]s
| value1 = 59
| color1 = darkgreen
| label2 = [[Catholic]]s
| value2 = 26.8
| color2 = brown
| label3 = [[Protestant]]s
| value3 = 2.5
| color3 = #08f
| other  = yes
| other-color = silver
}}

Parameters table

[edit]

Template to create pie charts (proportionally divided circles). Note that values are approximate and may be automatically scaled to 100%.

Template parameters

This template prefers block formatting of parameters.

ParameterDescriptionTypeStatus
Chart items1

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"}
Stringrequired
thumbthumb

Specifies whether the chart is floated to the left or right, similar to an image.

Suggested values
right left none
Default
right
Lineoptional
legendlegend

If set to true, a legend will be added. Defaults to true if the thumb parameter is used.

Suggested values
true false
Default
true
Lineoptional
directiondirection

Determines the placement of the legend. The chart and legend can be arranged in a column or a row.

Suggested values
row row-reverse column column-reverse
Default
column-reverse
Lineoptional
autoscaleautoscale

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.

Suggested values
true false
Default
false
Lineoptional
captioncaption

Specifies the chart's caption, which appears before the legend. Only use this parameter if the legend is enabled. The caption can contain references.

Lineoptional
footerfooter

Specifies the chart's footer, which appears after the legend. Only use this parameter if the legend is enabled.

Lineoptional
radiusradius

Specifies the radius of the pie chart in pixels. If omitted, the default is 100.

Numberoptional
widthwidth

Specifies the box size in pixels. Remove this parameter to allow the size to adjust automatically (based on labels and graph size).

Numberoptional
value1value1

Value for the first item (used instead of the chart items array).

Unknownoptional
label1label1

Label for the first item (used instead of the chart items array).

Unknownoptional
color1color1

Color for the first item/slice (used instead of the chart items array).

Unknownoptional
value2value2

no description

Unknownoptional
label2label2

no description

Unknownoptional
color2color2

no description

Unknownoptional
value3value3

no description

Unknownoptional
label3label3

no description

Unknownoptional
color3color3

no description

Unknownoptional
value4value4

no description

Unknownoptional
label4label4

no description

Unknownoptional
color4color4

no description

Unknownoptional
value5value5

no description

Unknownoptional
label5label5

no description

Unknownoptional
color5color5

no description

Unknownoptional
value6value6

no description

Unknownoptional
label6label6

no description

Unknownoptional
color6color6

no description

Unknownoptional
value7value7

no description

Unknownoptional
label7label7

no description

Unknownoptional
color7color7

no description

Unknownoptional
value8value8

no description

Unknownoptional
label8label8

no description

Unknownoptional
color8color8

no description

Unknownoptional
value9value9

no description

Unknownoptional
label9label9

no description

Unknownoptional
color9color9

no description

Unknownoptional
value10value10

no description

Unknownoptional
label10label10

no description

Unknownoptional
color10color10

no description

Unknownoptional
other-labelother-label

no description

Unknownoptional
other-colorother-color

no description

Unknownoptional
otherother

no description

Unknownoptional

See also

[edit]