2016-02-05 55 views
0

我已經有Kendo餅圖,它的傳奇依然顯示,即使沒有價值。 是否有可能隱藏沒有值或值= 0的圖例。我通過函數傳遞值,所以我不知道哪個值是0.我試圖設置數組中的數據,但我卡住了。任何人都可以幫我...如果值爲0,隱藏圖例 - Kendo UI餅圖

這裏是我的腳本:

function createPieChart(a,b,c,d) { 
    var e = a+b+c+d; 
    var aa = a/e*100; 
    var bb = b/e*100; 
    var cc = c/e*100; 
    var dd = d/e*100; 

    var perA = Math.round(aa*100.0)/100.0; 
    var perB = Math.round(bb*100.0)/100.0; 
    var perC = Math.round(cc*100.0)/100.0; 
    var perD = Math.round(dd*100.0)/100.0; 

    var arrValue = [perA, perB, perC, perD]; 

    var data = [{ 
     "source": "Positive", 
     "percentage": perA, 
     "color": "#9de219", 
     "explode": true 
    },{ 
     "source": "Neutral", 
     "percentage": perB, 
     "color": "#90cc38" 
    },{ 
     "source": "Negative", 
     "percentage": perC, 
     "color": "#068c35" 
    },{ 
     "source": "Unknown", 
     "percentage": perD, 
     "color": "#006634" 
    }]; 

    $("#chart_div").kendoChart({ 
     dataSource: { 
      transport: { 
      read: function(e) { 
       e.success(data); 
      } 
      } 
     }, 
     title: { 
      position: "top", 
      text: "Sentiment Result" 
     }, 
     legend: { 
      position: "bottom", 
      visible: true 
     }, 
     seriesDefaults: { 
      labels: { 
       visible: false, 
       template: "#= category #: \n #= value#%" 
      } 
     }, 
     series: [{ 
      type: "pie", 
      startAngle: 150, 
      field: "percentage", 
      categoryField: "source", 
      colorField: "color", 
      explodeField: "explode" 
     }], 
     tooltip: { 
      visible: true, 
      template: "${ category } - ${ value }%" 
     } 
} 
+0

你介意減少你的代碼問題存在的地方,請詳細說明這個問題? – Zach

+0

我找到了方法.. – N85

回答

0

替換此代碼:

var data = [{ 
     "source": "Positive", 
     "percentage": perA, 
     "color": "#9de219", 
     "explode": true 
    },{ 
     "source": "Neutral", 
     "percentage": perB, 
     "color": "#90cc38" 
    },{ 
     "source": "Negative", 
     "percentage": perC, 
     "color": "#068c35" 
    },{ 
     "source": "Unknown", 
     "percentage": perD, 
     "color": "#006634" 
    }]; 

有了這個代碼:

var arrValue = [perA, perB, perC, perD]; 
    var arrSource = ["Positive","Neutral","Negative","Unknown"]; 
    var arrColor = ["#9de219","#90cc38","#068c35","#006634"]; 
    var data=[]; 

    for (var i=0; i<arrValue.length; i++){ 
     if(arrValue[i]>0){ 
      data.push({ 
        source: arrSource[i], 
        percentage: arrValue[i], 
        color: arrColor[i], 
        explode: true 
      }); 
     } 
    } 
+0

我指的是這個http://stackoverflow.com/questions/25270719/building-the-series-of-a-kendo-chart-by-looping-through-the-json -數據 – N85

相關問題