1

bar chart在带有数据标签的 android 应用程序中显示一个。highchart例外只是数值,尽管我尝试使用字符串作为图表不呈现的值。下面是我的代码

 public void golyChartView(Number prev,Number curr)
{
    HIOptions options = new HIOptions();
    HIChart chart = new HIChart();
    chart.setType("bar");
    options.setChart(chart);


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

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

    HIXAxis xaxis = new HIXAxis();
    String[] categories = new String[] { "2020", "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("Sale Amount");
    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 Value");
    Number[] bar1Data = new Number[]{prev,curr};
    bar1.setColorByPoint(true);
    bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));

    float numb = (((float)curr - (float)prev)/(float)prev)*100;

    String percentage = String.valueOf(numb);

    HIDataLabels dataLabels = new HIDataLabels();
    dataLabels.setEnabled(true);


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

    HIDataLabels hiDataLabels = new HIDataLabels();
    hiDataLabels.setEnabled(true);

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


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

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

    options.setExporting(exporting);

    golyChart.setOptions(options);
    golyChart.update(options,true,false);



}

输出

在此处输入图像描述

我想做的事?

如上面的代码所示,我有一个字符串percentage,我想在第二个栏下方添加 如下所示

在此处输入图像描述

我坚持下去,不知道该怎么办

任何帮助将不胜感激。

4

1 回答 1

0

您可以使用文本渲染器在栏旁边添加文本。

我不知道 android-highcharts 但在 javascript 这里是一个例子

let customLabel,
    labelInit = false;

Highcharts.chart('container', {
    chart: {
        type: 'bar',
        events: {
            render: function () {

                const chart = this,
              point = chart.series[0].data[1];

                // prevent multiple label creation if used in render event but useless in "load" event
                if (labelInit) customLabel.destroy();

                customLabel = chart.renderer.text('+25 %', point.shapeArgs.height, point.tooltipPos[1] + chart.series[0].itemWidth + 10)
                    .add()
                    .attr({
                        zIndex: 3
                    });
                labelInit = true;

            }
        }
    },
    title:{
        text: "GOLY"
    },
    plotOptions: {
        bar: {
            grouping: false
        }
    },
    xAxis: {
        type: "category",
        categories: ["2020", "2021"]
    },
    series: [{
        data: [200, 400],
        colorByPoint: true

    }]
});
于 2021-05-29T23:50:21.513 回答