Page 1 of 1

Pie charts SQLite Data

Posted: Wed Feb 24, 2021 3:23 pm
by weado
Hello,
I used highcharts since a long time to do the line charts, but I would like to make a simple pie charts. I'm looking for a long time on this forum, but I didn't find the solution.
This is my data.php :

Code: Select all

<?php

try {

    $db = new SQLite3('../../cost.db');

    $reponse = $db->query("SELECT name, sum(total) FROM cost");

    $rows = array();
    $rows['name'] = 'name';
    $rows1 = array();
    $rows1['name'] = 'total';

    while ($valeur = $reponse->fetchArray())
    {
        $rows['data'][] = $valeur['name'];
        $rows1['data'][] = $valeur['sum(total)'];
    }

} catch(Exception $e) {
    echo "ERROR DATABASE";
    die();
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);

print json_encode($result, JSON_NUMERIC_CHECK);

 ?>
 


This file work well. And this is my JS file (graphe.js)

Code: Select all

$(function () {
    $.getJSON('data.php', function (data) { 

        $('#graphe').highcharts({
            chart: {
                 type: 'pie'
            },
            title: {
                text: 'Cost'
            },
            credits: {
                enabled: false
            },
            legend: {
                enabled: false
            },

            yAxis: [{
            title: {
                enabled: false
            },
            tickAmount: 11,
            labels: {
                format: '{value}€',
            }

            }],

            plotOptions: {
                series: {
            borderRadius: 8,
            dataLabels: {
                enabled: true,
                format: '{point.y:,.2f} €',
            }
        }

                            },
            series: [{
                name: 'Cost',
                    data: [{name: data[0].data, y: data[1].data}]
            }]
        });
    });
});
Thank you for your help :D

Re: Pie charts SQLite Data

Posted: Thu Feb 25, 2021 7:15 am
by dominik.c
Hello weado!

Welcome to our forum and thanks for contacting us with your question!

As far as I understand your PHP code works fine, correct? If it doesn't work correctly I suggest asking your question on SO, on the forum we focus on providing support related directly to Highcharts. :-)

Do you have any errors in your console? Are you using export server in your project? I reproduced your issue in an online editor and it works fine: https://jsfiddle.net/BlackLabel/50f7kpzd/

Please provide me some more details and make sure that PHP code works fine and you've got your data in your JS file.

Best regards!

Re: Pie charts SQLite Data

Posted: Thu Feb 25, 2021 8:06 am
by weado
Hello Dominik ! Thanks for your help.

Correct, my PHP code works well with this result :

Code: Select all

[{"name":"name","data":["Article1","Article2","Article3","Article4","Article5"]},{"name":"total","data":[541,924.48,99,330.3,305.7]}]
Unfortunately, I don't have a message when I launch my page, but, I just have a circle on the page, as you see.

Image
https://ibb.co/c8Q6cHV

Re: Pie charts SQLite Data

Posted: Thu Feb 25, 2021 1:21 pm
by dominik.c
Hi again!

It probably happens because of your data. When we don't have any data in our chart we've got a blank circle as a result.

Demo: https://jsfiddle.net/BlackLabel/r8cg57qk/

I suggest checking in data.complete function if your data is correctly provided to the chart.

API references:
https://api.highcharts.com/highcharts/data.complete

Demo:
https://jsfiddle.net/BlackLabel/ehfqpoL7/

Best regards!

Re: Pie charts SQLite Data

Posted: Mon Mar 01, 2021 11:45 am
by weado
That's crazy because, if I put just the data (without name of cost), it works without problem.
Image.


I'll try different things.

Re: Pie charts SQLite Data

Posted: Tue Mar 02, 2021 7:40 am
by dominik.c
Hi again!

Maybe Highcharts didn't read the name values properly and that was the reason why the chart crashed. Good luck with your project.

In case of any further questions, feel free to contact us again.