utils.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. import { orderBy } from 'lodash';
  2. import { Padding } from 'uplot';
  3. import {
  4. ArrayVector,
  5. DataFrame,
  6. Field,
  7. FieldType,
  8. formattedValueToString,
  9. getDisplayProcessor,
  10. getFieldColorModeForField,
  11. getFieldSeriesColor,
  12. GrafanaTheme2,
  13. outerJoinDataFrames,
  14. reduceField,
  15. VizOrientation,
  16. } from '@grafana/data';
  17. import { maybeSortFrame } from '@grafana/data/src/transformations/transformers/joinDataFrames';
  18. import {
  19. AxisPlacement,
  20. ScaleDirection,
  21. ScaleDistribution,
  22. ScaleOrientation,
  23. StackingMode,
  24. VizLegendOptions,
  25. } from '@grafana/schema';
  26. import { FIXED_UNIT, measureText, UPlotConfigBuilder, UPlotConfigPrepFn, UPLOT_AXIS_FONT_SIZE } from '@grafana/ui';
  27. import { getStackingGroups } from '@grafana/ui/src/components/uPlot/utils';
  28. import { findField } from 'app/features/dimensions';
  29. import { BarsOptions, getConfig } from './bars';
  30. import { BarChartFieldConfig, PanelOptions, defaultBarChartFieldConfig } from './models.gen';
  31. import { BarChartDisplayValues, BarChartDisplayWarning } from './types';
  32. function getBarCharScaleOrientation(orientation: VizOrientation) {
  33. if (orientation === VizOrientation.Vertical) {
  34. return {
  35. xOri: ScaleOrientation.Horizontal,
  36. xDir: ScaleDirection.Right,
  37. yOri: ScaleOrientation.Vertical,
  38. yDir: ScaleDirection.Up,
  39. };
  40. }
  41. return {
  42. xOri: ScaleOrientation.Vertical,
  43. xDir: ScaleDirection.Down,
  44. yOri: ScaleOrientation.Horizontal,
  45. yDir: ScaleDirection.Right,
  46. };
  47. }
  48. export interface BarChartOptionsEX extends PanelOptions {
  49. rawValue: (seriesIdx: number, valueIdx: number) => number | null;
  50. getColor?: (seriesIdx: number, valueIdx: number, value: any) => string | null;
  51. fillOpacity?: number;
  52. }
  53. export const preparePlotConfigBuilder: UPlotConfigPrepFn<BarChartOptionsEX> = ({
  54. frame,
  55. theme,
  56. orientation,
  57. showValue,
  58. groupWidth,
  59. barWidth,
  60. barRadius = 0,
  61. stacking,
  62. text,
  63. rawValue,
  64. getColor,
  65. fillOpacity,
  66. allFrames,
  67. xTickLabelRotation,
  68. xTickLabelMaxLength,
  69. xTickLabelSpacing = 0,
  70. legend,
  71. }) => {
  72. const builder = new UPlotConfigBuilder();
  73. const defaultValueFormatter = (seriesIdx: number, value: any) => {
  74. return shortenValue(formattedValueToString(frame.fields[seriesIdx].display!(value)), xTickLabelMaxLength);
  75. };
  76. // bar orientation -> x scale orientation & direction
  77. const vizOrientation = getBarCharScaleOrientation(orientation);
  78. const formatValue = defaultValueFormatter;
  79. // Use bar width when only one field
  80. if (frame.fields.length === 2) {
  81. groupWidth = barWidth;
  82. barWidth = 1;
  83. }
  84. const opts: BarsOptions = {
  85. xOri: vizOrientation.xOri,
  86. xDir: vizOrientation.xDir,
  87. groupWidth,
  88. barWidth,
  89. barRadius,
  90. stacking,
  91. rawValue,
  92. getColor,
  93. fillOpacity,
  94. formatValue,
  95. text,
  96. showValue,
  97. legend,
  98. xSpacing: xTickLabelSpacing,
  99. xTimeAuto: frame.fields[0]?.type === FieldType.time && !frame.fields[0].config.unit?.startsWith('time:'),
  100. };
  101. const config = getConfig(opts, theme);
  102. builder.setCursor(config.cursor);
  103. builder.addHook('init', config.init);
  104. builder.addHook('drawClear', config.drawClear);
  105. builder.addHook('draw', config.draw);
  106. builder.setTooltipInterpolator(config.interpolateTooltip);
  107. if (vizOrientation.xOri === ScaleOrientation.Horizontal && xTickLabelRotation !== 0) {
  108. builder.setPadding(getRotationPadding(frame, xTickLabelRotation, xTickLabelMaxLength));
  109. }
  110. builder.setPrepData(config.prepData);
  111. builder.addScale({
  112. scaleKey: 'x',
  113. isTime: false,
  114. range: config.xRange,
  115. distribution: ScaleDistribution.Ordinal,
  116. orientation: vizOrientation.xOri,
  117. direction: vizOrientation.xDir,
  118. });
  119. const xFieldAxisPlacement =
  120. frame.fields[0].config.custom?.axisPlacement !== AxisPlacement.Hidden
  121. ? vizOrientation.xOri === ScaleOrientation.Horizontal
  122. ? AxisPlacement.Bottom
  123. : AxisPlacement.Left
  124. : AxisPlacement.Hidden;
  125. const xFieldAxisShow = frame.fields[0].config.custom?.axisPlacement !== AxisPlacement.Hidden;
  126. builder.addAxis({
  127. scaleKey: 'x',
  128. isTime: false,
  129. placement: xFieldAxisPlacement,
  130. label: frame.fields[0].config.custom?.axisLabel,
  131. splits: config.xSplits,
  132. values: config.xValues,
  133. grid: { show: false },
  134. ticks: { show: false },
  135. gap: 15,
  136. tickLabelRotation: xTickLabelRotation * -1,
  137. theme,
  138. show: xFieldAxisShow,
  139. });
  140. let seriesIndex = 0;
  141. const legendOrdered = isLegendOrdered(legend);
  142. // iterate the y values
  143. for (let i = 1; i < frame.fields.length; i++) {
  144. const field = frame.fields[i];
  145. seriesIndex++;
  146. const customConfig: BarChartFieldConfig = { ...defaultBarChartFieldConfig, ...field.config.custom };
  147. const scaleKey = field.config.unit || FIXED_UNIT;
  148. const colorMode = getFieldColorModeForField(field);
  149. const scaleColor = getFieldSeriesColor(field, theme);
  150. const seriesColor = scaleColor.color;
  151. // make barcharts start at 0 unless explicitly overridden
  152. let softMin = customConfig.axisSoftMin;
  153. let softMax = customConfig.axisSoftMax;
  154. if (softMin == null && field.config.min == null) {
  155. softMin = 0;
  156. }
  157. if (softMax == null && field.config.max == null) {
  158. softMax = 0;
  159. }
  160. builder.addSeries({
  161. scaleKey,
  162. pxAlign: true,
  163. lineWidth: customConfig.lineWidth,
  164. lineColor: seriesColor,
  165. fillOpacity: customConfig.fillOpacity,
  166. theme,
  167. colorMode,
  168. pathBuilder: config.barsBuilder,
  169. show: !customConfig.hideFrom?.viz,
  170. gradientMode: customConfig.gradientMode,
  171. thresholds: field.config.thresholds,
  172. hardMin: field.config.min,
  173. hardMax: field.config.max,
  174. softMin,
  175. softMax,
  176. // The following properties are not used in the uPlot config, but are utilized as transport for legend config
  177. // PlotLegend currently gets unfiltered DataFrame[], so index must be into that field array, not the prepped frame's which we're iterating here
  178. dataFrameFieldIndex: {
  179. fieldIndex: legendOrdered
  180. ? i
  181. : allFrames[0].fields.findIndex(
  182. (f) => f.type === FieldType.number && f.state?.seriesIndex === seriesIndex - 1
  183. ),
  184. frameIndex: 0,
  185. },
  186. });
  187. // The builder will manage unique scaleKeys and combine where appropriate
  188. builder.addScale({
  189. scaleKey,
  190. min: field.config.min,
  191. max: field.config.max,
  192. softMin,
  193. softMax,
  194. orientation: vizOrientation.yOri,
  195. direction: vizOrientation.yDir,
  196. distribution: customConfig.scaleDistribution?.type,
  197. log: customConfig.scaleDistribution?.log,
  198. });
  199. if (customConfig.axisPlacement !== AxisPlacement.Hidden) {
  200. let placement = customConfig.axisPlacement;
  201. if (!placement || placement === AxisPlacement.Auto) {
  202. placement = AxisPlacement.Left;
  203. }
  204. if (vizOrientation.xOri === 1) {
  205. if (placement === AxisPlacement.Left) {
  206. placement = AxisPlacement.Bottom;
  207. }
  208. if (placement === AxisPlacement.Right) {
  209. placement = AxisPlacement.Top;
  210. }
  211. }
  212. builder.addAxis({
  213. scaleKey,
  214. label: customConfig.axisLabel,
  215. size: customConfig.axisWidth,
  216. placement,
  217. formatValue: (v) => formattedValueToString(field.display!(v)),
  218. theme,
  219. grid: { show: customConfig.axisGridShow },
  220. });
  221. }
  222. }
  223. let stackingGroups = getStackingGroups(frame);
  224. builder.setStackingGroups(stackingGroups);
  225. return builder;
  226. };
  227. function shortenValue(value: string, length: number) {
  228. if (value.length > length) {
  229. return value.substring(0, length).concat('...');
  230. } else {
  231. return value;
  232. }
  233. }
  234. function getRotationPadding(frame: DataFrame, rotateLabel: number, valueMaxLength: number): Padding {
  235. const values = frame.fields[0].values;
  236. const fontSize = UPLOT_AXIS_FONT_SIZE;
  237. const displayProcessor = frame.fields[0].display ?? ((v) => v);
  238. let maxLength = 0;
  239. for (let i = 0; i < values.length; i++) {
  240. let size = measureText(
  241. shortenValue(formattedValueToString(displayProcessor(values.get(i))), valueMaxLength),
  242. fontSize
  243. );
  244. maxLength = size.width > maxLength ? size.width : maxLength;
  245. }
  246. // Add padding to the right if the labels are rotated in a way that makes the last label extend outside the graph.
  247. const paddingRight =
  248. rotateLabel > 0
  249. ? Math.cos((rotateLabel * Math.PI) / 180) *
  250. measureText(
  251. shortenValue(formattedValueToString(displayProcessor(values.get(values.length - 1))), valueMaxLength),
  252. fontSize
  253. ).width
  254. : 0;
  255. // Add padding to the left if the labels are rotated in a way that makes the first label extend outside the graph.
  256. const paddingLeft =
  257. rotateLabel < 0
  258. ? Math.cos((rotateLabel * -1 * Math.PI) / 180) *
  259. measureText(shortenValue(formattedValueToString(displayProcessor(values.get(0))), valueMaxLength), fontSize)
  260. .width
  261. : 0;
  262. // Add padding to the bottom to avoid clipping the rotated labels.
  263. const paddingBottom = Math.sin(((rotateLabel >= 0 ? rotateLabel : rotateLabel * -1) * Math.PI) / 180) * maxLength;
  264. return [0, paddingRight, paddingBottom, paddingLeft];
  265. }
  266. /** @internal */
  267. export function prepareBarChartDisplayValues(
  268. series: DataFrame[],
  269. theme: GrafanaTheme2,
  270. options: PanelOptions
  271. ): BarChartDisplayValues | BarChartDisplayWarning {
  272. if (!series?.length) {
  273. return { warn: 'No data in response' };
  274. }
  275. // Bar chart requires a single frame
  276. const frame =
  277. series.length === 1
  278. ? maybeSortFrame(
  279. series[0],
  280. series[0].fields.findIndex((f) => f.type === FieldType.time)
  281. )
  282. : outerJoinDataFrames({ frames: series });
  283. if (!frame) {
  284. return { warn: 'Unable to join data' };
  285. }
  286. // Color by a field different than the input
  287. let colorByField: Field | undefined = undefined;
  288. if (options.colorByField) {
  289. colorByField = findField(frame, options.colorByField);
  290. if (!colorByField) {
  291. return { warn: 'Color field not found' };
  292. }
  293. }
  294. let xField: Field | undefined = undefined;
  295. if (options.xField) {
  296. xField = findField(frame, options.xField);
  297. if (!xField) {
  298. return { warn: 'Configured x field not found' };
  299. }
  300. }
  301. let stringField: Field | undefined = undefined;
  302. let timeField: Field | undefined = undefined;
  303. let fields: Field[] = [];
  304. for (const field of frame.fields) {
  305. if (field === xField) {
  306. continue;
  307. }
  308. switch (field.type) {
  309. case FieldType.string:
  310. if (!stringField) {
  311. stringField = field;
  312. }
  313. break;
  314. case FieldType.time:
  315. if (!timeField) {
  316. timeField = field;
  317. }
  318. break;
  319. case FieldType.number: {
  320. const copy = {
  321. ...field,
  322. state: {
  323. ...field.state,
  324. seriesIndex: fields.length, // off by one?
  325. },
  326. config: {
  327. ...field.config,
  328. custom: {
  329. ...field.config.custom,
  330. stacking: {
  331. group: '_',
  332. mode: options.stacking,
  333. },
  334. },
  335. },
  336. values: new ArrayVector(
  337. field.values.toArray().map((v) => {
  338. if (!(Number.isFinite(v) || v == null)) {
  339. return null;
  340. }
  341. return v;
  342. })
  343. ),
  344. };
  345. if (options.stacking === StackingMode.Percent) {
  346. copy.config.unit = 'percentunit';
  347. copy.display = getDisplayProcessor({ field: copy, theme });
  348. }
  349. fields.push(copy);
  350. }
  351. }
  352. }
  353. let firstField = xField;
  354. if (!firstField) {
  355. firstField = stringField || timeField;
  356. }
  357. if (!firstField) {
  358. return {
  359. warn: 'Bar charts requires a string or time field',
  360. };
  361. }
  362. if (!fields.length) {
  363. return {
  364. warn: 'No numeric fields found',
  365. };
  366. }
  367. // Show the first number value
  368. if (colorByField && fields.length > 1) {
  369. const firstNumber = fields.find((f) => f !== colorByField);
  370. if (firstNumber) {
  371. fields = [firstNumber];
  372. }
  373. }
  374. if (isLegendOrdered(options.legend)) {
  375. const sortKey = options.legend.sortBy!.toLowerCase();
  376. const reducers = options.legend.calcs ?? [sortKey];
  377. fields = orderBy(
  378. fields,
  379. (field) => {
  380. return reduceField({ field, reducers })[sortKey];
  381. },
  382. options.legend.sortDesc ? 'desc' : 'asc'
  383. );
  384. }
  385. // String field is first
  386. fields.unshift(firstField);
  387. return {
  388. aligned: frame,
  389. colorByField,
  390. viz: [
  391. {
  392. length: firstField.values.length,
  393. fields: fields, // ideally: fields.filter((f) => !Boolean(f.config.custom?.hideFrom?.viz)),
  394. },
  395. ],
  396. };
  397. }
  398. export const isLegendOrdered = (options: VizLegendOptions) => Boolean(options?.sortBy && options.sortDesc !== null);