1

I'm using the d3.js charting library and I'm using a radar chart extension which seems to only accept the data objects in one way. Instead of trying to change the code for the extension, I thought it would be easier to just manipulate my data into the kind it's coded to accept. If it makes sense?

Anyhow, this is my code;

My JSON:

{
    "questions": ["Staff is courteous and polite","Attentive staff","Modern brand","Innovative brand","Good employer","Company I trust","Place for kids and family","Place for young people","Affordable food"],
    "organizations": ["MC", "BK", "KFC"],
    "dates": ["Jan", "Feb", "Mar"],
    "values": [ [
            [40, 15, 13],
            [25, 24, 14],
            [1, 23, 20]] ... etc etc

Javascript:

d3.json("data.json", function(error, data) {

    var newValue =[];

    var yellow = [
        [
            {"label":"A","value":6},
            {"label":"B","value":4},
            {"label":"C","value":6},
            {"label":"D","value":5.5},
            {"label":"E","value":8},
            {"label":"F","value":7},
            {"label":"G","value":9},
            {"label":"H","value":10},
            {"label":"I","value":3.5}
        ]
    ];
    if (error) {
        console.log(error);
    }
    else {
        data = data;
    }

     var newValue = [];
        var orgS = "MC";
        var dateS = "Jan";
        for (var question = 0; question < data.questions.length; question++) {
            var organization = data.organizations.indexOf(orgS);
            var date = data.dates.indexOf(dateS);
            newValue.push({
                label: data.questions[question],
                value: data.values[question][organization][date]
            });
        }

    console.log(newValue);
    console.log(yellow);


});

console output structure:

enter image description here

So my question is, how can I output my data to the console like the var "yellow" (the bottom one in the pic)?

I've tried to wrap [] around the newValue.push but it didn't return the desired effect.

I'm hoping this is possible, any advice is much appreciated!

Here is a plnk all set up -

https://plnkr.co/edit/EBcxa39sal0PAOJxSYKb?p=preview

(Oh - and I really wasn't sure what an appropriate title for this question should be, please feel welcome to edit/suggest a new one to more accurately describe the problem).

3
  • Not very clear what relationship of the original properties organizations, dates, values are to final result as represented by yellow. Show example of real expected output Commented Jun 4, 2016 at 19:44
  • @charlietfl I'm not really sure how to answer, there isn't any intended relationship between the two. It's just this is the method I have for filtering through my data, and I would like to try figure out a way to output it in the same way as the yellow is as thats the structure that is accepted by the plugin. Basically, I'm trying to take a shortcut if that helps.. Commented Jun 4, 2016 at 19:50
  • Well without knowing how to match value and label that makes it difficult for us to know what to do. ie "expected results" Commented Jun 4, 2016 at 19:52

1 Answer 1

2

Although I don't see the point ...

d3.json("data.json", function(error, data) {

    var newValue =[];

    var yellow = [
        [
            {"label":"A","value":6},
            {"label":"B","value":4},
            {"label":"C","value":6},
            {"label":"D","value":5.5},
            {"label":"E","value":8},
            {"label":"F","value":7},
            {"label":"G","value":9},
            {"label":"H","value":10},
            {"label":"I","value":3.5}
        ]
    ];
    if (error) {
        console.log(error);
    }
    else {
        data = data;
    }

     var newValue = [];
        var orgS = "MC";
        var dateS = "Jan";
        for (var question = 0; question < data.questions.length; question++) {
            var organization = data.organizations.indexOf(orgS);
            var date = data.dates.indexOf(dateS);
            newValue.push({
                label: data.questions[question],
                value: data.values[question][organization][date]
            });
        }

    var newArrayValue = [];
    newArrayValue.push(newValue);

    console.log(newValue);
    console.log(newArrayValue);
    console.log(yellow);


});

https://plnkr.co/edit/UrmWt5AgqAuJWe8cO1Bi?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Ah okay, does what I need! Thanks :)

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.