Android Question Trying to save a graph as an image

Smee

Well-Known Member
Licensed User
Longtime User
I have tried saving as a screenshot with this code which I know works elsewhere in the program

B4X:
'Public Sub CaptureScreen( act As Activity, dir As String, fileName As String)
Public Sub CaptureScreen(dir As String, fileName As String)
    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
    bmp.InitializeMutable(Activity.Width, Activity.Height)
    c.Initialize2(bmp)
    Dim args(1) As Object
    Dim types(1) As String
    Obj2.Target = c
    Obj2.Target = Obj2.GetField("canvas")
    args(0) = Obj2.Target
    types(0) = "android.graphics.Canvas"
    Obj1.RunMethod4("draw", args, types)
    Dim Out As OutputStream
    Out = File.OpenOutput( dir, fileName, False)
    Dim fmt As String = "PNG"
    If fileName.ToUpperCase.Contains("JPG") Then fmt = "JPEG"
    If fileName.ToUpperCase.Contains("JPEG") Then fmt = "JPEG"
    bmp.WriteToStream(Out, 100, fmt)
    Out.Close
End Sub

However with the charts I am playing around with I just get a black screen. The charts show on the screen ok. The relevant code is just putting a chart on the activity screen

B4X:
    pv1.Initialize("pv1")
    Activity.Color=Colors.black
    Activity.AddView(pv1, -1%x, 1%y, 60%y, 60%y)
    Dim piedata() As Float = Array As Float(35, 65)
    pv1.Data = piedata
    Dim piecolors() As Int = Array As Int(Colors.green, Colors.Blue)
    pv1.PieColors = piecolors
    pv1.ShowPercentLabel = True
    pv1.BringToFront

Can anyone advise how I maight save the graphs? Sometimes I may have a bar chart on the screen as well as a pie chart
TIA for any replies
 

Smee

Well-Known Member
Licensed User
Longtime User
Thanks Klaus, I actually had tried that earlier using a panel and then adding the charts to the panel but I got errors. I will run the code again with that code and post the errors. Obviously I was not doing it correctly.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Klaus, I am just playing around with the code and it is only a bit of test code so i have zipped it up. I wonder if you might take a look and point me to where I am going wrong
Many Thanks
 

Attachments

  • test.zip
    9.5 KB · Views: 268
Upvote 0

klaus

Expert
Licensed User
Longtime User
I looked at your project.
I wasn't aware that you are using Johan Schoeman's libarary.
I thought you were using the Chart module from Erel. The link I posted was with this module.

I uploaded the library and ran your project.
Does the version you posted work for you ?!
I get a balck screen with 4 white texts.
A long click on Home Pass raises an error: Object must be initialized.
I initialized the Label but no chart.
How do we get this to work !?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for that Klaus. Yes it works fine here. I will re-check it but all it does is a count of clicks. Press left label 3 or 4 times them press right label. Repeat a couple of times then long click on top label and graphs will be drawn. The graph is then saved in external directory but I just get a pic of blank screen when i look at pic.
Just checked. Yes sorry the topbar label had just been added and I forgot to initialise. So if you just press the left label a couple of times then the right label and repeat then long click the top label it will draw.
Thanks again for taking a look
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks very much for taking the time to look Klaus. @Johan Schoeman is there a solution to this?
What graphs are you making use of? I have done a saving option for one of them - think it was multi line charts.
I need to revisit this library as I need to reverse some changes that I have done recently. I will then add save options to the other graph type that I have not done so before.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
What graphs are you making use of? I have done a saving option for one of them - think it was multi line charts.
I need to revisit this library as I need to reverse some changes that I have done recently. I will then add save options to the other graph type that I have not done so before.

tHANKS VERY MUCH, The only graphs I am using ( at this point) are the bar and pie graphs. There is a small test program attached at post #4.
Thanks again for any help and for your efforts with the library
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
tHANKS VERY MUCH, The only graphs I am using ( at this point) are the bar and pie graphs. There is a small test program attached at post #4.
Thanks again for any help and for your efforts with the library
The bar and pie graphs in the library already provides for the below methods (the wrapper code) - you should be able to get a Bitmap back and then save it:
B4X:
    /**
     * Saves the current state of the chart to the gallery as a JPEG image. The
     * filename and compression can be set. 0 == maximum compression, 100 = low
     * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE
     *
     * fileName e.g. "my_image"
     * quality e.g. 50, min = 0, max = 100
     */
       public void saveToGallery(String fileName, int quality) {
            cv.saveToGallery(fileName, quality);
       }


    /**
     * Saves the current chart state with the given name to the given path on
     * the sdcard leaving the path empty "" will put the saved file directly on
     * the SD card chart is saved as a PNG image, example:
     * saveToPath("myfilename", "foldername1/foldername2");
     *
     * fileName e.g. "my_image"
     * pathOnSD e.g. "folder1/folder2/folder3"
     */
    public void saveToPath(String fileName, String pathOnSD) {
      cv.saveToPath(fileName, pathOnSD);
    }

    /**
     * Returns the bitmap that represents the chart.
     *
     * @return
     */
    public Bitmap getChartBitmap() {
        Bitmap image = cv.getChartBitmap();
//        byte[] byteArray = cv.getChartBitmap();
//        image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);     //added by johan schoeman
        BitmapWrapper bw = new BitmapWrapper();                                    //added by johan schoeman - Erel advised
        bw.setObject(image);
        return image;
    }

    public void setSavedImageBackgroundColor(int color){
       cv.setSavedImageBackgroundColor(color);
    }

If you don't come right then let me know. But I nevertheless attached the updated library (V1.08) as I have reversed some changes that I made in early July 2017 but they were only applicable to the CandleStickCharts (I will have to redo this part for the CandleStickCharts).

So, you will have to do something like this in your code to get the bitmap:
B4X:
Dim bm As Bitmap
bm = mpc1.ChartBitmap

And then do with the bitmap whatever it is you want to do (i.e save it, etc)
 

Attachments

  • mpChartLib_V1_08.zip
    362.1 KB · Views: 302
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks very much @Johan . I have not tried it yet but is this the same library? the one I have is called AndroidCharts.jar V1.00
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
I suspected that it may be from another library so I searched and downloaded the project from your mpchartlib. I tried to run the sample project with this version but i just got a whole load of errors.

Logger connected to: samsung GT-N7100
--------- beginning of /dev/log/main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: mpandroidchartwrapper.pieViewWrapper, trying: JHS.MPChart.pieViewWrapper
Error occurred on line: 30 (Main)
java.lang.RuntimeException: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at JHS.MPChart.main.afterFirstLayout(main.java:102)
at JHS.MPChart.main.access$000(main.java:17)
at JHS.MPChart.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at java.lang.Class.forName(Class.java:216)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:377)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:425)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:144)
... 21 more
Caused by: java.lang.NoClassDefFoundError: JHS/MPChart/pieViewWrapper
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "JHS.MPChart.pieViewWrapper" on path: DexPathList[[zip file "/data/app/JHS.MPChart-1.apk"],nativeLibraryDirectories=[/data/app-lib/JHS.MPChart-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 27 more
** Activity (main) Resume **
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: mpandroidchartwrapper.pieViewWrapper, trying: JHS.MPChart.pieViewWrapper
Error occurred on line: 30 (Main)
java.lang.RuntimeException: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at JHS.MPChart.main.afterFirstLayout(main.java:102)
at JHS.MPChart.main.access$000(main.java:17)
at JHS.MPChart.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at java.lang.Class.forName(Class.java:216)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:377)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:425)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:144)
... 21 more
Caused by: java.lang.NoClassDefFoundError: JHS/MPChart/pieViewWrapper
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "JHS.MPChart.pieViewWrapper" on path: DexPathList[[zip file "/data/app/JHS.MPChart-1.apk"],nativeLibraryDirectories=[/data/app-lib/JHS.MPChart-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 27 more
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I suspected that it may be from another library so I searched and downloaded the project from your mpchartlib. I tried to run the sample project with this version but i just got a whole load of errors.

Logger connected to: samsung GT-N7100
--------- beginning of /dev/log/main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: mpandroidchartwrapper.pieViewWrapper, trying: JHS.MPChart.pieViewWrapper
Error occurred on line: 30 (Main)
java.lang.RuntimeException: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at JHS.MPChart.main.afterFirstLayout(main.java:102)
at JHS.MPChart.main.access$000(main.java:17)
at JHS.MPChart.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at java.lang.Class.forName(Class.java:216)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:377)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:425)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:144)
... 21 more
Caused by: java.lang.NoClassDefFoundError: JHS/MPChart/pieViewWrapper
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "JHS.MPChart.pieViewWrapper" on path: DexPathList[[zip file "/data/app/JHS.MPChart-1.apk"],nativeLibraryDirectories=[/data/app-lib/JHS.MPChart-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 27 more
** Activity (main) Resume **
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: mpandroidchartwrapper.pieViewWrapper, trying: JHS.MPChart.pieViewWrapper
Error occurred on line: 30 (Main)
java.lang.RuntimeException: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at JHS.MPChart.main.afterFirstLayout(main.java:102)
at JHS.MPChart.main.access$000(main.java:17)
at JHS.MPChart.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at java.lang.Class.forName(Class.java:216)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:377)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:425)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:144)
... 21 more
Caused by: java.lang.NoClassDefFoundError: JHS/MPChart/pieViewWrapper
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "JHS.MPChart.pieViewWrapper" on path: DexPathList[[zip file "/data/app/JHS.MPChart-1.apk"],nativeLibraryDirectories=[/data/app-lib/JHS.MPChart-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 27 more
Will look into it tonight - there is probably still some "noise" that I have not reversed out.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks very much @Johan . I have not tried it yet but is this the same library? the one I have is called AndroidCharts.jar V1.00
Will look into the possibility to bring back a bitmap. Sorry, but thought you were using MPAndoidCharts
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I suspected that it may be from another library so I searched and downloaded the project from your mpchartlib. I tried to run the sample project with this version but i just got a whole load of errors.

Logger connected to: samsung GT-N7100
--------- beginning of /dev/log/main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: mpandroidchartwrapper.pieViewWrapper, trying: JHS.MPChart.pieViewWrapper
Error occurred on line: 30 (Main)
java.lang.RuntimeException: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at JHS.MPChart.main.afterFirstLayout(main.java:102)
at JHS.MPChart.main.access$000(main.java:17)
at JHS.MPChart.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at java.lang.Class.forName(Class.java:216)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:377)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:425)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:144)
... 21 more
Caused by: java.lang.NoClassDefFoundError: JHS/MPChart/pieViewWrapper
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "JHS.MPChart.pieViewWrapper" on path: DexPathList[[zip file "/data/app/JHS.MPChart-1.apk"],nativeLibraryDirectories=[/data/app-lib/JHS.MPChart-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 27 more
** Activity (main) Resume **
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: mpandroidchartwrapper.pieViewWrapper, trying: JHS.MPChart.pieViewWrapper
Error occurred on line: 30 (Main)
java.lang.RuntimeException: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at JHS.MPChart.main.afterFirstLayout(main.java:102)
at JHS.MPChart.main.access$000(main.java:17)
at JHS.MPChart.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: JHS.MPChart.pieViewWrapper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at java.lang.Class.forName(Class.java:216)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:377)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:425)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:144)
... 21 more
Caused by: java.lang.NoClassDefFoundError: JHS/MPChart/pieViewWrapper
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "JHS.MPChart.pieViewWrapper" on path: DexPathList[[zip file "/data/app/JHS.MPChart-1.apk"],nativeLibraryDirectories=[/data/app-lib/JHS.MPChart-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 27 more

OK - so at this stage not sure why you get the error. With the exact same library files and the attached project it works 100%. The only change that I have done to the attached B4A project was to move the resources from the Object/res folder to a new folder called ProjectRes. See the B4A project's code at the top:
B4X:
#End Region

#AdditionalRes: ..\ProjectRes

#Region  Activity Attributes

Instructions:
1. Download the attached B4A project
2. Download ProjectRes.zip and then extract the folder called ProjectRes and copy the folder (and its contents) to be on the same folder level as that of the /Objects and /Files folders of the B4A project
3. Run the app - I have added an ImageView to the left of the pie chart.
4. Long Click on the pie chart being displayed. It will return a bitmap of the pie chart from the library via the long click event (mpc1_long_click) and display it in the ImageView.


Pie chart created with the library and a blank B4A ImageView on the left
1.png



The Pie chart on the right (created by the library) long clicked and the bitmap returned to B4A and displayed in the B4A ImageView on the left.
2.png



How you handle the returned bitmap is up to you (i.e saving it etc). There are also the other "save" methods that are in the library as per the Java code posted further up in this thread. They are available/pop up when you type for eg mpc1.

Sample Code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: MPPieChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\ProjectRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private mpc1 As PieChart
  
    Private iv1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
  
    mpc1.UsePercentValues = True
  
    mpc1.DrawHoleEnabled = True
    mpc1.HoleColorTransparent = True

    mpc1.TransparentCircleColor = Colors.White
    mpc1.TransparentCircleAlpha = 110

    mpc1.HoleRadius = 58.0
    mpc1.TransparentCircleRadius = 61.0

    mpc1.DrawCenterText = True

    mpc1.CenterText = "Wrapped by Johan"
    mpc1.CenterTextColor = Colors.White
    mpc1.CenterTextRadiusPercent = 100.0
    mpc1.CenterTextSize = 12.0

    mpc1.DrawSliceText = True
    mpc1.HoleColor = Colors.Black
    mpc1.TransparentCircleColor = Colors.Transparent

    mpc1.RotationEnabled = True


'    RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE,
'    LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE,
'    BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER,
'    PIECHART_CENTER
    mpc1.TheLegendPosition = "RIGHT_OF_CHART"
  

  
    mpc1.TheLegendColor = Colors.yellow
    mpc1.TheLegendTextSize = 12.0
    mpc1.LegendTitle = "MONTHS"

    mpc1.ChartDescription = "TITLE : Some Arbitrary Data"
    mpc1.ChartDescriptionColor = Colors.ARGB(200,0,255,255)
    mpc1.ChartDescriptionTextSize = 14

    mpc1.ValueTextColor = Colors.Black
    mpc1.ValueTextSize = 12.0

    mpc1.PieColors = Array As Int(Colors.Blue, Colors.Yellow, Colors.Green, Colors.Red, Colors.Magenta, Colors.Cyan)
    mpc1.LegendText = Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun")
    mpc1.ChartData = Array As Float(128.0, 16.0, 46.0, 40.0, 30.0, 40.0)    'values - it will be converted to %
  
    mpc1.PieData = 6    
  
       

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub mpc1_value_selected(index As Int, value As Float)
  
    Log("index = " & index)
    Log("value = " & value)
  
  
End Sub

Sub mpc1_long_pressed
  
    Log("Long Pressed")
    Dim bm As Bitmap = mpc1.ChartBitmap
    iv1.Bitmap = bm
  
End Sub

The LineChart should work the same way - the method to bring back the line chart is already in V1.08 (and earlier versions) of the library.
 

Attachments

  • b4aMPPieChart.zip
    8.5 KB · Views: 324
  • ProjectRes.zip
    24.4 KB · Views: 290
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks very much @Johan . I have not tried it yet but is this the same library? the one I have is called AndroidCharts.jar V1.00
Attached are new B4A lib files for AndroidCharts that will return a bitmap to the B4A project (have added it for bar, pie, and line charts). Sure you will figure out how to use it - see the sample code below and take note of the files in the /Objects/res folder of the original B4A projects that use this library. The property of these files need to be set to READ ONLY else they will get deleted when you compile the project(s) for the first time.

You need to handle the returned Bitmaps (i.e save them wherever you want to).



Sample code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: AndroidPieChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private pv1 As PieView
    Private iv1 As ImageView
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    iv1.Initialize("")
    pv1.Initialize("pv1")
   
    Activity.AddView(pv1, 51%x, 5%y, 49%x, 90%y)
    Activity.AddView(iv1, 0%x, 5%y, 49%x, 90%y)
   
    Dim piedata() As Float = Array As Float(10, 15, 20, 25, 15, 10, 5)
    pv1.Data = piedata
   
    Dim piecolors() As Int = Array As Int(Colors.Green, Colors.Blue, Colors.Magenta, Colors.Yellow, Colors.DarkGray, Colors.Cyan, Colors.Red)
    pv1.PieColors = piecolors
   
    pv1.PieTextColor = Colors.Black
   
    pv1.ShowPercentLabel = True
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pv1_pie_clicked
   
Log("Touched pie slice with index = " & pv1.PieClickedIndex)
Dim bm As Bitmap
bm = pv1.ChartBitmap
iv1.Bitmap = bm
   
End Sub

1.png
 

Attachments

  • AndroidCharts.xml
    31.3 KB · Views: 329
  • AndroidCharts.jar
    39.6 KB · Views: 257
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
@Johan Thank you so much for your work, I will test both these out as soon as I am able, thanks again
 
Upvote 0
Top