Menu

Pie chart only works if the data totaling 100

2012-02-14
2012-09-07
  • Eduardo Donato

    Eduardo Donato - 2012-02-14

    My pie chart plot doesn't work for any values, only when the data array
    totaling 100.

    This data array doens't work:

    $data = array(
    array('Australia', 7849),
    array('Dem Rep Congo', 299),
    array('Canada', 5447),
    array('Columbia', 944),
    array('Ghana', 541),
    array('China', 3215),
    array('Philippines', 791),
    array('South Africa', 19454),
    array('Mexico', 311),
    array('United States', 9458),
    array('USSR', 9710),
    );
    

    But this woks fine:

    $data2 = array(
    array('Australia', 20),
    array('Dem Rep Congo', 5),
    array('Canada', 5),
    array('Columbia', 10),
    array('Ghana', 10),
    array('China', 10),
    array('Philippines', 10),
    array('South Africa', 10),
    array('Mexico', 10),
    array('United States', 10),
    );
    

    When I use the first array, I get a empty image, but no errors. The second
    array works fine, even when I use the pie chart example code from
    documentation (item 5.8).

    Am I missing some configuration?

     
  • Eduardo Donato

    Eduardo Donato - 2012-02-14

    Using the command line, I get this error message:

    C:\xampp\htdocs\SisPlan>..\..\php\php.exe seguranca_grafico_pizza.php > teste.png
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 26071
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1612
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\S
    isPlan\phplot\phplot.php on line 1613
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1614
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1615
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1622
    

    This is about decimal and thousand number separator.

     
  • lbayuk

    lbayuk - 2012-02-14

    Hi,

    There is no requirement that the data add up to 100. Each segment of the pie
    is plotted as a percentage of the total of all segments. So something else is
    going wrong here.

    I get a good pie chart plot with your data array, data type 'text-data-
    single', plot type 'pie'.

    If you are using that exact data array you include above, I don't see how
    decimal and thousands separator could be the issue, but let's find out. First,
    I need your PHPlot version so I can figure out where that error on line 2601
    is. Also, what is your system locale set to?

    What about pie chart labels - are you using the new SetPieLabelType() to
    display other than the default labels? That is the only way I see decimal and
    thousands separators playing a role.

    (FYI, re: your other comment about arc angle must be greater than 1 degree, it
    is in the manual, but it isn't completely accurate. The actual truth is that
    arcs with ((int)(360-start_angle) - (int)(360-end_angle)) < 1 wil be skipped.
    That is not quite the same as saying the arc angle must be 1 degree or
    greater. See also this bug report, including the coments.)

     
  • Eduardo Donato

    Eduardo Donato - 2012-02-15

    I am using XAMPP 1.7.4 (PHP 5.3.5) and PHPlot 5.6.0. My Windows is in
    Portuguese and decimal separator is "," (comma) and thousand is "." (point).

    Even I use the example from documentation doesn't works.

    <?php
    # PHPlot Example: Pie/text-data-single
    require_once './phplot/phplot.php';//mude de acrodo com a sua situação
    # The data labels aren't used directly by PHPlot. They are here for our
    # reference, and we copy them to the legend below.
    
    $data1 = array(
    array('Australia', 20),
    array('Dem Rep Congo', 5),
    array('Canada', 5),
    array('Columbia', 10),
    array('Ghana', 10),
    array('China', 10),
    array('Philippines', 10),
    array('South Africa', 10),
    array('Mexico', 10),
    array('United States', 10),
    );
    
    $data = array(
    array('Australia', 7849),
    array('Dem Rep Congo', 299),
    array('Canada', 5447),
    array('Columbia', 944),
    array('Ghana', 541),
    array('China', 3215),
    array('Philippines', 791),
    array('South Africa', 19454),
    array('Mexico', 311),
    array('United States', 9458),
    array('USSR', 9710),
    );
    
    $plot = new PHPlot(800,600);
    $plot->SetImageBorderType('plain');
    $plot->SetPlotType('pie');
    $plot->SetDataType('text-data-single');
    $plot->SetDataValues($data);
    # Set enough different colors;
    $plot->SetDataColors(array('red', 'green', 'blue', 'yellow', 'cyan',
    'magenta', 'brown', 'lavender', 
    'gray', 'orange'));
    # Main plot title:
    $plot->SetTitle("World Gold Production, 1990\n(1000s of Troy Ounces)");
    # Build a legend from our data array.
    # Each call to SetLegend makes one line as "label: value".
    foreach ($data as $row)
        $plot->SetLegend(implode(': ', $row));
    # Place the legend in the upper left corner:
    $plot->SetLegendPixels(5, 5);
    $plot->DrawGraph();
    

    I am not a professional programmer. I am learning how to use PHP as a hobby.

    Sorry my bad English.

    Ibayuk, thank you for your time.

     
  • Eduardo Donato

    Eduardo Donato - 2012-02-15

    the example in the previous post, If I use the $data array doesn't work, but
    the $data1 works fine.

    To $data I get this errors messages:

    C:\xampp\htdocs\SisPlan>..\..\php\php.exe teste_grafico.php > teste.png
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Notice:  A non well formed numeric value encountered in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 2601
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1612
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1613
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1614
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1615
    PHP Warning:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\SisPlan\phplot\phplot.php:2601) in C:\xampp\htdocs\SisPlan\phplot\phplot.php on line 1622
    
     
  • lbayuk

    lbayuk - 2012-02-15

    I tried your code from post 4 above and it worked, with both $data and $data1.
    But this is on Linux, with the en_US locale. Let me try this on a Windows
    system and see if I can duplicate your locale. Probably not. But based on the
    error and line number, something is going wrong with locale-based number
    conversion of the pie chart labels.

    In the meantime, could you try adding this line to your script, anywhere
    between creating the PHPlot object and before you call DrawGraph():

    $plot->SetPieLabelType('percent', 'printf', '%.1f%%');
    

    This bypasses the locale-based number conversion (label format type 'data')
    and instead uses printf-type formatting. The output should be the same as you
    would get with the default (if it worked), except I am not 100% sure you will
    get a comma decimal separator. Let me know if it works, please.

    This isn't intended to fix the problem, but should tell me if we are on the
    right track. Even if the above does work, I hope you will be willing to help
    me continue to trouble-shoot this, as I am going to need help if I cannot
    duplicate the environment and locale.

     
  • lbayuk

    lbayuk - 2012-02-15

    Progress.

    I duplicated the problem on a Windows PC, after setting my locale. (There are
    2 choices for Portuguese - Brazil or Portugal - I guessed Brazil, but looking
    at your post times now, I think I guessed wrong. But I still get the error.)

    Even better, I duplicated it on Linux, so I can debug on my Linux PC instead
    of Windows, and also means it isn't a Windows problem.

    So I get the same error as you when it tries to format the pie chart data
    labels, if I use the $data array. With the $data1 array, I do not get the
    error, and I know why: in $data1, the labels are all whole numbers (integers),
    and there are no decimal points in the results.

    The bad news, for me, is that the problem does not occur with the previous
    release PHPlot-5.5.0. This means it is a bug introduced in 5.6.0 that was not
    caught by my testing. (I do test different locales, but just unit tests, not
    complete plots.)

    Now I have to go figure out what broke.

     
  • lbayuk

    lbayuk - 2012-02-16

    Well, I found the problem, but I have no idea how to fix it yet.

    In the meantime, here is a more solid work-around. Instead of using
    SetPieLabelType() to select 'printf' type as I suggested above, use this:

    $plot->SetNumberFormat(',', '.');
    

    This explicitly sets the decimal separator to comma, and thousands separator
    to dot. It bypasses the PHP functions used in PHPlot to set the locale-
    dependent values, which seems to be the source of the trouble.

    (The problem is that the value for the pie label is getting converted to a
    string, as a side-effect of supporting multiple label sources, then back to
    double so it can be formatted. When it is converted to string, it uses your
    locale's decimal separator. The conversion back to double fails if the string
    contains a comma, even if that is your locale's decimal point. This only
    affects pie labels because of the double-to-string and back conversion.

    To me, this is a PHP bug: you can't do round-trip double to string and back
    conversion in a locale that uses comma for decimal. I found several existing
    PHP bug reports on this, including #45365. The bug reports go back 4-8 years,
    are still open, with no comments at all. So I need to find a work-around.)

     
  • Eduardo Donato

    Eduardo Donato - 2012-02-16

    lbayuk,

    Thank you for your time. I use your last suggestion and works well.

     
  • lbayuk

    lbayuk - 2012-02-24

    As I indicated in the comments on this bug report, I have 2 partial
    fixes for this which will be in the next release (soon). It isn't perfect, but
    should work a lot better.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.