-
Notifications
You must be signed in to change notification settings - Fork 14k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: drill by display chart (#23524)
- Loading branch information
Lily Kuang
authored
Apr 4, 2023
1 parent
95db6c0
commit 4452a65
Showing
9 changed files
with
292 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
superset-frontend/src/components/Chart/DrillBy/DrillByChart.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React from 'react'; | ||
import { render, screen, waitFor } from 'spec/helpers/testing-library'; | ||
import chartQueries, { sliceId } from 'spec/fixtures/mockChartQueries'; | ||
import fetchMock from 'fetch-mock'; | ||
import DrillByChart from './DrillByChart'; | ||
|
||
const CHART_DATA_ENDPOINT = | ||
'glob:*api/v1/chart/data?form_data=%7B%22slice_id%22%3A18%7D'; | ||
|
||
const chart = chartQueries[sliceId]; | ||
|
||
const fetchWithNoData = () => { | ||
fetchMock.post(CHART_DATA_ENDPOINT, { | ||
result: [ | ||
{ | ||
total_count: 0, | ||
data: [], | ||
colnames: [], | ||
coltypes: [], | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
const setup = (overrides: Record<string, any> = {}) => { | ||
const props = { | ||
column: { column_name: 'state' }, | ||
formData: { ...chart.form_data, viz_type: 'pie' }, | ||
groupbyFieldName: 'groupby', | ||
...overrides, | ||
}; | ||
return render( | ||
<DrillByChart | ||
filters={[ | ||
{ | ||
col: 'gender', | ||
op: '==', | ||
val: 'boy', | ||
formattedVal: 'boy', | ||
}, | ||
]} | ||
{...props} | ||
/>, | ||
{ | ||
useRedux: true, | ||
}, | ||
); | ||
}; | ||
|
||
const waitForRender = (overrides: Record<string, any> = {}) => | ||
waitFor(() => setup(overrides)); | ||
|
||
afterEach(fetchMock.restore); | ||
|
||
test('should render', async () => { | ||
fetchWithNoData(); | ||
const { container } = await waitForRender(); | ||
expect(container).toBeInTheDocument(); | ||
}); | ||
|
||
test('should render loading indicator', async () => { | ||
setup(); | ||
await waitFor(() => | ||
expect(screen.getByLabelText('Loading')).toBeInTheDocument(), | ||
); | ||
}); | ||
|
||
test('should render the "No results" components', async () => { | ||
fetchWithNoData(); | ||
setup(); | ||
expect(await screen.findByText('No Results')).toBeInTheDocument(); | ||
}); |
94 changes: 94 additions & 0 deletions
94
superset-frontend/src/components/Chart/DrillBy/DrillByChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
Behavior, | ||
BinaryQueryObjectFilterClause, | ||
Column, | ||
css, | ||
SuperChart, | ||
} from '@superset-ui/core'; | ||
import { simpleFilterToAdhoc } from 'src/utils/simpleFilterToAdhoc'; | ||
import { getChartDataRequest } from 'src/components/Chart/chartAction'; | ||
import Loading from 'src/components/Loading'; | ||
|
||
interface DrillByChartProps { | ||
column?: Column; | ||
filters?: BinaryQueryObjectFilterClause[]; | ||
formData: { [key: string]: any; viz_type: string }; | ||
groupbyFieldName?: string; | ||
} | ||
|
||
export default function DrillByChart({ | ||
column, | ||
filters, | ||
formData, | ||
groupbyFieldName = 'groupby', | ||
}: DrillByChartProps) { | ||
let updatedFormData = formData; | ||
let groupbyField: any = []; | ||
const [chartDataResult, setChartDataResult] = useState(); | ||
|
||
if (groupbyFieldName && column) { | ||
groupbyField = Array.isArray(formData[groupbyFieldName]) | ||
? [column.column_name] | ||
: column.column_name; | ||
} | ||
|
||
if (filters) { | ||
const adhocFilters = filters.map(filter => simpleFilterToAdhoc(filter)); | ||
updatedFormData = { | ||
...formData, | ||
adhoc_filters: [...formData.adhoc_filters, ...adhocFilters], | ||
[groupbyFieldName]: groupbyField, | ||
}; | ||
} | ||
|
||
useEffect(() => { | ||
getChartDataRequest({ | ||
formData: updatedFormData, | ||
}).then(({ json }) => { | ||
setChartDataResult(json.result); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div | ||
css={css` | ||
width: 100%; | ||
height: 100%; | ||
`} | ||
> | ||
{chartDataResult ? ( | ||
<SuperChart | ||
disableErrorBoundary | ||
behaviors={[Behavior.INTERACTIVE_CHART]} | ||
chartType={formData.viz_type} | ||
enableNoResults | ||
formData={updatedFormData} | ||
height="100%" | ||
queriesData={chartDataResult} | ||
width="100%" | ||
/> | ||
) : ( | ||
<Loading /> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.