我正在使用MPAndroidChart,我有一个问题:
如何更改用户点击的圆圈颜色?
以下是我目前在OnChartValueSelectedListener
:
lineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry entry, int i, Highlight highlight) {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getContext());
Intent intent = new Intent(NewMainActivity.DIARY_ACTION);
intent.putExtra("date", points.get(highlight.getXIndex()).getDate());
manager.sendBroadcast(intent);
////////////////Here I want to change the color//////////////
}
@Override
public void onNothingSelected() {
}
});
答案 0 :(得分:1)
我找到的答案是提取一个名为myHighlight
的类字段,并在监听器中更新它,如下所示:
lineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry entry, int i, Highlight highlight) {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getContext());
Intent intent = new Intent(NewMainActivity.DIARY_ACTION);
intent.putExtra("date", points.get(highlight.getXIndex()).getDate());
manager.sendBroadcast(intent);
myHighlight = highlight.getXIndex();
valuesCount = new int[lineChart.getValueCount()];
setClickedPointGraphColor(valuesCount, clickedGraphPointColor);
}
@Override
public void onNothingSelected() {
}
});
然后我写了一个方法setClickedPointGraphColor()
,我在其中设置圆圈的颜色以匹配myHighlight
的状态:
public void setClickedPointGraphColor(int[] valuesCount, int color) {
for (int i =0; i < valuesCount.length; i++){
if (i == myHighlight){
valuesCount[i] = color;
} else {
valuesCount[i] = regularGraphPointColor;
}
}
}
由于我现在更新了数据集,因此我需要调用notifyDataSetChanged()
。