Skip to content

After scroll the chart then reset data and set chart.moveViewToX did not work? #4270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
t2314862168 opened this issue Oct 24, 2018 · 7 comments

Comments

@t2314862168
Copy link

t2314862168 commented Oct 24, 2018

Fisrt I use chart.setData to show data in ui , then i scroll x from right to left,then reset data used chart.setData and chart.moveViewToX(0); the follow is my code

  CombinedData combinedData = new CombinedData();
        combinedData.setData(getBarData(barChartY1, barChartY2, barName1, barName2, barColor1, barColor2));
        combinedData.setData(getLineData(barChartY1, barChartY2, barName1 + "曲线", barName2 + "曲线", barColor1, barColor2));
        mCombinedChart.setData(combinedData);
        mCombinedChart.setVisibleXRange(0, showCount);
        mCombinedChart.setVisibleXRangeMaximum(showCount);
        mCombinedChart.moveViewToX(0);

if first set 31 data , then scrolled the chart ; after reset 12 data the chart just like remain the tranX in the xAis and xAxis draw first data not from index=0; I need xAxis both from first entry data from screen left to show.
@PhilJay Sorry for my poor english , could you help me please

@t2314862168
Copy link
Author

finally i found a solution,before use moveViewToX use BarLineChartTouchListener.stopDeceleration() ,so just used the folllow code :

CombinedData combinedData = new CombinedData();
        combinedData.setData(getBarData(barChartY1, barChartY2, barName1, barName2, barColor1, barColor2));
        combinedData.setData(getLineData(barChartY1, barChartY2, barName1 + "曲线", barName2 + "曲线", barColor1, barColor2));
        mCombinedChart.clear();
        mCombinedChart.fitScreen();
        Method method = ReflectUtils.getMethod(listener.getClass(), "stopDeceleration", null);
        try {
            method.invoke(listener, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mCombinedChart.setData(combinedData);
        mCombinedChart.setVisibleXRange(0, showCount);
        mCombinedChart.setVisibleXRangeMaximum(showCount);
        //在拖动之后马上切换数据源的话,由于数据还在draw,需要等一段时间才能draw;所以调用moveViewToX没用
        mCombinedChart.moveViewToX(-0.5f);
 //打印日志
        mCombinedChart.setLogEnabled(true);
        //获取BarLineChartTouchListener
        listener = (BarLineChartTouchListener) ReflectUtils.getField(mCombinedChart, "mChartTouchListener");

and the need ReflectUtils.java is:

package com.tangxb.killdebug.boss.util;

import android.text.TextUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * <a href="https://github.com/iReaderAndroid/ZeusPlugin/blob/master/ZeusPlugin/src/main/java/zeus/plugin/PluginUtil.java">反射的方式</a><br>
 * <a href="https://blog.csdn.net/qq_35146878/article/details/78504268">java 反射机制 之 getMethod</a><br>
 */
public class ReflectUtils {
    /**
     * 反射的方式设置某个类的成员变量的值
     *
     * @param paramClass  类对象
     * @param paramString 域的名称
     * @param newClass    新的对象
     */
    public static void setField(Object paramClass, String paramString, Object newClass) {
        if (paramClass == null || TextUtils.isEmpty(paramString)) return;
        Field field = null;
        Class cl = paramClass.getClass();
        for (; field == null && cl != null; ) {
            try {
                field = cl.getDeclaredField(paramString);
                if (field != null) {
                    field.setAccessible(true);
                }
            } catch (Throwable ignored) {

            }
            if (field == null) {
                cl = cl.getSuperclass();
            }
        }
        if (field != null) {
            try {
                field.set(paramClass, newClass);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        } else {
            System.err.println(paramString + " is not found in " + paramClass.getClass().getName());
        }
    }

    /**
     * 反射的方式获取某个类的方法
     *
     * @param cl             类的class
     * @param name           方法名称
     * @param parameterTypes 方法对应的输入参数类型
     * @return 方法
     */
    public static Method getMethod(Class cl, String name, Class... parameterTypes) {
        Method method = null;
        for (; method == null && cl != null; ) {
            try {
                method = cl.getDeclaredMethod(name, parameterTypes);
                if (method != null) {
                    method.setAccessible(true);
                }
            } catch (Exception ignored) {

            }
            if (method == null) {
                cl = cl.getSuperclass();
            }
        }
        return method;
    }

    /**
     * 反射的方式获取某个类的某个成员变量值
     *
     * @param paramClass  类对象
     * @param paramString field的名字
     * @return field对应的值
     */
    public static Object getField(Object paramClass, String paramString) {
        if (paramClass == null) return null;
        Field field = null;
        Object object = null;
        Class cl = paramClass.getClass();
        for (; field == null && cl != null; ) {
            try {
                field = cl.getDeclaredField(paramString);
                if (field != null) {
                    field.setAccessible(true);
                }
            } catch (Exception ignored) {

            }
            if (field == null) {
                cl = cl.getSuperclass();
            }
        }
        try {
            if (field != null)
                object = field.get(paramClass);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return object;
    }
}

@Lesincs
Copy link

Lesincs commented Dec 29, 2018

Amazing! it is work for me! How did you find it?

@MatteCarra
Copy link

Thanks a lot.
It worked

@Oleksandr32
Copy link

Oleksandr32 commented Dec 18, 2019

Hi, I fixed it without java reflection. For BarChart
(Kotlin)
chart.fitScreen()
(chart.onTouchListener as BarLineChartTouchListener).stopDeceleration()

@weiaiSanli
Copy link

厉害,太牛啦,困容我三天的问题啊

@wangshanhai
Copy link

厉害了,太牛了

@zmyqucai8
Copy link

6666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants