3

I'm trying to work out how to plot labels to the correct places using Chart.js

var json = {
    "competition one": [
        {
            "date": "2015-05-20",
            "position": 37
        },
        {
            "date": "2015-05-21",
            "position": 22
        }
    ],
    "competition two": [
        {
            "date": "2015-05-20",
            "position": 29
        },
        {
        "date": "2015-05-21",
        "position": 19
        }
    ]
}

How can I plot the labels to the correct places? With the dates going to the correct labels so it isn't repeated?

Specifically, I'm struggling to get "competition one" into the label of the dataset array (label: "competition one")

I need it to resemble the following data structure that is required by Chart.js?

var data = {
    labels: ["2015-05-20", "2015-05-21"],
    datasets: [
        {
            label: "competition one",
            data: [37, 22]
        },
        {
            label: "Competition two",
            data: [29, 19]
        }
    ]
};
6
  • I have tried it myself, I don't get how to get the labels in the correct places Commented May 26, 2015 at 12:31
  • 1
    I will reword my question Commented May 26, 2015 at 12:33
  • thank you for helping reword it Commented May 26, 2015 at 12:37
  • I'd suggest that your JSON is wrong. Do you have control over how this is generated? It will make your life much easier if you could change the json structure so that "competition one", etc. are values and not variables, so something like json = [{label:"competition one", data:[{...}, {..}]}, ..] Commented May 26, 2015 at 12:40
  • No, I don't, that is how it is outputted from a db. Commented May 26, 2015 at 12:42

1 Answer 1

1

As mentioned in comments you can get the properties names like thus:

var json = {
    "competition one": [
        {
            "date": "2015-05-20",
            "position": 37
        },
        {
            "date": "2015-05-21",
            "position": 22
        }
    ],
    "competition two": [
        {
            "date": "2015-05-20",
            "position": 29
        },
        {
        "date": "2015-05-21",
        "position": 19
        }
    ]
}

var keys = Object.keys(json);
for (var i = 0; i < keys.length; i++)
{
    var key = keys[i];
    //"competition one", "competition two", etc
    console.log(key);   
}

Fiddle

you then just need to manipulate these values into your desired object structure.

var keys = Object.keys(json);
//set up our object containing an array called datasets
var data = {datasets:[]};
for (var i = 0; i < keys.length; i++)
{
    var key = keys[i];
    //push the key into the dataset array as an object {}
    data.datasets.push({label:key, data:...});   
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this was a great start to solving my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.