我有两个折线图,它们显示了同一时间段内的互补数据。有没有办法将一个图形接收到的任何触摸事件发送到另一个图形?我本质上想要它,以便图表始终显示相同的查看矩形(至少在水平方向上)。因此,如果用户在顶部图形上向左滑动“n”个单位,底部图形将自动向左滚动“n”个单位以匹配。
问问题
3162 次
2 回答
16
可以使用当前的 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 }));
于 2015-04-12T18:22:33.967 回答
2
您需要将此添加到 CoupleChartGestureListener
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
((BarLineChartTouchListener) srcChart.getOnTouchListener()).stopDeceleration();
for (Chart dstChart : dstCharts) {
if (dstChart.getVisibility() == View.VISIBLE) {
((BarLineChartTouchListener) dstChart.getOnTouchListener()).stopDeceleration();
}
}
}
于 2018-09-11T06:05:32.793 回答