MPAndroid Group条形图未显示

时间:2017-12-07 08:28:15

标签: android bar-chart mpandroidchart

以下是我用于使用MPAndroid库版本3的组bar_chart的代码。我在较低版本中尝试了组条形图,它工作正常,但BarData构造函数在最新版本中已更改。而这段代码不起作用。没有崩溃和错误日志,仍然正在加载图表,并且没有数据显示在其中。帮我找到马瑕疵

        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:我已经编辑了我当前的问题,因为我被禁止提出新问题。但它对我很重要。谢谢大家。

4 个答案:

答案 0 :(得分:1)

创建Application课程并添加前台活动的计数器,如果计数器为0则表示您的activity位于后台

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--;
                }
        }
   }
}

修改

要回答您的问题,Application类是生命周期的单例,因此您可以注册上述活动生命周期回调,这将由您的所有87个活动触发。

假设, 如果您的activities中的任何一个已启动,则会调用onActivityStarted()方法,其中前景活动的计数将变为1,如果activity关闭,则会调用{ {1}}将前台活动的数量减少回onActivityStopped()

因此,您可以根据计数器检测应用程序是在前台还是后台,

0

答案 1 :(得分:1)

这不是我的答案,我之前已经看过这个问题,并在我的应用程序中实现它就像一个魅力。找不到原始答案的链接,所以我发布了我使用的代码。 您可以使用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();
}
}

然后您可以在应用程序类中注册它

 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 :(得分:1)

使用吹码:

    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;
}

这里com.example.testapp是你的包名

需要为您的清单添加权限..

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

它通过使用名称来返回运行或不运行的活动,并根据您的要求进行更改。

<强>更新

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;
}
}

此方法列出项目中所有正在运行的活动

答案 3 :(得分:0)

您可以使用活动hasWindowFocus()布尔方法。然后,如果它具有焦点,则显示活动的弹出窗口,否则,您只显示通知。