Menu

#162 Pie label failures in locales with comma decimal

closed
lbayuk
None
7
2012-09-07
2012-02-16
lbayuk
No

Reference: https://sourceforge.net/projects/phplot/forums/forum/46382/topic/5019387
Problem discovered by cujobr

In a locale which uses comma for decimal point (e.g. pt_BR, fr_CA, and many more, pie charts are failing at PHPlot-5.6.0 with this message: PHP Notice: A non well formed numeric value encountered in ... line 2601

The plot works OK in the previous release PHPlot-5.5.0.

Discussion

  • lbayuk

    lbayuk - 2012-02-16

    The new code to support multiple pie label sources (e.g. value and label) does an implicit conversion from double to string, via implode(). Then it gets converted back from string to double to format it. The problem is that in these locales the first conversion is using comma for decimal, and the second conversion fails because it does not accept the locale-specified decimal mark.

    This is a known issue with PHP. See this PHP bug report from the year 2008: https://bugs.php.net/bug.php?id=45365
    There are others, even older, but no apparent action. So PHPlot needs to find a work-around.

     
  • lbayuk

    lbayuk - 2012-02-17

    I have developed 2 separate fixes. Neither is a complete fix to the underlying problem, but each does fix the most common cases of the error. I will probably put both fixes into the release.

    1) Have the number_format method be much more conservative dealing with the locale. Use LC_NUMERIC instead of LC_ALL, and save the current setting before loading the environment or system settings, then restore the saved locale setting after getting the decimal and thousands separator.

    This fix means PHPlot itself won't leave the loaded locale settings in place, which causes PHP to convert numbers into strings that it is unable to convert back. (For example: 123.45 to "123,45" to either an error or 123, losing the fractional part.) However, if your application has loaded the locale settings with setlocale(LC_ALL, ''), then this fix won't help.

    2) When formatting a pie chart label, PHPlot-5.6.0 handles the case of a single source (e.g. "percent") the same as multiple sources (e.g. array("value","label")) - it builds an array of values, then implodes() them into a string. With this fix applied, if there is a single source, rather than an array with multiple sources, the implode() step will not happen. This avoids a number-to-string conversion, which was causing the locale-dependent separators to be used, which resulted in the number formatting failure. So this fixes the problem, whenever there is a single source for the pie label. If there are multiple sources, you should be using a custom function to split them up, and you are on your own as to formatting them.

     
  • lbayuk

    lbayuk - 2012-02-17

    Fixes have been applied in Subversion and will be in the next release.

    Changes described in the preceeding comment have been applied. The two fixes partly overlap in the areas they cover, and together they should solve most of the problem. There are still serious, underlying issues with PHP (see PHP bug reports 45356, 33988, 26992, etc.).

     
  • lbayuk

    lbayuk - 2012-02-26

    Fixed in PHPlot-5.7.0

     

Log in to post a comment.