highcharts / highcharts-android

Android wrapper for Highcharts usage
Other
126 stars 30 forks source link

HIOptions not found in HIChartView #82

Closed myst-al closed 5 years ago

myst-al commented 5 years ago

this is error log : E/AndroidRuntime: FATAL EXCEPTION: main Process: com.uajy.monitor, PID: 9495 java.util.NoSuchElementException: HIOptions not found in HIChartView at com.highsoft.highcharts.core.HIChartView.a(SourceFile:277) at com.highsoft.highcharts.core.HIChartView.a(SourceFile:204) at com.highsoft.highcharts.core.HIChartView.onDraw(SourceFile:196) at android.view.View.draw(View.java:19218) at android.view.View.updateDisplayListIfDirty(View.java:18168) at android.view.View.draw(View.java:18946) at android.view.ViewGroup.drawChild(ViewGroup.java:4238) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024) at android.view.View.updateDisplayListIfDirty(View.java:18159) at android.view.View.draw(View.java:18946) at android.view.ViewGroup.drawChild(ViewGroup.java:4238) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024) at android.view.View.updateDisplayListIfDirty(View.java:18159) at android.view.View.draw(View.java:18946) at android.view.ViewGroup.drawChild(ViewGroup.java:4238) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024) at android.view.View.updateDisplayListIfDirty(View.java:18159) at android.view.View.draw(View.java:18946) at android.view.ViewGroup.drawChild(ViewGroup.java:4238) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024) at android.view.View.updateDisplayListIfDirty(View.java:18159) at android.view.View.draw(View.java:18946) at android.view.ViewGroup.drawChild(ViewGroup.java:4238) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024) at android.view.View.updateDisplayListIfDirty(View.java:18159) at android.view.View.draw(View.java:18946) at android.view.ViewGroup.drawChild(ViewGroup.java:4238) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024) at android.view.View.draw(View.java:19221) at com.android.internal.policy.DecorView.draw(DecorView.java:791) at android.view.View.updateDisplayListIfDirty(View.java:18168) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:685) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:691) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:799) at android.view.ViewRootImpl.draw(ViewRootImpl.java:3064) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2859) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2412) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1439) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6899) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029) at android.view.Choreographer.doCallbacks(Choreographer.java:841) at android.view.Choreographer.doFrame(Choreographer.java:772) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015) at android.os.Handler.handleCallback(Handler.java:794) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6635) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

and this my code:

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("Response", response);

            try {
                JSONObject jsonObject = new JSONObject(response).getJSONObject("data");
                Data data = new Data();
                ArrayList<Data> dataArrayList = new ArrayList<>();

                data.setDatetime(jsonObject.getString("datetime"));
                data.setKadar(jsonObject.getString("KADAR"));

                dataArrayList.add(data);

                datetime = Long.parseLong(data.getDatetime());
                kadar = Integer.parseInt(data.getKadar());

                HIChartView chartView = findViewById(R.id.hc);
                HIOptions options = new HIOptions();

                chartView.plugins = new ArrayList<>(Arrays.asList("export-data"));

                HIChart chart = new HIChart();
                chart.setType("spline");
                chart.setMarginRight(10);
                options.setChart(chart);

                HIEvents events = new HIEvents();
                events.setLoad(
                        new HIFunction(
                                "function () { " +
                                        "var series = this.series[0]; " +
                                        "setInterval(function () { " +
                                        "   var x = "+datetime+", y = "+kadar+";"+
                                        "   series.addPoint([x, y], true, true); " +
                                        "}, 60000); " +
                                        "}"
                        ));
                chart.setEvents(events);
                options.setChart(chart);

                HITitle title = new HITitle();
                title.setText("Live Data");
                options.setTitle(title);

                HIXAxis xaxis =  new HIXAxis();
                xaxis.setType("datetime");
                xaxis.setTickPixelInterval(150);
                options.setXAxis(new ArrayList<>(Collections.singletonList(xaxis)));

                HIYAxis yaxis =  new HIYAxis();
                yaxis.setTitle(new HITitle());
                yaxis.getTitle().setText("value");
                HIPlotLines plotLines = new HIPlotLines();
                plotLines.setValue(0);
                plotLines.setWidth(1);
                plotLines.setColor(HIColor.initWithHexValue("808080"));
                yaxis.setPlotLines(new ArrayList<>(Collections.singletonList(plotLines)));
                options.setYAxis(new ArrayList<>(Collections.singletonList(yaxis)));

                HITooltip tooltip =  new HITooltip();
                tooltip.setFormatter(
                        new HIFunction(
                                "function () { " +
                                        "return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + Highcharts.numberFormat(this.y, 2); }"
                        ));
                options.setTooltip(tooltip);

                HILegend legend =  new HILegend();
                legend.setEnabled(true);
                options.setLegend(legend);

                HIExporting exporting = new HIExporting();
                exporting.setEnabled(false);
                options.setExporting(exporting);

                HISpline spline =  new HISpline();
                spline.setName("Kadar");
                HashMap<String, Object> splineData1 = new HashMap<>();
                HashMap<String, Object> splineData2 = new HashMap<>();
                HashMap<String, Object> splineData3 = new HashMap<>();
                HashMap<String, Object> splineData4 = new HashMap<>();
                HashMap<String, Object> splineData5 = new HashMap<>();
                HashMap<String, Object> splineData6 = new HashMap<>();
                HashMap<String, Object> splineData7 = new HashMap<>();
                HashMap<String, Object> splineData8 = new HashMap<>();
                HashMap<String, Object> splineData9 = new HashMap<>();
                HashMap<String, Object> splineData10 = new HashMap<>();
                spline.setData(new ArrayList<>(Arrays.asList(splineData1, splineData2, splineData3, splineData4, splineData5, splineData6, splineData7, splineData8, splineData9, splineData10)));

                options.setSeries(new ArrayList<HISeries>(Collections.singletonList(spline)));
                chartView.setOptions(options);
                chartView.reload();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(error != null){
                        Toast.makeText(getApplicationContext(),"Something went wrong", Toast.LENGTH_LONG).show();
                    }
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
soommy12 commented 5 years ago

Hi @albert0106 . You need to attach empty options for your chart and update them in onResponse() method. It was already discussed many times in #12 #15 #33 .

myst-al commented 5 years ago

OMG THANKYOU SO MUCH LORD