0

I'm trying to implement a multiple series line chart in HighCharts android with x-axis containing months (eg. 'Jan','Feb') and values on y-axis(avg_iex,pxil values etc.). I have implemented other charts with similar approach but this chart is not showing up. I've tried using static data on which the chart shows when code is written outside of StringRequest scope. Although, log result shows:

'MONPR'[3.58, 4.6, 7.95, 9.52, 6.81, 6.88, 5.5, 5.43, 5.87, 3.96, 4.8, 5.58].

It shows correct value like it should.

Following is the code:

        HIChartView monprchart = findViewById(R.id.monprchart); 
        HIOptions options = new HIOptions();
       
        HIChart chart = new HIChart();
        chart.setType("line");  // Use 'line' for step line chart
        options.setChart(chart);

        HITitle title = new HITitle();
        title.setText("Volume (MW)");
        HICSSObject style = new HICSSObject();
        style.setFontSize("15px");
        title.setStyle(style);
        options.setTitle(title);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_MONPRCHART,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("status");
                            final JSONArray jsonArray1 = jsonObject.getJSONArray("price_mmr");
//                            Log.v("Data_", String.valueOf(jsonArray1));
                            if (success.equals("true")) {

                                List<String> block_time = new ArrayList<>();
                                List<Number> avg_iex_price = new ArrayList<>();
                                List<Number> avg_pxil_price = new ArrayList<>();
                                List<Number> avg_traders_price = new ArrayList<>();
                                List<Number> avg_ui_price = new ArrayList<>();


                                for (int i = 0; i < jsonArray1.length(); i++) {
                                    JSONObject object1 = jsonArray1.getJSONObject(i);

                                    block_time.add(object1.getString("block_time"));
                                    avg_iex_price.add(parseNumber(object1.optString("avg_iex_price", "0")));
                                    avg_pxil_price.add(parseNumber(object1.optString("avg_pxil_price", "0")));
                                    avg_traders_price.add(parseNumber(object1.optString("avg_traders_price", "0")));
                                    avg_ui_price.add(parseNumber(object1.optString("avg_ui_price", "0")));
                                }

                                // 3. X-Axis (e.g., categories or time)
                                HIXAxis xAxis = new HIXAxis();
                                xAxis.setCategories(new ArrayList<>(block_time)); // Example
                                options.setXAxis(new ArrayList<>(Collections.singletonList(xAxis)));

                                Log.d("MONPR",avg_iex_price.toString());

                                HIYAxis yAxis = new HIYAxis();
                                HITitle yTitle = new HITitle();
                                yTitle.setText("");
                                yAxis.setTitle(yTitle);
                                options.setYAxis(new ArrayList<>(Collections.singletonList(yAxis)));

                                // 5. Create multiple step line series
                                List<HISeries> seriesList = new ArrayList<>();

                                HILine series1 = new HILine();
                                series1.setName("Avg. IEX");
                                series1.setData(new ArrayList<>(avg_iex_price));
//                                series1.setStep("left");
                                series1.setColor(HIColor.initWithHexValue("007bff"));
                                seriesList.add(series1);

                                HILine series2 = new HILine();
                                series2.setName("Avg. PXIL");
                                series2.setData(new ArrayList<>(avg_pxil_price));
//                                series2.setStep("left");
                                series2.setColor(HIColor.initWithHexValue("28a745"));
                                seriesList.add(series2);

                                options.setSeries(new ArrayList<>(seriesList));

                               
                                monprchart.setOptions(options);//monprchart chart id
                                monprchart.reload();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                }


API response is:

 "status": true,
    "price_mmr": [
        {
            "block_time1": "2024-01-01",
            "block_time": "Jan",
            "max_iex_price": "10.00",
            "max_pxil_price": "10.00",
            "max_traders_price": "8.63",
            "max_ui_price": "10.00",
            "avg_iex_price": "6.03",
            "avg_pxil_price": "9.50",
            "avg_traders_price": "6.66",
            "avg_ui_price": "6.28",
            "min_iex_price": "1.87",
            "min_pxil_price": "9.50",
            "min_traders_price": "4.27",
            "min_ui_price": "2.00"
        }]

P.S: 'HIOptions not attached. Chart won't render without options. Uncaught ReferenceError: Highcharts is not defined' error is showing up in the Logcat.

5
  • Check that your data arrays for categories and series have the same length and that your parseNumber() function returns a valid Number, not a String. Also, I suggest console logging your data before setting options to catch any nulls or mismatches. If the chart renders with static data but not API data, the issue is most probably with data processing inside your callback. Commented Aug 14 at 8:18
  • @DominikChudy I replaced the null values with zero, checked all the data. Data is retreived within the success condition scope. I tried dummy data outside API call, the chart is working then. But, within the success condition data is fetched but chart isn't rendering. Commented Aug 14 at 11:49
  • Based on what I read in Android Highcharts API (Search and click HIChartView), the reload function is deprecated. Perhaps you should replace it with redraw(). monprchart.redraw() after the setOptions() line. Fingers crossed. Commented Aug 15 at 0:57
  • Another way is with update(HIOptions options, boolean redraw) method: monprchart.update(options, true); Commented Aug 15 at 1:00
  • 1
    @YongShun Thanks, but those two commands you suggested didn't worked. The issue still exists. Inside the API call the chart isn't loading but outside with static data it's working. However, I want to show the API data. Commented Aug 18 at 7:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.