简体   繁体   中英

Format data for Google Charts in PHP

I am preparing data from Google Charts, but unable to create formatted data array for Google Chart.

I have array for object in this format:

[
    {
        "total_amount": 6572.67,
        "o_date": "2020-06-01",
        "storeName": "Store1"
    },
    {
        "total_amount": 314.21,
        "o_date": "2020-06-01",
        "storeName": "Store2"
    },
    {
        "total_amount": 5550.34,
        "o_date": "2020-06-02",
        "storeName": "Store1"
    },
    {
        "total_amount": 461.87,
        "o_date": "2020-06-02",
        "storeName": "Store2"
    },
    {
        "total_amount": 6471.37,
        "o_date": "2020-06-03",
        "storeName": "Store1"
    },
    {
        "total_amount": 547.99,
        "o_date": "2020-06-03",
        "storeName": "Store2"
    },
]

Google Chart needs data in this format:

['Store', 'Store1', 'Store2', { role: 'annotation' } ],
['2020-06-01', 6572.67, 314.21, ''],
['2020-06-02', 5550.34, 461.87, ''],
['2020-06-03', 6471.37, 547.99, '']

I tried different ways to make new array inside foreach loop, but could not format the array. Can anyone please suggest me the right way? Thank you.

Update

Thank you everyone for you comments and answers.

Yes, I am using PHP/Laravel.

To fetch the orders from database my query is:

$orders =   $orders->select(DB::raw('SUM(orders.quantity) as total_quantity'), DB::raw('SUM(orders.totalAmount) as total_amount'), DB::raw('count(orders.id) as count'), DB::raw("DATE_FORMAT(date, '%Y-%m-%d') as o_date"), 'storeName')->groupBy('o_date', 'storeName')->get()->toArray();

The above query is returning array of objects as mentioned above. Than I extracted unique dates using foreach loop.

foreach($orders as $order)
{
    if(!in_array($order->o_date, $dates))
    {
        array_push($dates, $order->o_date);
    }
}

Then I am loop through all the dates like this

foreach($dates as $date)
{
    $dataArray[] = [$date];

    foreach($orders as $order)
    {
        if($order->storeName == $store && $order->o_date == $date)
        {
            $dataArray[] = [$date, $order->total_amount];

        }
    }
}

The above code is wrong. First element in the array should be date , second element should be total_amount for store1 and third element should be total_amount for store2 like below

['2020-06-01', 6572.67, 314.21, ''],
['2020-06-02', 5550.34, 461.87, ''],
['2020-06-03', 6471.37, 547.99, '']

Note:- I am preparing data for Google Stacked Column Chart

A little scrappy, but this should do it. The $chart variable will hold what you are looking for.

    // Assuming data is the "array for object in this format"
    $dates = array_reduce($data, function ($dates, $record) {
        if (!in_array($record['o_date']), $dates) {
            $dates[] = $record['o_date'];
        }

        return $dates;
    }, []);

    $stores = array_reduce($data, function ($stores, $record) {
        if (!in_array($record['storeName'], $stores)) {        
            $stores[] = $record['storeName'];
        }

        return $stores;
    }, []);
    
    $headers = $stores;
    $headers[] = [ 'role' => 'annotation' ])

    $chart = [ $headers ];

    foreach ($dates as $date) {
        $datapoint = [ $date ];

        foreach ($stores as $store) {
            $total = 0;

            foreach ($data as $record) {
                $isStore = $record['storeName'] === $store;
                $isDate = $record['o_date'] === $date;
                if($isStore && $isDate) {
                    $total += (float) $record['total_amount'];
               }
            }

            $datapoint[] = $total;
        }
            
        $chart[] = $datapoint;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM