weado
Posts: 4
Joined: Wed Feb 24, 2021 2:58 pm

Pie charts SQLite Data

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
dominik.c
Posts: 2081
Joined: Fri Aug 07, 2020 7:07 am

Re: Pie charts SQLite Data

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!
Dominik Chudy
Highcharts Developer
weado
Posts: 4
Joined: Wed Feb 24, 2021 2:58 pm

Re: Pie charts SQLite Data

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
dominik.c
Posts: 2081
Joined: Fri Aug 07, 2020 7:07 am

Re: Pie charts SQLite Data

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!
Dominik Chudy
Highcharts Developer
weado
Posts: 4
Joined: Wed Feb 24, 2021 2:58 pm

Re: Pie charts SQLite Data

That's crazy because, if I put just the data (without name of cost), it works without problem.
Image.


I'll try different things.
dominik.c
Posts: 2081
Joined: Fri Aug 07, 2020 7:07 am

Re: Pie charts SQLite Data

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.
Dominik Chudy
Highcharts Developer

Return to “Highcharts Stock”