diff --git a/packages/grafana-ui/src/components/uPlot/utils.test.ts b/packages/grafana-ui/src/components/uPlot/utils.test.ts index 20b3012c259..605c8649e63 100644 --- a/packages/grafana-ui/src/components/uPlot/utils.test.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.test.ts @@ -23,7 +23,7 @@ describe('preparePlotData2', () => { const df = new MutableDataFrame({ fields: [ { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, - { name: 'a', values: [-10, 20, 10] }, + { name: 'a', values: [-10, -20, 10] }, { name: 'b', values: [10, 10, 10] }, { name: 'c', values: [20, 20, 20] }, ], @@ -39,7 +39,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -61,7 +61,7 @@ describe('preparePlotData2', () => { const df = new MutableDataFrame({ fields: [ { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, - { name: 'a', values: [-10, 20, 10] }, + { name: 'a', values: [-10, -20, 10] }, { name: 'b', values: [10, 10, 10] }, { name: 'c', values: [20, 20, 20], config: { custom: { transform: GraphTransform.NegativeY } } }, ], @@ -75,7 +75,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -96,7 +96,7 @@ describe('preparePlotData2', () => { const df = new MutableDataFrame({ fields: [ { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, - { name: 'a', values: [-10, 20, 10, 30] }, + { name: 'a', values: [-10, -20, 10, -30] }, { name: 'b', values: [10, 10, 10, null] }, { name: 'c', values: [null, 20, 20, 20], config: { custom: { transform: GraphTransform.NegativeY } } }, { name: 'd', values: [20, 20, 20, null], config: { custom: { transform: GraphTransform.NegativeY } } }, @@ -117,9 +117,9 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, - 30, + -30, ], [ 10, @@ -176,7 +176,7 @@ describe('preparePlotData2', () => { const df = new MutableDataFrame({ fields: [ { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, - { name: 'a', values: [-10, 20, 10], config: { custom: { transform: GraphTransform.Constant } } }, + { name: 'a', values: [-10, -20, 10], config: { custom: { transform: GraphTransform.Constant } } }, { name: 'b', values: [10, 10, 10] }, { name: 'c', values: [20, 20, 20] }, ], @@ -214,7 +214,7 @@ describe('preparePlotData2', () => { { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, { name: 'a', - values: [-10, 20, 10], + values: [-10, -20, 10], config: { custom: { stacking: { mode: StackingMode.None } } }, }, { @@ -238,7 +238,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -261,7 +261,7 @@ describe('preparePlotData2', () => { { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, { name: 'a', - values: [-10, 20, 10], + values: [-10, -20, 10], config: { custom: { stacking: { mode: StackingMode.Normal, group: 'stackA' } } }, }, { @@ -285,7 +285,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -308,7 +308,7 @@ describe('preparePlotData2', () => { { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, { name: 'a', - values: [-10, 20, 10], + values: [-10, -20, 10], config: { custom: { stacking: { mode: StackingMode.Normal, group: 'stackA' } } }, }, { @@ -341,7 +341,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -351,12 +351,12 @@ describe('preparePlotData2', () => { ], [ -30, - 0, + -40, -10, ], [ -40, - -10, + -50, -20, ], ] @@ -369,7 +369,7 @@ describe('preparePlotData2', () => { { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, { name: 'a', - values: [-10, 20, 10], + values: [-10, -20, 10], config: { custom: { stacking: { mode: StackingMode.Normal, group: 'stackA' } } }, }, { @@ -409,7 +409,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -447,7 +447,7 @@ describe('preparePlotData2', () => { { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, { name: 'a', - values: [-10, 20, 10], + values: [-10, -20, 10], config: { custom: { stacking: { mode: StackingMode.Normal, group: 'stackA' }, hideFrom: { viz: true } } }, }, { @@ -484,7 +484,7 @@ describe('preparePlotData2', () => { ], [ -10, - 20, + -20, 10, ], [ @@ -1108,7 +1108,7 @@ describe('auto stacking groups', () => { }, { name: 'd', - values: [-0, null, 3], + values: [-0, null, -3], config: { custom: { stacking: { mode: StackingMode.Normal }, transform: GraphTransform.NegativeY } }, }, ], diff --git a/packages/grafana-ui/src/components/uPlot/utils.ts b/packages/grafana-ui/src/components/uPlot/utils.ts index f8100a1b7bc..22a7b3a41ec 100644 --- a/packages/grafana-ui/src/components/uPlot/utils.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.ts @@ -350,7 +350,7 @@ function getStackDirection(transform: GraphTransform, data: unknown[]) { } // similar to isLikelyAscendingVector() -function hasNegSample(data: unknown[], samples = 50) { +function hasNegSample(data: unknown[], samples = 100) { const len = data.length; if (len === 0) { @@ -369,16 +369,27 @@ function hasNegSample(data: unknown[], samples = 50) { lastIdx--; } + let negCount = 0; + let posCount = 0; + if (lastIdx >= firstIdx) { const stride = Math.max(1, Math.floor((lastIdx - firstIdx + 1) / samples)); for (let i = firstIdx; i <= lastIdx; i += stride) { const v = data[i]; - if (v != null && (v < 0 || Object.is(v, -0))) { - return true; + if (v != null) { + if (v < 0 || Object.is(v, -0)) { + negCount++; + } else if (v > 0) { + posCount++; + } } } + + if (negCount > posCount) { + return true; + } } return false;