【问题标题】:Google Chart to draw linechart谷歌图表绘制折线图
【发布时间】:2015-03-24 11:31:26
【问题描述】:

我设法按照我的要求获得了用于绘制图表的数据集,如下所示:

现在我想将它传递给我的谷歌图表 api 来绘制折线图。

我已经在我的表上进行了旋转以获得所需的数据集。在此示例中,column1 包含月份,其余列实际上来自数据库(因此不确定还有多少列)。

我想要这种格式的json字符串:

"{\"cols\":[
{\"id\":\"\",\"label\":\"Month\",\"pattern\":\"\",\"type\":\"string\"},
{\"id\":\"\",\"label\":\"Col1\",\"pattern\":\"\",\"type\":\"number\"},
{\"id\":\"\",\"label\":\"Col2\",\"pattern\":\"\",\"type\":\"number\"},
{\"id\":\"\",\"label\":\"Col3\",\"pattern\":\"\",\"type\":\"number\"}
],
\"rows\":[
{\"c\":[{\"v\":\"Jan\"},{\"v\":37.8},{\"v\":80.8},{\"v\":41.8}]},
{\"c\":[{\"v\":\"Feb\"},{\"v\":30.9},{\"v\":69.5},{\"v\":32.4}]},
{\"c\":[{\"v\":\"Mar\"},{\"v\":25.4},{\"v\":57},{\"v\":25.7}]},
{\"c\":[{\"v\":\"Apr\"},{\"v\":11.7},{\"v\":18.8},{\"v\":10.5}]},
{\"c\":[{\"v\":\"May\"},{\"v\":11.9},{\"v\":17.6},{\"v\":10.4}]},
{\"c\":[{\"v\":\"Jun\"},{\"v\":8.8},{\"v\":13.6},{\"v\":7.7}]},
{\"c\":[{\"v\":\"Jul\"},{\"v\":7.6},{\"v\":12.3},{\"v\":9.6}]},
{\"c\":[{\"v\":\"Aug\"},{\"v\":12.3},{\"v\":29.2},{\"v\":10.6}]},
{\"c\":[{\"v\":\"Sep\"},{\"v\":16.9},{\"v\":42.9},{\"v\":14.8}]},
{\"c\":[{\"v\":\"Oct\"},{\"v\":12.8},{\"v\":30.9},{\"v\":11.6}]},
{\"c\":[{\"v\":\"Nov\"},{\"v\":5.3},{\"v\":7.9},{\"v\":4.7}]},
{\"c\":[{\"v\":\"Dec\"},{\"v\":6.6},{\"v\":8.4},{\"v\":5.2}]}]}"

如何形成我的对象类来返回这个 json 字符串?任何想法都会有所帮助。

我能想到的最后一个选项是使用 stringBuilder 动态构建我的 json 字符串。有没有其他方法可以做到这一点?

【问题讨论】:

标签: c# json google-visualization linechart


【解决方案1】:

我终于设法让它工作了,这就是我序列化我的 Json 对象的方式:

 public class Col
    {
        public string id { get; set; }
        public string label { get; set; }
        public string pattern { get; set; }
        public string type { get; set; }
    }

    public class C
    {
        public object v { get; set; }
    }

    public class Row
    {
        public List<C> c { get; set; }
    }

    public class RootObject
    {
        public List<Col> cols { get; set; }
        public List<Row> rows { get; set; }
    }

【讨论】: