문제

I'm using the Graphite Render API to return data in JSON format to node.js, which I'd like to pass to Rickshaw for graphing. I'm using grockets.js to pass the data to the client via socket.io. I have tested Rickshaw with socket.io successfully by passing manual data using one of their examples. The issue I have is that the JSON format Graphite returns is not what Rickshaw is expecting and I'm not sure how to go about converting it.

The graphite output looks like so:

[
    {
        "target": "localhost_localdomain.cpu-0.cpu-idle", 
        "datapoints": [
        [99.999698, 1392728820],
        [100.000898, 1392728880],
        [99.999968, 1392728940],
        [99.299848, 1392732360]
        ]
    }
]

I need it to be in this format:

[
    {
        "target": "localhost_localdomain.cpu-0.cpu-idle", 
        "datapoints": [
        { "x": 99.999698, "y": 1392728820 },
        { "x": 100.000898, "y": 1392728880 },
        { "x": 99.999968, "y": 1392728940 },
        { "x": 99.299848, "y": 1392732360 }
        ]
    }
]

Am I right in saying this is an array of objects? Any help or guidance would be really appreciated!

Thanks!

도움이 되었습니까?

해결책

Given that it's just the datapoints that needs to be transformed, this should be fairly easy:

var myData = [{
    "target": "localhost_localdomain.cpu-0.cpu-idle",
        "datapoints": [
        [99.999698, 1392728820],
        [100.000898, 1392728880],
        [99.999968, 1392728940],
        [99.299848, 1392732360]
    ]
}];

var transformedPoints = myData[0].datapoints.map(function (pt) {
    return {
        x: pt[0],
        y: pt[1]
    };
});

myData[0].datapoints = transformedPoints;

console.log(myData);

Here's a fiddle

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top