日期范围 Google 图表工具

发布于 2025-01-03 03:10:46 字数 1194 浏览 6 评论 0原文

我正在尝试使用 Google Charts 在折线图上显示数据。数据显示正常,但我想设置要显示的日期范围。

数据以 JSON 文字格式从数据库发送:

{
    "cols": [
                {"label": "Week", "type": "date"},
                {"label": "Speed", "type": "number"},               
                {"type":"string","p":{"role":"tooltip"}},   
                {"type":"string","p":{"role":"tooltip"}},   
                {"type":"string","p":{"role":"tooltip"}},   
                {"type":"string","p":{"role":"tooltip"}},   

            ],
    "rows": [
               {"c":[{"v": "Date('.$date.')"},{"v": null},{"v": null},{"v": null},{"v": null},{"v": null}]},
               {"c":[{"v": "Date('.$date.')"},{"v": null},{"v": null},{"v": null},{"v": null},{"v": null}]}
            ] 
}

数据按周或月显示(null 以便于阅读),例如本周:

2012, 02, 06
2012, 02, 07
2012, 02, 09 

数据未设置为一周中的每一天,因此,在此示例中仅显示上述日期。我想显示的是本周开始 (2012, 02, 06) 到周末 (2012, 02, 12) 类似于第三个示例 此处

我设法通过检查数据库中是否存在日期来显示整周,如果不附加额外的行将为空数据,但这意味着该行不连续并且日期不按顺序排列。

有人可以就我如何去做这件事提供任何建议吗?

谢谢!

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 from the database in a JSON literal format:

{
    "cols": [
                {"label": "Week", "type": "date"},
                {"label": "Speed", "type": "number"},               
                {"type":"string","p":{"role":"tooltip"}},   
                {"type":"string","p":{"role":"tooltip"}},   
                {"type":"string","p":{"role":"tooltip"}},   
                {"type":"string","p":{"role":"tooltip"}},   

            ],
    "rows": [
               {"c":[{"v": "Date('.$date.')"},{"v": null},{"v": null},{"v": null},{"v": null},{"v": null}]},
               {"c":[{"v": "Date('.$date.')"},{"v": null},{"v": null},{"v": null},{"v": null},{"v": null}]}
            ] 
}

Data is either displayed by week or month (null for easy reading) for example this week:

2012, 02, 06
2012, 02, 07
2012, 02, 09 

Data isn't set for everyday of the week, therefore in this example only the dates above are shown. What I would like to be shown is the start of the week (2012, 02, 06) to the end of the week (2012, 02, 12) similar to the third example here.

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.

Could anyone offer any advice on how to I could go about doing this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2025-01-10 03:10:46

您是否尝试将缺失的日期保留为缺失的日期(即让数据库返回 2 个值而不是 7 个值)?

连续轴应该处理缺失的日期,您只需设置 轴范围从一周的开始到结束。

更新

交互式折线图 a> 轴范围可以这样设置(受 此帖子):

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

请参阅 http://jsfiddle.net/REgJu/

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/

作业与我同在 2025-01-10 03:10:46

“我设法通过检查数据库中是否存在日期来显示整周,如果不附加额外的行将使数据为空,但这意味着该行不连续并且日期不按顺序排列。”

我认为你走在正确的轨道上,你只需要以稍微不同的方式来做。我有像下面这样的函数来使数据连续。

$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 现在将按以下顺序保存:

$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,
);

,然后可以在图表中使用数据而没有任何间隙。

"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.

森林迷了鹿 2025-01-10 03:10:46

也许您可以使用不同的图表类型? Dygraphs 看起来可能会有帮助。

否则,您可能必须编写自己的自定义图表类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文