简体   繁体   English

D3的格式数据

[英]Format data for D3

How can I format this sample data for a graph I am making in d3.js? 如何格式化我在d3.js中制作的图表的示例数据?

[
    [
        [
            "2",
            "2013-01-02 00:00:00+00"
        ],
        [
            "3",
            "2012-12-28 00:00:00+00"
        ],
        [
            "3",
            "2012-12-27 00:00:00+00"
        ],
        [
            "1",
            "2012-12-26 00:00:00+00"
        ],
        [
            "1",
            "2012-12-24 00:00:00+00"
        ],
        [
            "4",
            "2012-12-21 00:00:00+00"
        ],
        [
            "1",
            "2012-12-20 00:00:00+00"
        ]
    ],
    [
        [
            "1",
            "2012-12-27 00:00:00+00"
        ],
        [
            "2",
            "2012-12-21 00:00:00+00"
        ],
        [
            "2",
            "2012-12-18 00:00:00+00"
        ],
        [
            "1",
            "2012-12-17 00:00:00+00"
        ],
        [
            "1",
            "2012-11-27 00:00:00+00"
        ],
        [
            "1",
            "2012-11-03 00:00:00+00"
        ],
        [
            "1",
            "2012-10-27 00:00:00+00"
        ]
    ]
]

currently, my javascript console gives me Uncaught TypeError: Cannot use 'in' operator to search for '_dateElement' in NaN within d3.v3.js:1033 when d3 tries to parse this data 目前,我的javascript控制台给了我Uncaught TypeError: Cannot use 'in' operator to search for '_dateElement' in NaN当d3尝试解析这些数据时, Uncaught TypeError: Cannot use 'in' operator to search for '_dateElement' in NaN :1033

it seems that d3 doesn't want to deal with numbers in quotation marks, without quotation marks makes invalid json, which my ajax call will not return, which means I won't be able to parse the arrays in my d3 function. 似乎d3不想处理引号中的数字,没有引号使得json无效,我的ajax调用将不会返回,这意味着我将无法在d3函数中解析数组。

I believe d3 can parse and format json but I am at an impasse. 我相信d3可以解析和格式化json,但我陷入了僵局。 How do I format this data, the date objects will be my X axis in ascending order, and the Y axis will be a max of the integer values 如何格式化此数据,日期对象将按升序排列为我的X轴,Y轴将是整数值的最大值

You will need to parse your input array into an output array with the data in the format you want. 您需要将输入数组解析为输出数组,其中包含所需格式的数据。 As C.Reed says in comments, use parseInt to convert string to number and use d3.time.format to convert string to date. 正如C.Reed在评论中所说,使用parseInt将字符串转换为数字并使用d3.time.format将字符串转换为日期。 https://github.com/mbostock/d3/wiki/Time-Formatting https://github.com/mbostock/d3/wiki/Time-Formatting

var date_in = "2013-01-02 00:00:00+00";
var format = d3.time.format("%Y-%m-%d 00:00:00+00");
var date_out = format.parse(date_in); 

(assumes time is always 0) (假设时间总是0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM