0

我正在构建一个折线图,记录每个步行者每天所走步数的读数,其中每个成员都是一个系列,x 轴是日期,y 轴是所走的步数。

但是,步行者可能每天都有读数,也可能没有读数,这会导致问题。当显示图表时,我收到一条错误消息,指出轴 #0 的数据列不能是字符串类型。

这是我的 json 示例

{
    "cols" : [{
            "id" : null,
            "label" : "Date",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Joe Bloggs",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "John Doe",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Jane Doe",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "John Bloggs",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Jane Bloggs",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Ann Walker",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Andy Walker",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Tom Jones",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Foo Bar",
            "type" : "string"
        }, {
            "id" : null,
            "label" : "Bar Foo",
            "type" : "string"
        }
    ],
    "rows" : [{
            "c" : [{
                    "v" : "02 Sep 13",
                    "f" : null
                }, {
                    "v" : 6787,
                    "f" : null
                }, {
                    "v" : 1558,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : 9913,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : 27881,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : 7410,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : null,
                    "f" : null
                }, {
                    "v" : 2126,
                    "f" : null
                }
            ]
        }]
}

我省略了额外的行以保持简短。出于格式化目的和简单性,我故意将日期视为字符串

在这种情况下,我可能有也可能没有行的值,我应该如何表示它?

4

1 回答 1

2

当它应该是数字时,您将列定义为 y 轴的字符串。除了第一列(您的日期列)之外,将所有其他列更改为“类型:数字”,它应该可以工作。

谷歌图表的 JSON 格式有时会有点棘手。测试它的一个好方法是创建一个谷歌电子表格作为源,并在电子表格上更改值并通过浏览器加载为 json,因为它将为您提供谷歌图表 api 所期望的正确格式。有关如何执行此操作的更多信息,请查看我对这篇文章的回答。

可用于统计的数据集

我希望它有帮助

于 2013-09-15T13:34:28.690 回答