series_overrides_ctrl.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { map, each, isUndefined } from 'lodash';
  2. import { textUtil } from '@grafana/data';
  3. import coreModule from 'app/angular/core_module';
  4. /** @ngInject */
  5. export function SeriesOverridesCtrl($scope: any, $element: JQuery, popoverSrv: any) {
  6. $scope.overrideMenu = [];
  7. $scope.currentOverrides = [];
  8. $scope.override = $scope.override || {};
  9. $scope.colorPickerModel = {};
  10. $scope.addOverrideOption = (name: string, propertyName: string, values: any) => {
  11. const option = {
  12. text: name,
  13. propertyName: propertyName,
  14. index: $scope.overrideMenu.length,
  15. values,
  16. submenu: map(values, (value) => {
  17. return { text: String(value), value: value };
  18. }),
  19. };
  20. $scope.overrideMenu.push(option);
  21. };
  22. $scope.setOverride = (item: { propertyName: string }, subItem: { value: any }) => {
  23. // handle color overrides
  24. if (item.propertyName === 'color') {
  25. $scope.openColorSelector($scope.override['color']);
  26. return;
  27. }
  28. $scope.override[item.propertyName] = subItem.value;
  29. // automatically disable lines for this series and the fill below to series
  30. // can be removed by the user if they still want lines
  31. if (item.propertyName === 'fillBelowTo') {
  32. $scope.override['lines'] = false;
  33. $scope.ctrl.addSeriesOverride({ alias: subItem.value, lines: false });
  34. }
  35. $scope.updateCurrentOverrides();
  36. $scope.ctrl.render();
  37. };
  38. $scope.colorSelected = (color: any) => {
  39. $scope.override['color'] = color;
  40. $scope.updateCurrentOverrides();
  41. $scope.ctrl.render();
  42. // update picker model so that the picker UI will also update
  43. $scope.colorPickerModel.series.color = color;
  44. };
  45. $scope.openColorSelector = (color: any) => {
  46. $scope.colorPickerModel = {
  47. autoClose: true,
  48. colorSelected: $scope.colorSelected,
  49. series: { color },
  50. };
  51. popoverSrv.show({
  52. element: $element.find('.dropdown')[0],
  53. position: 'top center',
  54. openOn: 'click',
  55. template: '<series-color-picker-popover color="series.color" onColorChange="colorSelected" />',
  56. classNames: 'drop-popover drop-popover--transparent',
  57. model: $scope.colorPickerModel,
  58. onClose: () => {
  59. $scope.ctrl.render();
  60. },
  61. });
  62. };
  63. $scope.removeOverride = (option: { propertyName: string | number }) => {
  64. delete $scope.override[option.propertyName];
  65. $scope.updateCurrentOverrides();
  66. $scope.ctrl.refresh();
  67. };
  68. $scope.getSeriesNames = () => {
  69. return map($scope.ctrl.seriesList, (series) => {
  70. return textUtil.escapeHtml(series.alias);
  71. });
  72. };
  73. $scope.updateCurrentOverrides = () => {
  74. $scope.currentOverrides = [];
  75. each($scope.overrideMenu, (option) => {
  76. const value = $scope.override[option.propertyName];
  77. if (isUndefined(value)) {
  78. return;
  79. }
  80. $scope.currentOverrides.push({
  81. name: option.text,
  82. propertyName: option.propertyName,
  83. value: String(value),
  84. });
  85. });
  86. };
  87. $scope.addOverrideOption('Bars', 'bars', [true, false]);
  88. $scope.addOverrideOption('Lines', 'lines', [true, false]);
  89. $scope.addOverrideOption('Line fill', 'fill', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  90. $scope.addOverrideOption('Fill gradient', 'fillGradient', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  91. $scope.addOverrideOption('Line width', 'linewidth', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  92. $scope.addOverrideOption('Null point mode', 'nullPointMode', ['connected', 'null', 'null as zero']);
  93. $scope.addOverrideOption('Fill below to', 'fillBelowTo', $scope.getSeriesNames());
  94. $scope.addOverrideOption('Staircase line', 'steppedLine', [true, false]);
  95. $scope.addOverrideOption('Dashes', 'dashes', [true, false]);
  96. $scope.addOverrideOption('Hidden Series', 'hiddenSeries', [true, false]);
  97. $scope.addOverrideOption(
  98. 'Dash Length',
  99. 'dashLength',
  100. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  101. );
  102. $scope.addOverrideOption(
  103. 'Dash Space',
  104. 'spaceLength',
  105. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  106. );
  107. $scope.addOverrideOption('Points', 'points', [true, false]);
  108. $scope.addOverrideOption('Points Radius', 'pointradius', [1, 2, 3, 4, 5]);
  109. $scope.addOverrideOption('Stack', 'stack', [true, false, 'A', 'B', 'C', 'D']);
  110. $scope.addOverrideOption('Color', 'color', ['change']);
  111. $scope.addOverrideOption('Y-axis', 'yaxis', [1, 2]);
  112. $scope.addOverrideOption('Z-index', 'zindex', [-3, -2, -1, 0, 1, 2, 3]);
  113. $scope.addOverrideOption('Transform', 'transform', ['constant', 'negative-Y']);
  114. $scope.addOverrideOption('Legend', 'legend', [true, false]);
  115. $scope.addOverrideOption('Hide in tooltip', 'hideTooltip', [true, false]);
  116. $scope.updateCurrentOverrides();
  117. }
  118. coreModule.controller('SeriesOverridesCtrl', SeriesOverridesCtrl);