1

I create 60 DataPoint (for every minute in hour) and show them on the graph. Set 7 labels for x axis. Labels with date.

The leftmost label and the rightmost label do not coincide with the beginning and end of the x axis. This screenshot shows mismatches:

bugaga

Code:

private void updateGraph(){

        DataPoint[] dataPoints = new DataPoint[mCurrencyStampList.size()];
        int i = 0;

        Double minY = null;
        Double maxY = 0D;

        for(CurrencyStamp  stamp : mCurrencyStampList){
            dataPoints[i] = new DataPoint(stamp.getDate(), stamp.getClose());
            if(maxY < stamp.getClose()){
                maxY = stamp.getClose().doubleValue();
            }
            if(minY == null || minY > stamp.getClose()){
                minY = stamp.getClose().doubleValue();
            }

            i++;
        }

        LineGraphSeries<DataPoint> points = new LineGraphSeries<>(dataPoints);
        mGraphView.addSeries(points);

        DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext());
        mGraphView.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity(), dateFormat));
        mGraphView.getGridLabelRenderer().setTextSize(32);
        mGraphView.getGridLabelRenderer().setNumHorizontalLabels(7);

        double minX = mCurrencyStampList.get(0).getDate().getTime();
        double maxX = mCurrencyStampList.get(mCurrencyStampList.size()-1).getDate().getTime();
        mGraphView.getViewport().setMinimalViewport(minX, maxX, minY == null ? 0 : minY, maxY);
        mGraphView.getViewport().setXAxisBoundsManual(true);
        mGraphView.getViewport().setYAxisBoundsManual(true);

    }

The divisions of the graph do not coincide in time. How to fix this?

upd: I think I understand why the extreme lines are moving to centre. unix timestamp lose precision when convertion to double. i don't know how fix this. Same problem on Mpandroidchart library. I try trial version of AnyChart, nice worked for me (in constructor of points used own format. not float and double) but this trial

Ilya Mashin
  • 904
  • 1
  • 12
  • 28

1 Answers1

0

I migrate to MPAndroid and applied coefficient. Just set the minimum and maximum along the x axis:

xAxis.setAxisMinimum(0);
xAxis.setAxisMaximum(100000);

Create coefficient:

long xMin = mCurrencyStampList.get(0).getDate().getTime();
long xMax = mCurrencyStampList.get(mCurrencyStampList.size() - 1).getDate().getTime();
float xCoefficient = (xMax - xMin) / 100000;

Multiple x value to different:

long currentTS = s.getDate().getTime();
Float x = (currentTS - xMin) / xCoefficient;

And in the widget display the return value:

XAxis xAxis = mLineChart.getXAxis();
xAxis.setValueFormatter((value, axis) -> {
            Long time = (long) (value * xCoefficient + xMin);
            Date date = new Date(time);
            DateFormat format;
            if(mInterval == Interval.OneHour || mInterval == Interval.SixHours || mInterval == Interval.OneDay){
                format = android.text.format.DateFormat.getTimeFormat(getContext());
            }
            else {
                format = android.text.format.DateFormat.getDateFormat(getContext());
            }
            return format.format(date);
        });

not a perfect solution but works for unix timestamp

Ilya Mashin
  • 904
  • 1
  • 12
  • 28