ChartDataProvider.stories.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import {
  20. SuperChart,
  21. ChartDataProvider,
  22. SupersetClient,
  23. } from '@superset-ui/core';
  24. import { BigNumberChartPlugin } from '@superset-ui/plugin-chart-echarts';
  25. import { WordCloudChartPlugin } from '@superset-ui/plugin-chart-word-cloud';
  26. import {
  27. bigNumberFormData,
  28. sankeyFormData,
  29. sunburstFormData,
  30. wordCloudFormData,
  31. } from '../../../../superset-ui-core/test/chart/fixtures/formData';
  32. import Expandable from '../../shared/components/Expandable';
  33. import VerifyCORS, { renderError } from '../../shared/components/VerifyCORS';
  34. const BIG_NUMBER = bigNumberFormData.viz_type;
  35. const SANKEY = sankeyFormData.viz_type;
  36. const SUNBURST = sunburstFormData.viz_type;
  37. const WORD_CLOUD_LEGACY = wordCloudFormData.viz_type;
  38. const WORD_CLOUD = 'new_word_cloud';
  39. new BigNumberChartPlugin().configure({ key: BIG_NUMBER }).register();
  40. // eslint-disable-next-line
  41. new WordCloudChartPlugin().configure({ key: WORD_CLOUD }).register();
  42. const VIS_TYPES = [BIG_NUMBER, SANKEY, SUNBURST, WORD_CLOUD, WORD_CLOUD_LEGACY];
  43. const FORM_DATA_LOOKUP = {
  44. [BIG_NUMBER]: bigNumberFormData,
  45. [SANKEY]: sankeyFormData,
  46. [SUNBURST]: sunburstFormData,
  47. [WORD_CLOUD]: { ...wordCloudFormData, viz_type: WORD_CLOUD },
  48. [WORD_CLOUD_LEGACY]: wordCloudFormData,
  49. };
  50. export default {
  51. title: 'Others/DataProvider',
  52. decorators: [],
  53. };
  54. export const dataProvider = ({
  55. host,
  56. visType,
  57. width,
  58. height,
  59. formData,
  60. }: {
  61. host: string;
  62. visType: string;
  63. width: number;
  64. height: number;
  65. formData: string;
  66. }) => (
  67. <div style={{ margin: 16 }}>
  68. <VerifyCORS host={host}>
  69. {() => (
  70. <ChartDataProvider
  71. client={SupersetClient}
  72. formData={JSON.parse(formData)}
  73. >
  74. {({ loading, payload, error }) => {
  75. if (loading) return <div>Loading!</div>;
  76. if (error) return renderError(error);
  77. if (payload)
  78. return (
  79. <>
  80. <SuperChart
  81. chartType={visType}
  82. formData={payload.formData}
  83. height={Number(height)}
  84. // @TODO fix typing
  85. // all vis's now expect objects but api/v1/ returns an array
  86. queriesData={payload.queriesData}
  87. width={Number(width)}
  88. />
  89. <br />
  90. <Expandable expandableWhat="payload">
  91. <pre style={{ fontSize: 11 }}>
  92. {JSON.stringify(payload, null, 2)}
  93. </pre>
  94. </Expandable>
  95. </>
  96. );
  97. return null;
  98. }}
  99. </ChartDataProvider>
  100. )}
  101. </VerifyCORS>
  102. </div>
  103. );
  104. dataProvider.storyName = 'ChartDataProvider';
  105. dataProvider.args = {
  106. host: 'localhost:8088',
  107. visType: VIS_TYPES[0],
  108. width: '500',
  109. height: '300',
  110. formData: JSON.stringify(FORM_DATA_LOOKUP[VIS_TYPES[0]]),
  111. };
  112. dataProvider.argTypes = {
  113. host: {
  114. control: 'text',
  115. description: 'Set Superset App host for CORS request',
  116. },
  117. visType: {
  118. control: 'select',
  119. options: VIS_TYPES,
  120. description: 'Chart Plugin Type',
  121. },
  122. width: { control: 'text', description: 'Vis width' },
  123. height: { control: 'text', description: 'Vis height' },
  124. formData: { control: 'text', description: 'Override formData' },
  125. };