[[+content_image]]
V
V
Vladimir2014-10-06 07:38:49
PHP
Vladimir, 2014-10-06 07:38:49

How to visualize data from the Yandex.metrics API in jQuery.flot.chart.js?

Good day, gurus!

The question is what do you need to make a dashboard for the admin panel, I found an interesting library (flot.js) but it wants to take an array of this form [[0,1],[1,3],[2,-4],[3, 2],[4,0.5],[5,0.7],[6,-1],[7,4],[8,2]], where the first is the y-axis and the second is x. But, the J.M API gives json like this:

array(5) {
  [0]=>
  object(stdClass)#5 (11) {
    ["wday"]=>
    int(0)
    ["denial"]=>
    int(0)
    ["visits"]=>
    int(2)
    ["new_visitors_perc"]=>
    int(0)
    ["page_views"]=>
    int(13)
    ["date"]=>
    string(8) "20141006"
    ["visit_time"]=>
    int(1243)
    ["depth"]=>
    float(6.5)
    ["new_visitors"]=>
    int(0)
    ["visitors"]=>
    int(1)
    ["id"]=>
    string(8) "20141006"
  }
  [1]=>
  object(stdClass)#6 (11) {
    ["wday"]=>
    int(6)
    ["denial"]=>
    int(0)
    ["visits"]=>
    int(2)
    ["new_visitors_perc"]=>
    int(0)
    ["page_views"]=>
    int(2)
    ["date"]=>
    string(8) "20141005"
    ["visit_time"]=>
    int(15)
    ["depth"]=>
    int(1)
    ["new_visitors"]=>
    int(0)
    ["visitors"]=>
    int(2)
    ["id"]=>
    string(8) "20141005"
  }
  [2]=>
  object(stdClass)#7 (11) {
    ["wday"]=>
    int(5)
    ["denial"]=>
    int(0)
    ["visits"]=>
    int(1)
    ["new_visitors_perc"]=>
    int(0)
    ["page_views"]=>
    int(1)
    ["date"]=>
    string(8) "20141004"
    ["visit_time"]=>
    int(16)
    ["depth"]=>
    int(1)
    ["new_visitors"]=>
    int(0)
    ["visitors"]=>
    int(1)
    ["id"]=>
    string(8) "20141004"
  }
  [3]=>
  object(stdClass)#8 (11) {
    ["wday"]=>
    int(4)
    ["denial"]=>
    int(0)
    ["visits"]=>
    int(1)
    ["new_visitors_perc"]=>
    int(0)
    ["page_views"]=>
    int(1)
    ["date"]=>
    string(8) "20141003"
    ["visit_time"]=>
    int(15)
    ["depth"]=>
    int(1)
    ["new_visitors"]=>
    int(0)
    ["visitors"]=>
    int(1)
    ["id"]=>
    string(8) "20141003"
  }
  [4]=>
  object(stdClass)#9 (11) {
    ["wday"]=>
    int(3)
    ["denial"]=>
    int(0)
    ["visits"]=>
    int(2)
    ["new_visitors_perc"]=>
    int(1)
    ["page_views"]=>
    int(62)
    ["date"]=>
    string(8) "20141002"
    ["visit_time"]=>
    int(5391)
    ["depth"]=>
    int(31)
    ["new_visitors"]=>
    int(2)
    ["visitors"]=>
    int(2)
    ["id"]=>
    string(8) "20141002"
  }
}

I need visits and date values ​​to display visitors. But how to pull them out and put them in the array that flot needs?
PS: I apologize for such a chaotic description of the task, my thoughts are confused because I haven’t slept for a day!

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
G
Gregory, 2014-10-06
@difiso

Something like this (wrote without checking, there may be errors)

<?php
$result = array_map(
    function ($source){
        $data = (array)$source;
        return array($data['visits'], $data['date']);
    },
    $yandex_data);
?>

Read more here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question