-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(partition): monotonic font size scaling #681
Merged
monfera
merged 15 commits into
elastic:master
from
monfera:monotonic-text-size-squashed
May 27, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
941db80
feat: globally monotonous fill text sizes
monfera b7697b7
refactor: text size calc improvements
monfera d453fa0
chore: extracted API
monfera dde2d84
chore: name simplification
monfera d217024
feat: activate monotonicity
monfera 0629ff2
chore: comment
monfera 2537397
chore: monotonicHillClimb doc
monfera 9372a7e
fix: remove useless generic arg
monfera 853f1fa
chore: added return type
monfera 5e9a3a0
feat: monotonic to maximizeFontSize
monfera e166746
test: partition mocks flipping to monotonic
monfera 3d940f1
chore: charts api extraction
monfera 4c28328
Merge remote-tracking branch 'origin/master' into monotonic-text-size…
monfera b5805e4
test: master merge image update update
monfera 0759cdd
fix: api changes
nickofthyme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Binary file modified
BIN
-8.86 KB
(92%)
...-visual-tests-for-all-stories-legend-piechart-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+571 Bytes
(100%)
...ests-for-all-stories-sunburst-bold-link-value-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-90 Bytes
(100%)
...for-all-stories-sunburst-clockwise-no-special-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+219 Bytes
(100%)
...ll-stories-sunburst-counter-clockwise-special-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.16 KB
(98%)
...stories-sunburst-donut-chart-with-fill-labels-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-4.93 KB
(93%)
...-tests-for-all-stories-sunburst-heterogeneous-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+454 Bytes
(100%)
...s-for-all-stories-sunburst-linked-labels-only-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+681 Bytes
(100%)
...ual-tests-for-all-stories-sunburst-most-basic-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-12 KB
(87%)
...ual-tests-for-all-stories-sunburst-percentage-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-3.61 KB
(96%)
...all-stories-sunburst-sunburst-with-two-layers-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+229 Bytes
(100%)
...ests-for-all-stories-sunburst-value-formatted-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+871 Bytes
(100%)
...alue-formatted-with-categorical-color-palette-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-763 Bytes
(97%)
...-all-stories-sunburst-with-direct-text-labels-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.58 KB
(98%)
...sts-for-all-stories-sunburst-with-fill-labels-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-6.24 KB
(84%)
...al-tests-for-all-stories-treemap-custom-style-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-13.2 KB
(85%)
...ual-tests-for-all-stories-treemap-groove-text-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-8.5 KB
(90%)
...-tests-for-all-stories-treemap-mid-two-layers-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-17.1 KB
(79%)
...ual-tests-for-all-stories-treemap-multi-color-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.78 KB
(98%)
...ual-tests-for-all-stories-treemap-one-layer-2-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-2.03 KB
(98%)
...isual-tests-for-all-stories-treemap-one-layer-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-15.5 KB
(81%)
...sual-tests-for-all-stories-treemap-percentage-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+5.37 KB
(100%)
...ual-tests-for-all-stories-treemap-three-layer-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-31.9 KB
(72%)
...or-all-stories-treemap-two-layers-stress-test-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-4.43 KB
(95%)
...ual-tests-for-all-stories-treemap-zero-values-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
127 changes: 127 additions & 0 deletions
127
src/chart_types/partition_chart/layout/utils/calcs.test.ts
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,127 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. */ | ||
|
||
import { integerSnap, monotonicHillClimb } from './calcs'; | ||
|
||
describe('monotonicHillClimb', () => { | ||
const arbitraryNumber = 27; | ||
|
||
describe('continuous functions', () => { | ||
test('linear case', () => { | ||
expect(monotonicHillClimb((n: number) => n, 100, arbitraryNumber)).toBeCloseTo(arbitraryNumber, 6); | ||
monfera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
test('flat case should yield `maxVar`', () => { | ||
expect(monotonicHillClimb(() => arbitraryNumber, 100, 50)).toBeCloseTo(100, 6); | ||
}); | ||
|
||
test('nonlinear case', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n), Math.PI / 2, Math.sqrt(2) / 2)).toBeCloseTo(Math.PI / 4, 6); | ||
}); | ||
|
||
test('non-compliant for even `minVar` should yield NaN', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n), Math.PI / 2, -1)).toBeNaN(); | ||
}); | ||
|
||
test('`loVar > hiVar` should yield NaN', () => { | ||
expect( | ||
monotonicHillClimb( | ||
(n: number) => Math.sin(n), | ||
1, | ||
arbitraryNumber, | ||
(n: number) => n, | ||
2, | ||
), | ||
).toBeNaN(); | ||
}); | ||
|
||
test('compliant for `maxVar` should yield `maxVar`', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n), Math.PI / 2, 1)).toBeCloseTo(Math.PI / 2, 6); | ||
}); | ||
|
||
test('`loVar === hiVar`, compliant', () => { | ||
expect( | ||
monotonicHillClimb( | ||
(n: number) => Math.sin(n), | ||
Math.PI / 2, | ||
1, | ||
(n: number) => n, | ||
Math.PI / 2, | ||
), | ||
).toBe(Math.PI / 2); | ||
}); | ||
|
||
test('`loVar === hiVar`, non-compliant', () => { | ||
expect( | ||
monotonicHillClimb( | ||
(n: number) => Math.sin(n), | ||
Math.PI / 2, | ||
Math.sqrt(2) / 2, | ||
(n: number) => n, | ||
Math.PI / 2, | ||
), | ||
).toBeNaN(); | ||
}); | ||
}); | ||
|
||
describe('integral domain functions', () => { | ||
test('linear case', () => { | ||
expect(monotonicHillClimb((n: number) => n, 100, arbitraryNumber, integerSnap)).toBe(arbitraryNumber); | ||
}); | ||
|
||
test('flat case should yield `maxVar`', () => { | ||
expect(monotonicHillClimb(() => arbitraryNumber, 100, 50)).toBe(100); | ||
}); | ||
|
||
test('nonlinear case', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, Math.sqrt(2) / 2, integerSnap)).toBe(7); | ||
}); | ||
|
||
test('non-compliant for even `minVar` should yield NaN', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n), Math.PI / 2, -1, integerSnap)).toBeNaN(); | ||
}); | ||
|
||
test('`loVar > hiVar` should yield NaN', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n), 1, arbitraryNumber, integerSnap, 2)).toBeNaN(); | ||
}); | ||
|
||
test('compliant for `maxVar` should yield `maxVar`', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, 1, integerSnap)).toBe(15); | ||
}); | ||
|
||
test('`loVar === hiVar`, compliant', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, 1, integerSnap, 15)).toBe(15); | ||
}); | ||
|
||
test('`loVar === hiVar`, non-compliant', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, Math.sqrt(2) / 2, integerSnap, 15)).toBeNaN(); | ||
}); | ||
|
||
test('`loVar + 1 === hiVar`, latter is compliant', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, 1, integerSnap, 14)).toBe(15); | ||
}); | ||
|
||
test('`loVar + 1 === hiVar`, only former is compliant', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, 0.99, integerSnap, 14)).toBe(14); | ||
}); | ||
|
||
test('`loVar + 1 === hiVar`, non-compliant', () => { | ||
expect(monotonicHillClimb((n: number) => Math.sin(n / 10), 15, Math.sqrt(2) / 2, integerSnap, 14)).toBeNaN(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -56,3 +56,78 @@ export function colorIsDark(color: Color) { | |
const a = rgba.hasOwnProperty('opacity') ? opacity : 1; | ||
return r * 0.299 + g * 0.587 + b * 0.114 < a * 150; | ||
} | ||
|
||
/** @internal */ | ||
export function getTextColor(shapeFillColor: Color, textColor: Color, textInvertible: boolean) { | ||
const { r: tr, g: tg, b: tb, opacity: to } = stringToRGB(textColor); | ||
const backgroundIsDark = colorIsDark(shapeFillColor); | ||
const specifiedTextColorIsDark = colorIsDark(textColor); | ||
const inverseForContrast = textInvertible && specifiedTextColorIsDark === backgroundIsDark; | ||
return inverseForContrast | ||
? to === undefined | ||
? `rgb(${255 - tr}, ${255 - tg}, ${255 - tb})` | ||
: `rgba(${255 - tr}, ${255 - tg}, ${255 - tb}, ${to})` | ||
: textColor; | ||
} | ||
|
||
/** @internal */ | ||
export function integerSnap(n: number) { | ||
return Math.floor(n); | ||
} | ||
|
||
type NumberMap = (n: number) => number; | ||
|
||
/** | ||
* `monotonicHillClimb` attempts to return a variable value that's associated with the highest valued response (as returned by invoking `getResponse` | ||
* with said variable) yet still within the bounds for that response value, ie. constrained to smaller than or equal `responseUpperConstraint`. | ||
* `minVar` and `maxVar` represent a closed interval constraint on the variable itself. | ||
* `domainSnap` is useful if all real values in the range can't be assumed by the variable; typically, if the variable is integer only, | ||
* such as the number of characters, or avoiding fractional font sizes. | ||
* It is required that `getResponse` is a monotonic function over [minVar, maxVar], ie. a larger `n` value in this domain can't lead to | ||
* a smaller return value. However, as it's an internal function with known use cases, there's no runtime check to assert this. | ||
* Which is why the name expresses it prominently. | ||
*/ | ||
/** @internal */ | ||
export function monotonicHillClimb( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even if this method is internal, a quick description of this method in a tsdoc is useful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
getResponse: NumberMap, | ||
maxVar: number, | ||
responseUpperConstraint: number, | ||
domainSnap: NumberMap = (n: number) => n, | ||
minVar: number = 0, | ||
) { | ||
let loVar = domainSnap(minVar); | ||
const loResponse = getResponse(loVar); | ||
let hiVar = domainSnap(maxVar); | ||
let hiResponse = getResponse(hiVar); | ||
|
||
if (loResponse > responseUpperConstraint || loVar > hiVar) { | ||
// bail if even the lowest value doesn't satisfy the constraint | ||
return NaN; | ||
} | ||
|
||
if (hiResponse <= responseUpperConstraint) { | ||
return hiVar; // early bail if maxVar is compliant | ||
} | ||
|
||
let pivotVar: number = NaN; | ||
let pivotResponse: number = NaN; | ||
let lastPivotResponse: number = NaN; | ||
while (loVar < hiVar) { | ||
const newPivotVar = (loVar + hiVar) / 2; | ||
const newPivotResponse = getResponse(domainSnap(newPivotVar)); | ||
if (newPivotResponse === pivotResponse || newPivotResponse === lastPivotResponse) { | ||
return domainSnap(loVar); // bail if we're good and not making further progress | ||
} | ||
pivotVar = newPivotVar; | ||
lastPivotResponse = pivotResponse; // for prevention of bistable oscillation around discretization snap | ||
pivotResponse = newPivotResponse; | ||
const pivotIsCompliant = pivotResponse <= responseUpperConstraint; | ||
if (pivotIsCompliant) { | ||
loVar = pivotVar; | ||
} else { | ||
hiVar = pivotVar; | ||
hiResponse = pivotResponse; | ||
} | ||
} | ||
return domainSnap(pivotVar); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion, but to me seems that a
monotonic
font scale avoids misunderstanding when reading the chart. I prefer having iton
by default. The consumer then can opt to turn it off to maximize the font size depending on space. As you already highlighted in the PR description it's a tradeoff between readability and understandability.@rshen91 @nickofthyme what do you think?
We should not be afraid of turning it on and mark the PR as breaking change, but we should prefer giving the user a good default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad to switch, I was on the fence about it and the config is there either way. A single long word or narrow box can turn subsequent boxes less legible, if at all (see PR desc.) ie. assumes more about the data, iow more vulnerable input-wise. But, monotonically shrinking text is preattentively preferable 😄 The breaking change tipped the balance for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...maybe it's not even a breaking change? We don't have an API contract that specifies text maximization. We should retain freedom for changing data and text ink that honors API contracts. Worth a discussion, a strict interpretation would require a breaking change for any pixel level visual change, while a too loose interpretation would throw curveballs to dependency maintainers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree, should default to
true
. I don't know if making it a breaking change is necessary though, It's not that noticeable of a change in most cases and it always looks better, IMHO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong feelings, but I think it's great we're making this available. I'm leaning towards the stance stated above, it's probably best to set it on by default and just have the option for consumers to set it off if readability suffers. I don't think it's a breaking change and it's nice that the consumer can always set it to false if they liked the font sizing beforehand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the common thinking, done in e166746