Java 如何在图表上显示数据集?

Java 如何在图表上显示数据集?,java,android,mpandroidchart,Java,Android,Mpandroidchart,我想在图表上显示金额和日期作为月份值。为此我搜索了图书馆 但我不知道如何添加数据集并将日期转换为月份 我想这样显示图形: 我有一个订单对象列表,上面有金额和日期 我尝试了以下代码: totalOrdersList = new ArrayList<>(); mChart = (BarChart) view.findViewById(R.id.chart); // mChart.setOnChartValueSelectedListener(Das

我想在图表上显示金额和日期作为月份值。为此我搜索了图书馆

但我不知道如何添加数据集并将日期转换为月份

我想这样显示图形:

我有一个订单对象列表,上面有金额和日期

我尝试了以下代码:

    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();


    }
totalOrdersList=new ArrayList();
mChart=(条形图)view.findviewbyd(R.id.chart);
//mChart.setOnChartValueSelectedListener(仪表板2Fragment.this);
//mChart.SetHighlightabled(假);
mChart.settrabarshadow(假);
mChart.SetDrawValueUpperBar(真实);
setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.lightGrey));
mChart.getDescription().setEnabled(false);
//如果图表中显示的条目超过60个,则不会显示任何值
//画
mChart.setMaxVisibleValueCount(60);
//现在只能分别在x轴和y轴上进行缩放
mChart.setPinchZoom(假);
//为显示最大值的每个条绘制阴影
//mChart.settrabarshadow(真);
mChart.setDrawGridBackground(假);
XAxis xl=mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(真);
xl.setDrawGridLines(假);
xl.设定粒度(10f);
YAxis yl=mChart.getAxisLeft();
yl.setDrawAxisLine(真);
yl.setDrawGridLines(真);
yl.setaxis最小值(0f);//这将替换SetStartZero(真)
//yl.setinversed(真);
YAxis yr=mChart.getAxisRight();
yr.setDrawAxisLine(真实);
年设置图网格线(假);
年设置轴最小值(0f);//这将替换SetStartZero(真)
//yr.SetInversed(真);
//setData();
mChart.setFitBars(真);
麦克哈特·阿尼泰(2500);
图例l=mChart.getLegend();
l、 setVerticalAlignment(图例.Legend VerticalAlignment.底部);
l、 setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l、 设置方向(图例.腿方向.水平);
l、 setDrawInside(假);
l、 setFormSize(8f);
l、 setXEntrySpace(4f);
私有void setData(){
mChart.setScaleEnabled(错误);
int i=0;
barEntries=新的ArrayList();
ArrayList amountArray=新的ArrayList();
ArrayList dateArray=新的ArrayList();
for(订单:totalOrdersList)
{
add(order.getAmount());
add(order.getDate());
}
for(字符串金额:金额)
{
添加(新的BarEntry(1,Float.parseFloat(amount));
}
BarDataSet completed=新的BarDataSet(barEntries,“金额”);
已完成。设定值(barEntries);
已完成。setValueTextColor(Color.WHITE);
已完成。setValueTextSize(12f);
图例=新图例();
legend=mChart.getLegend();
图例.setEnabled(真);
mChart.setEnabled(假);
mChart.setPinchZoom(假);
mChart.SetHighlights不允许(错误);
mChart.SetDrawValueUpperBar(真实);
mChart.setDoubleTaptoZoomeEnabled(假);
mChart.setHighlightPerDragEnabled(假);
mChart.setDragDecellerationEnabled(假);
XAxis xl=mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(真);
xl.setDrawGridLines(假);
xl.setDrawLabels(真);
xl.setTextColor(颜色为白色);
mChart.setDrawGridBackground(假);
YAxis yl=mChart.getAxisLeft();
yl.setDrawAxisLine(真);
yl.setDrawGridLines(假);
yl.setDrawLabels(真);
YAxis yll=mChart.getAxisRight();
yll.setDrawAxisLine(假);
yll.setAxisLineColor(颜色:白色);
yll.setDrawGridLines(假);
yll.setDrawLabels(假);
ArrayList数据集=新的ArrayList();
数据集。添加(已完成);
BarData数据=新的BarData(数据集);
mChart.setData(数据);
mChart.invalidate();
}
但是得到这样的图表:


出什么事了

问题:

您正在将所有
barEntries
设置为相同的索引

for (String amount : amountArray)
    {
        barEntries.add(new BarEntry(1, Float.parseFloat(amount)));
    }
您需要按如下方式进行更改:

int index = 0;

for (String amount : amountArray)
    {
        barEntries.add(new BarEntry(index, Float.parseFloat(amount)));
        index++;
    }
编辑:

现在要在X轴上显示日期,这里有一个很好的例子。您可以创建另一个
long
arrayList
,以毫秒为单位存储日期

private List<Long> timestampList = new ArrayList<>();

for (String dateString : dateArray) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMMM-dd");
            Date date = sdf.parse(dateString);

            long startDate = date.getTime();

            Log.d("timestamp:", String.valueOf(startDate));

            timestampList.add(startDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
最后,要设置X轴的值,可以调用

xl.setGranularity(1f); //So that the X-Axis values become 0, 1, 2, and so on...to query the timestampList() index
xl.setValueFormatter(formatter);

将为每个X轴值调用
getFormattedValue
,然后您可以相应地获得月份

如何将日期添加到x轴??我现在正在获取x轴和y轴的金额。如上所述,使用IAxisValueFormatter日期不是随机值,它们需要从totalOrderList获取并显示在x轴上。从totalOrderList获取的日期值是什么?{“金额”:“5506.4300”,“添加日期”:“2017-5-17”},值采用这种格式
xl.setGranularity(1f); //So that the X-Axis values become 0, 1, 2, and so on...to query the timestampList() index
xl.setValueFormatter(formatter);