Android 如果数据更改值,饼图不会刷新饼图
Android Pie Chart doesn't refresh pie graph if data change value
我无法通过我的侦听器 Spinner 刷新我的饼图:片段中的 setOnItemSelectedListener。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayAdapter adapterSpinnerTeams = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, teamsName);
adapterSpinnerTeams.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTeamsAnalysis.setAdapter(adapterSpinnerTeams);
spinnerTeamsAnalysis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
anyChartView2 = v.findViewById(R.id.anyChartView2);
APIlib.getInstance().setActiveAnyChartView(anyChartView2);
setUpPieChart(spinnerTeamsAnalysis.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
return v;
}
public void setUpPieChart(String teamSelected) {
anyChartView2 = v.findViewById(R.id.anyChartView2);
APIlib.getInstance().setActiveAnyChartView(anyChartView2);
Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();
creditsByRoles = calculateCredits(teamSelected, creditsByRoles);
for(int i=0; i<roles.length; i++) {
dataEntries.add(new ValueDataEntry(roles[i], creditsByRoles[i]));
}
pie.data(dataEntries);
pie.title("title");
anyChartView2.setChart(pie);
}
注意。方法 calculateCredits(teamSelected, creditsByRoles) return 一个整数数组。
当微调器更改值时,饼图不会更改值。
有什么解决办法吗?
如果你想刷新图表,你必须使用这一行:
pie.invalidate();
调用 invalidate() 后,图表 刷新 并绘制提供的数据。 docs.
我是这样解决的:
pieChart = v.findViewById(R.id.pieChart);
APIlib.getInstance().setActiveAnyChartView(pieChart);
final Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();
creditsByRoles = calculateCredits(teamSelected, creditsByRoles);
for(int i=0; i<roles.length; i++) {
dataEntries.add(new ValueDataEntry(roles[i], creditsByRoles[i]));
}
pie.data(dataEntries);
pieChart.setChart(pie);
/* Per permettere il refresh in caso di cambio valori */
final int delayMillis = 500;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
public void run() {
List<DataEntry> data = new ArrayList<>();
for(int i=0; i<roles.length; i++) {
data.add(new ValueDataEntry(roles[i], creditsByRoles[i]));
}
pie.data(data);
handler.postDelayed(this, delayMillis);
}
};
handler.postDelayed(runnable, delayMillis);
我无法通过我的侦听器 Spinner 刷新我的饼图:片段中的 setOnItemSelectedListener。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayAdapter adapterSpinnerTeams = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, teamsName);
adapterSpinnerTeams.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTeamsAnalysis.setAdapter(adapterSpinnerTeams);
spinnerTeamsAnalysis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
anyChartView2 = v.findViewById(R.id.anyChartView2);
APIlib.getInstance().setActiveAnyChartView(anyChartView2);
setUpPieChart(spinnerTeamsAnalysis.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
return v;
}
public void setUpPieChart(String teamSelected) {
anyChartView2 = v.findViewById(R.id.anyChartView2);
APIlib.getInstance().setActiveAnyChartView(anyChartView2);
Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();
creditsByRoles = calculateCredits(teamSelected, creditsByRoles);
for(int i=0; i<roles.length; i++) {
dataEntries.add(new ValueDataEntry(roles[i], creditsByRoles[i]));
}
pie.data(dataEntries);
pie.title("title");
anyChartView2.setChart(pie);
}
注意。方法 calculateCredits(teamSelected, creditsByRoles) return 一个整数数组。
当微调器更改值时,饼图不会更改值。
有什么解决办法吗?
如果你想刷新图表,你必须使用这一行:
pie.invalidate();
调用 invalidate() 后,图表 刷新 并绘制提供的数据。 docs.
我是这样解决的:
pieChart = v.findViewById(R.id.pieChart);
APIlib.getInstance().setActiveAnyChartView(pieChart);
final Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();
creditsByRoles = calculateCredits(teamSelected, creditsByRoles);
for(int i=0; i<roles.length; i++) {
dataEntries.add(new ValueDataEntry(roles[i], creditsByRoles[i]));
}
pie.data(dataEntries);
pieChart.setChart(pie);
/* Per permettere il refresh in caso di cambio valori */
final int delayMillis = 500;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
public void run() {
List<DataEntry> data = new ArrayList<>();
for(int i=0; i<roles.length; i++) {
data.add(new ValueDataEntry(roles[i], creditsByRoles[i]));
}
pie.data(data);
handler.postDelayed(this, delayMillis);
}
};
handler.postDelayed(runnable, delayMillis);