该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
小白一枚,刚学着使用 MPAndroidChart, 启动这个界面后BarChart会显示“No chart data available”。我这个需要等接口返回数据才有数据给 BarChart他。 但是数据回来后dayChart.setData(data);还是不显示,需要点击一下屏幕这个空间所在位置他才会刷新出来。并且我试过弄假数据直接放在BarChart初始化设置的下面,他就正常显示了。但是之后再给他新的数据。他也是需要点击一下控件他才会刷新。 有大神遇到过这个问题吗 ?我的结构是Activity上面添加了一个Fragment。Fragment上面有添加了Fragment1 和 Fragment2。这个Barchart 在Fragment2上面, 是否是因为多层Fragment的关系。
以下是代码
public class ReportGasFragment extends BaseFragment implements OnChartValueSelectedListener {
@BindView(R.id.devices_count)
TextView devicesCount;
@BindView(R.id.current_day_air_consumption)
TextView dayAri;
@BindView(R.id.current_month_air_consumption)
TextView monthAri;
@BindView(R.id.current_year_air_consumption)
TextView yearAri;
@BindView(R.id.second_area_device_count)
TextView secondDeviceCount;
@BindView(R.id.second_area)
LinearLayout linearLayout;
@BindView(R.id.day_chart)
BarChart dayChart;
private DeviceList deviceList;
private CommonAdapter selectorApater;
private BottomSheetDialog sheetDialog;
private HashSet hashSet;
@Override
protected int getContentLayout() {
return R.layout.fragment_report_gas;
}
@Override
protected void initView() {
hashSet = new HashSet<>();
requestGasCollectReport();
getDeviceList();
initDayChartView();
linearLayout.setOnClickListener(v -> {
showCustomDevicesSelector();
});
}
private void initDayChartView() {
dayChart.setOnChartValueSelectedListener(this);
dayChart.setDrawBarShadow(false);
dayChart.setDrawValueAboveBar(true);
dayChart.getDescription().setEnabled(false);
dayChart.setMaxVisibleValueCount(0);
dayChart.setDragEnabled(false);
dayChart.setScaleEnabled(false);
dayChart.setDrawGridBackground(false);
IAxisValueFormatter xAxisFormatter = new DateAxisValueFormatter(new ArrayList<>());
XAxis xAxis = dayChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setLabelCount(4);
xAxis.setValueFormatter(xAxisFormatter);
YAxis leftAxis = dayChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setLabelCount(6, false);
leftAxis.setSpaceTop(10f);
leftAxis.setAxisMinimum(0f);
YAxis rightAxis = dayChart.getAxisRight();
rightAxis.setEnabled(false);
Legend l = dayChart.getLegend();
l.setEnabled(false);
// XYMarkerView mv = new XYMarkerView(this.getContext(), xAxis.getValueFormatter());
// mv.setChartView(dayChart);
// dayChart.setMarker(mv);
}
private void requestGas(List deviceIds, String period, String time) {
OpenApiManager.getInstance().getUserApi()
.getGasreportStatistics(deviceIds, period, time)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(bindToLifecycle())
.subscribe(response -> {
ArrayList arrayList = (ArrayList) response.data;
ArrayList xFormatter = new ArrayList();
ArrayList dayVals = new ArrayList();
for (int i = 0; i < arrayList.size(); i++) {
ArrayList itemList = (ArrayList) arrayList.get(i);
BarEntry barEntry = new BarEntry(i, Float.valueOf(String.valueOf(itemList.get(1))));
barEntry.setData(itemList.get(0));
dayVals.add(barEntry);
}
XAxis xAxis = dayChart.getXAxis();
DateAxisValueFormatter xAxisFormatter = (DateAxisValueFormatter) xAxis.getValueFormatter();
xAxisFormatter.arrayList = xFormatter;
BarDataSet set;
if (dayChart.getData() != null && dayChart.getData().getDataSetCount() > 0) {
set = (BarDataSet) dayChart.getData().getDataSetByIndex(0);
set.setValues(dayVals);
dayChart.getData().notifyDataChanged();
} else {
set = new BarDataSet(dayVals, "");
set.setDrawIcons(false);
set.setColor(getResources().getColor(R.color.chart_color));
ArrayList dataSets = new ArrayList();
dataSets.add(set);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(0.9f);
dayChart.setData(data);
}
dayChart.notifyDataSetChanged();
}, throwable -> {
ToastUtil.show(this.getContext(), getString(R.string.network_request_failure));
});
}
protected RectF mOnValueSelectedRectF = new RectF();
@Override
public void onValueSelected(Entry e, Highlight h) {
}
@Override
public void onNothingSelected() {
}
}