-->

HighCharts pie chart X-axies values are not displa

2020-08-01 07:16发布

问题:

I am stuck at data connect from mysql database to HighCharts

highchart.js code below

            <script type="text/javascript">
    $(function () {
        var chart;
        $(document).ready(function() {
            chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container5',
                    plotBackgroundColor: null,
                    plotBorderWidth: 2,
                    plotShadow: false
                },
                title: {
                    text: '<p><?php echo $chart5; ?></p>'
                },
                // tooltip: {
                    // pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                    // percentageDecimals: 1
                // },
               plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: false
                        },
                        showInLegend: true
                    }
                },


                    xAxis: {
             categories: [<?php echo "'".implode("','",$data)."'"; ?>]
          },
          series: [{
           type: 'pie',
             data: [<?php echo implode(",",$data1);  ?>] 

                }]
            });
        });

    });

        </script>

and my database.php:

            $data = array();
        $sql = "SELECT x_axis FROM licence_chart ";
         $result9 = mysql_query($sql);
                $data9 = array();
                while ($row = mysql_fetch_array($result9)) {
                   $data9 = $row['x_axis'];
                   $data[] = $data9;
                }

        $data1 = array();
        $sql = "SELECT y_axis FROM licence_chart ";
         $result10 = mysql_query($sql);
                $data10 = array();
                while ($row = mysql_fetch_array($result10)) {
                   $data10 = $row['y_axis'];
                   $data1[] = $data10;
                }

                echo "'".join("','",$data)."'";
                echo join(",",$data1);

when I run this code in localhost then pie chart shows but in x-axies values are not shown that values are shown like Slice .but y-axies value are display correctly ,

in licence_chart table data like this in table x_axies, y_axies are columns

x_axies{crome,opera,ie,firefox,safari}
y_axies{0.12,0.23,23.2,56.2,2}

i want inhighchart.js file in data[] data will come like this

['safari',10], ['firefox',1.5],['ie',0.5]

so What am I doing wrong in database.php and highchatr.js file please tell me and correct my code .

回答1:

this correct answer for above question , i am using this and get above format

<php
    $sql = "SELECT x_axis,y_axis FROM licence_chart ";
    $result9 = mysql_query($sql);
    $data9 = array();
    while ($row = mysql_fetch_array($result9)) 
        {
            $data9 = "'".$row['x_axis']."'".",".$row['y_axis'];
            $data[] = $data9;
        }   
    for($ii=0;$ii<count($data);$ii++)
        {
            echo "[".$data[$ii]."]," ;  
        }   
?> 


回答2:

Like the error says: http://www.highcharts.com/errors/14

Highcharts Error #14 String value sent to series.data, expected Number

This happens if you pass in a string as a data point, for example in a setup like this:

series: [{ data: ["3", "5", "1", "6"] }]

Highcharts expects the data values to be numbers. The most common reason for this is that data is parsed from CSV or from a XML source, and the implementer forgot to run parseFloat on the parsed value.

For performance reasons internal type casting is not performed, and only the first value is checked (since 2.3).

Your problem is your echo join($data1, "','");. The yAxis values cannot be a string. And it looks like you are just getting one giant string:

'Safari','Opera','Firefox','IE','Chrome','Others10','6','40.5','20','10.6','0.5

By doing this join operation you are adding single quotes around your yAxis numbers. Simply make it:

echo join($data1, ",");

This should fix part of your issue. If you had read the error page you would have seen this - not a knock on you, but HighCharts has pretty great documentation and error reports.

To get this in the correct form you are also going to need to make distinct series elements with [name, yValue]. What you have is just a string of [name, name, name,...., yVal, yVal, yVal..].

How to parse that? Up to you really. But why not just do a SELECT '[''' + x_axis + '',' + CONVERT(VARCHAR(10), y_axis) + ']' FROM licence_chart then do a join like this:

echo join($data1, ",");

This should give you (not sure on mySQL syntax on doing text concatenation with numbers so I leave that to you. This is in MS SQL):

['Safari', 6], ['Opera', 40.5], ['Firefox', 20], ['IE', 10.6], ['Chrome', 0.5], ['Others10', <some number...looks like it got cut off in your question>]


回答3:

<?php

// Set the JSON header header("Content-type: text/json"); // The x value is the current JavaScript time, which is the Unix time multiplied by 1000. $x = time() * 1000; // The y value is a random number $y = rand(0, 100); // Create a PHP array and echo it as JSON $ret = array($x, $y); echo json_encode($ret); ?>

this is answer from HIGHCHARTS install instructions