0

I've got an object with some properties in which I want to display on a web page in a chart. Each object will have its own chart.

Currently receiving the objects and their properties but can't get them to display dynamically in a chart, this works with static data.

What I'm trying to achieve is to loop through the objects, create the chart and add the data to it dynamically, there isn't a constant of how many objects there will be.

IE - on page load I'll receive how many objects there are, 3 for example, as such, three boxes will be created, each with a chart inside. How do I achieve this?

I don't think there's an issue with the data I'm sending receiving, but more to do with how I'm creating an object and it's corresponding div with the pie chart inside.

Where am I going wrong?

Code:

        <style type="text/css">
            #box {
                border: 1px solid;
                width: 200px;
                height: 200px;
            }
            #objectBox {
                width: 100px;
                height: 100px;
            }
        </style>

            <script type="text/javascript">
                    function getData() {
                        $.ajax({
                            type: "GET",
                            url: "/Graphs/GetMyPie",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: drawChart
                        });
                    }



                    function drawChart(myObject) {
var dpoints = [];
                        // loop through array and create and display a pie for each object in array
                        for (var i = 0; i < myObject.length; i++) {
                            // each object
                            var prop1 = myObject[i].propertyOne;
                            var prop2 = myObject[i].propertyTwo;
                            var title = myObject[i].objectName;

                            // create container div and pie div
                            var outbox = document.createElement('div');
                            outbox.id = 'box';
                            document.body.appendChild('box');
                            var inbox = outbox.appendChild('objectBox' + i);
    // fill pie div

                            dpoints[i] = [{ label: "Free", data: prop1, color: '#7DCC3D' }, { label: "Used", data: prop2, color: '#333366' }];

                            $.plot($('#objectBox' + [i]), dpoints[i], {
                                series: {
                                    pie: {
                                        show: true
                                    }
                                }
                            });
                        }
                        $(document).ready(function () {
                            getData();
                        });

                    }
            </script>
7
  • Can you recreate the problem in a plunker or fiddle? Commented Jun 26, 2015 at 21:43
  • prop1[i] doesn't seem right. i was the index in myObject, why are you also using it as the index in myObject[i].propertyOne? Commented Jun 26, 2015 at 21:46
  • Not sure I can, the object(s) that are received are from an API which can't be recreated. To create the pie chart I'm using jQuery flot chart. Would there be a way to do this? Commented Jun 26, 2015 at 21:47
  • You're right, that's a typo - will correct. Commented Jun 26, 2015 at 21:48
  • What are you trying to accomplish with $().text(title)? Shouldn't the title go somewhere in the chart? Commented Jun 26, 2015 at 21:49

2 Answers 2

2

If you don't want to use jQuery, as it isn't much JavaScript, I have put together an example: https://jsfiddle.net/gg1kkqq7/2/

With the above example, put

var dpoints = [{ label: "Free", data: prop1, color: '#7DCC3D' }, { label: "Used", data: prop2, color: '#333366' }];
    $.plot(objectBox, dpoints, {
        series: {
            pie: {
                show: true
            }
        }
    });` 

below outbox.appendChild(inbox); which will then render whatever the data is.

On a side note, I believe your problem is with the var outbox = $('<div>', {id: 'box' + i}); var objectBox = $("<div>", { id: 'objectBox' + i }).appendTo(outbox); $('body').append(outbox);

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

Comments

1

Try this:

function drawChart(myObject) {
    // loop through array and create and display a pie for each object in array
    for (var i = 0; i < myObject.length; i++) {
        // each object
        var prop1 = myObject[i].propertyOne;
        var prop2 = myObject[i].propertyTwo;

        // create container div and pie div
        var outbox = $('<div>', {id: 'box' + i});
        var objectBox = $("<div>", { id: 'objectBox' + i }).appendTo(outbox);
        $('body').append(outbox);

        // fill pie div
        var dpoints = [{ label: "Free", data: prop1, color: '#7DCC3D' }, { label: "Used", data: prop2, color: '#333366' }];

        $.plot(objectBox, dpoints, {
            series: {
                pie: {
                    show: true
                }
            }
        });
    }
}

I've changed it to use jQuery to create the containers. Your code there was all wrong -- the argument to appendChild is the element to append, not an ID. I couldn't see any reason for the dpoints array, so I just use a local dpoints variable.

6 Comments

Thanks will amend my code, at present Chrome inspect element has decided to crash.
I've edited the question code to reflect what I currently have, still no errors or anything are appearing in the console.
Like I said in my answer, your calls to appendChild are totally wrong.
Ah yeah - am correcting now, thanks for your help! Getting errors regarding plot dimensions now, What's the best way to debug?
Thank you for your help - one more thing, if I want to give classes to the dynamic divs is this possible? I'm trying var inbox = $('div', { id: objectBox + i }).addClass('objectBox').appendTo(outbox);
|

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.