在这里,我正在尝试使用MPChart
库在图表中显示多个条形。所有值都是动态的,并且可以设置得很好。但是无论长度是24还是7或30,某些x轴标签总是会被隐藏。我试图使用:
if (dayDataList != null && dayDataList.size() > 0) {
chart.getXAxis().setAxisMaximum(dayDataList.size());
} else if (weekDataList != null && weekDataList.size() > 0) {
chart.getXAxis().setAxisMaximum(weekDataList.size());
} else if (monthDataList != null && monthDataList.size() > 0) {
chart.getXAxis().setAxisMaximum(monthDataList.size());
}
但是它对我也不起作用。有时,它也会跳过一些标签。为了进一步说明,还附有屏幕截图
在这里,我试图显示我保存在arrayList
中的最近24小时的数据,但是它隐藏了最后的小节和标签。我进行了调试,发现arraylist
有24条信息,但没有显示。 7天的数据也会发生同样的事情。
显示最近7天的数据,但它增加了条形图本身的大小,也减少了几根条形图。
在这里,它隐藏了一些小节以及some labels are also missing
,所以我尝试使其滚动,从而可以工作。希望您对问题有清楚的了解。有关更多信息,我还将共享代码以及我现在正在使用的代码。
private void initializeView() {
chart.setOnChartValueSelectedListener(this);
chart.setDrawBarShadow(false);
//Hide/Display texts over bars
chart.setDrawValueAboveBar(false);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
chart.setMaxVisibleValueCount(60);
chart.setDrawGridBackground(false);
chart.setScaleEnabled(false);
String[] array = null;
final ArrayList<String> xAxisLabel = new ArrayList<>();
//X-Axis for 24hours data
if (dayDataList != null && dayDataList.size() > 0) {
//TODO : Static Array
array = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24"};
for (int i = 0; i <= dayDataList.size(); i++) {
xAxisLabel.add(String.valueOf(i));
}
}
//X-Axis for 7 days data
else if (weekDataList != null && weekDataList.size() > 0) {
//TODO : Static Array
array = new String[]{"1", "2", "3", "4", "5", "6", "7"};
for (int i = 1; i <= weekDataList.size(); i++) {
xAxisLabel.add(String.valueOf(i));
}
}
//X-Axis for 30 days data
else if (monthDataList != null && monthDataList.size() > 0) {
//TODO : Static Array
array = new String[]{"2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30"};
for (int i = 1; i <= monthDataList.size(); i++) {
xAxisLabel.add(String.valueOf(i));
}
}
//X-axis
ValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart, array);
XAxis xAxis = chart.getXAxis();
xAxis.setCenterAxisLabels(true);//To align Center
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setGranularityEnabled(false); // To remove duplicate values
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
if (dayDataList != null && dayDataList.size() > 0) {
xAxis.setLabelCount(dayDataList.size(), true);
} else if (weekDataList != null && weekDataList.size() > 0) {
xAxis.setLabelCount(weekDataList.size(), true);
} else if (monthDataList != null && monthDataList.size() > 0) {
xAxis.setLabelCount(monthDataList.size(), true);
}
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
Log.e("TAG_VALUE", " is " + (int) value);
return xAxisLabel.get((int) value);
}
});
//Left Side Y-axis
ValueFormatter custom = new MyValueFormatter("$");
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setLabelCount(8, false);
leftAxis.setValueFormatter(custom);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
//Right side Y-axis
YAxis rightAxis = chart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setLabelCount(8, false);
rightAxis.setValueFormatter(custom);
rightAxis.setSpaceTop(15f);
rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
//To hide/Display Y axis labels
rightAxis.setEnabled(false);
leftAxis.setEnabled(false);
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setForm(Legend.LegendForm.SQUARE);
l.setFormSize(9f);
l.setTextSize(11f);
l.setMaxSizePercent(1f);
l.setXEntrySpace(4f);
XYDataMarkerView mv = new XYDataMarkerView(getActivity(), xAxisFormatter);
mv.setChartView(chart); // For bounds control
chart.setMarker(mv); // Set the marker to the chart
//Set Data
setData();
chart.setGridBackgroundColor(Color.rgb(234, 244, 255));//Set as a black
chart.setDrawGridBackground(true);//set this to true to draw the grid background, false if not
chart.invalidate();
}
private void setData() {
List<BarEntry> yVals1 = new ArrayList<BarEntry>();
List<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> values1 = new ArrayList<>();
ArrayList<BarEntry> values2 = new ArrayList<>();
if (dayDataList != null && dayDataList.size() > 0) {
for (int i = 0; i < dayDataList.size(); i++) {
values1.add(new BarEntry(i, (dayDataList.get(i).getDownloadedData()).floatValue()));
values2.add(new BarEntry(i, (dayDataList.get(i).getUploadedData()).floatValue()));
}
} else if (weekDataList != null && weekDataList.size() > 0) {
for (int i = 0; i < weekDataList.size(); i++) {
values1.add(new BarEntry(i, (weekDataList.get(i).getAvdDownloadedData()).floatValue()));
values2.add(new BarEntry(i, (weekDataList.get(i).getAvgUploadedData()).floatValue()));
}
} else if (monthDataList != null && monthDataList.size() > 0) {
for (int i = 0; i < monthDataList.size(); i++) {
values1.add(new BarEntry(i, (monthDataList.get(i).getAvdDownloadedData()).floatValue()));
values2.add(new BarEntry(i, (monthDataList.get(i).getAvgUploadedData()).floatValue()));
}
}
BarDataSet set1, set2;
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set2 = (BarDataSet) chart.getData().getDataSetByIndex(1);
set1.setValues(values1);
set2.setValues(values2);
set1.setValues(yVals1);
set2.setValues(yVals2);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create 2 DataSets
set1 = new BarDataSet(values1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
set2 = new BarDataSet(values2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
}
set1.setDrawIcons(false);
set1.setDrawValues(false);//TODO: To hide/show text above bars
set2.setDrawIcons(false);
set2.setDrawValues(false);
int startColor = ContextCompat.getColor(getActivity(), R.color.colorPrimary);
int endColor = ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark);
set1.setGradientColor(startColor, endColor);
int set2StartColor = ContextCompat.getColor(getActivity(), R.color.colorOrange);
int set2EndColor = ContextCompat.getColor(getActivity(), R.color.colorRedWifi);
set2.setGradientColor(set2StartColor, set2EndColor);
BarData data = new BarData(set1, set2);
data.setValueTextSize(10f);
chart.getLegend().setEnabled(false);
chart.setData(data);
chart.setEnabled(true);
float barSpace = 0.05f;
float groupSpace = 0.1f;
data.setBarWidth(0.42f);
chart.groupBars(0, groupSpace, barSpace);
chart.getXAxis().setAxisMinimum(0);
/* if (dayDataList != null && dayDataList.size() > 0) {
chart.getXAxis().setAxisMaximum(dayDataList.size());
} else if (weekDataList != null && weekDataList.size() > 0) {
chart.getXAxis().setAxisMaximum(weekDataList.size());
} else if (monthDataList != null && monthDataList.size() > 0) {
chart.getXAxis().setAxisMaximum(monthDataList.size());
}*/
}
xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
希望有人能解决我的问题,并能帮助我解决问题。预先感谢。