Reputation: 802
According to the photo. I'm using HorizontalBarChart
in MPAndroidChart
. I just want let the chart show about 10 items in x
direction of HorizontalBarChart
. I setVisibleXRangeMinimum
and setVisibleXRangeMaxmum
but it not working. What should i do to make it work???
Edit Jun 7 14:50
initialization
private void initCombinedChart() {
horizontalBarChart.setDescription("");
horizontalBarChart.setDrawGridBackground(false);
horizontalBarChart.setDrawBarShadow(false);
horizontalBarChart.getLegend().setTextColor(Color.rgb(255, 255, 255));
horizontalBarChart.setScaleEnabled(false);
horizontalBarChart.setOnChartValueSelectedListener(this);
horizontalBarChart.setDrawValueAboveBar(true);
Legend legend = horizontalBarChart.getLegend();
legend.setFormSize(8f);
YAxis rightAxis = horizontalBarChart.getAxisRight();
rightAxis.setEnabled(false);
YAxis leftAxis = horizontalBarChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinValue(0f);
leftAxis.setTextColor(Color.rgb(255, 255, 255));
leftAxis.setEnabled(false);
XAxis xAxis = horizontalBarChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
}
setData
private void setData() {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
ArrayList<String> xVals = new ArrayList<>();
DateHelper dateHelper = new DateHelper(DateHelper.FORMAT_ONLINE);
for (OnlineCountResult.OnlineCountModel model : mDatas) {
xVals.add(dateHelper.getDateSimpleDate(model.getBatchMinute()));
entries.add(new BarEntry((float) model.getCountNum(), mDatas.indexOf(model)));
}
BarDataSet set;
if (horizontalBarChart.getData() != null && horizontalBarChart.getData().getDataSetCount() > 0) {
set = (BarDataSet) horizontalBarChart.getData().getDataSetByIndex(0);
set.setYVals(entries);
horizontalBarChart.getData().setXVals(xVals);
horizontalBarChart.getData().notifyDataChanged();
horizontalBarChart.notifyDataSetChanged();
} else {
...
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set);
BarData data = new BarData(xVals, dataSets);
data.setValueTextSize(20f);
horizontalBarChart.setData(data);
horizontalBarChart.setVisibleXRangeMaximum(10);
horizontalBarChart.setVisibleXRangeMinimum(7);
horizontalBarChart.animateY(1000);
}
}
Upvotes: 1
Views: 1037
Reputation: 1799
The problem is, you are setting the dataset to chart and then you are setting its X Range instead of setting X Range before setting the dataset to chart.
You can add either following code in else part at the end:
horizontalBarChart.invalidate();
horizontalBarChart.refreshDrawableState();
Or following code in your initCombinedChart:
horizontalBarChart.setVisibleXRangeMaximum(10);
horizontalBarChart.setVisibleXRangeMinimum(7);
Edit 1:
Try with setVisibleYRangeMaximum
instead of setVisibleXRangeMaximum
.
Upvotes: 1