PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.63k stars 9.02k forks source link

Using setLineWidth/enableDashLine on LimitLine object results in Exception #64

Closed cavega closed 10 years ago

cavega commented 10 years ago

Following the example shown on LineChartActivity.java I'm using the following statements to modify each line in my Line Chart:

LineDataSet set1 = new LineDataSet(yVals.get(0), labels.get(0)); set1.disableDashedLine(); set1.setColor(context.getResources().getColor(Color.RED);

    LimitLine ll1 = new LimitLine(130f);
    ll1.setLineWidth(8f);
    ll1.enableDashedLine(10f, 10f, 0f);

When I start the app and the fragment that displays the graph is been created I get the following Exception:

08-28 08:51:20.870    2244-2244/com.app.alpha E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...).
        at com.github.mikephil.charting.utils.Utils.convertDpToPixel(Utils.java:67)
        at com.github.mikephil.charting.utils.LimitLine.setLineWidth(LimitLine.java:58)
        at com.app.widget.DataGraphFactory.buildLineChart(DataGraphFactory.java:176)
        at com.app.fragment.DataDisplayFragment.getGraphFromFactory(DataDisplayFragment.java:209)
        at com.app.fragment.DataDisplayFragment.onCreateView(DataDisplayFragment.java:109)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

The above error occurs while using either (or both) setLineWidth() and enableDashLine(). As a workaround I modified my local copy of the library to change the default values set by these methods. I'll appreciate some feedback as to what is causing the exception since I'm not doing anything that different from the sample code.

PhilJay commented 10 years ago

The crash comes from the setLineWidth(float width) method. The reason therefore is that this method internally uses the chartlibs Utils class to convert the line width you set into density pixels.

In order to use the Utils, they need to be initialized once with a Resources object. By default, this is done in the constructor of each chart class.

If data for the chart is created before the charts constructor is called, the Utils are not initialized.

In that case, you need to call Utils.initialize(getResources()) just once somewhere before creating the data for your chart.

I know that this is quite inconvenient and will work on a solution as soon as possible.

Regards, Phil