单击条形图条目时如何获取标签图例?
How to get label legend when clicking on BarChart entry?
我正在尝试以下行为:
单击条形图条目时,我想获取相应的图例标签(SF Zoo 或 LA Zoo)
构建 BarDataSet 的代码:
List<IBarDataSet> barDataSetList = new ArrayList<>();
for (Map.Entry<String, List<Float>> entry : hashMapCategorySpending.entrySet()) {
String category = entry.getKey(); // here category is either SF Zoo or LA Zoo
List<Float> values = entry.getValue();
List<BarEntry> valuesConverted = convertToBarEntryArray(values); // some utility function
BarDataSet barDataSet = new BarDataSet(valuesConverted, category); // create BarDataSet
barDataSet.setColor(StatisticsCategoryFragment.categoryColorHashMap.getOrDefault(category, 0));
barDataSetList.add(barDataSet);
}
/* Set data to chart */
BarData barData = new BarData(barDataSetList);
barData.setValueTextSize(12.5f);
barChart.setData(barData);
我的问题是,当覆盖 setOnChartValueSelectedListener
时,我无法将 Entry e
转换为有用的东西:
barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
BarEntry barDataSet = (BarEntry) e; // Doesn't help at all
// I want to do something like e.getLegendLabel(); and it should return SF Zoo or LA Zoo
}
@Override
public void onNothingSelected() {
}
});
想通了。您可以从 Highlight h
对象中获取信息:
String category = barChart.getLegend().getEntries()[h.getDataSetIndex()].label;
我正在尝试以下行为:
单击条形图条目时,我想获取相应的图例标签(SF Zoo 或 LA Zoo)
构建 BarDataSet 的代码:
List<IBarDataSet> barDataSetList = new ArrayList<>();
for (Map.Entry<String, List<Float>> entry : hashMapCategorySpending.entrySet()) {
String category = entry.getKey(); // here category is either SF Zoo or LA Zoo
List<Float> values = entry.getValue();
List<BarEntry> valuesConverted = convertToBarEntryArray(values); // some utility function
BarDataSet barDataSet = new BarDataSet(valuesConverted, category); // create BarDataSet
barDataSet.setColor(StatisticsCategoryFragment.categoryColorHashMap.getOrDefault(category, 0));
barDataSetList.add(barDataSet);
}
/* Set data to chart */
BarData barData = new BarData(barDataSetList);
barData.setValueTextSize(12.5f);
barChart.setData(barData);
我的问题是,当覆盖 setOnChartValueSelectedListener
时,我无法将 Entry e
转换为有用的东西:
barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
BarEntry barDataSet = (BarEntry) e; // Doesn't help at all
// I want to do something like e.getLegendLabel(); and it should return SF Zoo or LA Zoo
}
@Override
public void onNothingSelected() {
}
});
想通了。您可以从 Highlight h
对象中获取信息:
String category = barChart.getLegend().getEntries()[h.getDataSetIndex()].label;