Search code examples
reactjsapexcharts

Click event with react-apexcharts


I'm using react-apexcharts and I want to add an event on a pie chart once users click on one of the sections. Here is my chart:

<div className="pie">
    <Chart options={{
        labels: [
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        ],
        theme: {
            monochrome: {
                enabled: false
            }
        },
        responsive: [{
            breakpoint: 480,
            options: {
                chart: {
                    width: "100%"
                },
                legend: {
                    show: false
                }
            }
        }]
    }}
    colors={["#1B2260"]}
    series={[44, 55, 41, 17, 15]}
    labels={["Apple", "Mango", "Orange", "Watermelon"]}
    type="pie"
    width="300" />
</div>

When users click on the "Apple" section, I want to print "Apple". This is the best documentation I can find for the click event but I can't get it to work.

Any help would be great! Thanks


Solution

  • Try adding this to your options object:

    chart: {
        events: {
        dataPointSelection: (event, chartContext, config) => { 
            console.log(config.w.config.labels[config.dataPointIndex])}
        }
    }