-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
feat: drill by display chart #23524
feat: drill by display chart #23524
Changes from 8 commits
820c9c3
f859c8f
e4e4de1
bd21713
bc4fc85
8b684d1
b444ed0
33e9176
2a4f26c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* 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 fetchWithData = () => { | ||
// fetchMock.post(CHART_DATA_ENDPOINT, { | ||
// result: [ | ||
// { | ||
// cache_key: 'caaaf4408f8f440ac1c8fedc857d4e84', | ||
// cached_dttm: null, | ||
// cache_timeout: 300, | ||
// applied_template_filters: [], | ||
// annotation_data: {}, | ||
// error: null, | ||
// is_cached: null, | ||
// query: | ||
// "SELECT num_girls AS num_girls,\n SUM(num) AS sum__num\nFROM public.birth_names\nWHERE ds >= TO_TIMESTAMP('1923-04-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US')\n AND ds < TO_TIMESTAMP('2023-04-01 00:40:03.000000', 'YYYY-MM-DD HH24:MI:SS.US')\n AND gender = 'boy'\nGROUP BY num_girls\nORDER BY sum__num DESC\nLIMIT 50000;\n\n", | ||
// status: 'success', | ||
// stacktrace: null, | ||
// rowcount: 1, | ||
// from_dttm: -1475452800000, | ||
// to_dttm: 1680309603000, | ||
// label_map: { | ||
// num_girls: ['num_girls'], | ||
// sum__num: ['sum__num'], | ||
// }, | ||
// colnames: ['num_girls', 'sum__num'], | ||
// indexnames: [0], | ||
// coltypes: [0, 0], | ||
// data: [ | ||
// { | ||
// num_girls: 0, | ||
// sum__num: 48133355, | ||
// }, | ||
// ], | ||
// result_format: 'json', | ||
// applied_filters: [ | ||
// { | ||
// column: 'gender', | ||
// }, | ||
// ], | ||
// rejected_filters: [], | ||
// }, | ||
// ], | ||
// }); | ||
// }; | ||
|
||
const fetchWithNoData = () => { | ||
fetchMock.post(CHART_DATA_ENDPOINT, { | ||
result: [ | ||
{ | ||
total_count: 0, | ||
data: [], | ||
colnames: [], | ||
coltypes: [], | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
const setup = (overrides: Record<string, any> = {}) => { | ||
const props = { | ||
filters: [], | ||
column: { column_name: 'name' }, | ||
formData: chart.form_data, | ||
groupbyFieldName: '', | ||
...overrides, | ||
}; | ||
return render(<DrillByChart {...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(); | ||
}); | ||
|
||
// test('should render SuperChart', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this test be fixed/uncommented before merging or should it be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't get the test to work. i will remove it |
||
// fetchWithData(); | ||
// await waitForRender(); | ||
// screen.logTestingPlaygroundURL(); | ||
// expect(await screen.findByText('other')).toBeInTheDocument(); | ||
// }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* 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, | ||
}: 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 || 'groupby']: groupbyField, | ||
lilykuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
useEffect(() => { | ||
getChartDataRequest({ | ||
formData: updatedFormData, | ||
}).then(({ json }) => { | ||
setChartDataResult(json.result); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div | ||
css={css` | ||
width: 100%; | ||
height: 100%; | ||
`} | ||
> | ||
{chartDataResult ? ( | ||
<SuperChart | ||
behaviors={[Behavior.INTERACTIVE_CHART]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
chartType={formData.viz_type} | ||
enableNoResults | ||
formData={updatedFormData} | ||
height="100%" | ||
queriesData={chartDataResult} | ||
width="100%" | ||
/> | ||
) : ( | ||
<Loading /> | ||
)} | ||
</div> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove commented code?