Skip to content

Commit a756999

Browse files
committed
Scatter chart: update stories and tests
1 parent dcce4fd commit a756999

File tree

6 files changed

+243
-148
lines changed

6 files changed

+243
-148
lines changed

packages/charts/src/components/LineChart/LineChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const LineChart: FC<LineChartProps> = forwardRef((props: LineChartProps, ref: Re
158158
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset, measures);
159159
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
160160
const xAxisHeights = useObserveXAxisHeights(chartRef, props.dimensions.length);
161-
161+
console.log(dataset);
162162
return (
163163
<ChartContainer
164164
dataset={dataset}

packages/charts/src/components/ScatterChart/ScatterChart.stories.tsx

Lines changed: 33 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { action } from '@storybook/addon-actions';
22
import { boolean } from '@storybook/addon-knobs';
33
import React from 'react';
44
import { ScatterChart } from '../../lib/ScatterChart';
5-
import { bigDataSet, complexDataSet, scatterComplexDataSet } from '../../resources/DemoProps';
5+
import { bigDataSet, complexDataSet, scatterColorDataSet, scatterComplexDataSet } from '../../resources/DemoProps';
66

77
export default {
88
title: 'Charts / ScatterChart',
@@ -22,14 +22,11 @@ export const renderStory = () => (
2222
{
2323
accessor: 'users',
2424
label: 'Users',
25-
formatter: (e) => e + ' Users',
2625
axis: 'x'
2726
},
2827
{
2928
accessor: 'sessions',
30-
label: 'Active Sessions',
31-
formatter: (e) => e + ' Sessions',
32-
hideDataLabel: true,
29+
label: 'Sessions',
3330
axis: 'y'
3431
},
3532
{
@@ -50,67 +47,31 @@ export const renderStoryWithCustomColor = () => (
5047
noLegend={boolean('noLegend', false)}
5148
noAnimation={boolean('noAnimation', false)}
5249
onDataPointClick={action('onDataPointClick')}
53-
dimensions={{ accessor: 'name' }}
54-
measures={[{ accessor: 'users', color: 'red' }]}
55-
dataset={scatterComplexDataSet}
56-
style={{ width: '95%', height: '40vh' }}
50+
dataset={scatterColorDataSet}
51+
style={{ width: '100%' }}
52+
measures={[
53+
{
54+
accessor: 'users',
55+
label: 'Users',
56+
axis: 'x'
57+
},
58+
{
59+
accessor: 'sessions',
60+
label: 'Sessions',
61+
axis: 'y'
62+
},
63+
{
64+
accessor: 'volume',
65+
axis: 'z'
66+
}
67+
]}
5768
/>
5869
);
5970

6071
renderStoryWithCustomColor.story = {
6172
name: 'With custom color'
6273
};
6374

64-
export const withSecondaryDimension = () => (
65-
<ScatterChart
66-
loading={boolean('loading', false)}
67-
noLegend={boolean('noLegend', false)}
68-
noAnimation={boolean('noAnimation', false)}
69-
onDataPointClick={action('onDataPointClick')}
70-
dimensions={[{ accessor: 'name' }, { accessor: 'dimension' }]}
71-
measures={[{ accessor: 'users', color: 'red' }]}
72-
dataset={secondaryDimensionDataSet}
73-
style={{ width: '95%', height: '60vh' }}
74-
/>
75-
);
76-
77-
withSecondaryDimension.story = {
78-
name: 'With secondary dimension'
79-
};
80-
81-
export const renderLabelStory = () => {
82-
return (
83-
<ScatterChart
84-
loading={boolean('loading', false)}
85-
noLegend={boolean('noLegend', false)}
86-
noAnimation={boolean('noAnimation', false)}
87-
onDataPointClick={action('onDataPointClick')}
88-
onLegendClick={action('onLegendClick')}
89-
dimensions={[{ accessor: 'name' }]}
90-
measures={[
91-
{
92-
accessor: 'users'
93-
},
94-
{
95-
accessor: 'sessions'
96-
},
97-
{
98-
accessor: 'volume'
99-
}
100-
]}
101-
dataset={complexDataSet}
102-
style={{ width: '95%', height: '40vh' }}
103-
chartConfig={{
104-
zoomingTool: true
105-
}}
106-
/>
107-
);
108-
};
109-
110-
renderLabelStory.story = {
111-
name: 'With data labels'
112-
};
113-
11475
export const renderCustomDataLabelStory = () => {
11576
return (
11677
<ScatterChart
@@ -119,25 +80,27 @@ export const renderCustomDataLabelStory = () => {
11980
noAnimation={boolean('noAnimation', false)}
12081
onDataPointClick={action('onDataPointClick')}
12182
onLegendClick={action('onLegendClick')}
122-
dataset={complexDataSet}
123-
dimensions={[{ accessor: 'name', formatter: (element: string) => element.slice(0, 3) }]}
83+
dataset={scatterComplexDataSet}
84+
style={{ width: '100%' }}
12485
measures={[
12586
{
12687
accessor: 'users',
127-
formatter: (element: number) => `${element / 10}`,
128-
label: 'number of users'
88+
label: 'Users',
89+
formatter: (e) => e + ' Users',
90+
axis: 'x'
12991
},
13092
{
131-
accessor: 'sessions'
93+
accessor: 'sessions',
94+
label: 'Active Sessions',
95+
formatter: (e) => e + ' Sessions',
96+
hideDataLabel: true,
97+
axis: 'y'
13298
},
13399
{
134-
accessor: 'volume'
100+
accessor: 'volume',
101+
axis: 'z'
135102
}
136103
]}
137-
style={{ width: '95%', height: '40vh' }}
138-
chartConfig={{
139-
zoomingTool: true
140-
}}
141104
/>
142105
);
143106
};
@@ -146,45 +109,8 @@ renderCustomDataLabelStory.story = {
146109
name: 'With formatter'
147110
};
148111

149-
export const loadingPlaceholder = () => <LineChart style={{ width: '100%' }} dimensions={[]} measures={[]} />;
112+
export const loadingPlaceholder = () => <ScatterChart style={{ width: '100%' }} measures={[]} />;
150113

151114
loadingPlaceholder.story = {
152115
name: 'Loading placeholder'
153116
};
154-
155-
export const withReferenceLineStory = () => {
156-
return (
157-
<ScatterChart
158-
loading={boolean('loading', false)}
159-
noLegend={boolean('noLegend', false)}
160-
noAnimation={boolean('noAnimation', false)}
161-
onDataPointClick={action('onDataPointClick')}
162-
onLegendClick={action('onLegendClick')}
163-
dataset={bigDataSet}
164-
dimensions={[{ accessor: 'name' }]}
165-
measures={[
166-
{
167-
accessor: 'users'
168-
},
169-
{
170-
accessor: 'sessions'
171-
},
172-
{
173-
accessor: 'volume'
174-
}
175-
]}
176-
style={{ width: '95%', height: '40vh' }}
177-
chartConfig={{
178-
referenceLine: {
179-
color: 'red',
180-
label: 'MAX',
181-
value: 650
182-
}
183-
}}
184-
/>
185-
);
186-
};
187-
188-
withReferenceLineStory.story = {
189-
name: 'With reference line'
190-
};

packages/charts/src/components/ScatterChart/ScatterChart.test.tsx

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,33 @@ import { action } from '@storybook/addon-actions';
22
import { boolean } from '@storybook/addon-knobs';
33
import { mount } from 'enzyme';
44
import * as React from 'react';
5-
import { complexDataSet } from '../../resources/DemoProps';
6-
import { LineChart } from './LineChart';
5+
import { scatterComplexDataSet } from '../../resources/DemoProps';
6+
import { ScatterChart } from './ScatterChart';
77

88
describe('LineChart', () => {
99
test('Renders with data', () => {
1010
expect(
1111
mount(
12-
<LineChart
12+
<ScatterChart
1313
loading={boolean('loading', false)}
1414
onDataPointClick={action('onDataPointClick')}
1515
onLegendClick={action('onLegendClick')}
16-
dataset={complexDataSet}
16+
dataset={scatterComplexDataSet}
1717
style={{ width: '100%' }}
18-
dimensions={[
19-
{
20-
accessor: 'name',
21-
formatter: (d) => `${d} 2019`,
22-
interval: 0
23-
}
24-
]}
2518
measures={[
2619
{
2720
accessor: 'users',
2821
label: 'Users',
29-
formatter: (val) => val.toLocaleString()
22+
axis: 'x'
3023
},
3124
{
3225
accessor: 'sessions',
33-
label: 'Active Sessions',
34-
formatter: (val) => `${val} sessions`,
35-
hideDataLabel: true
26+
label: 'Sessions',
27+
axis: 'y'
3628
},
3729
{
3830
accessor: 'volume',
39-
label: 'Vol.'
31+
axis: 'z'
4032
}
4133
]}
4234
/>
@@ -45,7 +37,7 @@ describe('LineChart', () => {
4537
});
4638

4739
test('loading placeholder', () => {
48-
const wrapper = mount(<LineChart style={{ width: '30%' }} dimensions={[]} measures={[]} />);
40+
const wrapper = mount(<ScatterChart style={{ width: '30%' }} measures={[]} />);
4941
expect(wrapper.render()).toMatchSnapshot();
5042
});
5143
});

packages/charts/src/components/ScatterChart/ScatterChart.tsx

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { XAxisTicks } from '@ui5/webcomponents-react-charts/lib/components/XAxis
66
import { YAxisTicks } from '@ui5/webcomponents-react-charts/lib/components/YAxisTicks';
77
import { LineChartPlaceholder } from '@ui5/webcomponents-react-charts/lib/LineChartPlaceholder';
88
import { useLegendItemClick } from '@ui5/webcomponents-react-charts/lib/useLegendItemClick';
9-
import React, { FC, forwardRef, Ref, useCallback, useMemo } from 'react';
9+
import React, { CSSProperties, FC, forwardRef, Ref, useCallback, useMemo } from 'react';
1010
import {
1111
CartesianGrid,
1212
Legend,
@@ -48,7 +48,8 @@ interface MeasureConfig extends IChartMeasure {
4848

4949
interface ScatterDataObject {
5050
label?: string;
51-
data: any[];
51+
data?: any[];
52+
color?: CSSProperties['color'];
5253
}
5354

5455
export interface ScatterChartProps extends IChartBaseProps {
@@ -71,7 +72,7 @@ export interface ScatterChartProps extends IChartBaseProps {
7172
* - `opacity`: line opacity, defaults to `1`
7273
*
7374
*/
74-
measures: MeasureConfig[];
75+
measures?: MeasureConfig[];
7576
}
7677

7778
const measureDefaults = {
@@ -142,11 +143,10 @@ const ScatterChart: FC<ScatterChartProps> = forwardRef((props: ScatterChartProps
142143
const zMeasure = measures.filter(({ axis }) => axis === 'z');
143144

144145
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(
145-
dataset[0].data,
146+
dataset?.[0].data,
146147
measures.filter(({ axis }) => axis === 'y')
147148
);
148149
const xAxisHeights = useObserveXAxisHeights(chartRef, 1);
149-
150150
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
151151

152152
return (
@@ -174,41 +174,42 @@ const ScatterChart: FC<ScatterChartProps> = forwardRef((props: ScatterChartProps
174174
{chartConfig.xAxisVisible && (
175175
<XAxis
176176
type={'number'}
177-
key={xMeasure[0].accessor}
178-
label={{ position: 'bottom', value: xMeasure[0].label }}
179-
dataKey={xMeasure[0].accessor}
180-
interval={xMeasure[0]?.interval ?? (isBigDataSet ? 'preserveStart' : 0)}
181-
tick={<XAxisTicks config={xMeasure[0]} />}
177+
key={xMeasure?.[0]?.accessor}
178+
label={{ position: 'bottom', value: xMeasure?.[0]?.label }}
179+
dataKey={xMeasure?.[0]?.accessor}
180+
xAxisId={0}
181+
interval={xMeasure?.[0]?.interval ?? (isBigDataSet ? 'preserveStart' : 0)}
182+
tick={<XAxisTicks config={xMeasure?.[0]} />}
182183
padding={xAxisPadding}
183184
// height={xAxisHeights[0]}
184185
/>
185186
)}
186187
<YAxis
187-
label={yMeasure[0].label ? { position: 'top', value: yMeasure[0].label } : false}
188+
label={yMeasure?.[0]?.label ? { position: 'top', value: yMeasure?.[0]?.label } : false}
188189
type={'number'}
189-
name={yMeasure[0].accessor}
190+
name={yMeasure?.[0]?.accessor}
190191
axisLine={chartConfig.yAxisVisible}
191192
tickLine={tickLineConfig}
192-
key={yMeasure[0].accessor}
193-
dataKey={yMeasure[0].accessor}
194-
tickFormatter={yMeasure[0]?.formatter}
193+
key={yMeasure?.[0]?.accessor}
194+
dataKey={yMeasure?.[0]?.accessor}
195+
tickFormatter={yMeasure?.[0]?.formatter}
195196
interval={0}
196197
tick={<YAxisTicks config={yMeasure[0]} />}
197198
width={yAxisWidth}
198-
padding={yMeasure[0].label ? { top: 10 } : 0}
199+
padding={yMeasure?.[0]?.label ? { top: 10 } : 0}
199200
/>
200201
<ZAxis
201-
name={zMeasure[0].accessor}
202-
dataKey={zMeasure[0].accessor}
202+
name={zMeasure?.[0]?.accessor}
203+
dataKey={zMeasure?.[0]?.accessor}
203204
range={[0, 5000]}
204-
key={zMeasure[0].accessor}
205+
key={zMeasure?.[0]?.accessor}
205206
/>
206-
{dataset.map((dataSet, index) => {
207+
{dataset?.map((dataSet, index) => {
207208
return (
208209
<Scatter
209-
data={dataSet.data}
210-
name={dataSet.label}
211-
fill={`var(--sapChart_OrderedColor_${(index % 11) + 1})`}
210+
data={dataSet?.data}
211+
name={dataSet?.label}
212+
fill={dataSet?.color ?? `var(--sapChart_OrderedColor_${(index % 11) + 1})`}
212213
isAnimationActive={noAnimation === false}
213214
/>
214215
);

0 commit comments

Comments
 (0)