InspectDataOptions.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { FC } from 'react';
  2. import { DataFrame, DataTransformerID, getFrameDisplayName, SelectableValue } from '@grafana/data';
  3. import { Field, HorizontalGroup, Select, Switch, VerticalGroup } from '@grafana/ui';
  4. import { QueryOperationRow } from 'app/core/components/QueryOperationRow/QueryOperationRow';
  5. import { PanelModel } from 'app/features/dashboard/state';
  6. import { DetailText } from 'app/features/inspector/DetailText';
  7. import { GetDataOptions } from 'app/features/query/state/PanelQueryRunner';
  8. import { getPanelInspectorStyles } from './styles';
  9. interface Props {
  10. options: GetDataOptions;
  11. dataFrames: DataFrame[];
  12. transformId: DataTransformerID;
  13. transformationOptions: Array<SelectableValue<DataTransformerID>>;
  14. selectedDataFrame: number | DataTransformerID;
  15. downloadForExcel: boolean;
  16. onDataFrameChange: (item: SelectableValue<DataTransformerID | number>) => void;
  17. toggleDownloadForExcel: () => void;
  18. data?: DataFrame[];
  19. panel?: PanelModel;
  20. onOptionsChange?: (options: GetDataOptions) => void;
  21. }
  22. export const InspectDataOptions: FC<Props> = ({
  23. options,
  24. onOptionsChange,
  25. panel,
  26. data,
  27. dataFrames,
  28. transformId,
  29. transformationOptions,
  30. selectedDataFrame,
  31. onDataFrameChange,
  32. downloadForExcel,
  33. toggleDownloadForExcel,
  34. }) => {
  35. const styles = getPanelInspectorStyles();
  36. const panelTransformations = panel?.getTransformations();
  37. const showPanelTransformationsOption =
  38. Boolean(panelTransformations?.length) && (transformId as any) !== 'join by time';
  39. const showFieldConfigsOption = panel && !panel.plugin?.fieldConfigRegistry.isEmpty();
  40. let dataSelect = dataFrames;
  41. if (selectedDataFrame === DataTransformerID.seriesToColumns) {
  42. dataSelect = data!;
  43. }
  44. const choices = dataSelect.map((frame, index) => {
  45. return {
  46. value: index,
  47. label: `${getFrameDisplayName(frame)} (${index})`,
  48. } as SelectableValue<number>;
  49. });
  50. const selectableOptions = [...transformationOptions, ...choices];
  51. function getActiveString() {
  52. let activeString = '';
  53. if (!data) {
  54. return activeString;
  55. }
  56. const parts: string[] = [];
  57. if (selectedDataFrame === DataTransformerID.seriesToColumns) {
  58. parts.push('Series joined by time');
  59. } else if (data.length > 1) {
  60. parts.push(getFrameDisplayName(data[selectedDataFrame as number]));
  61. }
  62. if (options.withTransforms || options.withFieldConfig) {
  63. if (options.withTransforms) {
  64. parts.push('Panel transforms');
  65. }
  66. if (options.withTransforms && options.withFieldConfig) {
  67. }
  68. if (options.withFieldConfig) {
  69. parts.push('Formatted data');
  70. }
  71. }
  72. if (downloadForExcel) {
  73. parts.push('Excel header');
  74. }
  75. return parts.join(', ');
  76. }
  77. return (
  78. <div className={styles.dataDisplayOptions}>
  79. <QueryOperationRow
  80. id="Data options"
  81. index={0}
  82. title="Data options"
  83. headerElement={<DetailText>{getActiveString()}</DetailText>}
  84. isOpen={false}
  85. >
  86. <div className={styles.options} data-testid="dataOptions">
  87. <VerticalGroup spacing="none">
  88. {data!.length > 1 && (
  89. <Field label="Show data frame">
  90. <Select
  91. options={selectableOptions}
  92. value={selectedDataFrame}
  93. onChange={onDataFrameChange}
  94. width={30}
  95. aria-label="Select dataframe"
  96. />
  97. </Field>
  98. )}
  99. <HorizontalGroup>
  100. {showPanelTransformationsOption && onOptionsChange && (
  101. <Field
  102. label="Apply panel transformations"
  103. description="Table data is displayed with transformations defined in the panel Transform tab."
  104. >
  105. <Switch
  106. value={!!options.withTransforms}
  107. onChange={() => onOptionsChange({ ...options, withTransforms: !options.withTransforms })}
  108. />
  109. </Field>
  110. )}
  111. {showFieldConfigsOption && onOptionsChange && (
  112. <Field
  113. label="Formatted data"
  114. description="Table data is formatted with options defined in the Field and Override tabs."
  115. >
  116. <Switch
  117. id="formatted-data-toggle"
  118. value={!!options.withFieldConfig}
  119. onChange={() => onOptionsChange({ ...options, withFieldConfig: !options.withFieldConfig })}
  120. />
  121. </Field>
  122. )}
  123. <Field label="Download for Excel" description="Adds header to CSV for use with Excel">
  124. <Switch id="excel-toggle" value={downloadForExcel} onChange={toggleDownloadForExcel} />
  125. </Field>
  126. </HorizontalGroup>
  127. </VerticalGroup>
  128. </div>
  129. </QueryOperationRow>
  130. </div>
  131. );
  132. };