BarChart的自定义setRender()给出错误

时间:2017-11-08 10:59:22

标签: java android mpandroidchart

我有来自 MPandroidchart Horizo​​ntalBarChart ,我想为每个条形码旋转值。我创建了类MyBarChartRender.java但是当我将它放入chartBar.setRender()时,我得到一个错误

                                                             java.lang.NullPointerException: Attempt to read from null array
                                                                 at com.github.mikephil.charting.renderer.BarChartRenderer.drawDataSet(BarChartRenderer.java:136)
                                                                 at com.github.mikephil.charting.renderer.BarChartRenderer.drawData(BarChartRenderer.java:80)
                                                                 at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:231)
                                                                 at android.view.View.draw(View.java:17189)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16171)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16166)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16166)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.draw(View.java:17192)
                                                                 at android.widget.ScrollView.draw(ScrollView.java:1737)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16171)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.draw(View.java:17192)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16171)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16166)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16166)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16166)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16166)
                                                                 at android.view.View.draw(View.java:16955)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                 at android.view.View.draw(View.java:17192)
                                                                 at com.android.internal.policy.DecorView.draw(DecorView.java:756)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:16171)
                                                                 at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)
                                                                 at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)
                                                                 at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)
                                                                 at android.view.ViewRootImpl.draw(ViewRootImpl.java:2800)
                                                                 at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2608)
                                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2215)
                                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                 at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                                 at java.lang.reflect.Method.invoke(Native Method)

我不知道它尝试从哪个阵列读取,所以我不知道如何自己解决这个问题。

MyBarChartRender.java:

public class MyBarChartRender extends BarChartRenderer {

    public MyBarChartRender(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

        public void drawValue(Canvas c, IValueFormatter formatter, String value, Entry entry, int dataSetIndex, float x, float y, int color) {

            // super.drawValue(c, formatter, value, entry, dataSetIndex, x, y, color);

            Paint paint=super.mDrawPaint;
            paint.setTextSize(25f);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
            Utils.drawXAxisValue(c,value,x,y,paint, MPPointF.getInstance(),90);
        }}

BARCHART:

 BarDataSet barDataSet = new BarDataSet(barEntries, "");
                    BarData barData = new BarData(barDataSet);
                    barData.setBarWidth(1f);
                    barData.setValueFormatter(new MyValueFormatter());
                    barData.setValueTextSize(9);
                    barDataSet.setColors(colors);
                    chartBar.setData(barData);
                    chartBar.setVisibleXRangeMaximum(howMany);
                    chartBar.setDescription(null);
                    chartBar.setVerticalScrollBarEnabled(true);
                    chartBar.setHorizontalScrollBarEnabled(false);
                    chartBar.getXAxis().setDrawAxisLine(false);
                    chartBar.setDrawBorders(false);
                    chartBar.getAxisRight().setDrawAxisLine(false);
                    chartBar.getAxisRight().setDrawGridLines(false);
                    chartBar.getAxisRight().setDrawGridLines(false);
                    chartBar.getAxisRight().setDrawLabels(false);
                    chartBar.getLegend().setEnabled(false);
                    chartBar.setScaleEnabled(false);
                    chartBar.setTouchEnabled(false);
                    chartBar.getAxisLeft().setDrawGridLines(false);
                    chartBar.getAxisLeft().setDrawLabels(false);
                    chartBar.getAxisLeft().setDrawAxisLine(false);
                    chartBar.getAxisLeft().setDrawGridLines(false);
                    chartBar.getAxisLeft().setAxisMinimum(0f);
                    chartBar.setRenderer(new MyBarChartRender(chartBar,chartBar.getAnimator(),chartBar.getViewPortHandler()));
                    chartBar.getXAxis().setDrawGridLines(false);
                    chartBar.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
                    chartBar.getXAxis().setDrawLabels(true);
                    chartBar.getXAxis().setTextSize(12);
                    XAxis xAxis = chartBar.getXAxis();
                    xAxis.setLabelCount(howMany);
                    xAxis.setLabelRotationAngle(90);

                    xAxis.setValueFormatter(new MyXAxisValueFormatter(partiesName));
                    xAxis.setGranularity(1f);

我不知道出了什么问题。在我尝试使用渲染之前,每件事情都可以。

编辑:

错误问题已解决但使用MyBarChartRender.java旋转条和值。如何才能使其仅旋转值?

0 个答案:

没有答案