Date Range Google Chart Tools

后端 未结 3 407
半阙折子戏
半阙折子戏 2021-01-14 00:17

I\'m trying to display data on a line graph using Google Charts. The data displays fine, however I would like to set a date range to be displayed.

The data is sent f

3条回答
  •  不思量自难忘°
    2021-01-14 00:58

    "I managed to get the whole week showing by checking if the date exists in the database and if not append an extra row will null data, this however meant the line was not continuous and the dates where not in order."

    I think you are on the right track you just need to do it in a slightly different way. I have the function like the below to make data continuous.

    $data = array(
        1 => 50,
        2 => 75,
        4 => 65,
        7 => 60,
    );
    $dayAgoStart = 1;
    $daysAgoEnd = 14;
    
    $continuousData = array();
    
    for($daysAgo=$daysAgoStart ; $daysAgo<=$daysAgoEnd ; $daysAgo++){
        if(array_key_exists($daysAgo, $data) == true){
            $continuousData[$daysAgo] = $data[$daysAgo];
        }
        else{
            $continuousData[$daysAgo] = 0;
        }
    }
    

    continuousData will now hold:

    $data = array(
        1 => 50,
        2 => 75,
        3 => 0,
        4 => 65,
        5 => 0,
        6 => 0,
        7 => 60,
        8 => 0,
        9 => 0,
        10 => 0,
        11 => 0,
        12 => 0,
        13 => 0,
        14 => 0,
    );
    

    in that order, and then the data can be used in the charts without any gaps.

提交回复
热议问题