Browse Source

FieldDisplay: Return field defaults when there are no data (#18357)

Torkel Ödegaard 6 years ago
parent
commit
202c136238

+ 26 - 0
packages/grafana-ui/src/utils/fieldDisplay.test.ts

@@ -135,4 +135,30 @@ describe('FieldDisplay', () => {
     expect(field.thresholds!.length).toEqual(2);
     expect(field.thresholds![0].value).toBe(-Infinity);
   });
+
+  it('Should return field thresholds when there is no data', () => {
+    const options: GetFieldDisplayValuesOptions = {
+      data: [
+        {
+          name: 'No data',
+          fields: [],
+          rows: [],
+        },
+      ],
+      replaceVariables: (value: string) => {
+        return value;
+      },
+      fieldOptions: {
+        calcs: [],
+        override: {},
+        defaults: {
+          thresholds: [{ color: '#F2495C', value: 50 }],
+        },
+      },
+      theme: getTheme(GrafanaThemeType.Dark),
+    };
+
+    const display = getFieldDisplayValues(options);
+    expect(display[0].field.thresholds!.length).toEqual(1);
+  });
 });

+ 5 - 1
packages/grafana-ui/src/utils/fieldDisplay.ts

@@ -182,7 +182,10 @@ export const getFieldDisplayValues = (options: GetFieldDisplayValuesOptions): Fi
 
   if (values.length === 0) {
     values.push({
-      field: { name: 'No Data' },
+      field: {
+        ...defaults,
+        name: 'No Data',
+      },
       display: {
         numeric: 0,
         text: 'No data',
@@ -244,6 +247,7 @@ type PartialField = Partial<Field>;
 
 export function getFieldProperties(...props: PartialField[]): Field {
   let field = props[0] as Field;
+
   for (let i = 1; i < props.length; i++) {
     field = applyFieldProperties(field, props[i]);
   }