Search code examples
iosswifthighcharts

Custom label on top of the Highchart using swift and highchart cocoapods


I have column chart in which I want to show rank of the chart on top of the column. I tried to show dataLabels but it only shows y value on the top. Is there any way to add custom label on the top?

I tried to find answer on Highchart forums and Github forums for the same. Please help me with the code example.

Current column chart

I want to show custom value on top of the column.

let column = HIColumn()
column.data = arySerisModel[i].aryData

where 'arydata' is array of float values.

I have another

array = [1,1,2,3,3,4,5,6,6,7]

which I want to show on middle of column.


Solution

  • You can use the dataLabels.format property or the dataLabels.formatter() callback function to set custom label text.

    plotOptions: {
        series: {
            dataLabels: {
                enabled: true,
                format: 'Custom label'
            }
        }
    },
    

    Implementing this in Swift will look like this:

    let options = HIOptions()
    let plotOptions = HIPlotOptions()
    plotOptions.series = HISeries()
    let dataLabels = HIDataLabels()
    dataLabels.enabled = true
    dataLabels.formatter = HIFunction(jsFunction: "function() { const point = this.point, customLabel = point.customLabel; return `${customLabel ? customLabel : point.y}`; }")
    plotOptions.series.dataLabels = [dataLabels]
    options.plotOptions = plotOptions
    let series = HISeries()
    series.data = [
        ["y": 29.9],
        ["y": 71.5, "customLabel": "Custom A"],
        ["y": 129.2, "customLabel": "Custom B"],
        ["y": 144]
    ]
    options.series = [series]
    

    Demo: https://jsfiddle.net/BlackLabel/afub368p/
    Docs: https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting
    API: https://api.highcharts.com/highcharts/series.pie.dataLabels.format