Domanda

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!

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top