How to redirect user when clicking on a Chart element

How to redirect user when clicking on a Chart element



I am using react-chartJs-2 library to display the charts. Suppose, user clicks on the bar/Doughnut chart section he must be redirected to a specific page. Following is the code I have done for DoughnutChart:


<div className="DonutChartSection" >
<Doughnut labelArray = this.state.labelArr DataArray = this.state.DoughnutDataArr colorArray = this.state.DoughnutColorArr title="Employees" />
</div>



Doughnut.jsx


import React from 'react';
import Doughnut from 'react-chartjs-2';

Chart.defaults.global.defaultFontStyle = 'Bold';
Chart.defaults.global.defaultLegendPosition = 'left';

export default class DoughnutChartView extends React.Component
constructor(props)
     super(props);
     this.state = ;


render()
let data =
labels: this.props.labelArray,
datasets: [
data: this.props.DataArray,
backgroundColor: this.props.colorArray,
hoverBackgroundColor: this.props.colorArray,
]

return (
<div>
<span className="titleName" >this.props.title</span>
<Doughnut data=data />
</div>
);

;



I went through the github page and found the following event which can be used for click event but can't figure out how can I use it in my code.



https://github.com/jerairrest/react-chartjs-2#onelementsclick--getelementsatevent-function




1 Answer
1



Indeed, you actually answered your question already: onElementsClick can be used to execute an action when user clicks on an element of the chart. onElementsClick is a prop of the chart itself, so:


onElementsClick


onElementsClick


<Doughnut data=data onElementsClick=elems =>
// if required to build the URL, you can
// get datasetIndex and value index from an `elem`:
console.log(elems[0]._datasetIndex + ', ' + elems[0]._index);
// and then redirect to the target page:
window.location = "https://example.com";
/>



Note that elems may include more than one element, for example in case of a stacked bar chart (in which case it includes all elements of a bar clicked).


elems





Thanks a lot Timur for the apt example.
– Liz.
Sep 5 '18 at 12:00





I am trying to add dynamic link in this way : <Doughnut data=data onElementsClick=data => this.props.handleTabClick.bind(this.state.currentObj, 'view_page', this.props.pageId, 'ei') /> but nothing is happening. I have included handleTabClick and pageId in the Donughnut page
– Liz.
Sep 6 '18 at 6:42


<Doughnut data=data onElementsClick=data => this.props.handleTabClick.bind(this.state.currentObj, 'view_page', this.props.pageId, 'ei') />


handleTabClick


pageId





Hm. I'm not sure why you use bind - this only binds this in the scope of the function, but doesn't call it. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Maybe what you need is onElementsClick=data => this.props.handleTabClick(this.state.currentObj, 'view_page', this.props.pageId, 'ei') ... Otherwise it is probably best to post this as a separate question with more context (handleTabClick function and how the component is used). Hope it helps.
– Timur
Sep 6 '18 at 7:22


bind


this


onElementsClick=data => this.props.handleTabClick(this.state.currentObj, 'view_page', this.props.pageId, 'ei')





how to get the element data which is being clicked? Ex. if the chart has 2 categories - manual, automation. I want if user clicks on manual then manual to be returned .
– Liz.
Oct 10 '18 at 10:57


2 categories - manual, automation



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế