apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
60.49k stars 13.07k forks source link

sql query is different from chart results #28785

Open msx-xt opened 1 month ago

msx-xt commented 1 month ago

微信截图_20240531163715 chart has only six columns,but sql query has seven columns;

dosubot[bot] commented 1 month ago

Hey @msx-xt! I'm here to help you with any bugs, questions, or contributions while you wait for a human maintainer. Let's tackle this together!

The discrepancy between the number of columns in your SQL query results and the number of columns displayed in your chart is likely due to the configuration specified in the params section of the chart's schema. The params section lists the columns that are included in the chart:

params:
  all_columns:
  - name
  - global_sales
  - platform
  - genre
  - publisher
  - year

Only these columns will be displayed in the chart, regardless of how many columns are returned by your SQL query. If your SQL query returns more columns than these, the additional columns will not be displayed in the chart [[1]](https://github.com/apache/superset/blob/master/superset/examples/configs/charts/Video Game Sales/Games.yaml).

Additionally, the chart's schema in superset/charts/schemas.py defines how the data is processed and displayed. The ChartDataQueryContextSchema and related schemas specify the structure and fields that are used to generate the chart data, which includes selecting specific columns and applying post-processing operations. This ensures that only the specified columns are included in the final chart output [2].

Moreover, in the superset-frontend/plugins/plugin-chart-table/test/testData.ts file, the TableChartProps and TableChartFormData configurations further illustrate how the chart properties and form data are set up. For example, the colnames array specifies the columns that will be included in the chart, which may differ from the columns returned by the SQL query [3].

Furthermore, the useResultsTableView function in superset-frontend/src/components/Chart/DrillBy/useResultsTableView.tsx shows how the chart data is processed and displayed in a table view. The function uses the colnames and coltypes from the chartDataResult to render the table [4].

This configuration ensures that only the desired columns are displayed in the final chart output.

To continue talking to Dosu, mention @dosu.

msx-xt commented 1 month ago

how can i make that same

dosubot[bot] commented 1 month ago

To ensure that all columns returned by your SQL query are displayed in the chart, you need to configure the chart settings appropriately. Here are the steps to achieve this:

  1. Modify the Chart Configuration: Ensure that the column_config property is set up to include all columns. Here is an example configuration:

    const config: ControlPanelConfig = {
     controlPanelSections: [
       {
         label: t('Query'),
         expanded: true,
         controlSetRows: [
           // other controls
           [
             {
               name: 'all_columns',
               config: {
                 type: 'SelectControl',
                 label: t('Columns'),
                 description: t('Columns to display'),
                 multi: true,
                 freeForm: true,
                 allowAll: true,
                 commaChoosesOption: false,
                 optionRenderer: c => <ColumnOption showType column={c} />,
                 valueRenderer: c => <ColumnOption column={c} />,
                 valueKey: 'column_name',
                 mapStateToProps: ({ datasource, controls }, controlState) => ({
                   options: datasource?.columns || [],
                   queryMode: getQueryMode(controls),
                   externalValidationErrors:
                     isRawMode({ controls }) && ensureIsArray(controlState?.value).length === 0
                       ? [t('must have a value')]
                       : [],
                 }),
                 visibility: isRawMode,
                 resetOnHide: false,
               },
             },
           ],
           // other controls
         ],
       },
       {
         label: t('Options'),
         expanded: true,
         controlSetRows: [
           // other controls
           [
             {
               name: 'column_config',
               config: {
                 type: 'ColumnConfigControl',
                 label: t('Customize columns'),
                 description: t('Further customize how to display each column'),
                 width: 400,
                 height: 320,
                 renderTrigger: true,
                 shouldMapStateToProps() {
                   return true;
                 },
                 mapStateToProps(explore, _, chart) {
                   return {
                     queryResponse: chart?.queriesResponse?.[0] as
                       | ChartDataResponseResult
                       | undefined,
                   };
                 },
               },
             },
           ],
           // other controls
         ],
       },
     ],
     formDataOverrides: formData => ({
       ...formData,
       metrics: getStandardizedControls().popAllMetrics(),
       groupby: getStandardizedControls().popAllColumns(),
     }),
    };
  2. Ensure All Columns are Included in the Data: Verify that the queriesData prop passed to the SuperChart component in ChartRenderer.jsx includes all columns from your SQL query:

    <SuperChart
     disableErrorBoundary
     key={`${chartId}${webpackHash}`}
     id={`chart-id-${chartId}`}
     className={chartClassName}
     chartType={vizType}
     width={width}
     height={height}
     annotationData={annotationData}
     datasource={datasource}
     initialValues={initialValues}
     formData={currentFormData}
     ownState={ownState}
     filterState={filterState}
     hooks={this.hooks}
     behaviors={behaviors}
     queriesData={this.mutableQueriesResponse}
     onRenderSuccess={this.handleRenderSuccess}
     onRenderFailure={this.handleRenderFailure}
     noResults={noResultsComponent}
     postTransformProps={postTransformProps}
     emitCrossFilters={emitCrossFilters}
     legendState={this.state.legendState}
     {...drillToDetailProps}
    />
  3. Validate with Test Data: Use the testData.ts file to validate that all columns are displayed correctly. Here is an example test:

    import testData from './testData';
    
    const { basic } = testData;
    
    describe('Table Chart', () => {
     it('should display all columns', () => {
       const { colnames, data } = basic.queriesData[0];
    
       const renderedTable = renderTableChart(basic);
    
       colnames.forEach((colname) => {
         expect(renderedTable).toContain(colname);
       });
    
       data.forEach((row) => {
         Object.values(row).forEach((value) => {
           expect(renderedTable).toContain(value);
         });
       });
     });
    });

By following these steps, you can ensure that all columns returned by your SQL query are displayed in the chart [1][2][3].

To continue talking to Dosu, mention @dosu.

rusackas commented 1 month ago

Might be helpful to show your control panel config for the chart in question.