khushboo29
khushboo29

Reputation: 916

Highchart Pie chart, de-structure dataLabels and define custom formatter

I am using Highchart in a react application. I am defining pie configs in a config file and it is used in a component. The component is responsible for rendering highchart by using updated chart config with dynamic values.

import {renderToString} from 'react-dom/server';
import {pieConfig} from './pieConfig';
import {Text} from './Text';

export const PieContainer = ({apiResponse}) => {
  const updatedPiConfig = {
    ...pieConfig,
    plotOptions: {
      pie: {
        dataLabels: {
          connectorShape: 'straight',
          distance: 20,
          style: {
            textOverflow: 'clip'
          },
          formatter: function() {
             return renderToString(<Text>{this.point.name}</Text>)
          }
        }
      }
    },
    series: [{data: apiResponse}]
  }
}

When I am trying to de-structure dataLabel configs instead of re-defining them like this,

...
dataLabel: {
  ...pieConfig.plotOptions.pie.dataLabels,
  formatter: function() {
    return renderToString(<Text>{this.point.name}</Text>)
  }
}

My formatter is giving me error that - Property point does not exist on type '{formatter: () => any...

I tried converting formatter to arrow function, but I am unable to access point.name inside arrow function.

https://codesandbox.io/s/highcharts-react-demo-forked-r4pso

Any help or suggestion will be appreciated.

Upvotes: 0

Views: 592

Answers (1)

Sebastian Wędzel
Sebastian Wędzel

Reputation: 11633

Notice that Highcharts callbacks don't accept the JSX components. Take a look at this example if you want to use the JSX components in Highcharts - https://gist.github.com/jon-a-nygaard/7d0253b2c73ae634d5804d6794a67c0c

Example without using the JSX: https://codesandbox.io/s/highcharts-react-demo-forked-z13sh

Upvotes: 1

Related Questions