Search code examples
androidchartsandroid-graphview

Android Graphview changing x axis data


in my app I'm using GraphView library to create a LineGraphView. It shows data from specified dates from example from 18.01 to 30.01. When I change the boundaries for example from 25.01 to 29.01 GraphView updates the chart (cuts not needed lines) but still shows wrong x axis data (18.01 to 30.01), however when user starts to interact with chart (zooming, scrolling) it updates it's x axis and everything is fine. Is there a method to force GraphView to update it's x axis? I have only encountered this problem if user has been interacting with chart (zooming, scrolling) before changing dates if there was no interaction graphview updates properly. I'm creating charts this way:

Inside onCreate:

mGraphView = new LineGraphView(this, "Temperature Measurements");

And a function to create a chart when I fetch data from database:

public void createChart(Cursor cursor) {
        GraphViewData[] values = new GraphViewData[cursor.getCount()];
        while (cursor.moveToNext()) {
            String time = cursor.getString(cursor
                    .getColumnIndex(MeasurementEntry.DATE_IN_MINUTES));
            String temp = cursor.getString(cursor
                    .getColumnIndex(MeasurementEntry.TEMPERATURE));
            values[cursor.getPosition()] = new GraphViewData(
                    Double.parseDouble(time), Double.parseDouble(temp));
        }

        mGraphView.removeAllSeries();
        mGraphView.addSeries(new GraphViewSeries(values));
        mGraphView.setScalable(true);
        mGraphView.getGraphViewStyle().setTextSize(15);

        final SimpleDateFormat formatter = new SimpleDateFormat(
                "dd.MM.yyyy HH:mm");
        mGraphView.setCustomLabelFormatter(new CustomLabelFormatter() {

            @Override
            public String formatLabel(double value, boolean isValueX) {
                if (isValueX) {
                    return formatter.format(new Date((long) value));
                }
                return null;
            }
        });
    }

And here is the screenshot with the problem:

enter image description here


Solution

  • OK, so I made it however I think it can be done better, what I done is removing graphView from parent layout, creating new graphview and adding new graphview - I do this sequence everytime user is changing the boundaries. Maybe it will help somebody (short snippet of createChart method, container is my parent layout):

    container.removeView(mGraphView);
    mGraphView = new LineGraphView(this, "Temperature Measurements");
    container.addView(mGraphView);
    mGraphView.removeAllSeries();
    mGraphView.addSeries(new GraphViewSeries(values));
    mGraphView.setScalable(true);
    mGraphView.getGraphViewStyle().setTextSize(15);