SetXLabelType — Set formatting type for X tick labels
$plot->SetXLabelType($type
, [...
])
SetXLabelType
sets the formatting type for X tick labels,
and the default formatting type for X data labels.
(If SetXDataLabelType is never called,
SetXLabelType
effectively sets the formatting type
for both X tick labels and X data labels.)
By default, there is no special formatting, so the labels are output as-is.
Available format types are 'data', 'time', 'printf', and 'custom'.
'data' formatting formats the labels as floating point numbers, with digits grouped into thousands (3 digit groups), and with user-defined precision. Grouping separator characters can be set with SetNumberFormat. The precision (number of digits after the decimal point) can be set with SetPrecisionX, or as an additional argument to SetXLabelType. A prefix and suffix string can also be specified.
'time' formatting formats the labels as date/time values, using a format specifier set by SetXTimeFormat or using an additional argument to SetXLabelType.
'printf' formatting formats the labels using the standard
sprintf
function. One, two, or three format strings
are specified as additional arguments to SetXLabelType.
'custom' formatting formats the labels using a caller-provided function, with an optional pass-through argument. This provides the maximum flexibility in formatting labels.
There is one required argument, $type. Other arguments depend on the value of the $type argument.
$type
A string indicating the desired formatting mode: 'data', 'time', 'printf', or 'custom'. Or, an empty string meaning revert to no formatting.
For type 'data', there are three optional arguments:
$precision
The formatting precision, or number of decimal places (optional). If omitted, the value set with SetPrecisionX is used, or if that was never called then the default is 1.
$prefix
A prefix string to be placed before the formatted label values. This could be used for a currency symbol, for example. The default is an empty string.
$suffix
A suffix string to be placed after the formatted label values. This could be used for a currency symbol, for example. The default is an empty string.
For type 'time', there is one optional argument:
$format
Format string, used with strftime()
.
For example, '%Y-%m-%d' results in formatting a time_t
value as a year, month, and day numbers.
If omitted, the value set with SetXTimeFormat is used,
or if that was never called then the default is '%H:%M:%S' (hours, minutes,
and seconds).
For type 'printf', there can be one, two, or three optional arguments:
$format1
,
[$format2
,
[$format3
]]
Format string(s), used with sprintf()
.
The format string(s) must contain at most one conversion specification
(%-code) which consumes a single argument.
If no format strings are specified, the default value of '%e' uses scientific
notation with default field sizes.
If a single format string is given ($format1
),
it is used for all label values.
If two format strings are given ($format1, $format2
),
then the first string $format1
is used to format the value
of the label if it is greater than or equal to zero.
The second string $format2
is used to format
the absolute value of the label if it is less than zero.
If three format strings are given ($format1, $format2,
$format3
),
then the first string $format1
is used to format the value
of the label if it is greater than zero.
The second string $format2
is used to format
the absolute value of the label if it is less than zero.
The third string $format3
is used when the value of the
label is zero.
For type 'custom', there is one required argument and one optional argument:
$callback
A callback function to format the label. This is either the name of a function (as a string), or a two-element array with an object instance and method name. (Refer to the PHP documentation for more information on the callback type.) The callback will be called with two, three, or four arguments: the value of the label to be formatted, the pass-through argument (see next), and the row and column of the data point (if applicable to the label type). See Section 3.6.5.5, “Formatting Labels: Extended 'custom' type” for more on the row and column arguments supplied to the callback.
$callback_arg
A pass-through argument for the callback function. If omitted, NULL is used.
See Section 3.6, “Labels” for more information on labels, and specifically Section 3.6.5, “Formatting Labels” for more information on formatting labels.
The default formatting mode is to do no special formatting of the labels. Strings will be output as-is, and numbers will be output using PHP's default formatting. If you need to change label formatting back to the default, use SetXLabelType without arguments, or with an empty string argument.
A side effect of SetPrecisionX is to call this function SetXLabelType and set the format type to 'data'. Note that SetXTimeFormat does not have a corresponding side effect on the format type.
When using a custom label formatting function, do not assume the labels are formatted in any particular order, or only once each.
When using 2 or 3 'printf' format strings, the labels being formatted must be numeric values.
When using 2 or 3 'printf' format strings, the second one is used to format the absolute value of the label, so you generally must provide some indication in the format string that the value is negative.
The following tables show some label formatting examples.
These also apply to SetXLabelType
,
SetXDataLabelType, SetYLabelType,
and SetYDataLabelType.
Data (numeric) formatting with two digits of precision. Grouping and decimal separators depend on locale.
Code: | Value: | Result: |
---|---|---|
$plot->SetXLabelType('data', 2); | 1234.56 | 1,234.56 |
3.14159 | 3.14 |
Data (numeric) formatting with prefix. € is the entity code for the Euro sign in Unicode. (Numeric entity codes are handled by the GD library, but not named character entity codes.) Here we use it as a prefix, common usage for English. The Euro sign may appear differently in your browser. But when used with PHPlot it requires a Unicode font on the server.
Code: | Value: | Result: |
---|---|---|
$plot->SetXLabelType('data', 0, '€'); | 1000000 | €1,000,000 |
Data (numeric) formatting with suffix. Here we use the Euro as a suffix, common usage for French, represented by a 3 byte UTF code. You can use html_entity_decode() with UTF-8 as the character set to translate € into this sequence. SetNumberFormat is used here to override the locale settings for thousands and decimal separators.
Code: | Value: | Result: |
---|---|---|
$plot->SetNumberFormat(',', '.'); $plot->SetXLabelType('data', 2, '', "\xe2\x82\xac"); | 1e6 | 1.000.000,00€ |
4321.123 | 4.321,123€ |
Date/time formatting. The given value is mktime(0,0,0,4,15,2008). The format string could be set with SetXTimeFormat instead.
Code: | Value: | Result: |
---|---|---|
$plot->SetXLabelType('time', '%m/%Y'); | 1208232000 | 04/2008 |
Formatting using printf. Note PHP printf may differ from the standard C library. For example, PHP outputs only a one digit exponent here.
Code: | Value: | Result: |
---|---|---|
$plot->SetXLabelType('printf', '%8.2e'); | 1234 | 1.23e+3 |
When two format strings are used with printf formatting, the first is used for non-negative values, and the second for negative values (with the absolute value formatted).
Code: | Value: | Result: |
---|---|---|
$plot->SetXLabelType('printf', '%.2f', '(%.2f)'); | 15.6 | 15.60 |
-9.87 | (9.87) |
When three format strings are used with printf formatting, the first is used for positive values, the second for negative values (with the absolute value formatted), and the third for zero. You can suppress zero with an empty third format string.
Code: | Value: | Result: |
---|---|---|
$plot->SetXLabelType('printf', 'GAIN:$%.2f', 'LOSS:($%.2f)', '[Unchanged]'); | 9.1 | GAIN:$9.10 |
-26.35 | LOSS:($26.35) | |
0 | [Unchanged] |
In this example, a custom formatting function is used to format values in decimal degrees as degrees, minutes, and seconds. (This only works for non-negative angles.)
Code: | Value: | Result: |
---|---|---|
function deg_min_sec($value) { $deg = (int)$value; $value = ($value - $deg) * 60; $min = (int)$value; $sec = (int)(($value - $min) * 60); return "{$deg}d {$min}m {$sec}s"; } $plot->SetXLabelType('custom', 'deg_min_sec'); | 75.12345 | 75d 7m 24s |
0 | 0d 0m 0s | |
136.5 | 136d 30m 0s |
The 'printf' label format type was extended to support 2 or 3 format strings in PHPlot-6.2.0. Before that release, only a single format string could be used.
Custom label formatting functions are passed the row and column arguments (if applicable) starting with PHPlot-5.8.0.
Through PHPlot-5.0.7, this function set the format type for both X tick
labels and X data labels.
Starting with PHPlot-5.1.0, a new function SetXDataLabelType
was added to allow separate control of tick and data labels.
SetXLabelType
now sets the format type for X tick labels,
and the default format type for X data labels.
New label format types 'printf' and 'custom' were added at PHPlot-5.0.6,
as well as all arguments after the first. In PHPlot-5.0.5 and earlier, you
must use SetXTimeFormat and SetPrecisionX to set the formatting parameters.
Starting with PHPlot-5.0.6, you have the choice of using those, or
providing additional arguments to SetXLabelType.
Also added was the ability to add a prefix and suffix to 'data' formatted
labels. In PHPlot-5.0.5 and earlier, there was an undocumented class variable
data_units_text
that was applied as a suffix to 'data'
mode labels, for both X and Y. This continues to work, but is deprecated.
Starting with PHPlot-5.0.6, you can use an empty string or no argument at all to reset to the default of no formatting.
Starting with PHPlot-5.0.4, empty string data labels are ignored when formatting with 'data' or 'time' formats. You can use this to suppress some data labels, or control label density, with 'data' and 'time' formatted labels.
Through PHPlot-5.0rc3, empty strings would still be formatted. With 'data' format, an empty string would result in a zero value, and with 'time' format an empty string would cause an error. As a result, with older releases, if you don't want to use data labels when using 'data' or 'time' formats, you must turn off X data label display with SetXDataLabelPos, even if your data array labels are empty strings.
Through PHPlot-5.0rc3, when the formatting mode is 'data' the thousands grouping separator was always a comma, and a period was used as a decimal point. Starting with 5.0.4, PHPlot attempts to get the correct values for your locale. You can set the separator characters yourself instead with SetNumberFormat.
This version of the manual was produced for the
PHPlot Sourceforge project web service site, which requires the logo on each
page.
To download a logo-free copy of the manual, see the
PHPlot project downloads
area.