How to organize array to display on graph (Chart)

Asked

Viewed 178 times

0

This graphic aims to list the total monthly accesses by user type of the current year.

It is divided into 3 series, each series represents a type of user, within it has the total of accesses of that type per month:

labels: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
series: [
    [100, 200, 120, 280, 150, 300, 350, 200, 180, 350, 420, 500], // Total Access Users
    [80, 150, 100, 180, 80, 250, 150, 280, 360, 450, 520, 650], // Total artists
    [30, 80, 40, 90, 50, 100, 70, 160, 220, 240, 280, 350], // total studios
]

I tried it this way:

    public function get_monthly_accesses() {

        $current_year = date('Y');
        $responseseries = array();
        for ($j = 1; $j <= 3; $j++) {

            for ($i = 1; $i <= 12; $i++) {
                $query = "select count(date_login) as accesses from user_access as ua
inner join user_details as ud on ud.user = ua.user
inner join user as u on ua.user = u.id
where type = {$j} AND YEAR(date_login) = {$current_year} AND MONTH(date_login) = {$i}";

                $total = $this->db->query($query)->result()[0]->accesses;
                $responseseries[] = $total;
            }

        }

        echo '<pre>';
        var_dump(json_encode($responseseries));
        echo '</pre>';
        die;

        return json_encode($responseseries);
    }

The return is this:

string(147) "["0","0","0","0","0","0","0","0","0","0","7","0","0","0","0","0","0","0","0","0","0","1","13","0","0","0","0","0","0","0","0","0","0","2","10","0"]"

I’m not able to divide this array into 3 to display on the graph.

  • If you group your sql by user type would not solve? pq at the end your array should have at least 3 other arrays with 12 positions. These are chained I didn’t understand...

  • then I would take out the Where type and group by user type? but and the for 3 positions I created, will serve only to assemble the series?

  • Without touching the query change $responseseries[] = $total for $responseseries[$j][] = $total see how the array is mounted.

  • Our almost worked, the array was divided into 3, only it is showing its position in json see https://pastebin.com/VMcG2aGy

  • What’s the problem? I don’t understand.

  • I got it, thank you very much! i have only been calling Response in ajax by the position of the array within series: series: [ Response[1], Response[2], Response[3], ]

  • So for some reason your return is being an object, when it should be an array. Is there anything else in this php code? your json should be something like [[0,0,0,0,0,0,0,0,0,0,7,0], [0,0,0,0,0,0,0,0,0,1,13,0], [0,0,0,0,0,0,0,0,0,2,10,0]] see that the initial caraceter is a clasp and not a key.

  • Yes the return of json should be the one you quoted, in my php code there is nothing else, it’s just that method.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.