问题
Here, I'm trying to show multiple bars in a chart using MPChart
library. All values are dynamic and getting set fine. But some x-axis labels are always getting hidden whether length is 24 or 7 or 30. I tried to make it scrollable using :
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());
}
But its also not working for me. And sometimes, it skips some labels too. For more clarification, attaching screenshots also
Here I'm trying to show data of last 24 hours which i'm holding in an arrayList
but it's hiding last bars as well as the labels. I debugged and got that arraylist
has 24 balues but its not getting displayed. Same thing happening with 7 days of data.
Displaying data of last 7 days but it increased the size of bars itself and cut off the few bars as well.
Here, its hiding some bars as well as some labels are also missing
so I tried to make it scrollable which dint work. Hope you got the clear picture of the issue. For more I'm sharing the code also what I'm using right now.
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());
}*/
}
And xml
looks like:
<?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>
Hope someone will get my problem and can help me out of it. Thanks in advance.
来源:https://stackoverflow.com/questions/55613135/mpchart-library-is-hiding-x-axis-labels-including-bars