suggestions.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { VisualizationSuggestionsBuilder } from '@grafana/data';
  2. import { LegendDisplayMode } from '@grafana/schema';
  3. import { SuggestionName } from 'app/types/suggestions';
  4. import { PieChartLabels, PieChartOptions, PieChartType } from './types';
  5. export class PieChartSuggestionsSupplier {
  6. getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
  7. const list = builder.getListAppender<PieChartOptions, {}>({
  8. name: SuggestionName.PieChart,
  9. pluginId: 'piechart',
  10. options: {
  11. reduceOptions: {
  12. values: false,
  13. calcs: ['lastNotNull'],
  14. },
  15. displayLabels: [PieChartLabels.Percent],
  16. legend: {
  17. placement: 'right',
  18. values: [],
  19. } as any,
  20. },
  21. cardOptions: {
  22. previewModifier: (s) => {
  23. // Hide labels in preview
  24. s.options!.legend.displayMode = LegendDisplayMode.Hidden;
  25. },
  26. },
  27. });
  28. const { dataSummary } = builder;
  29. if (!dataSummary.hasNumberField) {
  30. return;
  31. }
  32. if (dataSummary.hasStringField && dataSummary.frameCount === 1) {
  33. // if many values this or single value PieChart is not a good option
  34. if (dataSummary.rowCountTotal > 30 || dataSummary.rowCountTotal < 2) {
  35. return;
  36. }
  37. list.append({
  38. name: SuggestionName.PieChart,
  39. options: {
  40. reduceOptions: {
  41. values: true,
  42. calcs: [],
  43. },
  44. },
  45. });
  46. list.append({
  47. name: SuggestionName.PieChartDonut,
  48. options: {
  49. reduceOptions: {
  50. values: true,
  51. calcs: [],
  52. },
  53. pieType: PieChartType.Donut,
  54. },
  55. });
  56. return;
  57. }
  58. if (dataSummary.numberFieldCount > 30 || dataSummary.numberFieldCount < 2) {
  59. return;
  60. }
  61. list.append({
  62. name: SuggestionName.PieChart,
  63. });
  64. list.append({
  65. name: SuggestionName.PieChartDonut,
  66. options: {
  67. pieType: PieChartType.Donut,
  68. },
  69. });
  70. }
  71. }