|
| 1 | +import { Link } from 'react-router'; |
| 2 | +import { Bar, BarChart, Pie, PieChart, Tooltip } from 'recharts'; |
| 3 | +import { SupportedLocale } from '../../locale'; |
| 4 | +import { TargetBlankLink } from '../Shared/TargetBlankLink.tsx'; |
| 5 | +import Highlight from '../../utils/Highlight.tsx'; |
| 6 | + |
| 7 | +export function ActiveIndex({ locale }: { locale: SupportedLocale }) { |
| 8 | + return ( |
| 9 | + <article> |
| 10 | + <h1>Active Index</h1> |
| 11 | + <p> |
| 12 | + Recharts 2.x used to have a prop named <code>activeIndex</code> that was setting which item the user was |
| 13 | + interacting with. In version 3.0, this prop has been removed. |
| 14 | + </p> |
| 15 | + <h2>Why?</h2> |
| 16 | + <p> |
| 17 | + Trouble is that this prop was already set internally - by Tooltip component. Things behaved weird when both the |
| 18 | + activeIndex and Tooltip were set. Unpredictable! |
| 19 | + </p> |
| 20 | + <h2>What should I do instead?</h2> |
| 21 | + <p> |
| 22 | + <Link to={`/${locale}/api/Tooltip`}>Tooltip</Link> component is controlling user interaction with the chart. Use |
| 23 | + Tooltip! |
| 24 | + </p> |
| 25 | + <p>Tooltip has several props that allow you to control the interaction in detail:</p> |
| 26 | + <ul> |
| 27 | + <li> |
| 28 | + <code>defaultIndex</code>: Sets the initial index of the item that is highlighted when the chart is rendered, |
| 29 | + before any user interactions |
| 30 | + </li> |
| 31 | + <li> |
| 32 | + <code>active</code>: If true, the tooltip remains active even when the user interaction has completed (for |
| 33 | + example, when user hovers over a different item) |
| 34 | + </li> |
| 35 | + <li> |
| 36 | + <code>content</code>: This prop decides what content is displayed in the tooltip. You can turn off the |
| 37 | + rendering completely by passing <code>{`content={() => null}`}</code>. |
| 38 | + </li> |
| 39 | + <li> |
| 40 | + <code>cursor</code>: Is what renders in the plot area, to draw attention to the item that is being interacted |
| 41 | + with. You can turn it off by passing <code>cursor={false}</code>. |
| 42 | + </li> |
| 43 | + </ul> |
| 44 | + <h2>Example 1: PieChart with default index</h2> |
| 45 | + <p> |
| 46 | + The example below shows how to use the <code>defaultIndex</code> prop to set the initial item that is |
| 47 | + highlighted when the chart is rendered. |
| 48 | + </p> |
| 49 | + <p> |
| 50 | + Also see{' '} |
| 51 | + <TargetBlankLink href="https://github.com/recharts/recharts/issues/5999"> |
| 52 | + GitHub issue #5999 for discussion. |
| 53 | + </TargetBlankLink> |
| 54 | + </p> |
| 55 | + <PieChart width={400} height={400}> |
| 56 | + <Pie |
| 57 | + activeShape={{ |
| 58 | + fill: 'red', |
| 59 | + }} |
| 60 | + data={[ |
| 61 | + { name: 'Page A', uv: 590 }, |
| 62 | + { name: 'Page B', uv: 590 }, |
| 63 | + { name: 'Page C', uv: 868 }, |
| 64 | + ]} |
| 65 | + dataKey="uv" |
| 66 | + /> |
| 67 | + <Tooltip defaultIndex={2} /> |
| 68 | + </PieChart> |
| 69 | + <Highlight className="jsx"> |
| 70 | + {`import { Pie, PieChart, Tooltip } from 'recharts'; |
| 71 | +
|
| 72 | +<PieChart width={400} height={400}> |
| 73 | +<Pie |
| 74 | + activeShape={{ |
| 75 | + fill: 'red', |
| 76 | + }} |
| 77 | + data={[ |
| 78 | + { name: 'Page A', uv: 590 }, |
| 79 | + { name: 'Page B', uv: 590 }, |
| 80 | + { name: 'Page C', uv: 868 }, |
| 81 | + ]} |
| 82 | + dataKey="uv" |
| 83 | +/> |
| 84 | +<Tooltip defaultIndex={2} /> |
| 85 | +</PieChart>`} |
| 86 | + </Highlight> |
| 87 | + <p> |
| 88 | + <TargetBlankLink href="https://main--63da8268a0da9970db6992aa.chromatic.com/?path=/story/api-chart-piechart--simple"> |
| 89 | + View this example in Storybook |
| 90 | + </TargetBlankLink> |
| 91 | + </p> |
| 92 | + <h2>Example 2: BarChart with clickable items and hidden tooltip</h2> |
| 93 | + <p> |
| 94 | + The example below shows how to use the <code>trigger</code> prop to highlight the item that is being clicked, |
| 95 | + and how to hide the tooltip by passing <code>{`content={() => null}`}</code> and <code>{`cursor={false}`}</code> |
| 96 | + . |
| 97 | + </p> |
| 98 | + <p> |
| 99 | + Also see{' '} |
| 100 | + <TargetBlankLink href="https://github.com/recharts/recharts/issues/6047"> |
| 101 | + GitHub issue #6047 for discussion. |
| 102 | + </TargetBlankLink> |
| 103 | + </p> |
| 104 | + <BarChart |
| 105 | + width={550} |
| 106 | + height={240} |
| 107 | + data={[ |
| 108 | + { name: 'Page A', uv: 590 }, |
| 109 | + { name: 'Page B', uv: 290 }, |
| 110 | + { name: 'Page C', uv: 868 }, |
| 111 | + ]} |
| 112 | + > |
| 113 | + <Tooltip trigger="click" content={() => null} cursor={false} shared={false} /> |
| 114 | + <Bar dataKey="uv" stackId="a" fill="green" activeBar={{ stroke: 'black', strokeWidth: 7 }} /> |
| 115 | + </BarChart> |
| 116 | + <Highlight className="jsx"> |
| 117 | + {`import { Bar, BarChart, Tooltip } from 'recharts'; |
| 118 | +
|
| 119 | +<BarChart |
| 120 | + width={550} |
| 121 | + height={240} |
| 122 | + data={[ |
| 123 | + { name: 'Page A', uv: 590 }, |
| 124 | + { name: 'Page B', uv: 290 }, |
| 125 | + { name: 'Page C', uv: 868 }, |
| 126 | + ]} |
| 127 | +> |
| 128 | + <Tooltip trigger="click" content={() => null} cursor={false} shared={false} /> |
| 129 | + <Bar dataKey="uv" stackId="a" fill="green" activeBar={{ stroke: 'black', strokeWidth: 7 }} /> |
| 130 | +</BarChart>`} |
| 131 | + </Highlight> |
| 132 | + </article> |
| 133 | + ); |
| 134 | +} |
0 commit comments