Sarah
Sarah

Reputation: 131

Quotes error in Google chart "Data column(s) for axis #0 cannot be of type string"

I know this question has been asked many times but I have not really found a solution for my code. I choose supplier and year and i want to display a list of all products. For some suppliers it works but for others no.

After a many tests I found that my code works for all suppliers except suppliers name with quotes and acccents.

Here is my error:

"Data column(s) for axis #0 cannot be of type string"

code:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {
        packages: ["corechart"]
    });
    google.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['Month', 'Product', 'Items'],
            <?php foreach ($my_data as $a => $b): ?> ['<?php echo $month[$b['
                month ']]?>', <?php echo $b['product'] ?>, <?php echo $b['items'] ?>,
            ],
            <?php endforeach; ?>
        ]);

        var options = {
            title: 'supplier',
            hAxis: {
                title: 'product supplier',
                titleTextStyle: {
                    color: '#333'
                }
            },
            vAxis: {
                minValue: 0
            }
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }
</script>

This's my query :

 if(isset($_POST['$value1'] ))
    {

    $month = array('','January','February ','March','April','May','June','July','August','September','October','November','December');
    $value1 = $_POST['$value1'] ;
    $value2 = $_POST['$value1'] ;
    $sql="  SELECT

                name,
                count(items),
                MONTH(created_at) as month,
                YEAR(created_at) as year,
                sum(product) as product

            FROM `supplier_product`
            where 
                name = '".$value1."' and YEAR(date.created_at) = '".$value2."'
                GROUP BY name,month
                ORDER BY month ASC
        ";

This my code PHP :

            <label for="supplier"> Select name : </label>
                                        <select name = "value1" id="sup">
                                            <?php
                                            $sql = "SELECT name FROM supplier_product";

                                            $req = $mydatabase->prepare($sql);
                                            $req->execute();
                                            $data = $req->fetchAll(PDO::FETCH_ASSOC);
                                            foreach ($data as $a => $b):

                                            ?>
                                            <option value="<?php echo utf8_encode($v['name']); ?>"><?php echo utf8_encode($v['name']); ?></option>

                                             <?php
                                            endforeach;
                                                ?>
                                        </select>
                                        <select name= "value2">
                                            <option value="2015">2015</option>
                                            <option value="2014">2014</option>
                                            <option value="2014">2013</option>
                                            <option value="2014">2012</option>
                                            <option value="2014">2011</option>
                                            <option value="2014">2010</option>
                                            <option value="2014">2019</option>
                                        </select>

Help me please. Many Thanks for any help.

Upvotes: 0

Views: 1242

Answers (2)

Sarah
Sarah

Reputation: 131

Here is the solution of my question I added addslashes and utf8_decode it works now.

$value1 = addslashes(utf8_decode($_POST['value1 ']));

Upvotes: 2

Sotiris Kiritsis
Sotiris Kiritsis

Reputation: 3336

Put your php variables in javascript variables, then parse them as integers and it should work. Google Charts cannot perform calculations with strings.

EDIT: added example. I was thinking about something like this(note i meant converting to integers only numbers not names). Seems like your problem is different thought.

var month = parseInt(<?= $month; ?>, 10);

EDIT2: added second example.

var products = [];
//your loop here
products[key] = <?= $b['product']; ?>;
products[key].replace(/["']/g, "");
//end loop

And then use the javascript variable 'products' in your charts.

Upvotes: -1

Related Questions