作者:勇士8853 | 来源:互联网 | 2023-05-21 21:20
我有两个线图,显示同一时间段内的补充数据.有没有办法将一张图表收到的任何触摸事件发送给另一张图表?我基本上想要它,以便图形始终显示相同的查看矩形(至少在水平方向上).因此,如果用户在顶部图表上向左滑动"n"个单位,则底部图表将自动向左滚动"n"个单位以匹配.
1> 小智..:
可以使用当前的MPAndroidChart版本完成缩放,滚动签出或等待我的扩展合并:https:
//github.com/PhilJay/MPAndroidChart/pull/545
您需要在主图表上设置一个OnChartGestureListener,将转换矩阵值复制到从属图表:
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);
}
}
}
}
然后设置主/从连接,例如:
//
// 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 }));