heatmap.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { FieldType } from '@grafana/data';
  2. import { toDataFrame } from '@grafana/data/src/dataframe/processDataFrame';
  3. import { rowsToCellsHeatmap, calculateHeatmapFromData } from './heatmap';
  4. import { HeatmapCalculationOptions } from './models.gen';
  5. describe('Heatmap transformer', () => {
  6. it('calculate heatmap from input data', async () => {
  7. const options: HeatmapCalculationOptions = {
  8. //
  9. };
  10. const data = toDataFrame({
  11. fields: [
  12. { name: 'time', type: FieldType.time, values: [1, 2, 3, 4] },
  13. { name: 'temp', type: FieldType.number, config: { unit: 'm2' }, values: [1.1, 2.2, 3.3, 4.4] },
  14. ],
  15. });
  16. const heatmap = calculateHeatmapFromData([data], options);
  17. expect(heatmap.fields.map((f) => ({ name: f.name, type: f.type, config: f.config }))).toMatchInlineSnapshot(`
  18. Array [
  19. Object {
  20. "config": Object {},
  21. "name": "xMin",
  22. "type": "time",
  23. },
  24. Object {
  25. "config": Object {
  26. "custom": Object {
  27. "scaleDistribution": Object {
  28. "type": "linear",
  29. },
  30. },
  31. "unit": "m2",
  32. },
  33. "name": "yMin",
  34. "type": "number",
  35. },
  36. Object {
  37. "config": Object {
  38. "unit": "short",
  39. },
  40. "name": "Count",
  41. "type": "number",
  42. },
  43. ]
  44. `);
  45. });
  46. it('convert heatmap buckets to scanlines', async () => {
  47. const frame = toDataFrame({
  48. fields: [
  49. { name: 'time', type: FieldType.time, values: [1, 2, 3] },
  50. { name: 'A', type: FieldType.number, config: { unit: 'm2' }, values: [1.1, 1.2, 1.3] },
  51. { name: 'B', type: FieldType.number, config: { unit: 'm2' }, values: [2.1, 2.2, 2.3] },
  52. { name: 'C', type: FieldType.number, config: { unit: 'm2' }, values: [3.1, 3.2, 3.3] },
  53. ],
  54. });
  55. const heatmap = rowsToCellsHeatmap({ frame, value: 'Speed' });
  56. expect(heatmap.fields.map((f) => ({ name: f.name, type: f.type, config: f.config }))).toMatchInlineSnapshot(`
  57. Array [
  58. Object {
  59. "config": Object {},
  60. "name": "xMax",
  61. "type": "time",
  62. },
  63. Object {
  64. "config": Object {
  65. "unit": "short",
  66. },
  67. "name": "y",
  68. "type": "number",
  69. },
  70. Object {
  71. "config": Object {
  72. "unit": "m2",
  73. },
  74. "name": "Speed",
  75. "type": "number",
  76. },
  77. ]
  78. `);
  79. expect(heatmap.meta).toMatchInlineSnapshot(`
  80. Object {
  81. "custom": Object {
  82. "yMatchWithLabel": undefined,
  83. "yOrdinalDisplay": Array [
  84. "A",
  85. "B",
  86. "C",
  87. ],
  88. },
  89. "type": "heatmap-cells",
  90. }
  91. `);
  92. expect(heatmap.fields[1].values.toArray()).toMatchInlineSnapshot(`
  93. Array [
  94. 0,
  95. 1,
  96. 2,
  97. 0,
  98. 1,
  99. 2,
  100. 0,
  101. 1,
  102. 2,
  103. ]
  104. `);
  105. });
  106. });