0

I am working on a flot chart and I am unable to covert json string to required flot graph format. Below is what I tried.

My Json

        Data: [
                  { "Day": 8, "Visits": 145 },
                  { "Day": 7, "Visits": 26 }
              ];

Format I am trying to achieve: [[0, 12], [1, 2], [2, 2], [3, 3], [4, 4]];

 $(document).ready(function () {
        var data = [{ "Day": 8, "Visits": 145 }, { "Day": 7, "Visits": 26 }];
        var dailyHits = [];
        $.each(data, function (_index, _item) {
            var c = [_item["Day"], _item["Visits"]];
            dailyHits.push(c);
        });

        //var dailyHits1= [[0, 12], [1, 2], [2, 2], [3, 3], [4, 4]];
        var chartData = [dailyHits];
        console.log(chartData);
        $.plot($("#chartHits"), chartData);
    });

Any help would be highly appreciated.

2
  • Your code looks correct. Do you get any javascript errors on the console? Commented Oct 9, 2013 at 13:10
  • no errros in console. I finally tried alternate way and it worked var dailyHits = []; $.each(data, function (index, item) { var temp = [item["Day"], item["Visits"]]; dailyHits.push(temp); }); dailyHits.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); var chartData = [dailyHits]; $.plot($("#chartHits"), chartData); Commented Oct 9, 2013 at 21:20

1 Answer 1

1

Hey i have same problem and i think problem is that you get data in string format, but array should be with Integers

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

Comments

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.