migrations.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { FieldColorModeId, FieldConfigProperty, FieldMatcherID, PanelModel } from '@grafana/data';
  2. import { LegendDisplayMode } from '@grafana/schema';
  3. import { PieChartOptions, PieChartLabels, PieChartLegendValues, PieChartType } from './types';
  4. export const PieChartPanelChangedHandler = (
  5. panel: PanelModel<Partial<PieChartOptions>> | any,
  6. prevPluginId: string,
  7. prevOptions: any
  8. ) => {
  9. if (prevPluginId === 'grafana-piechart-panel' && prevOptions.angular) {
  10. const angular = prevOptions.angular;
  11. const overrides = [];
  12. let options: PieChartOptions = panel.options;
  13. // Migrate color overrides for series
  14. if (angular.aliasColors) {
  15. for (const alias of Object.keys(angular.aliasColors)) {
  16. const color = angular.aliasColors[alias];
  17. if (color) {
  18. overrides.push({
  19. matcher: {
  20. id: FieldMatcherID.byName,
  21. options: alias,
  22. },
  23. properties: [
  24. {
  25. id: FieldConfigProperty.Color,
  26. value: {
  27. mode: FieldColorModeId.Fixed,
  28. fixedColor: color,
  29. },
  30. },
  31. ],
  32. });
  33. }
  34. }
  35. }
  36. panel.fieldConfig = {
  37. overrides,
  38. defaults: {
  39. unit: angular.format,
  40. decimals: angular.decimals ? angular.decimals : 0, // Old piechart defaults to 0 decimals while the new one defaults to 1
  41. },
  42. };
  43. options.legend = { placement: 'right', values: [], displayMode: LegendDisplayMode.Table, calcs: [] };
  44. if (angular.valueName) {
  45. options.reduceOptions = { calcs: [] };
  46. switch (angular.valueName) {
  47. case 'current':
  48. options.reduceOptions.calcs = ['lastNotNull'];
  49. break;
  50. case 'min':
  51. options.reduceOptions.calcs = ['min'];
  52. break;
  53. case 'max':
  54. options.reduceOptions.calcs = ['max'];
  55. break;
  56. case 'avg':
  57. options.reduceOptions.calcs = ['mean'];
  58. break;
  59. case 'total':
  60. options.reduceOptions.calcs = ['sum'];
  61. break;
  62. }
  63. }
  64. switch (angular.legendType) {
  65. case 'Under graph':
  66. options.legend.placement = 'bottom';
  67. break;
  68. case 'Right side':
  69. options.legend.placement = 'right';
  70. break;
  71. }
  72. switch (angular.pieType) {
  73. case 'pie':
  74. options.pieType = PieChartType.Pie;
  75. break;
  76. case 'donut':
  77. options.pieType = PieChartType.Donut;
  78. break;
  79. }
  80. if (angular.legend) {
  81. if (!angular.legend.show) {
  82. options.legend.displayMode = LegendDisplayMode.Hidden;
  83. }
  84. if (angular.legend.values) {
  85. options.legend.values.push(PieChartLegendValues.Value);
  86. }
  87. if (angular.legend.percentage) {
  88. options.legend.values.push(PieChartLegendValues.Percent);
  89. }
  90. if (!angular.legend.percentage && !angular.legend.values) {
  91. // If you deselect both value and percentage in the old pie chart plugin, the legend is hidden.
  92. options.legend.displayMode = LegendDisplayMode.Hidden;
  93. }
  94. }
  95. // Set up labels when the old piechart is using 'on graph', for the legend option.
  96. if (angular.legendType === 'On graph') {
  97. options.legend.displayMode = LegendDisplayMode.Hidden;
  98. options.displayLabels = [PieChartLabels.Name];
  99. if (angular.legend.values) {
  100. options.displayLabels.push(PieChartLabels.Value);
  101. }
  102. if (angular.legend.percentage) {
  103. options.displayLabels.push(PieChartLabels.Percent);
  104. }
  105. }
  106. return options;
  107. }
  108. return {};
  109. };