suggestions.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { VisualizationSuggestionsBuilder, VizOrientation } from '@grafana/data';
  2. import { BarGaugeDisplayMode } from '@grafana/ui';
  3. import { SuggestionName } from 'app/types/suggestions';
  4. import { BarGaugeOptions } from './types';
  5. export class BarGaugeSuggestionsSupplier {
  6. getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
  7. const { dataSummary } = builder;
  8. if (!dataSummary.hasData || !dataSummary.hasNumberField) {
  9. return;
  10. }
  11. const list = builder.getListAppender<BarGaugeOptions, {}>({
  12. name: '',
  13. pluginId: 'bargauge',
  14. options: {},
  15. fieldConfig: {
  16. defaults: {
  17. custom: {},
  18. },
  19. overrides: [],
  20. },
  21. });
  22. // This is probably not a good option for many numeric fields
  23. if (dataSummary.numberFieldCount > 50) {
  24. return;
  25. }
  26. // To use show individual row values we also need a string field to give each value a name
  27. if (dataSummary.hasStringField && dataSummary.frameCount === 1 && dataSummary.rowCountTotal < 30) {
  28. list.append({
  29. name: SuggestionName.BarGaugeBasic,
  30. options: {
  31. reduceOptions: {
  32. values: true,
  33. calcs: [],
  34. },
  35. displayMode: BarGaugeDisplayMode.Basic,
  36. orientation: VizOrientation.Horizontal,
  37. },
  38. fieldConfig: {
  39. defaults: {
  40. color: {
  41. mode: 'continuous-GrYlRd',
  42. },
  43. },
  44. overrides: [],
  45. },
  46. });
  47. list.append({
  48. name: SuggestionName.BarGaugeLCD,
  49. options: {
  50. reduceOptions: {
  51. values: true,
  52. calcs: [],
  53. },
  54. displayMode: BarGaugeDisplayMode.Lcd,
  55. orientation: VizOrientation.Horizontal,
  56. },
  57. fieldConfig: {
  58. defaults: {
  59. color: {
  60. mode: 'continuous-GrYlRd',
  61. },
  62. },
  63. overrides: [],
  64. },
  65. });
  66. } else {
  67. list.append({
  68. name: SuggestionName.BarGaugeBasic,
  69. options: {
  70. displayMode: BarGaugeDisplayMode.Basic,
  71. orientation: VizOrientation.Horizontal,
  72. reduceOptions: {
  73. values: false,
  74. calcs: ['lastNotNull'],
  75. },
  76. },
  77. fieldConfig: {
  78. defaults: {
  79. color: {
  80. mode: 'continuous-GrYlRd',
  81. },
  82. },
  83. overrides: [],
  84. },
  85. });
  86. list.append({
  87. name: SuggestionName.BarGaugeLCD,
  88. options: {
  89. displayMode: BarGaugeDisplayMode.Lcd,
  90. orientation: VizOrientation.Horizontal,
  91. reduceOptions: {
  92. values: false,
  93. calcs: ['lastNotNull'],
  94. },
  95. },
  96. fieldConfig: {
  97. defaults: {
  98. color: {
  99. mode: 'continuous-GrYlRd',
  100. },
  101. },
  102. overrides: [],
  103. },
  104. });
  105. }
  106. }
  107. }