1
我想在图表上显示金额和日期作为月份值。为此我搜索了图书馆MPAndroid Chart。如何在图MPAndroidChart上显示数据集?
但我没有得到如何添加数据集并将日期转换为月份。
我想说明图是这样的:
我有拥有量和日期的顺序对象列表。
我试过这段代码:
totalOrdersList = new ArrayList<>();
mChart = (BarChart) view.findViewById(R.id.chart);
// mChart.setOnChartValueSelectedListener(Dashboard2Fragment.this);
// mChart.setHighlightEnabled(false);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.lightGrey));
mChart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
mChart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
xl.setGranularity(10f);
YAxis yl = mChart.getAxisLeft();
yl.setDrawAxisLine(true);
yl.setDrawGridLines(true);
yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
// yl.setInverted(true);
YAxis yr = mChart.getAxisRight();
yr.setDrawAxisLine(true);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f); // this replaces setStartAtZero(true)
// yr.setInverted(true);
// setData();
mChart.setFitBars(true);
mChart.animateY(2500);
Legend l = mChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setFormSize(8f);
l.setXEntrySpace(4f);
private void setData() {
mChart.setScaleEnabled(false);
int i=0;
barEntries = new ArrayList<>();
ArrayList<String> amountArray = new ArrayList<>();
ArrayList<String> dateArray = new ArrayList<>();
for(Order order : totalOrdersList)
{
amountArray.add(order.getAmount());
dateArray.add(order.getDate());
}
for (String amount : amountArray)
{
barEntries.add(new BarEntry(1,Float.parseFloat(amount)));
}
BarDataSet completed = new BarDataSet(barEntries, "Amount");
completed.setValues(barEntries);
completed.setValueTextColor(Color.WHITE);
completed.setValueTextSize(12f);
Legend legend = new Legend();
legend = mChart.getLegend();
legend.setEnabled(true);
mChart.setEnabled(false);
mChart.setPinchZoom(false);
mChart.setHighlightPerTapEnabled(false);
mChart.setDrawValueAboveBar(true);
mChart.setDoubleTapToZoomEnabled(false);
mChart.setHighlightPerDragEnabled(false);
mChart.setDragDecelerationEnabled(false);
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
xl.setDrawLabels(true);
xl.setTextColor(Color.WHITE);
mChart.setDrawGridBackground(false);
YAxis yl = mChart.getAxisLeft();
yl.setDrawAxisLine(true);
yl.setDrawGridLines(false);
yl.setDrawLabels(true);
YAxis yll = mChart.getAxisRight();
yll.setDrawAxisLine(false);
yll.setAxisLineColor(Color.WHITE);
yll.setDrawGridLines(false);
yll.setDrawLabels(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(completed);
BarData data = new BarData(dataSets);
mChart.setData(data);
mChart.invalidate();
}
,但像这样得到图:
什么错?
如何将日期添加到x轴?我现在正在获取x轴和y轴的数量。 – Sid
如上所述使用IAxisValueFormatter –
日期不是随机值,它们需要从totalOrderList中获取并显示在x轴上。 – Sid