在amCharts饼图中只使用5个更高的值?
问题描述:
我的数据调用可能有很多我想忽略的小元素(百分比),我只需要我的amCharts派中的前五名。在amCharts饼图中只使用5个更高的值?
这可以用amCharts完成,否则我应该先处理数据?
请参阅我的[的jsfiddle] [1]
[1]: http://jsfiddle.net/pbarros/xznxbnc7/3/
感谢
答
可以使用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();
}
}
谢谢你这么much..but真正的一半,因为你实际看到的片,我想这是一个更好的选择,为我做一个Ajax调用和选择makeChart前德数据 – PauloB
@PauloB - 啊,我误解你只想显示五个切片。我更新了我的答案以处理这种情况。 – xorspark
是的,那真的是我需要的! – PauloB