其他分享
首页 > 其他分享> > MPAndroid组条形图未显示

MPAndroid组条形图未显示

作者:互联网

这个问题已经在这里有了答案:            >            How to check if activity is in foreground or in visible background?                                    22个
这是我用于使用MPAndroid库版本3的组bar_chart的代码.此代码不起作用.没有崩溃和错误日志,仍在加载图形,并且没有数据显示.帮我找出毛病

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

解决方法:

这不是我的答案,我以前看过这些问题,并在我的应用程序中实现,它像一种魅力.找不到原始答案的链接,所以我要张贴我使用的代码.
您可以使用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");
        }
    }));

标签:mpandroidchart,android,bar-chart
来源: https://codeday.me/bug/20191010/1886416.html