Date Range Google Chart Tools

后端 未结 3 401
半阙折子戏
半阙折子戏 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:39

    Did you try leaving the missing dates be missing dates (i.e. let the database return 2 values instead of 7)?

    The continuous axis should handle missing dates, you just need to set the axis range from start to end of the week.

    UPDATE

    for interactive line chart the axis ranges can be set like this (as inspired by this thread):

    hAxis: {...
            viewWindowMode: 'explicit',
            viewWindow: {min: new Date(2007,1,1),
                         max: new Date(2010,1,1)}
            ...}
    

    see http://jsfiddle.net/REgJu/

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-14 01:06

    Perhaps you can use a different chart type? Dygraphs looks like it might be helpful.

    Otherwise you may have to write your own custom chart type.

    0 讨论(0)
提交回复
热议问题