-
Notifications
You must be signed in to change notification settings - Fork 19.6k
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/box plot labels #17579
base: master
Are you sure you want to change the base?
Feat/box plot labels #17579
Changes from all commits
677c4fd
9df531d
7732ae1
d94f926
70e835b
4fffef0
fb1267f
fa891fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,15 +30,16 @@ import { | |
ColorString, | ||
ZRStyleProps, | ||
AnimationOptionMixin, | ||
InterpolatableValue | ||
InterpolatableValue, | ||
ViewRootGroup | ||
} from '../util/types'; | ||
import GlobalModel from '../model/Global'; | ||
import { isFunction, retrieve2, extend, keys, trim } from 'zrender/src/core/util'; | ||
import { SPECIAL_STATES, DISPLAY_STATES } from '../util/states'; | ||
import { deprecateReplaceLog } from '../util/log'; | ||
import { makeInner, interpolateRawValues } from '../util/model'; | ||
import SeriesData from '../data/SeriesData'; | ||
import { initProps, updateProps } from '../util/graphic'; | ||
import * as graphic from '../util/graphic'; | ||
|
||
type TextCommonParams = { | ||
/** | ||
|
@@ -292,6 +293,66 @@ function setLabelStyle<TLabelDataIndex>( | |
} | ||
export { setLabelStyle }; | ||
|
||
/** | ||
* Set and create specific labels for box plot. | ||
* | ||
* This method should only be used for this case, | ||
* as it manually creates multiple alternating labels | ||
* specifically for the box plot. | ||
*/ | ||
export function setBoxLabels(boxItemPositions: number[][], formattedLabels: Array<string>, | ||
labelOption: any, group: ViewRootGroup) { | ||
// get sorted and unique y positions of box items | ||
const yPositions = boxItemPositions.map((pos: number[]) => pos[1]); | ||
const uniqueYPositions: number[] = []; | ||
yPositions.forEach(position => { | ||
if (!uniqueYPositions.includes(position)) { | ||
uniqueYPositions.push(position); | ||
} | ||
}); | ||
uniqueYPositions.sort(function (a: number, b: number) { | ||
return b - a; | ||
}); | ||
|
||
boxItemPositions.sort(function (a: number[], b: number[]) { | ||
return a[0] - b[0]; | ||
}); | ||
|
||
// get alternating y positions for labels to avoid overlap | ||
const uniqueAlternatingPositions = uniqueYPositions.map((posY: number, ind: number) => { | ||
const matchingPositions = boxItemPositions.filter((orgPos: number[]) => orgPos[1] === posY); | ||
const index = (ind % 2 === 0) ? 0 : matchingPositions.length - 1; | ||
return matchingPositions[index]; | ||
}); | ||
|
||
// create labels and add them to their respective positions | ||
formattedLabels.forEach((labelText: string, ind: number) => { | ||
if (labelOption.show === 'iqr' && (ind === 0 || ind === 4)) { | ||
return; | ||
} | ||
if (labelOption.show === 'median' && (ind !== 2)) { | ||
return; | ||
} | ||
if (labelOption.show === 'whiskers' && (ind !== 0 && ind !== 4)) { | ||
return; | ||
} | ||
const label = new graphic.Text({ | ||
style: { | ||
text: labelText | ||
}, | ||
z2: 1000 | ||
}); | ||
const defaultOffset = 5; | ||
const defaultXOffset = (ind % 2) === 0 ? (-(label.getBoundingRect().width + defaultOffset)) : defaultOffset; | ||
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. As I said before, we should provide a way to let developers decide where to display the label to the left or right. The 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. @Ovilia Sorry, maybe I am missing something here as well but there is no single label. We have five labels, right? I still don't get how someone could choose the align for the five in a way that makes sense - e.g. all labels on one side - even though they might overlap? |
||
const defaultYOffset = -defaultOffset; | ||
const customOffset = labelOption.offset ? labelOption.offset : [0, 0]; | ||
label.x = uniqueAlternatingPositions[ind][0] + defaultXOffset + customOffset[0], | ||
label.y = uniqueAlternatingPositions[ind][1] + defaultYOffset + customOffset[1], | ||
group.add(label); | ||
}); | ||
} | ||
|
||
|
||
export function getLabelStatesModels<LabelName extends string = 'label'>( | ||
itemModel: Model<StatesOptionMixin<any, any> & Partial<Record<LabelName, any>>>, | ||
labelName?: LabelName | ||
|
@@ -726,8 +787,8 @@ export function animateLabelValue( | |
|
||
(textEl as ZRText & {percent?: number}).percent = 0; | ||
(labelInnerStore.prevValue == null | ||
? initProps | ||
: updateProps | ||
? graphic.initProps | ||
: graphic.updateProps | ||
)<TextProps & {percent?: number}>(textEl, { | ||
// percent is used to prevent animation from being aborted #15916 | ||
percent: 1 | ||
|
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.
This is not the correct way to format labels. If
formatter
is not defined, this should go wrong. You should probably do like sunburst.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.
@Ovilia I am not sure if I understood correctly -
getFormattedLabel
is defined to return astring
but somehow returns anobject
when being called withdataIndex
.I think I am missing something here, could you clarify how you would implement the formatting of the labels?
Thanks