MPAndroidChart:让一个图形反映姊妹图形上的缩放/滑动 [英] MPAndroidChart: Have one graph mirror the zoom/swipes on a sister graph
问题描述
我有两个折线图显示了同一时间段内的互补数据.有没有办法将一个图形接收到的任何触摸事件发送到另一个图形?我基本上想要它,以便图形始终显示相同的查看矩形(至少在水平方向上).因此,如果用户在顶部图形上向左滑动n"个单位,底部图形将自动向左滚动n"个单位以进行匹配.
I have two line graphs that show complementary data over the same time period. Is there any way to send any touch events received by one graph to the other? I essentially want it so that the graphs always show the same viewing rectangle (at least on horizontally). So if a user swipes left 'n' units on the top graph, the bottom graph will automatically scroll left 'n' units to match.
推荐答案
可以使用当前的 MPAndroidChart 版本进行缩放,用于滚动签出或等待我的扩展被合并:https://github.com/PhilJay/MPAndroidChart/pull/545
Zooms can be done with the current MPAndroidChart release, for scrolling check out or wait for my extension to be merged: https://github.com/PhilJay/MPAndroidChart/pull/545
您需要在主图上设置一个 OnChartGestureListener 来将平移矩阵值复制到从图:
You need to set up an OnChartGestureListener on the master chart that copies the translation matrix values to the slave charts:
public class CoupleChartGestureListener implements OnChartGestureListener {
private Chart srcChart;
private Chart[] dstCharts;
public CoupleChartGestureListener(Chart srcChart, Chart[] dstCharts) {
this.srcChart = srcChart;
this.dstCharts = dstCharts;
}
[...other overrides...]
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
//Log.d(TAG, "onChartScale " + scaleX + "/" + scaleY + " X=" + me.getX() + "Y=" + me.getY());
syncCharts();
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
//Log.d(TAG, "onChartTranslate " + dX + "/" + dY + " X=" + me.getX() + "Y=" + me.getY());
syncCharts();
}
public void syncCharts() {
Matrix srcMatrix;
float[] srcVals = new float[9];
Matrix dstMatrix;
float[] dstVals = new float[9];
// get src chart translation matrix:
srcMatrix = srcChart.getViewPortHandler().getMatrixTouch();
srcMatrix.getValues(srcVals);
// apply X axis scaling and position to dst charts:
for (Chart dstChart : dstCharts) {
if (dstChart.getVisibility() == View.VISIBLE) {
dstMatrix = dstChart.getViewPortHandler().getMatrixTouch();
dstMatrix.getValues(dstVals);
dstVals[Matrix.MSCALE_X] = srcVals[Matrix.MSCALE_X];
dstVals[Matrix.MTRANS_X] = srcVals[Matrix.MTRANS_X];
dstMatrix.setValues(dstVals);
dstChart.getViewPortHandler().refresh(dstMatrix, dstChart, true);
}
}
}
}
然后像这样设置主/从连接:
Then set up your master/slave connections for example like this:
//
// Couple chart viewports:
//
tripChart.setOnChartGestureListener(new CoupleChartGestureListener(
tripChart, new Chart[] { powerChart, energyChart }));
powerChart.setOnChartGestureListener(new CoupleChartGestureListener(
powerChart, new Chart[] { tripChart, energyChart }));
energyChart.setOnChartGestureListener(new CoupleChartGestureListener(
energyChart, new Chart[] { tripChart, powerChart }));
这篇关于MPAndroidChart:让一个图形反映姊妹图形上的缩放/滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!