Skip to content

Click handler? #3

Description

@lekoaf

I decided to see if I could just replace react-chartjs-2 with this library.

Most things worked smoothly, but it seems like this library doesn't support click handlers? Might just be me that's missing something.

This is my chart with react-chartjs-2.

<Bar
	getElementAtEvent={handleClick}
	data={{
		labels,
		datasets: [{ data }]
	}}
	options={barOptions}
/>

The getElementAtEvent doesn't seem to have an equivalence in this library?

This my click handler:

 /**
   * Extracting the label is the only way we can know what has been
   * clicked. Therefore we need to get it from the click event for
   * each bar and compute what properties should be used for filtering.
   *
   * The entire chart is clickable, that's why we return early if we didn't
   * get any data, which indicates we clicked a bar.
   */
  const handleClick = useCallback(
    (datasets: Array<IDataSets>) => {
      if (datasets.length === 0 || datasets[0]._model === undefined) {
        return
      }

      const { label } = datasets[0]._model

      if (label !== undefined) {
        setFilterValue(getFilterValue(label))
        setClicked()
      }
    },
    [setClicked]
  )

Maybe all of this can be solved in a better way?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions