javaandroidchartshighchartsandroid-highcharts

Why numbers are shown on my x-axis stackedcolumn instead of String?


I'm trying to show the discom names on my x-axis labels in HIchartview in Android, but instead it's showing 0, 1, and so on.

Following is the code:

List<String> discomCodes = new ArrayList<>();
List<Number> tbl_PPC = new ArrayList<>();
List<Number> tbl_TC = new ArrayList<>();
                     
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject object = jsonArray.getJSONObject(i);

    discomCodes.add(object.getString("discom_code"));
    tbl_PPC.add(parseNumber(object.getString("tbl_power_purchase_cost_val")));
    tbl_TC.add(parseNumber(object.getString("tbl_transmission_cost")));                     
}

// X-Axis
HIXAxis xAxis = new HIXAxis();
xAxis.setCategories(discomCodes);
options.setXAxis(new ArrayList<>(Collections.singletonList(xAxis)));
Log.d("Logdisc",discomCodes.toString());

// Y-Axis
HIYAxis yAxis = new HIYAxis();
yAxis.setMin(0);
HITitle yTitle = new HITitle();
yTitle.setText("Rs./kWh");
yAxis.setTitle(yTitle);
options.setYAxis(new ArrayList<>(Collections.singletonList(yAxis)));

// Plot Options (stacked)
HIPlotOptions plotOptions = new HIPlotOptions();
HIColumn column = new HIColumn();
column.setStacking("normal");
plotOptions.setColumn(column);
options.setPlotOptions(plotOptions);

// Series
List<HISeries> seriesList = new ArrayList<>();

HIColumn s1 = new HIColumn();
s1.setName("PPC");
s1.setData(new ArrayList<Number>(tbl_PPC));
seriesList.add(s1);

HIColumn s2 = new HIColumn();
s2.setName("Transmission Cost");
s2.setData(new ArrayList<Number>(tbl_TC));
seriesList.add(s2);
                      
options.setSeries(new ArrayList<>(seriesList));

barChartDDAPPC.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
barChartDDAPPC.setOptions(options);
barChartDDAPPC.reload(); // 🔁 force redraw  VVIMP

enter image description here

How do I correct the plotting of the chart on Android?


Solution

  • PS: Not HighCharts Android developer.

    My suspection is that you should call setCategories(java.util.ArrayList<java.lang.String> categories) method

    If categories are present for the xAxis, names are used instead of numbers for that axis.

    instead of setCategories(java.util.List<java.lang.String> categories) method.

    Refer to this tutorial, you should provide the discomCodes as ArrayList type.

    xAxis.setCategories(new ArrayList<>(Arrays.asList(discomCodes)));