waqas ali
waqas ali

Reputation: 1238

Sync the two mpandroidcharts graphs dragging and zomming

I am using MPAndroidChart and I have a requirement that i have to sync the dragging and zooming of two graphs, like if i zoom out, zoom in or drag one any of one graph then other graph should be zoomed out, zoomed in or dragged to same extent on X-axis. Example: If i drag the upper graph to 12th point on X-axis then lower graph should also be dragged to 12th point on X-axis automatically.

MyGraphs

Guys i need need some idea how to do this, i am enough familiar with mpchartandroid library.

Upvotes: 3

Views: 1660

Answers (2)

waqas ali
waqas ali

Reputation: 1238

I wrote a function to do this, i am calling this function of Drag and scale listener. Working perfectly.

private void syncCharts(Chart mainChart, LineChart[] otherCharts) {
        Matrix mainMatrix;
        float[] mainVals = new float[9];
        Matrix otherMatrix;
        float[] otherVals = new float[9];
        mainMatrix = mainChart.getViewPortHandler().getMatrixTouch();
        mainMatrix.getValues(mainVals);


        for (LineChart tempChart : otherCharts) {

            otherMatrix = tempChart.getViewPortHandler().getMatrixTouch();
            otherMatrix.getValues(otherVals);
            otherVals[Matrix.MSCALE_X] = mainVals[Matrix.MSCALE_X];
            otherVals[Matrix.MTRANS_X] = mainVals[Matrix.MTRANS_X];
            otherVals[Matrix.MSKEW_X] = mainVals[Matrix.MSKEW_X];
            otherMatrix.setValues(otherVals);
            tempChart.getViewPortHandler().refresh(otherMatrix, tempChart, true);

        }
    }

Upvotes: 6

Philipp Jahoda
Philipp Jahoda

Reputation: 51421

Use the OnChartGestureListener.

https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart

Upvotes: 2

Related Questions