DateTime axis in MPAndroidChart

2020-07-22 19:35发布

问题:

I am using MPAndroidChart in my app. I want to plot the chart using values on yAxis and dates on the xAxis. I didn't find any datetime axis in the MPAndroidChart API. Is there any way to plot the xAxis using date? I have values to plot against dates. I want to show the dates in (ddMMM-yy) format on xAxis and the values on yAxis. Can anyone can pass me the sample link for the same?

Sample data:

Date(xAxis) | Value(yAxis)
------------|-------------
01/01/2001  |  966.78
01/02/2001  |  666.78
01/03/2001  |  966.78
01/04/2001  |  966.78
01/05/2001  |  966.78
01/06/2001  |  966.78
01/07/2001  |  966.78

回答1:

It is one of the oldest enhancements. Unfortunatelly MPAndroid does not support it yet. Source: https://github.com/PhilJay/MPAndroidChart/issues/12 https://github.com/PhilJay/MPAndroidChart/issues/133



回答2:

I think you can Adjust the Xaxis array according to your dates and parse it to the chart like

           if (mRecordDate.equals(mCurrentDate)) {
                        float weightVal = 0.0f, bmiValue = 0.0f;
                        try {
                            weightVal = (float) (profileWeightInfo.getWeight());
                        } catch (Exception e) {
                            Log.e("Error" + "Parsing weightVal Entry");
                        }
                        try {
                            bmiValue = (float) (profileWeightInfo.getBmi());
                        } catch (Exception e) {
                            Log.e("Error" + "Parsing diaVal Entry");
                        }
                        valuesWeights.add(new Entry(weightVal, counter));
                        valueBMI.add(new Entry(bmiValue, counter));
                        xVals.add(mRecordDate);

                    }
                }
                if (xVals.indexOf(mCurrentDate) < 0) {
                    valuesWeights.add(new Entry(0, counter));
                    valueBMI.add(new Entry(0, counter));
                    xVals.add(mCurrentDate);
                }
                counter++;

Thanks



回答3:

I know it's late but it might be helpful for someone.

You can store the dates in an array and then can display it on the x-axis of the chart using setValueFormatter property of chart. At the top declare dev array. final String dev1[] = new String[100];

Then where you have your chart

   final JSONObject object = jsonArray.getJSONObject(i);

   dev1[i] = object.getString("real_time");
   barEntries.add(new BarEntry(i, Float.parseFloat(value)));
   final BarDataSet barDataSetgen = new BarDataSet(barEntries, " ");

   XAxis bottomAxis = mcvbarchart.getXAxis();

   bottomAxis.setValueFormatter(new IndexAxisValueFormatter(dev1));   /*for x axis values*/
   bottomAxis.setLabelCount(barEntries.size());
   bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);