forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'grafana/master' into annotations-created
* grafana/master: (30 commits) changelog: adds note about closing grafana#11278 docs: spelling docs: add intro paragraph to provisioning page Cleanup CircleCI V2 Conversion changelog: notes for grafana#1271 and grafana#2740 graph: minor fixes to y-axes alignment feature added save icon to save buttons removed trash can icon from save buttons Return actual user ID in UserProfileDTO dashboard version cleanup: more tests and refactor minor refactor of dashboard version cleanup refactor: dashboard version cleanup limit number of rows deleted by dashboard version cleanup fix dashboard version cleanup on large datasets Allocated to a separate alignment block. Replaced the attribute of the second axis by the attribute of the axes. Fixed unit test. Changed the way this feature was activated. And changed tolltip. Added validation of input parameters. Resolved conflict Corrected work for graphs created before this feature. ...
- Loading branch information
Showing
14 changed files
with
481 additions
and
63 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import _ from 'lodash'; | ||
|
||
/** | ||
* To align two Y axes by Y level | ||
* @param yAxes data [{min: min_y1, min: max_y1}, {min: min_y2, max: max_y2}] | ||
* @param level Y level | ||
*/ | ||
export function alignYLevel(yAxes, level) { | ||
if (isNaN(level) || !checkCorrectAxis(yAxes)) { | ||
return; | ||
} | ||
|
||
var [yLeft, yRight] = yAxes; | ||
moveLevelToZero(yLeft, yRight, level); | ||
|
||
expandStuckValues(yLeft, yRight); | ||
|
||
// one of graphs on zero | ||
var zero = yLeft.min === 0 || yRight.min === 0 || yLeft.max === 0 || yRight.max === 0; | ||
|
||
var oneSide = checkOneSide(yLeft, yRight); | ||
|
||
if (zero && oneSide) { | ||
yLeft.min = yLeft.max > 0 ? 0 : yLeft.min; | ||
yLeft.max = yLeft.max > 0 ? yLeft.max : 0; | ||
yRight.min = yRight.max > 0 ? 0 : yRight.min; | ||
yRight.max = yRight.max > 0 ? yRight.max : 0; | ||
} else { | ||
if (checkOppositeSides(yLeft, yRight)) { | ||
if (yLeft.min >= 0) { | ||
yLeft.min = -yLeft.max; | ||
yRight.max = -yRight.min; | ||
} else { | ||
yLeft.max = -yLeft.min; | ||
yRight.min = -yRight.max; | ||
} | ||
} else { | ||
var rate = getRate(yLeft, yRight); | ||
|
||
if (oneSide) { | ||
// all graphs above the Y level | ||
if (yLeft.min > 0) { | ||
yLeft.min = yLeft.max / rate; | ||
yRight.min = yRight.max / rate; | ||
} else { | ||
yLeft.max = yLeft.min / rate; | ||
yRight.max = yRight.min / rate; | ||
} | ||
} else { | ||
if (checkTwoCross(yLeft, yRight)) { | ||
yLeft.min = yRight.min ? yRight.min * rate : yLeft.min; | ||
yRight.min = yLeft.min ? yLeft.min / rate : yRight.min; | ||
yLeft.max = yRight.max ? yRight.max * rate : yLeft.max; | ||
yRight.max = yLeft.max ? yLeft.max / rate : yRight.max; | ||
} else { | ||
yLeft.min = yLeft.min > 0 ? yRight.min * rate : yLeft.min; | ||
yRight.min = yRight.min > 0 ? yLeft.min / rate : yRight.min; | ||
yLeft.max = yLeft.max < 0 ? yRight.max * rate : yLeft.max; | ||
yRight.max = yRight.max < 0 ? yLeft.max / rate : yRight.max; | ||
} | ||
} | ||
} | ||
} | ||
|
||
restoreLevelFromZero(yLeft, yRight, level); | ||
} | ||
|
||
function expandStuckValues(yLeft, yRight) { | ||
// wide Y min and max using increased wideFactor | ||
var wideFactor = 0.25; | ||
if (yLeft.max === yLeft.min) { | ||
yLeft.min -= wideFactor; | ||
yLeft.max += wideFactor; | ||
} | ||
if (yRight.max === yRight.min) { | ||
yRight.min -= wideFactor; | ||
yRight.max += wideFactor; | ||
} | ||
} | ||
|
||
function moveLevelToZero(yLeft, yRight, level) { | ||
if (level !== 0) { | ||
yLeft.min -= level; | ||
yLeft.max -= level; | ||
yRight.min -= level; | ||
yRight.max -= level; | ||
} | ||
} | ||
|
||
function restoreLevelFromZero(yLeft, yRight, level) { | ||
if (level !== 0) { | ||
yLeft.min += level; | ||
yLeft.max += level; | ||
yRight.min += level; | ||
yRight.max += level; | ||
} | ||
} | ||
|
||
function checkCorrectAxis(axis) { | ||
return axis.length === 2 && checkCorrectAxes(axis[0]) && checkCorrectAxes(axis[1]); | ||
} | ||
|
||
function checkCorrectAxes(axes) { | ||
return 'min' in axes && 'max' in axes; | ||
} | ||
|
||
function checkOneSide(yLeft, yRight) { | ||
// on the one hand with respect to zero | ||
return (yLeft.min >= 0 && yRight.min >= 0) || (yLeft.max <= 0 && yRight.max <= 0); | ||
} | ||
|
||
function checkTwoCross(yLeft, yRight) { | ||
// both across zero | ||
return yLeft.min <= 0 && yLeft.max >= 0 && yRight.min <= 0 && yRight.max >= 0; | ||
} | ||
|
||
function checkOppositeSides(yLeft, yRight) { | ||
// on the opposite sides with respect to zero | ||
return (yLeft.min >= 0 && yRight.max <= 0) || (yLeft.max <= 0 && yRight.min >= 0); | ||
} | ||
|
||
function getRate(yLeft, yRight) { | ||
var rateLeft, rateRight, rate; | ||
if (checkTwoCross(yLeft, yRight)) { | ||
rateLeft = yRight.min ? yLeft.min / yRight.min : 0; | ||
rateRight = yRight.max ? yLeft.max / yRight.max : 0; | ||
} else { | ||
if (checkOneSide(yLeft, yRight)) { | ||
var absLeftMin = Math.abs(yLeft.min); | ||
var absLeftMax = Math.abs(yLeft.max); | ||
var absRightMin = Math.abs(yRight.min); | ||
var absRightMax = Math.abs(yRight.max); | ||
var upLeft = _.max([absLeftMin, absLeftMax]); | ||
var downLeft = _.min([absLeftMin, absLeftMax]); | ||
var upRight = _.max([absRightMin, absRightMax]); | ||
var downRight = _.min([absRightMin, absRightMax]); | ||
|
||
rateLeft = downLeft ? upLeft / downLeft : upLeft; | ||
rateRight = downRight ? upRight / downRight : upRight; | ||
} else { | ||
if (yLeft.min > 0 || yRight.min > 0) { | ||
rateLeft = yLeft.max / yRight.max; | ||
rateRight = 0; | ||
} else { | ||
rateLeft = 0; | ||
rateRight = yLeft.min / yRight.min; | ||
} | ||
} | ||
} | ||
|
||
rate = rateLeft > rateRight ? rateLeft : rateRight; | ||
|
||
return rate; | ||
} |
Oops, something went wrong.