Search code examples
androidhighchartsbar-chartandroid-highcharts

Android highcharts how to enable or disable data labels


I am working on android. By using highchart I have created a barchart.

HIOptions options = new HIOptions();
    HIChart chart = new HIChart();
    chart.setType("bar");
    options.setChart(chart);

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

    HISubtitle subtitle = new HISubtitle();
    subtitle.setText("Growth Over Last Month");
    options.setSubtitle(subtitle);

    HIXAxis xaxis = new HIXAxis();
    String[] categories = new String[] { "Mar-2021", "Apr-2021","May-2021"};
    xaxis.setCategories(new ArrayList<>(Arrays.asList(categories)));
    options.setXAxis(new ArrayList<HIXAxis>(){{add(xaxis);}});

    HIYAxis yaxis = new HIYAxis();
    yaxis.setMin(0);
    yaxis.setTitle(new HITitle());
    yaxis.getTitle().setText("Sales");
    yaxis.getTitle().setAlign("high");
    yaxis.setLabels(new HILabels());
    yaxis.getLabels().setOverflow("justify");
    options.setYAxis(new ArrayList<HIYAxis>(){{add(yaxis);}});

    HITooltip tooltip = new HITooltip();

    options.setTooltip(tooltip);

    HILegend legend = new HILegend();
    legend.setLayout("vertical");
    legend.setAlign("right");
    legend.setVerticalAlign("top");
    legend.setX(-30);
    legend.setY(40);
    legend.setFloating(true);
    legend.setBorderWidth(1);
    legend.setBackgroundColor(HIColor.initWithHexValue("FFFFFF"));
    legend.setShadow(true);
    options.setLegend(legend);

    HICredits credits = new HICredits();
    credits.setEnabled(false);
    options.setCredits(credits);

    HIBar bar1 = new HIBar();
    bar1.setName("Sale");
    Number[] bar1Data = new Number[]{10,15,20};
    bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
    bar1.setColorByPoint(true);

    HILegend hiLegend = new HILegend();
    hiLegend.setEnabled(false);
    options.setLegend(hiLegend);

    options.setSeries(new ArrayList<>(Collections.singletonList(bar1)));


    golmChart.setOptions(options);

Output

enter image description here

I want to enable the data labels. The sample code does give a code to enable data labels but it's not working

    HIPlotOptions plotOptions = new HIPlotOptions();
    plotOptions.setBar(new HIBar());
    plotOptions.getBar().setDataLabels(new HIDataLabels());
    plotOptions.getBar().getDataLabels().setEnabled(true);
    options.setPlotOptions(plotOptions);

It's giving me an error

error: incompatible types: HIDataLabels cannot be converted to ArrayList plotOptions.getBar().setDataLabels(new HIDataLabels());

Source: Android Basic Bar

Any help would be highly appreciated.


Solution

  • They have changed the datalabel property. Now you have to do the following

    HIDataLabels dataLabels = new HIDataLabels();
    dataLabels.setEnabled(false);
    ArrayList<HIDataLabels> dataLabelsList = new ArrayList<>();
    dataLabelsList.add(dataLabels);
    bar1.setDataLabels(dataLabelsList);
    

    Try the above code after you set data to bar1

    For more information see this comment