MPAndroid Group bar chart is not showing [duplicate]

≯℡__Kan透↙ 提交于 2019-12-19 13:48:26

问题


Here is the code i used for group bar_chart using MPAndroid library version 3. I've tried group bar chart in lower version and it works fine but the BarData constructor is changed in latest version. And this code doesn't works. There is no crashes and error logs, still graph is loading and no data shows in it. Help me to find ma flaws

        List<BarEntry> entriesGroup1 = new ArrayList<>();
        List<BarEntry> entriesGroup2 = new ArrayList<>();
        List<BarEntry> entriesGroup3 = new ArrayList<>();

        entriesGroup1.add(new BarEntry(0, 8f));
        entriesGroup1.add(new BarEntry(1, 2f));
        entriesGroup1.add(new BarEntry(2, 5f));
        entriesGroup1.add(new BarEntry(3, 20f));
        entriesGroup1.add(new BarEntry(4, 15f));
        entriesGroup1.add(new BarEntry(5, 19f));

        entriesGroup2.add(new BarEntry(0, 6f));
        entriesGroup2.add(new BarEntry(1, 10f));
        entriesGroup2.add(new BarEntry(2, 5f));
        entriesGroup2.add(new BarEntry(3, 25f));
        entriesGroup2.add(new BarEntry(4, 4f));
        entriesGroup2.add(new BarEntry(5, 17f));

        entriesGroup3.add(new BarEntry(0, 9f));
        entriesGroup3.add(new BarEntry(1, 1f));
        entriesGroup3.add(new BarEntry(2, 15f));
        entriesGroup3.add(new BarEntry(3, 13f));
        entriesGroup3.add(new BarEntry(4, 40f));
        entriesGroup3.add(new BarEntry(5, 25f));

        BarDataSet set1 = new BarDataSet(entriesGroup1, "Group 1");
        BarDataSet set2 = new BarDataSet(entriesGroup2, "Group 2");
        BarDataSet set3 = new BarDataSet(entriesGroup3, "Group 3");

        final ArrayList<String> labels = new ArrayList<String>();
        labels.add("2016");
        labels.add("2015");
        labels.add("2014");
        labels.add("2013");
        labels.add("2012");
        labels.add("2011");
        IAxisValueFormatter formatter = new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {

                if((int) value < 0 || (int) value >= labels.size()){
                    return "";
                }else{
                    return labels.get((int) value);
                }
            }

            // we don't draw numbers, so no decimal digits needed
            @Override
            public int getDecimalDigits() {  return 0; }
        };

        set1.setColor(Color.parseColor("#cd5080"));
        set2.setColor(Color.parseColor("#0d5080"));
        set3.setColor(Color.parseColor("#fc5080"));;

        float groupSpace = 0.06f;
        float barSpace = 0.02f; // x2 dataset
        float barWidth = 0.45f; // x2 dataset
// (0.02 + 0.45) * 2 + 0.06 = 1.00 -> interval per "group"

        XAxis xAxis = barChart.getXAxis();
        xAxis.setCenterAxisLabels(true);
        xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
        xAxis.setValueFormatter(formatter);

        BarData data = new BarData(set1, set2, set3);
        data.setBarWidth(barWidth); // set the width of each bar
        barChart.setData(data);
        barChart.groupBars(2016, groupSpace, barSpace); 
        barChart.invalidate(); // refresh

        barChart.animateY(5000);

Nb: I'm edited my current question since i'm banned to ask new questions. But its important for me. Thanks everyone.


回答1:


This is not my answer, i have seen this ques before and impleneted in my app it worked like a charm. Couldn't find the link to the original answer so i am posting the code that i used. You can use ActivityLifecycleCallbacks :

public class AppTransitionStatus implements Application.ActivityLifecycleCallbacks {

private static final long MAX_BACKGROUND_TIME = 2000;
private Timer timer;
private TimerTask task;
private boolean isBackground;

private final Transition transition;

private void goToForeground(Activity iActivity) {
    transition.appToForeground(iActivity);
}

private void goToBackground() {
    transition.appToBackground();
}

private void stopTimer() {
    if (task != null) task.cancel();
    if (timer != null) timer.cancel();
}

public AppTransitionStatus(@NonNull Transition transition) {
    this.transition = transition;
}

@Override
public void onActivityCreated(Activity activity, Bundle bundle) {

}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {
    if (isBackground) {
        goToForeground(activity);
    }
    stopTimer();
    isBackground = false;
}

@Override
public void onActivityPaused(Activity activity) {
    timer = new Timer();
    task = new TimerTask() {
        public void run() {
            isBackground = true;
            goToBackground();
        }
    };
    timer.schedule(task, MAX_BACKGROUND_TIME);
}

@Override
public void onActivityStopped(Activity activity) {

}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

}

@Override
public void onActivityDestroyed(Activity activity) {

}

public interface Transition {
    void appToForeground(Activity iActivity);

    void appToBackground();
}
}

And then you can register this in your application class

 registerActivityLifecycleCallbacks(new AppTransitionStatus(new AppTransitionStatus.Transition() {
        @Override
        public void appToForeground(Activity iActivity) {

            Log.d("Transition", "App in Foreground");

        }

        @Override
        public void appToBackground() {
            Log.d("Transition", "App in Background");
        }
    }));



回答2:


Create a Application class and add counter of foreground activities, if counter is 0 means your activity is in background

class MyApp extends Application{
 public int foregroundActivities  = 0 ;

 @override
 public void onCreate(){
     super.onCreate();
     this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

                @Override
                public void onActivityStarted(Activity activity) {
                    foregroundActivities++;
                }

                @Override
                public void onActivityStopped(Activity activity) {
                    foregroundActivities--;
                }
        }
   }
}

EDIT

To answer your question, Application class is singleton to Lifecycle so you can register Activity Life cycle call backs as above which will be triggered by all your 87 activities.

Suppose, If any of your 87 activities is started, it will call onActivityStarted() method, where the count of foreground activity will become 1, if activity is closes, it will call onActivityStopped() which will decrement count of foreground activities back to 0.

So you can detect whether application is in foreground or background based on counter,

if(applicationInstance.foregroundActivities > 0){
    //App is in foreground
 }else{
    //App is in background
 }



回答3:


Use blow code :

    public boolean isActivityRunning(Context context) {
   boolean isActivityFound = false;
    ActivityManager activityManager = (ActivityManager)context.getSystemService (Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (int i = 0; i < activitys.size(); i++) {
        if (activitys.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.example.testapp/com.example.testapp.Your_Activity_Name}")) {
            isActivityFound = true;
        }
    }
    return isActivityFound;
}

here com.example.testapp is your package name

need to add the permission to your manifest..

<uses-permission  android:name="android.permission.GET_TASKS"/>

it returns u activity running or not by using name on it and also change as your requirement .

UPDATE

public static ArrayList<ActivityInfo> getAllRunningActivities(Context context) {
try {
    PackageInfo pi = context.getPackageManager().getPackageInfo(
            context.getPackageName(), PackageManager.GET_ACTIVITIES);

    return new ArrayList<>(Arrays.asList(pi.activities));

} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
    return null;
}
}

This method give u list of all running activitys in your project




回答4:


You can use the activity hasWindowFocus() boolean method. Then if it has focus you display a pop-up from the activity, if not, you just display the notification.



来源:https://stackoverflow.com/questions/47690661/mpandroid-group-bar-chart-is-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!