2016-12-01 35 views
-2

jqplotを使用して円グラフを作成しようとしていますが、タイトルにエラーが表示されます。最終的に、データ形式がほとんど同じであるため、以下のコードを使用してさまざまな種類のjqplotグラフを作成したいと考えています。 のforループで、retrieveData()は、円グラフまたは棒グラフを生成するすべてのアイテム配列にデータを挿入するデータを作成しています。私は最初に円グラフのために働きたいです。あなたはそれを削除し、それが動作する、ローカル変数にアクセスするためにwindowを使用しないでくださいhttps://jsfiddle.net/isogunro/5peuchqe/Uncaught TypeError:未定義の 'addItems'プロパティを読み取れません

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' } 
} 
); 

} 
+2

誤差がかなり明確、your'reは実在しないプロパティにアクセスしようとしている: 'window.allItems'は未定義である、したがって、あなたはそれはできません。 – DCruz22

+0

[未定義オブジェクトプロパティの検出]の可能な複製(http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) –

答えて

1

あなたも見つけることができます。

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; 
} 

}

関連する問題