-1

I am trying to create a pie chart using jqplot but I get the error displayed in the title. Ultimately, I want to use the code below to create different types of jqplot charts since the data format is mostly the same. The for loop in the retrieveData() is where I am creating the data that will populate the all items array that will generate the pie or bar chart. I want to get it working for pie chart first. You can also find https://jsfiddle.net/isogunro/5peuchqe/

var schoolApp = window.schoolApp || {};
schoolApp.itemType = new Array();

$(document).ready(function () {
retrieveData();
});

function retrieveData() {
var allItems = new getItems();

var k=0;
for (i=0; i<10; i++){
k +=i;
window.allItems.addItems("Hello"+i, k);
}

chartArray = window.allItems.getChartData();

plotChart(chartArray);
}

function getItems() {
this.inputs = {};
this.items = [];

this.addItems = function (unqItem, amount1) {
    if (!this.inputs[unqItem]) {
        this.items.push(unqItem);
        this.inputs[unqItem] = 0;
    }
    this.inputs[unqItem] += amount1;
};

this.getChartData = function () {
    var chartAry = [];
    for (i = 0; i < this.items.length; ++i) {
        chartAry.push([this.items[i], this.inputs[this.items[i]]]);
    }
    return chartAry;
}

} // end of function truck2pie


function plotChart(data) {

var plot1 = jQuery.jqplot('pieChart', [data],
{
    seriesDefaults: {
        // Make this a pie chart.
        renderer: jQuery.jqplot.PieRenderer,
        rendererOptions: {
            fill: true,
            sliceMargin: 7,
            dataLabels: 'value',  //Show data instead of label
            showDataLabels: true,
            linewidth: 5,
            shadowOffset: 2,
            shadowDepth: 5, //Number of strokes to make when drawing shadow. Each stroke offset by shadowOfset from the last.
            shadowAlpha: 0.07
        }
    },
    legend: { show: true, location: 'e' }
}
);

}
JustMe
  • 147
  • 2
  • 11
  • 2
    The error is pretty clear, your're trying to access a non existent property: `window.allItems` is undefined, therefore you can't it. – DCruz22 Dec 02 '16 at 16:12
  • Possible duplicate of [Detecting an undefined object property](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Heretic Monkey Dec 02 '16 at 16:50

1 Answers1

1

You don't to use window to access a local variable, just remove it and it will work.

function retrieveData() {
        var allItems = new getItems();    
        var k=0;
        for (i=0; i<10; i++){

        k +=i;
        allItems.addItems("Hello"+i, k);
    }

    chartArray = allItems.getChartData();

    plotChart(chartArray);
}

function getItems() {
    this.inputs = {};
    this.items = [];

this.addItems = function (unqItem, amount1) {
    if (!this.inputs[unqItem]) {
        this.items.push(unqItem);
        this.inputs[unqItem] = 0;
    }
    this.inputs[unqItem] += amount1;
};

this.getChartData = function () {
    var chartAry = [];
    for (i = 0; i < this.items.length; ++i) {
        chartAry.push([this.items[i], this.inputs[this.items[i]]]);
    }
    return chartAry;
}

}

DCruz22
  • 806
  • 1
  • 9
  • 18