Table.stories.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 memoizeOne from 'memoize-one';
  20. import { SuperChart, VizType } from '@superset-ui/core';
  21. import TableChartPlugin, {
  22. TableChartProps,
  23. } from '@superset-ui/plugin-chart-table';
  24. import { basicFormData, basicData, birthNames } from './testData';
  25. import { withResizableChartDemo } from '../../../shared/components/ResizableChartDemo';
  26. export default {
  27. title: 'Chart Plugins/plugin-chart-table',
  28. decorators: [withResizableChartDemo],
  29. args: {
  30. rows: 2046,
  31. cols: 8,
  32. pageLength: 50,
  33. includeSearch: true,
  34. alignPn: false,
  35. showCellBars: true,
  36. allowRearrangeColumns: false,
  37. },
  38. argTypes: {
  39. rows: {
  40. control: 'number',
  41. name: 'Records',
  42. min: 0,
  43. max: 50000,
  44. },
  45. cols: {
  46. control: 'number',
  47. name: 'Columns',
  48. min: 1,
  49. max: 20,
  50. },
  51. pageLength: {
  52. control: 'number',
  53. name: 'Page size',
  54. min: 0,
  55. max: 100,
  56. },
  57. includeSearch: {
  58. control: 'boolean',
  59. name: 'Include search',
  60. },
  61. alignPn: {
  62. control: 'boolean',
  63. name: 'Align PosNeg',
  64. },
  65. showCellBars: {
  66. control: 'boolean',
  67. name: 'Show Cell Bars',
  68. },
  69. allowRearrangeColumns: {
  70. control: 'boolean',
  71. name: 'Allow end user to drag-and-drop column headers to rearrange them.',
  72. },
  73. },
  74. };
  75. new TableChartPlugin().configure({ key: VizType.Table }).register();
  76. function expandArray<T>(input: T[], targetSize: number) {
  77. if (!input || input.length === 0) {
  78. throw new Error('Cannot expand an empty array');
  79. }
  80. let arr = input;
  81. while (arr.length < targetSize) {
  82. arr = arr.concat(arr);
  83. }
  84. return arr.slice(0, targetSize);
  85. }
  86. // memoize expanded array so to make sure we always return the same
  87. // data when changing page sizes
  88. const expandRecords = memoizeOne(expandArray);
  89. const expandColumns = memoizeOne(expandArray);
  90. /**
  91. * Load sample data for testing
  92. * @param props the original props passed to SuperChart
  93. * @param pageLength number of records per page
  94. * @param rows the target number of records
  95. * @param cols the target number of columns
  96. */
  97. function loadData(
  98. props: TableChartProps,
  99. {
  100. pageLength = 50,
  101. rows = 1042,
  102. cols = 8,
  103. alignPn = false,
  104. showCellBars = true,
  105. includeSearch = true,
  106. allowRearrangeColumns = false,
  107. },
  108. ): TableChartProps {
  109. if (!props.queriesData?.[0]) return props;
  110. const records = props.queriesData?.[0].data || [];
  111. const columns = props.queriesData?.[0].colnames || [];
  112. return {
  113. ...props,
  114. queriesData: [
  115. {
  116. ...props.queriesData[0],
  117. data: expandRecords(records, rows),
  118. colnames: expandColumns(columns, cols),
  119. },
  120. ],
  121. formData: {
  122. ...props.formData,
  123. align_pn: alignPn,
  124. page_length: pageLength,
  125. show_cell_bars: showCellBars,
  126. include_search: includeSearch,
  127. allow_rearrange_columns: allowRearrangeColumns,
  128. },
  129. height: window.innerHeight - 130,
  130. };
  131. }
  132. export const Basic = ({ width, height }: { width: number; height: number }) => (
  133. <SuperChart
  134. chartType={VizType.Table}
  135. datasource={{
  136. columnFormats: {},
  137. }}
  138. width={width}
  139. height={height}
  140. queriesData={[basicData]}
  141. formData={basicFormData}
  142. />
  143. );
  144. Basic.parameters = {
  145. initialSize: {
  146. width: 680,
  147. height: 420,
  148. },
  149. };
  150. export const BigTable = (
  151. {
  152. rows,
  153. cols,
  154. pageLength,
  155. includeSearch,
  156. alignPn,
  157. showCellBars,
  158. allowRearrangeColumns,
  159. }: {
  160. rows: number;
  161. cols: number;
  162. pageLength: number;
  163. includeSearch: boolean;
  164. alignPn: boolean;
  165. showCellBars: boolean;
  166. allowRearrangeColumns: boolean;
  167. },
  168. { width, height }: { width: number; height: number },
  169. ) => {
  170. const chartProps = loadData(birthNames, {
  171. pageLength,
  172. rows,
  173. cols,
  174. alignPn,
  175. showCellBars,
  176. includeSearch,
  177. allowRearrangeColumns,
  178. });
  179. return (
  180. <SuperChart
  181. chartType={VizType.Table}
  182. {...chartProps}
  183. width={width}
  184. height={height}
  185. />
  186. );
  187. };
  188. BigTable.parameters = {
  189. initialSize: {
  190. width: 620,
  191. height: 440,
  192. },
  193. };