Chart Comments Setup
ChartJS Comments Setup
1
Import Components from Chart.js
2
Import Comments Components and Hooks from Velt
- Add
VeltComments
to the root of your app. This component is required to render comments in your app. - Add the
VeltCommentTool
component wherever you want to show the comment tool button. Clicking on it initiates comment mode & changes your mouse cursor to a comment pin.
3
Create a ref for the ChartJS component
- Create a
ref
and pass it into theref
property of theChartJS
component.
4
Add a container div for the ChartJS component
- Apply
position: relative
style to the div. This ensures accurate positioning of Velt Comment Pins. - Set
data-velt-manual-comment-container
totrue
. This:
- informs Velt which div to use for positioning
- disables Velt’s automatic comment positioning system within this div, allowing for manual positioning of comment pins
- Give a unique ID to the chart to scope comments to the specific chart, isolating them from other charts.
5
Add a Comment when the user clicks on the chart
- Handle chart click to find nearest data point and add a comment. (Check
handleChartClick() method
on the right) - Create
context
object which contains the information about the series, x-axis value, y-axis value, chartId and anything else that’s relevant. This will be used when rendering the comment pin. (CheckhandleChartClick() method
on the right) - Add comment with the
context
data. (CheckaddManualComment() method
on the right)
- Only add a comment if the Velt
commentModeState
is true and the Veltclient
is available.
6
Render the Velt Comment Pins and set its position
- Get all comment annotations.
- Loop through it and render the comment pin.
- Use the
context
data inCommentAnnotation
, to set the position of the comment pin. (CheckshowCommentPin() method
on the right)
Was this page helpful?