2016-09-29 31 views
0

我的數據調用可能有很多我想忽略的小元素(百分比),我只需要我的amCharts派中的前五名。在amCharts餅圖中只使用5個更高的值?

這可以用amCharts完成,否則我應該先處理數據?

請參閱我的[的jsfiddle] [1]

[1]: http://jsfiddle.net/pbarros/xznxbnc7/3/ 

感謝

回答

1

可以使用hideLabelsPercent property設置爲你想要的標籤的最低允許百分比的閾值。如果您想要動態執行此操作,則可以在init事件中設置此值,方法是在圖表的chartData array中找到第5大percents值,並將其用作hideLabelsPercent閾值。我已經更新了你的handleInit的方法來做到這一點:

function handleInit(e) { 
    if (e.chart.chartData.length > 5) { 
    //sort the copy of the chartData array from largest to smallest 
    //if your data is pre-sorted, then you can skip this step 
    var sortedData = e.chart.chartData.slice().sort(function(lhs, rhs) { 
     return rhs.percents - lhs.percents; 
    }); 

    //set the threshold equal to the 5th largest slice's percents value so that the rest are hidden 
    e.chart.hideLabelsPercent = sortedData[4].percents; 

    //redraw the chart 
    e.chart.validateNow(); 
    } 
} 

更新小提琴:http://jsfiddle.net/xznxbnc7/9/

編輯,因爲我誤解了問題

如果你只想顯示前五名切片您數據集,您可以過濾後端或使用init方法對您的dataProvider進行排序和修改以僅包含前五個數據集。

function handleInit(e) { 
    if (e.chart.chartData.length > 5) { 
    //sort the copy of the chartData array from largest to smallest 
    //if your data is pre-sorted, then you can skip this step 
    var sortedData = e.chart.dataProvider.slice().sort(function(lhs, rhs) { 
     return rhs[e.chart.valueField] - lhs[e.chart.valueField]; 
    }); 

    //only put in the top 5. 
    e.chart.dataProvider = sortedData.slice(0, 5); 

    // redraw the chart 
    e.chart.validateData(); 
    } 
} 

小提琴:http://jsfiddle.net/g3cchyjg/1

+0

謝謝你這麼much..but真正的一半,因爲你實際看到的片,我想這是一個更好的選擇,爲我做一個Ajax調用和選擇makeChart前德數據 – PauloB

+0

@PauloB - 啊,我誤解你只想顯示五個切片。我更新了我的答案以處理這種情況。 – xorspark

+0

是的,那真的是我需要的! – PauloB

相關問題