Difference between revisions of "Template:Bartable/doc"

From blackwiki
Jump to navigation Jump to search
imported>Timwi
(Add wiki mark-up for the real-world example)
imported>Cmglee
(Update documentation for 2/3-column modes)
Line 2: Line 2:
 
<!-- Categories and interwikis go at the bottom of this page. -->
 
<!-- Categories and interwikis go at the bottom of this page. -->
  
This template adds an inline bar graph to tables with numeric (including negative) values that a reader may wish to compare. It takes up 3 columns of a table, so headers should have '''colspan="3"''' (or similar combination) to correctly span the columns.
+
This template adds an inline bar graph to tables with numeric (including negative) values that a reader may wish to compare. It takes up either 2 or 3 columns &mdash; determined by the sign of the third (''scale'') parameter &mdash; of a table, so headers should have '''colspan="2"''' or '''colspan="3"''' (or similar combinations), respectively, to correctly span the columns.
  
Notes:
+
== Usage ==
# A "negative" bars column is created even if there are no negative values (and vice versa); if anyone knows how to collapse the column (zero width/padding/margins), please help.
 
# A negative value of scale will not change the side of the bar.
 
 
 
=== Usage ===
 
  
 
  <nowiki>{{</nowiki>bartable|''value''|''unit''|''scale''|''height''|''style''<nowiki>}}</nowiki>
 
  <nowiki>{{</nowiki>bartable|''value''|''unit''|''scale''|''height''|''style''<nowiki>}}</nowiki>
  
* ''value'' (mandatory) is any real number or math expression that evaluates to a number.
+
* ''value'' (mandatory) is any real number or math expression that evaluates to a number. Commas can be used to [[Digit_grouping|group digits]]. See [[Help:Calculation]] for the syntax for math expressions.
* ''unit'' (default: empty string) is a suffix to be appended to ''value''. If ''unit'' starts with "/", the '''convert''' template is used as follows: "/''unit1''/''unit2''/''round''" is translated to "<nowiki>{{</nowiki>convert|''value''|''unit1''|''unit2''|''round''|abbr=on<nowiki>}}</nowiki>. (Note: ''unit1'' and ''unit2'' cannot contain "/" or "=". If anyone knows how to allow it (or translate other characters to it), please help.)
+
* ''unit'' (default: empty string) is a suffix to be appended to ''value''. Prepend a space to have a space between the value and the unit. If ''unit'' starts with "''/''", the '''[[Template:Convert|convert]]''' template is used as follows:
* ''scale'' (default: "1") is a positive factor multiplied by ''value'' to give the width of the bar in pixels.
+
:"''/unit1/unit2/round''" is translated to "<nowiki>{{</nowiki>convert|''value''|''unit1''|''unit2''|''round''|abbr=on<nowiki>}}</nowiki>. (Note: ''unit1'', ''unit2'' or ''round'' cannot contain "''/''" or "=". If anyone knows how to allow it (or translate other characters to it), please help.)
 +
* ''scale'' (default: "1") is a factor multiplied by ''value'' to give the width of the bar in pixels.
 +
:Note: If ''scale'' is negative, an additional column (for showing negative values) is created. See [[#3-column (positive and negative values)|example below]].
 
* ''height'' (default: "2ex") is the height of the bar as a [[cascading style sheet|CSS]] measurement e.g. "1em" or "16px".
 
* ''height'' (default: "2ex") is the height of the bar as a [[cascading style sheet|CSS]] measurement e.g. "1em" or "16px".
 
* ''style'' (default: empty string) is the content of a CSS style string to apply to the bar e.g. "background:red;".
 
* ''style'' (default: empty string) is the content of a CSS style string to apply to the bar e.g. "background:red;".
  
=== Examples ===
+
== Examples ==
  
Click '''Edit''' to view source.
+
{{for|pages using this template|Special:WhatLinksHere/Template:Bartable}}
  
 +
=== Real-world example ===
 +
<pre>
 
{| class="wikitable"
 
{| class="wikitable"
! Example                || colspan="3" | Rendering
+
|+ Melting point at 1 atmosphere
 +
! Substance  !! Temperature !! colspan="2" | Relative size
 
|-
 
|-
| No parameters          || {{bartable}}
+
| Mercury    || {{bartable| -38.72|/C/F|-2|1em|background:silver}}
 
|-
 
|-
| Only value              || {{bartable|272}}
+
| Bromine    || {{bartable| -7.10|/C/F|-2|1em|background:brown}}
 
|-
 
|-
| Negative value          || {{bartable|-72}}
+
| Water      || {{bartable|      0|/C/F|-2|1em}}
 
|-
 
|-
| Math expression        || {{bartable|exp(1)*1e2}}
+
| Phosphorus || {{bartable| 44.10|/C/F|-2|1em|background:red}}
 
|-
 
|-
| Value and ordinary unit || {{bartable|272|km/h}}
+
| Iodine    || {{bartable| 113.50|/C/F|-2|1em|background:indigo}}
 +
|-
 +
| Sulphur    || {{bartable| 115.36|/C/F|-2|1em|background:yellow}}
 +
|}
 +
</pre>
 +
{| class="wikitable"
 +
|+ Melting point at 1 atmosphere
 +
! Substance  !! Temperature !! colspan="2" | Relative size
 
|-
 
|-
| Value and convert unit  || {{bartable|272|/km/nmi}}
+
| Mercury    || {{bartable| -38.72|/C/F|-2|1em|background:silver}}
 
|-
 
|-
| Value and scale        || {{bartable|272||1/(2^0.5)}}
+
| Bromine    || {{bartable| -7.10|/C/F|-2|1em|background:brown}}
 
|-
 
|-
| Value and height        || {{bartable|272|||4px}}
+
| Water      || {{bartable|     0|/C/F|-2|1em}}
 
|-
 
|-
| Value and style        || {{bartable|272||||background:blue;}}
+
| Phosphorus || {{bartable| 44.10|/C/F|-2|1em|background:red}}
 
|-
 
|-
| The full monty          || {{bartable|-72|/km/nmi/0|-1|2em|border:ridge red;}}
+
| Iodine    || {{bartable| 113.50|/C/F|-2|1em|background:indigo}}
 
|-
 
|-
 +
| Sulphur    || {{bartable| 115.36|/C/F|-2|1em|background:yellow}}
 
|}
 
|}
  
=== Real-world example ===
+
=== 2-column (only positive values) ===
 +
<pre>
 +
{| class="wikitable"
 +
! Example                    || colspan="2" | Rendering
 +
|-
 +
| Only value                  || {{subst:Hidden/sandbox|272}}
 +
|-
 +
| Negative value              || {{subst:Hidden/sandbox|-72}}
 +
|-
 +
| Math expression            || {{subst:Hidden/sandbox|exp(1)*1e2}}
 +
|-
 +
| Value and ordinary unit    || {{subst:Hidden/sandbox|272| km/h}}
 +
|-
 +
| Value and convert unit      || {{subst:Hidden/sandbox|272|/km/nmi}}
 +
|-
 +
| Value and scale            || {{subst:Hidden/sandbox|272||1/(2^0.5)}}
 +
|-
 +
| Value and height            || {{subst:Hidden/sandbox|272|||4px}}
 +
|-
 +
| Value and style            || {{subst:Hidden/sandbox|272||||background:blue;}}
 +
|-
 +
| Value with commas and scale || {{subst:Hidden/sandbox|2,720||.1}}
 +
|-
 +
| The full monty              || {{subst:Hidden/sandbox|18^2|%|sin(1)|2em|border:ridge red;}}
 +
|-
 +
|}
 +
</pre>
 +
{| class="wikitable"
 +
! Example                    || colspan="2" | Rendering
 +
|-
 +
| Only value                  || {{#if: | {{#if:{{#titleparts:{{{2}}}|1|2}}|{{#if:{{#titleparts:{{{2}}}|1|1}}|272{{{2}}}|{{convert|272|{{#titleparts:{{{2}}}|1|2}}|{{#titleparts:{{{2}}}|1|3}}|{{#titleparts:{{{2}}}|1|4}}|abbr=on}} }}|272{{{2}}} }} | 272 }} {{#ifexpr:{{#if:|{{{3}}}|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:|{{{3}}}|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:|{{{3}}}|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Negative value              || {{#if: | {{#if:{{#titleparts:{{{2}}}|1|2}}|{{#if:{{#titleparts:{{{2}}}|1|1}}|-72{{{2}}}|{{convert|-72|{{#titleparts:{{{2}}}|1|2}}|{{#titleparts:{{{2}}}|1|3}}|{{#titleparts:{{{2}}}|1|4}}|abbr=on}} }}|-72{{{2}}} }} | -72 }} {{#ifexpr:{{#if:|{{{3}}}|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:-72|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:|{{{3}}}|1}})*{{formatnum:-72|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:-72|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:|{{{3}}}|1}})*{{formatnum:-72|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Math expression            || {{#if: | {{#if:{{#titleparts:{{{2}}}|1|2}}|{{#if:{{#titleparts:{{{2}}}|1|1}}|exp(1)*1e2{{{2}}}|{{convert|exp(1)*1e2|{{#titleparts:{{{2}}}|1|2}}|{{#titleparts:{{{2}}}|1|3}}|{{#titleparts:{{{2}}}|1|4}}|abbr=on}} }}|exp(1)*1e2{{{2}}} }} | exp(1)*1e2 }} {{#ifexpr:{{#if:|{{{3}}}|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:exp(1)*1e2|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:|{{{3}}}|1}})*{{formatnum:exp(1)*1e2|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:exp(1)*1e2|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:|{{{3}}}|1}})*{{formatnum:exp(1)*1e2|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Value and ordinary unit    || {{#if: km/h | {{#if:{{#titleparts: km/h|1|2}}|{{#if:{{#titleparts: km/h|1|1}}|272 km/h|{{convert|272|{{#titleparts: km/h|1|2}}|{{#titleparts: km/h|1|3}}|{{#titleparts: km/h|1|4}}|abbr=on}} }}|272 km/h }} | 272 }} {{#ifexpr:{{#if:|{{{3}}}|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:|{{{3}}}|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:|{{{3}}}|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Value and convert unit      || {{#if:/km/nmi | {{#if:{{#titleparts:/km/nmi|1|2}}|{{#if:{{#titleparts:/km/nmi|1|1}}|272/km/nmi|{{convert|272|{{#titleparts:/km/nmi|1|2}}|{{#titleparts:/km/nmi|1|3}}|{{#titleparts:/km/nmi|1|4}}|abbr=on}} }}|272/km/nmi }} | 272 }} {{#ifexpr:{{#if:|{{{3}}}|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:|{{{3}}}|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:|{{{3}}}|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Value and scale            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:1/(2^0.5)|1/(2^0.5)|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:1/(2^0.5)|1/(2^0.5)|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:1/(2^0.5)|1/(2^0.5)|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Value and height            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:||0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:||1}})*{{formatnum:272|R}} }}px;height:{{#if:4px|4px|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:||1}})*{{formatnum:272|R}} }}px;height:{{#if:4px|4px|2ex}};" />|}}
 +
|-
 +
| Value and style            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:||0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:||1}})*{{formatnum:272|R}} }}px;height:{{#if:||2ex}};background:blue;" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:||1}})*{{formatnum:272|R}} }}px;height:{{#if:||2ex}};background:blue;" />|}}
 +
|-
 +
| Value with commas and scale || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|2,720|{{convert|2,720|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|2,720 }} | 2,720 }} {{#ifexpr:{{#if:.1|.1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:2,720|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:.1|.1|1}})*{{formatnum:2,720|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:2,720|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:.1|.1|1}})*{{formatnum:2,720|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| The full monty              || {{#if:% | {{#if:{{#titleparts:%|1|2}}|{{#if:{{#titleparts:%|1|1}}|18^2%|{{convert|18^2|{{#titleparts:%|1|2}}|{{#titleparts:%|1|3}}|{{#titleparts:%|1|4}}|abbr=on}} }}|18^2% }} | 18^2 }} {{#ifexpr:{{#if:sin(1)|sin(1)|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:18^2|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:sin(1)|sin(1)|1}})*{{formatnum:18^2|R}} }}px;height:{{#if:2em|2em|2ex}};border:ridge red;" />|}} | }}
 +
| {{#ifexpr:{{formatnum:18^2|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:sin(1)|sin(1)|1}})*{{formatnum:18^2|R}} }}px;height:{{#if:2em|2em|2ex}};border:ridge red;" />|}}
 +
|-
 +
|}
  
{|
+
=== 3-column (positive and negative values) ===
|
 
 
<pre>
 
<pre>
 
{| class="wikitable"
 
{| class="wikitable"
|+ Melting point at 1 atmosphere
+
! Example                    || colspan="3" | Rendering
! Substance  !! Temperature !! colspan="2" | Relative size
+
|-
 +
| Only value                  || {{subst:Hidden/sandbox|272||-1}}
 +
|-
 +
| Negative value              || {{subst:Hidden/sandbox|-72||-1}}
 +
|-
 +
| Math expression            || {{subst:Hidden/sandbox|exp(1)*1e2||-1}}
 +
|-
 +
| Value and ordinary unit    || {{subst:Hidden/sandbox|272| km/h|-1}}
 +
|-
 +
| Value and convert unit      || {{subst:Hidden/sandbox|272|/km/mi|-1}}
 
|-
 
|-
| Mercury    || {{bartable| -38.72|/C/F|2|1em|background:silver}}
+
| Value and scale            || {{subst:Hidden/sandbox|272||-1/(2^0.5)}}
 
|-
 
|-
| Bromine    || {{bartable|  -7.10|/C/F|2|1em|background:brown}}
+
| Value and height            || {{subst:Hidden/sandbox|272||-1|4px}}
 
|-
 
|-
| Water      || {{bartable|     0|/C/F|2|1em}}
+
| Value and style            || {{subst:Hidden/sandbox|272||-1||background:blue;}}
 
|-
 
|-
| Phosphorus || {{bartable|  44.10|/C/F|2|1em|background:red}}
+
| Value with commas and scale || {{subst:Hidden/sandbox|2,720||-.1}}
 
|-
 
|-
| Iodine    || {{bartable| 113.50|/C/F|2|1em|background:indigo}}
+
| The full monty              || {{subst:Hidden/sandbox|18^2|%|sin(-1)|2em|border:ridge red;}}
 
|-
 
|-
| Sulphur    || {{bartable| 115.36|/C/F|2|1em|background:yellow}}
 
 
|}
 
|}
 
</pre>
 
</pre>
|
 
 
{| class="wikitable"
 
{| class="wikitable"
|+ Melting point at 1 atmosphere
+
! Example                    || colspan="3" | Rendering
! Substance  !! Temperature !! colspan="2" | Relative size
+
|-
 +
| Only value                  || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Negative value              || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|-72|{{convert|-72|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|-72 }} | -72 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:-72|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:-72|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:-72|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:-72|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Math expression            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|exp(1)*1e2|{{convert|exp(1)*1e2|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|exp(1)*1e2 }} | exp(1)*1e2 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:exp(1)*1e2|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:exp(1)*1e2|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:exp(1)*1e2|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:exp(1)*1e2|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Value and ordinary unit    || {{#if: km/h | {{#if:{{#titleparts: km/h|1|2}}|{{#if:{{#titleparts: km/h|1|1}}|272 km/h|{{convert|272|{{#titleparts: km/h|1|2}}|{{#titleparts: km/h|1|3}}|{{#titleparts: km/h|1|4}}|abbr=on}} }}|272 km/h }} | 272 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 +
|-
 +
| Value and convert unit      || {{#if:/km/mi | {{#if:{{#titleparts:/km/mi|1|2}}|{{#if:{{#titleparts:/km/mi|1|1}}|272/km/mi|{{convert|272|{{#titleparts:/km/mi|1|2}}|{{#titleparts:/km/mi|1|3}}|{{#titleparts:/km/mi|1|4}}|abbr=on}} }}|272/km/mi }} | 272 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 
|-
 
|-
| Mercury    || {{bartable| -38.72|/C/F|2|1em|background:silver}}
+
| Value and scale            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:-1/(2^0.5)|-1/(2^0.5)|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1/(2^0.5)|-1/(2^0.5)|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1/(2^0.5)|-1/(2^0.5)|1}})*{{formatnum:272|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 
|-
 
|-
| Bromine    || {{bartable| -7.10|/C/F|2|1em|background:brown}}
+
| Value and height            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:4px|4px|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:4px|4px|2ex}};" />|}}
 
|-
 
|-
| Water      || {{bartable|     0|/C/F|2|1em}}
+
| Value and style            || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|272|{{convert|272|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|272 }} | 272 }} {{#ifexpr:{{#if:-1|-1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:272|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:||2ex}};background:blue;" />|}} | }}
 +
| {{#ifexpr:{{formatnum:272|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-1|-1|1}})*{{formatnum:272|R}} }}px;height:{{#if:||2ex}};background:blue;" />|}}
 
|-
 
|-
| Phosphorus || {{bartable| 44.10|/C/F|2|1em|background:red}}
+
| Value with commas and scale || {{#if: | {{#if:{{#titleparts:|1|2}}|{{#if:{{#titleparts:|1|1}}|2,720|{{convert|2,720|{{#titleparts:|1|2}}|{{#titleparts:|1|3}}|{{#titleparts:|1|4}}|abbr=on}} }}|2,720 }} | 2,720 }} {{#ifexpr:{{#if:-.1|-.1|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:2,720|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:-.1|-.1|1}})*{{formatnum:2,720|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}} | }}
 +
| {{#ifexpr:{{formatnum:2,720|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:-.1|-.1|1}})*{{formatnum:2,720|R}} }}px;height:{{#if:|{{{4}}}|2ex}};" />|}}
 
|-
 
|-
| Iodine    || {{bartable| 113.50|/C/F|2|1em|background:indigo}}
+
| The full monty              || {{#if:% | {{#if:{{#titleparts:%|1|2}}|{{#if:{{#titleparts:%|1|1}}|18^2%|{{convert|18^2|{{#titleparts:%|1|2}}|{{#titleparts:%|1|3}}|{{#titleparts:%|1|4}}|abbr=on}} }}|18^2% }} | 18^2 }} {{#ifexpr:{{#if:sin(-1)|sin(-1)|0}}<0 | {{!}}{{!}} {{#ifexpr:{{formatnum:18^2|R}}<0 | align="right" {{!}} <hr style="width:{{#expr:-abs({{#if:sin(-1)|sin(-1)|1}})*{{formatnum:18^2|R}} }}px;height:{{#if:2em|2em|2ex}};border:ridge red;" />|}} | }}
 +
| {{#ifexpr:{{formatnum:18^2|R}}>0 | align="left" {{!}} <hr style="width:{{#expr:abs({{#if:sin(-1)|sin(-1)|1}})*{{formatnum:18^2|R}} }}px;height:{{#if:2em|2em|2ex}};border:ridge red;" />|}}
 
|-
 
|-
| Sulphur    || {{bartable| 115.36|/C/F|2|1em|background:yellow}}
 
|}
 
 
|}
 
|}
  

Revision as of 13:42, 5 June 2011

This template adds an inline bar graph to tables with numeric (including negative) values that a reader may wish to compare. It takes up either 2 or 3 columns — determined by the sign of the third (scale) parameter — of a table, so headers should have colspan="2" or colspan="3" (or similar combinations), respectively, to correctly span the columns.

Usage

{{bartable|value|unit|scale|height|style}}
  • value (mandatory) is any real number or math expression that evaluates to a number. Commas can be used to group digits. See Help:Calculation for the syntax for math expressions.
  • unit (default: empty string) is a suffix to be appended to value. Prepend a space to have a space between the value and the unit. If unit starts with "/", the convert template is used as follows:
"/unit1/unit2/round" is translated to "{{convert|value|unit1|unit2|round|abbr=on}}. (Note: unit1, unit2 or round cannot contain "/" or "=". If anyone knows how to allow it (or translate other characters to it), please help.)
  • scale (default: "1") is a factor multiplied by value to give the width of the bar in pixels.
Note: If scale is negative, an additional column (for showing negative values) is created. See example below.
  • height (default: "2ex") is the height of the bar as a CSS measurement e.g. "1em" or "16px".
  • style (default: empty string) is the content of a CSS style string to apply to the bar e.g. "background:red;".

Examples

Real-world example

{| class="wikitable"
|+ Melting point at 1 atmosphere
! Substance  !! Temperature !! colspan="2" | Relative size
|-
| Mercury    || {{bartable| -38.72|/C/F|-2|1em|background:silver}}
|-
| Bromine    || {{bartable|  -7.10|/C/F|-2|1em|background:brown}}
|-
| Water      || {{bartable|      0|/C/F|-2|1em}}
|-
| Phosphorus || {{bartable|  44.10|/C/F|-2|1em|background:red}}
|-
| Iodine     || {{bartable| 113.50|/C/F|-2|1em|background:indigo}}
|-
| Sulphur    || {{bartable| 115.36|/C/F|-2|1em|background:yellow}}
|}
Melting point at 1 atmosphere
Substance Temperature Relative size
Mercury −38.72 °C (−37.70 °F)
 
Bromine −7.10 °C (19.22 °F)
 
Water 0 °C (32 °F)
Phosphorus 44.10 °C (111.38 °F) 44.1
 
Iodine 113.50 °C (236.30 °F) 113.5
 
Sulphur 115.36 °C (239.65 °F) 115.36
 

2-column (only positive values)

{| class="wikitable"
! Example                     || colspan="2" | Rendering
|-
| Only value                  || {{subst:Hidden/sandbox|272}}
|-
| Negative value              || {{subst:Hidden/sandbox|-72}}
|-
| Math expression             || {{subst:Hidden/sandbox|exp(1)*1e2}}
|-
| Value and ordinary unit     || {{subst:Hidden/sandbox|272| km/h}}
|-
| Value and convert unit      || {{subst:Hidden/sandbox|272|/km/nmi}}
|-
| Value and scale             || {{subst:Hidden/sandbox|272||1/(2^0.5)}}
|-
| Value and height            || {{subst:Hidden/sandbox|272|||4px}}
|-
| Value and style             || {{subst:Hidden/sandbox|272||||background:blue;}}
|-
| Value with commas and scale || {{subst:Hidden/sandbox|2,720||.1}}
|-
| The full monty              || {{subst:Hidden/sandbox|18^2|%|sin(1)|2em|border:ridge red;}}
|-
|}
Example Rendering
Only value 272
Negative value -72
Math expression exp(1)*1e2
Value and ordinary unit 272 km/h
Value and convert unit 272 km (147 nmi)
Value and scale 272
Value and height 272
Value and style 272
Value with commas and scale 2,720
The full monty 18^2%

3-column (positive and negative values)

{| class="wikitable"
! Example                     || colspan="3" | Rendering
|-
| Only value                  || {{subst:Hidden/sandbox|272||-1}}
|-
| Negative value              || {{subst:Hidden/sandbox|-72||-1}}
|-
| Math expression             || {{subst:Hidden/sandbox|exp(1)*1e2||-1}}
|-
| Value and ordinary unit     || {{subst:Hidden/sandbox|272| km/h|-1}}
|-
| Value and convert unit      || {{subst:Hidden/sandbox|272|/km/mi|-1}}
|-
| Value and scale             || {{subst:Hidden/sandbox|272||-1/(2^0.5)}}
|-
| Value and height            || {{subst:Hidden/sandbox|272||-1|4px}}
|-
| Value and style             || {{subst:Hidden/sandbox|272||-1||background:blue;}}
|-
| Value with commas and scale || {{subst:Hidden/sandbox|2,720||-.1}}
|-
| The full monty              || {{subst:Hidden/sandbox|18^2|%|sin(-1)|2em|border:ridge red;}}
|-
|}
Example Rendering
Only value 272
Negative value -72
Math expression exp(1)*1e2
Value and ordinary unit 272 km/h
Value and convert unit 272 km (169 mi)
Value and scale 272
Value and height 272
Value and style 272
Value with commas and scale 2,720
The full monty 18^2%