Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Pass onClick to chart item #10549

Closed
1 of 4 tasks
alexfauquette opened this issue Oct 2, 2023 · 8 comments · Fixed by #11411 · May be fixed by #10797
Closed
1 of 4 tasks

[charts] Pass onClick to chart item #10549

alexfauquette opened this issue Oct 2, 2023 · 8 comments · Fixed by #11411 · May be fixed by #10797
Labels
component: charts This is the name of the generic UI component, not the React module!

Comments

@alexfauquette
Copy link
Member

alexfauquette commented Oct 2, 2023

Follow up to #10506

Allows to add onClick which should be triggered when clicking on an item

Charts todo

  • Pie chart
  • Line chart
  • Bar chart
  • scatter chart

How

Follow #10506 to get inspiration. In short:

  • The onClick goes the the chart component and the plot components
  • it takes as an argument: the event, the item identifier, and the item content
  • modify the cursor based on the presence or not of an onClick

Search keywords:

@alexfauquette alexfauquette added good first issue Great for first contributions. Enable to learn the contribution process. component: charts This is the name of the generic UI component, not the React module! labels Oct 2, 2023
@alexfauquette alexfauquette removed the good first issue Great for first contributions. Enable to learn the contribution process. label Oct 18, 2023
@yaredtsy
Copy link
Contributor

hey @alexfauquette

I have also been working on this one, But I was trying to implement it in a different way. I have tried to adopt the ChartsTooltip approach. I have created an OnClickHandler component that listens to the mousedown event on svgRef.

https://github.com/yaredtsy/mui-x/blob/39f99c8ea51cd69a8ae26a85b8f1f7886d958ff7/packages/x-charts/src/OnClickHandler.tsx#L40-L99

Lines Demo : https://codesandbox.io/s/misty-currying-z9nwrz?file=/src/Demo.tsx
ScatterDemo : https://codesandbox.io/s/tender-herschel-4zgpjg?file=/src/Demo.tsx
Bar Demo : https://codesandbox.io/s/great-bardeen-23d62k?file=/src/Demo.tsx:147-233

@alexfauquette
Copy link
Member Author

Hi @yaredtsy could you open a PR to see the entire file modification?

@alexfauquette
Copy link
Member Author

The more I think about it, the more I wonder if it's not closely related to #9358 which would also move the mouse interaction from the scatter chart items to the svg component

@wascou
Copy link
Contributor

wascou commented Nov 15, 2023

I would love to have this feature on scatter chart! It would ease the reading of data in the Tooltip with a fixed placed holder for example. Or we can use to select one item in the chart and compare it with the data in the tooltip.

@konrazem
Copy link

konrazem commented Nov 24, 2023

You can use color as reference of slice of pie chart (uniq color is id).
NOTE: Of course u should limit number of colors and hence number of pie chart items!

example:
https://codesandbox.io/s/quirky-roentgen-vj8df8?file=/src/Demo.tsx

    <PieChart
      colors={itemsWithColors.map((item) => item.color)}
      series={[
        {
          data: itemsWithColors
        }
      ]}
      slotProps={{
        pieArc: {
          onClick: (e) => handleArcClick(e.target),
          cursor: "pointer"
        }
      }}
      width={400}
      height={200}
    />

@nico-martinucci
Copy link

@alexfauquette Just want to say thanks for working on this! This would be a huge improvement to one of our chart implementations. Appreciate you!

@alexfauquette
Copy link
Member Author

@nico-martinucci Based on you use cases, do you have insight regarding the different option in the PR?

@nico-martinucci
Copy link

nico-martinucci commented Jan 10, 2024

Probably idea 2, but would love your thoughts - we have a summary graph, calculated from many many rows of raw data. That raw data is rolled up into months, weeks, or days depending on user-set filters. My hope is for a user to be able to click on one of the stacked bars (which represents a single month, week, or day) and this would navigate to a page with a table of the rows that were rolled-up into that particular bar. For example, clicking on "June '23" bar would take you to the table with only records from that month, or clicking on "June 13, 2023" would take you to a table of records just for that day. Ideally the onClick would take the raw data of the clicked item as a param, so that that could be passed into another custom handler to set state, url params, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants