Skip to content

Commit f19b8e7

Browse files
committed
added example - Purple with bottom aligned legend
1 parent 49ea162 commit f19b8e7

File tree

2 files changed

+84
-71
lines changed

2 files changed

+84
-71
lines changed

packages/react-charts/src/victory/components/ChartBar/examples/ChartBar.md

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -29,77 +29,7 @@ The examples below are based on the [Victory](https://formidable.com/open-source
2929

3030
This demonstrates an alternate way of applying tooltips using data labels.
3131

32-
```js
33-
import { Chart, ChartAxis, ChartBar, ChartGroup, ChartThemeColor, ChartTooltip } from '@patternfly/react-charts/victory';
34-
35-
class EmbeddedLegend extends React.Component {
36-
render() {
37-
const label = ({ datum }) => `${datum.name}: ${datum.y}`;
38-
39-
return (
40-
<div style={{ height: '275px', width: '450px' }}>
41-
<Chart
42-
ariaDesc="Average number of pets"
43-
ariaTitle="Bar chart example"
44-
domainPadding={{ x: [30, 25] }}
45-
legendData={[{ name: 'Cats' }, { name: 'Dogs' }, { name: 'Birds' }, { name: 'Mice' }]}
46-
legendPosition="bottom"
47-
height={275}
48-
name="chart2"
49-
padding={{
50-
bottom: 75, // Adjusted to accommodate legend
51-
left: 50,
52-
right: 50,
53-
top: 50
54-
}}
55-
themeColor={ChartThemeColor.purple}
56-
width={450}
57-
>
58-
<ChartAxis />
59-
<ChartAxis dependentAxis showGrid />
60-
<ChartGroup offset={11}>
61-
<ChartBar
62-
data={[
63-
{ name: 'Cats', x: '2015', y: 1, label },
64-
{ name: 'Cats', x: '2016', y: 2, label },
65-
{ name: 'Cats', x: '2017', y: 5, label },
66-
{ name: 'Cats', x: '2018', y: 3, label }
67-
]}
68-
labelComponent={<ChartTooltip constrainToVisibleArea />}
69-
/>
70-
<ChartBar
71-
data={[
72-
{ name: 'Dogs', x: '2015', y: 2, label },
73-
{ name: 'Dogs', x: '2016', y: 1, label },
74-
{ name: 'Dogs', x: '2017', y: 7, label },
75-
{ name: 'Dogs', x: '2018', y: 4, label }
76-
]}
77-
labelComponent={<ChartTooltip constrainToVisibleArea />}
78-
/>
79-
<ChartBar
80-
data={[
81-
{ name: 'Birds', x: '2015', y: 4, label },
82-
{ name: 'Birds', x: '2016', y: 4, label },
83-
{ name: 'Birds', x: '2017', y: 9, label },
84-
{ name: 'Birds', x: '2018', y: 7, label }
85-
]}
86-
labelComponent={<ChartTooltip constrainToVisibleArea />}
87-
/>
88-
<ChartBar
89-
data={[
90-
{ name: 'Mice', x: '2015', y: 3, label },
91-
{ name: 'Mice', x: '2016', y: 3, label },
92-
{ name: 'Mice', x: '2017', y: 8, label },
93-
{ name: 'Mice', x: '2018', y: 5, label }
94-
]}
95-
labelComponent={<ChartTooltip constrainToVisibleArea />}
96-
/>
97-
</ChartGroup>
98-
</Chart>
99-
</div>
100-
);
101-
}
102-
}
32+
```ts file = "ChartBarPurple.tsx"
10333
```
10434

10535
### Multi-color (ordered) with bottom-left aligned legend
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
Chart,
3+
ChartAxis,
4+
ChartBar,
5+
ChartGroup,
6+
ChartThemeColor,
7+
ChartTooltip
8+
} from '@patternfly/react-charts/victory';
9+
10+
interface PetData {
11+
name: string;
12+
x: string;
13+
y: number;
14+
label: string;
15+
}
16+
17+
export const EmbeddedLegend: React.FunctionComponent = () => {
18+
const label =
19+
() =>
20+
({ datum }) =>
21+
`${datum.name}: ${datum.y}`;
22+
23+
const catsData: PetData[] = [
24+
{ name: 'Cats', x: '2015', y: 1, label },
25+
{ name: 'Cats', x: '2016', y: 2, label },
26+
{ name: 'Cats', x: '2017', y: 5, label },
27+
{ name: 'Cats', x: '2018', y: 3, label }
28+
];
29+
30+
const dogsData: PetData[] = [
31+
{ name: 'Dogs', x: '2015', y: 2, label },
32+
{ name: 'Dogs', x: '2016', y: 1, label },
33+
{ name: 'Dogs', x: '2017', y: 7, label },
34+
{ name: 'Dogs', x: '2018', y: 4, label }
35+
];
36+
37+
const birdsData: PetData[] = [
38+
{ name: 'Birds', x: '2015', y: 4, label },
39+
{ name: 'Birds', x: '2016', y: 4, label },
40+
{ name: 'Birds', x: '2017', y: 9, label },
41+
{ name: 'Birds', x: '2018', y: 7, label }
42+
];
43+
44+
const miceData: PetData[] = [
45+
{ name: 'Mice', x: '2015', y: 3, label },
46+
{ name: 'Mice', x: '2016', y: 3, label },
47+
{ name: 'Mice', x: '2017', y: 8, label },
48+
{ name: 'Mice', x: '2018', y: 5, label }
49+
];
50+
51+
const legendData = [{ name: 'Cats' }, { name: 'Dogs' }, { name: 'Birds' }, { name: 'Mice' }];
52+
53+
return (
54+
<div style={{ height: '275px', width: '450px' }}>
55+
<Chart
56+
ariaDesc="Average number of pets"
57+
ariaTitle="Bar chart example"
58+
domainPadding={{ x: [30, 25] }}
59+
legendData={legendData}
60+
legendPosition="bottom"
61+
height={275}
62+
name="chart2"
63+
padding={{
64+
bottom: 75, // Adjusted to accommodate legend
65+
left: 50,
66+
right: 50,
67+
top: 50
68+
}}
69+
themeColor={ChartThemeColor.purple}
70+
width={450}
71+
>
72+
<ChartAxis />
73+
<ChartAxis dependentAxis showGrid />
74+
<ChartGroup offset={11}>
75+
<ChartBar data={catsData} labelComponent={<ChartTooltip constrainToVisibleArea />} />
76+
<ChartBar data={dogsData} labelComponent={<ChartTooltip constrainToVisibleArea />} />
77+
<ChartBar data={birdsData} labelComponent={<ChartTooltip constrainToVisibleArea />} />
78+
<ChartBar data={miceData} labelComponent={<ChartTooltip constrainToVisibleArea />} />
79+
</ChartGroup>
80+
</Chart>
81+
</div>
82+
);
83+
};

0 commit comments

Comments
 (0)