suggestions.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { VisualizationSuggestionsBuilder, VizOrientation } from '@grafana/data';
  2. import { LegendDisplayMode, StackingMode, VisibilityMode } from '@grafana/schema';
  3. import { SuggestionName } from 'app/types/suggestions';
  4. import { BarChartFieldConfig, PanelOptions } from './models.gen';
  5. export class BarChartSuggestionsSupplier {
  6. getListWithDefaults(builder: VisualizationSuggestionsBuilder) {
  7. return builder.getListAppender<PanelOptions, BarChartFieldConfig>({
  8. name: SuggestionName.BarChart,
  9. pluginId: 'barchart',
  10. options: {
  11. showValue: VisibilityMode.Never,
  12. legend: {
  13. displayMode: LegendDisplayMode.Hidden,
  14. placement: 'right',
  15. } as any,
  16. },
  17. fieldConfig: {
  18. defaults: {
  19. unit: 'short',
  20. custom: {},
  21. },
  22. overrides: [],
  23. },
  24. cardOptions: {
  25. previewModifier: (s) => {
  26. s.options!.barWidth = 0.8;
  27. },
  28. },
  29. });
  30. }
  31. getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
  32. const list = this.getListWithDefaults(builder);
  33. const { dataSummary } = builder;
  34. if (dataSummary.frameCount !== 1) {
  35. return;
  36. }
  37. if (!dataSummary.hasNumberField || !dataSummary.hasStringField) {
  38. return;
  39. }
  40. // if you have this many rows barchart might not be a good fit
  41. if (dataSummary.rowCountTotal > 50) {
  42. return;
  43. }
  44. // Vertical bars
  45. list.append({
  46. name: SuggestionName.BarChart,
  47. });
  48. if (dataSummary.numberFieldCount > 1) {
  49. list.append({
  50. name: SuggestionName.BarChartStacked,
  51. options: {
  52. stacking: StackingMode.Normal,
  53. },
  54. });
  55. list.append({
  56. name: SuggestionName.BarChartStackedPercent,
  57. options: {
  58. stacking: StackingMode.Percent,
  59. },
  60. });
  61. }
  62. // horizontal bars
  63. list.append({
  64. name: SuggestionName.BarChartHorizontal,
  65. options: {
  66. orientation: VizOrientation.Horizontal,
  67. },
  68. });
  69. if (dataSummary.numberFieldCount > 1) {
  70. list.append({
  71. name: SuggestionName.BarChartHorizontalStacked,
  72. options: {
  73. stacking: StackingMode.Normal,
  74. orientation: VizOrientation.Horizontal,
  75. },
  76. });
  77. list.append({
  78. name: SuggestionName.BarChartHorizontalStackedPercent,
  79. options: {
  80. orientation: VizOrientation.Horizontal,
  81. stacking: StackingMode.Percent,
  82. },
  83. });
  84. }
  85. }
  86. }