suggestions.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { FieldColorModeId, VisualizationSuggestionsBuilder } from '@grafana/data';
  2. import {
  3. GraphDrawStyle,
  4. GraphFieldConfig,
  5. GraphGradientMode,
  6. LegendDisplayMode,
  7. LineInterpolation,
  8. StackingMode,
  9. } from '@grafana/schema';
  10. import { SuggestionName } from 'app/types/suggestions';
  11. import { TimeSeriesOptions } from './types';
  12. export class TimeSeriesSuggestionsSupplier {
  13. getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
  14. const { dataSummary } = builder;
  15. if (!dataSummary.hasTimeField || !dataSummary.hasNumberField || dataSummary.rowCountTotal < 2) {
  16. return;
  17. }
  18. const list = builder.getListAppender<TimeSeriesOptions, GraphFieldConfig>({
  19. name: SuggestionName.LineChart,
  20. pluginId: 'timeseries',
  21. options: {
  22. legend: {} as any,
  23. },
  24. fieldConfig: {
  25. defaults: {
  26. custom: {},
  27. },
  28. overrides: [],
  29. },
  30. cardOptions: {
  31. previewModifier: (s) => {
  32. s.options!.legend.displayMode = LegendDisplayMode.Hidden;
  33. if (s.fieldConfig?.defaults.custom?.drawStyle !== GraphDrawStyle.Bars) {
  34. s.fieldConfig!.defaults.custom!.lineWidth = Math.max(s.fieldConfig!.defaults.custom!.lineWidth ?? 1, 2);
  35. }
  36. },
  37. },
  38. });
  39. const maxBarsCount = 100;
  40. list.append({
  41. name: SuggestionName.LineChart,
  42. });
  43. if (dataSummary.rowCountMax < 200) {
  44. list.append({
  45. name: SuggestionName.LineChartSmooth,
  46. fieldConfig: {
  47. defaults: {
  48. custom: {
  49. lineInterpolation: LineInterpolation.Smooth,
  50. },
  51. },
  52. overrides: [],
  53. },
  54. });
  55. }
  56. // Single series suggestions
  57. if (dataSummary.numberFieldCount === 1) {
  58. list.append({
  59. name: SuggestionName.AreaChart,
  60. fieldConfig: {
  61. defaults: {
  62. custom: {
  63. fillOpacity: 25,
  64. },
  65. },
  66. overrides: [],
  67. },
  68. });
  69. list.append({
  70. name: SuggestionName.LineChartGradientColorScheme,
  71. fieldConfig: {
  72. defaults: {
  73. color: {
  74. mode: FieldColorModeId.ContinuousGrYlRd,
  75. },
  76. custom: {
  77. gradientMode: GraphGradientMode.Scheme,
  78. lineInterpolation: LineInterpolation.Smooth,
  79. lineWidth: 3,
  80. fillOpacity: 20,
  81. },
  82. },
  83. overrides: [],
  84. },
  85. });
  86. if (dataSummary.rowCountMax < maxBarsCount) {
  87. list.append({
  88. name: SuggestionName.BarChart,
  89. fieldConfig: {
  90. defaults: {
  91. custom: {
  92. drawStyle: GraphDrawStyle.Bars,
  93. fillOpacity: 100,
  94. lineWidth: 1,
  95. gradientMode: GraphGradientMode.Hue,
  96. },
  97. },
  98. overrides: [],
  99. },
  100. });
  101. list.append({
  102. name: SuggestionName.BarChartGradientColorScheme,
  103. fieldConfig: {
  104. defaults: {
  105. color: {
  106. mode: FieldColorModeId.ContinuousGrYlRd,
  107. },
  108. custom: {
  109. drawStyle: GraphDrawStyle.Bars,
  110. fillOpacity: 90,
  111. lineWidth: 1,
  112. gradientMode: GraphGradientMode.Scheme,
  113. },
  114. },
  115. overrides: [],
  116. },
  117. });
  118. }
  119. return;
  120. }
  121. // Multiple series suggestions
  122. list.append({
  123. name: SuggestionName.AreaChartStacked,
  124. fieldConfig: {
  125. defaults: {
  126. custom: {
  127. fillOpacity: 25,
  128. stacking: {
  129. mode: StackingMode.Normal,
  130. group: 'A',
  131. },
  132. },
  133. },
  134. overrides: [],
  135. },
  136. });
  137. list.append({
  138. name: SuggestionName.AreaChartStackedPercent,
  139. fieldConfig: {
  140. defaults: {
  141. custom: {
  142. fillOpacity: 25,
  143. stacking: {
  144. mode: StackingMode.Percent,
  145. group: 'A',
  146. },
  147. },
  148. },
  149. overrides: [],
  150. },
  151. });
  152. if (dataSummary.rowCountTotal / dataSummary.numberFieldCount < maxBarsCount) {
  153. list.append({
  154. name: SuggestionName.BarChartStacked,
  155. fieldConfig: {
  156. defaults: {
  157. custom: {
  158. drawStyle: GraphDrawStyle.Bars,
  159. fillOpacity: 100,
  160. lineWidth: 1,
  161. gradientMode: GraphGradientMode.Hue,
  162. stacking: {
  163. mode: StackingMode.Normal,
  164. group: 'A',
  165. },
  166. },
  167. },
  168. overrides: [],
  169. },
  170. });
  171. list.append({
  172. name: SuggestionName.BarChartStackedPercent,
  173. fieldConfig: {
  174. defaults: {
  175. custom: {
  176. drawStyle: GraphDrawStyle.Bars,
  177. fillOpacity: 100,
  178. lineWidth: 1,
  179. gradientMode: GraphGradientMode.Hue,
  180. stacking: {
  181. mode: StackingMode.Percent,
  182. group: 'A',
  183. },
  184. },
  185. },
  186. overrides: [],
  187. },
  188. });
  189. }
  190. }
  191. }