提交 bf6c1441 编写于 作者: P pissang

refact(label): add minMargin for layouting. the original margin is renamed to edgeDistance

The new add config is minMargin instead of margin is for not breaking the previous code using margin.
上级 e629132e
...@@ -44,7 +44,7 @@ import List from '../../data/List'; ...@@ -44,7 +44,7 @@ import List from '../../data/List';
interface PieLabelOption extends Omit<LabelOption, 'rotate' | 'position'> { interface PieLabelOption extends Omit<LabelOption, 'rotate' | 'position'> {
rotate?: number rotate?: number
alignTo?: 'none' | 'labelLine' | 'edge' alignTo?: 'none' | 'labelLine' | 'edge'
margin?: string | number edgeDistance?: string | number
bleedMargin?: number bleedMargin?: number
distanceToLabelLine?: number distanceToLabelLine?: number
...@@ -255,7 +255,7 @@ class PieSeriesModel extends SeriesModel<PieSeriesOption> { ...@@ -255,7 +255,7 @@ class PieSeriesModel extends SeriesModel<PieSeriesOption> {
alignTo: 'none', alignTo: 'none',
// Closest distance between label and chart edge. // Closest distance between label and chart edge.
// Works only position is 'outer' and alignTo is 'edge'. // Works only position is 'outer' and alignTo is 'edge'.
margin: '25%', edgeDistance: '25%',
// Works only position is 'outer' and alignTo is not 'edge'. // Works only position is 'outer' and alignTo is not 'edge'.
bleedMargin: 10, bleedMargin: 10,
// Distance between text and label line. // Distance between text and label line.
......
...@@ -42,7 +42,7 @@ interface LabelLayout { ...@@ -42,7 +42,7 @@ interface LabelLayout {
textAlign: HorizontalAlign textAlign: HorizontalAlign
labelDistance: number, labelDistance: number,
labelAlignTo: PieSeriesOption['label']['alignTo'], labelAlignTo: PieSeriesOption['label']['alignTo'],
labelMargin: number, edgeDistance: number,
bleedMargin: PieSeriesOption['label']['bleedMargin'], bleedMargin: PieSeriesOption['label']['bleedMargin'],
rect: BoundingRect rect: BoundingRect
} }
...@@ -178,10 +178,10 @@ function avoidOverlap( ...@@ -178,10 +178,10 @@ function avoidOverlap(
if (isAlignToEdge) { if (isAlignToEdge) {
if (label.x < cx) { if (label.x < cx) {
targetTextWidth = linePoints[2][0] - layout.labelDistance targetTextWidth = linePoints[2][0] - layout.labelDistance
- viewLeft - layout.labelMargin; - viewLeft - layout.edgeDistance;
} }
else { else {
targetTextWidth = viewLeft + viewWidth - layout.labelMargin targetTextWidth = viewLeft + viewWidth - layout.edgeDistance
- linePoints[2][0] - layout.labelDistance; - linePoints[2][0] - layout.labelDistance;
} }
} }
...@@ -206,10 +206,10 @@ function avoidOverlap( ...@@ -206,10 +206,10 @@ function avoidOverlap(
const dist = linePoints[1][0] - linePoints[2][0]; const dist = linePoints[1][0] - linePoints[2][0];
if (isAlignToEdge) { if (isAlignToEdge) {
if (label.x < cx) { if (label.x < cx) {
linePoints[2][0] = viewLeft + layout.labelMargin + realTextWidth + layout.labelDistance; linePoints[2][0] = viewLeft + layout.edgeDistance + realTextWidth + layout.labelDistance;
} }
else { else {
linePoints[2][0] = viewLeft + viewWidth - layout.labelMargin linePoints[2][0] = viewLeft + viewWidth - layout.edgeDistance
- realTextWidth - layout.labelDistance; - realTextWidth - layout.labelDistance;
} }
} }
...@@ -265,7 +265,7 @@ export default function ( ...@@ -265,7 +265,7 @@ export default function (
const labelPosition = labelModel.get('position') || itemModel.get(['emphasis', 'label', 'position']); const labelPosition = labelModel.get('position') || itemModel.get(['emphasis', 'label', 'position']);
const labelDistance = labelModel.get('distanceToLabelLine'); const labelDistance = labelModel.get('distanceToLabelLine');
const labelAlignTo = labelModel.get('alignTo'); const labelAlignTo = labelModel.get('alignTo');
const labelMargin = parsePercent(labelModel.get('margin'), viewWidth); const edgeDistance = parsePercent(labelModel.get('edgeDistance'), viewWidth);
const bleedMargin = labelModel.get('bleedMargin'); const bleedMargin = labelModel.get('bleedMargin');
const labelLineModel = itemModel.getModel('labelLine'); const labelLineModel = itemModel.getModel('labelLine');
...@@ -316,8 +316,8 @@ export default function ( ...@@ -316,8 +316,8 @@ export default function (
if (labelAlignTo === 'edge') { if (labelAlignTo === 'edge') {
// Adjust textX because text align of edge is opposite // Adjust textX because text align of edge is opposite
textX = dx < 0 textX = dx < 0
? viewLeft + labelMargin ? viewLeft + edgeDistance
: viewLeft + viewWidth - labelMargin; : viewLeft + viewWidth - edgeDistance;
} }
else { else {
textX = x3 + (dx < 0 ? -labelDistance : labelDistance); textX = x3 + (dx < 0 ? -labelDistance : labelDistance);
...@@ -355,10 +355,11 @@ export default function ( ...@@ -355,10 +355,11 @@ export default function (
const textRect = label.getBoundingRect().clone(); const textRect = label.getBoundingRect().clone();
textRect.applyTransform(label.getComputedTransform()); textRect.applyTransform(label.getComputedTransform());
// Text has a default 1px stroke. Exclude this. // Text has a default 1px stroke. Exclude this.
textRect.x -= 1; const margin = (label.style.margin || 0) + 2.1;
textRect.y -= 1; textRect.x -= margin / 2;
textRect.width += 2.1; textRect.y -= margin / 2;
textRect.height += 2.1; textRect.width += margin;
textRect.height += margin;
labelLayoutList.push({ labelLayoutList.push({
label, label,
...@@ -371,7 +372,7 @@ export default function ( ...@@ -371,7 +372,7 @@ export default function (
textAlign: textAlign, textAlign: textAlign,
labelDistance: labelDistance, labelDistance: labelDistance,
labelAlignTo: labelAlignTo, labelAlignTo: labelAlignTo,
labelMargin: labelMargin, edgeDistance: edgeDistance,
bleedMargin: bleedMargin, bleedMargin: bleedMargin,
rect: textRect rect: textRect
}); });
......
...@@ -45,7 +45,7 @@ import Transformable from 'zrender/src/core/Transformable'; ...@@ -45,7 +45,7 @@ import Transformable from 'zrender/src/core/Transformable';
import { updateLabelLinePoints, setLabelLineStyle } from './labelGuideHelper'; import { updateLabelLinePoints, setLabelLineStyle } from './labelGuideHelper';
import SeriesModel from '../model/Series'; import SeriesModel from '../model/Series';
import { makeInner } from '../util/model'; import { makeInner } from '../util/model';
import { retrieve2, each, keys, isFunction, filter, indexOf, map, guid } from 'zrender/src/core/util'; import { retrieve2, each, keys, isFunction, filter, indexOf } from 'zrender/src/core/util';
import { PathStyleProps } from 'zrender/src/graphic/Path'; import { PathStyleProps } from 'zrender/src/graphic/Path';
import Model from '../model/Model'; import Model from '../model/Model';
import { prepareLayoutList, hideOverlap, shiftLayoutOnX, shiftLayoutOnY } from './labelLayoutHelper'; import { prepareLayoutList, hideOverlap, shiftLayoutOnX, shiftLayoutOnY } from './labelLayoutHelper';
......
...@@ -57,14 +57,13 @@ export function prepareLayoutList(input: LabelLayoutListPrepareInput[]): LabelLa ...@@ -57,14 +57,13 @@ export function prepareLayoutList(input: LabelLayoutListPrepareInput[]): LabelLa
continue; continue;
} }
const layoutOption = rawItem.computedLayoutOption;
const label = rawItem.label; const label = rawItem.label;
const transform = label.getComputedTransform(); const transform = label.getComputedTransform();
// NOTE: Get bounding rect after getComputedTransform, or label may not been updated by the host el. // NOTE: Get bounding rect after getComputedTransform, or label may not been updated by the host el.
const localRect = label.getBoundingRect(); const localRect = label.getBoundingRect();
const isAxisAligned = !transform || (transform[1] < 1e-5 && transform[2] < 1e-5); const isAxisAligned = !transform || (transform[1] < 1e-5 && transform[2] < 1e-5);
const minMargin = layoutOption.minMargin || 0; const minMargin = label.style.margin || 0;
const globalRect = localRect.clone(); const globalRect = localRect.clone();
globalRect.applyTransform(transform); globalRect.applyTransform(transform);
globalRect.x -= minMargin / 2; globalRect.x -= minMargin / 2;
......
...@@ -26,6 +26,7 @@ import { Dictionary } from 'zrender/src/core/types'; ...@@ -26,6 +26,7 @@ import { Dictionary } from 'zrender/src/core/types';
import { ECUnitOption, SeriesOption } from '../util/types'; import { ECUnitOption, SeriesOption } from '../util/types';
import { __DEV__ } from '../config'; import { __DEV__ } from '../config';
import type { BarSeriesOption } from '../chart/bar/BarSeries'; import type { BarSeriesOption } from '../chart/bar/BarSeries';
import { PieSeriesOption } from '../chart/pie/PieSeries';
function get(opt: Dictionary<any>, path: string): any { function get(opt: Dictionary<any>, path: string): any {
const pathArr = path.split(','); const pathArr = path.split(',');
...@@ -94,6 +95,18 @@ function compatBarItemStyle(option: Dictionary<any>) { ...@@ -94,6 +95,18 @@ function compatBarItemStyle(option: Dictionary<any>) {
} }
} }
function compatPieLabel(option: Dictionary<any>) {
if (!option) {
return;
}
if (option.alignTo === 'edge' && option.margin != null && option.edgeDistance == null) {
if (__DEV__) {
deprecateLog('label.margin has been changed to label.edgeDistance in pie.');
}
option.edgeDistance = option.margin;
}
}
export default function (option: ECUnitOption, isTheme?: boolean) { export default function (option: ECUnitOption, isTheme?: boolean) {
compatStyle(option, isTheme); compatStyle(option, isTheme);
...@@ -122,6 +135,13 @@ export default function (option: ECUnitOption, isTheme?: boolean) { ...@@ -122,6 +135,13 @@ export default function (option: ECUnitOption, isTheme?: boolean) {
seriesOpt.clockwise = seriesOpt.clockWise; seriesOpt.clockwise = seriesOpt.clockWise;
deprecateLog('clockWise has been changed to clockwise.'); deprecateLog('clockWise has been changed to clockwise.');
} }
compatPieLabel((seriesOpt as PieSeriesOption).label);
const data = seriesOpt.data;
if (data && !isTypedArray(data)) {
for (let i = 0; i < data.length; i++) {
compatPieLabel(data[i]);
}
}
} }
else if (seriesType === 'gauge') { else if (seriesType === 'gauge') {
const pointerColor = get(seriesOpt, 'pointer.color'); const pointerColor = get(seriesOpt, 'pointer.color');
...@@ -136,7 +156,7 @@ export default function (option: ECUnitOption, isTheme?: boolean) { ...@@ -136,7 +156,7 @@ export default function (option: ECUnitOption, isTheme?: boolean) {
const data = seriesOpt.data; const data = seriesOpt.data;
if (data && !isTypedArray(data)) { if (data && !isTypedArray(data)) {
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
if (data[i]) { if (typeof data[i] === 'object') {
compatBarItemStyle(data[i]); compatBarItemStyle(data[i]);
compatBarItemStyle(data[i] && data[i].emphasis); compatBarItemStyle(data[i] && data[i].emphasis);
} }
......
...@@ -961,6 +961,10 @@ function setTextStyleCommon( ...@@ -961,6 +961,10 @@ function setTextStyleCommon(
if (overflow) { if (overflow) {
textStyle.overflow = overflow; textStyle.overflow = overflow;
} }
const margin = textStyleModel.get('minMargin');
if (margin != null) {
textStyle.margin = margin;
}
setTokenTextStyle(textStyle, textStyleModel, globalTextStyle, opt, isNotNormal, isAttached, true); setTokenTextStyle(textStyle, textStyleModel, globalTextStyle, opt, isNotNormal, isAttached, true);
} }
......
...@@ -774,6 +774,12 @@ export interface LabelOption extends TextCommonOption { ...@@ -774,6 +774,12 @@ export interface LabelOption extends TextCommonOption {
rotate?: number rotate?: number
offset?: number[] offset?: number[]
/**
* Min margin between labels. Used when label has layout.
*/
// It's minMargin instead of margin is for not breaking the previous code using margin.
minMargin?: number
overflow?: TextStyleProps['overflow'] overflow?: TextStyleProps['overflow']
silent?: boolean silent?: boolean
precision?: number | 'auto' precision?: number | 'auto'
...@@ -851,12 +857,6 @@ export interface LabelLayoutOption { ...@@ -851,12 +857,6 @@ export interface LabelLayoutOption {
* @default 'none' * @default 'none'
*/ */
hideOverlap?: boolean hideOverlap?: boolean
/**
* Minimal margin between two labels which will be considered as overlapped.
*/
minMargin?: number
/** /**
* If label is draggable. * If label is draggable.
*/ */
......
...@@ -440,8 +440,7 @@ under the License. ...@@ -440,8 +440,7 @@ under the License.
draggable: true, draggable: true,
align: 'center', align: 'center',
moveOverlap: 'shift-x', moveOverlap: 'shift-x',
hideOverlap: true, hideOverlap: true
minMargin: 10
}, },
labelLine: { labelLine: {
show: true, show: true,
...@@ -455,6 +454,7 @@ under the License. ...@@ -455,6 +454,7 @@ under the License.
formatter: function (param) { formatter: function (param) {
return param.data[3]; return param.data[3];
}, },
minMargin: 10,
color: '#333', color: '#333',
textBorderColor: '#fff', textBorderColor: '#fff',
textBorderWidth: 1, textBorderWidth: 1,
...@@ -504,8 +504,7 @@ under the License. ...@@ -504,8 +504,7 @@ under the License.
x: 500, x: 500,
draggable: true, draggable: true,
moveOverlap: 'shift-y', moveOverlap: 'shift-y',
// hideOverlap: true, // hideOverlap: true
minMargin: 2
}, },
labelLine: { labelLine: {
show: true, show: true,
...@@ -521,7 +520,8 @@ under the License. ...@@ -521,7 +520,8 @@ under the License.
}, },
textBorderColor: '#fff', textBorderColor: '#fff',
textBorderWidth: 1, textBorderWidth: 1,
position: 'top' position: 'top',
minMargin: 2
} }
}] }]
}; };
......
...@@ -73,7 +73,7 @@ under the License. ...@@ -73,7 +73,7 @@ under the License.
length2: 15 length2: 15
}, },
label: { label: {
margin: 20, edgeDistance: 20,
position: 'outer' position: 'outer'
} }
}] }]
...@@ -89,7 +89,7 @@ under the License. ...@@ -89,7 +89,7 @@ under the License.
length2: 15 length2: 15
}, },
label: { label: {
margin: 20, edgeDistance: 20,
position: 'outer', position: 'outer',
alignTo: 'labelLine' alignTo: 'labelLine'
} }
...@@ -106,7 +106,7 @@ under the License. ...@@ -106,7 +106,7 @@ under the License.
length2: 15 length2: 15
}, },
label: { label: {
margin: 20, edgeDistance: 20,
position: 'outer', position: 'outer',
alignTo: 'edge' alignTo: 'edge'
} }
...@@ -141,7 +141,7 @@ under the License. ...@@ -141,7 +141,7 @@ under the License.
length2: 15 length2: 15
}, },
label: { label: {
margin: 20, edgeDistance: 20,
position: 'outer', position: 'outer',
alignTo: 'labelLine' alignTo: 'labelLine'
}, },
...@@ -176,7 +176,7 @@ under the License. ...@@ -176,7 +176,7 @@ under the License.
length2: 15 length2: 15
}, },
label: { label: {
margin: 20, edgeDistance: 20,
position: 'outer', position: 'outer',
alignTo: 'labelLine' alignTo: 'labelLine'
}, },
...@@ -211,7 +211,7 @@ under the License. ...@@ -211,7 +211,7 @@ under the License.
}); });
var config = { var config = {
length2: 15, length2: 15,
margin: 20, edgeDistance: 20,
overflow: 'truncate' overflow: 'truncate'
}; };
...@@ -222,7 +222,7 @@ under the License. ...@@ -222,7 +222,7 @@ under the License.
length2: config.length2 length2: config.length2
}, },
label: { label: {
margin: config.margin, edgeDistance: config.edgeDistance,
overflow: config.overflow overflow: config.overflow
} }
}] }]
...@@ -238,7 +238,7 @@ under the License. ...@@ -238,7 +238,7 @@ under the License.
length2: config.length2, length2: config.length2,
}, },
label: { label: {
margin: config.margin, edgeDistance: config.edgeDistance,
overflow: config.overflow overflow: config.overflow
} }
}) })
...@@ -247,7 +247,7 @@ under the License. ...@@ -247,7 +247,7 @@ under the License.
} }
gui.add(config, 'length2', 0, 300).onChange(update); gui.add(config, 'length2', 0, 300).onChange(update);
gui.add(config, 'margin', 0, 300).onChange(update); gui.add(config, 'edgeDistance', 0, 300).onChange(update);
gui.add(config, 'overflow', ['truncate', 'break', 'breakAll']).onChange(update); gui.add(config, 'overflow', ['truncate', 'break', 'breakAll']).onChange(update);
}); });
</script> </script>
......
...@@ -121,9 +121,9 @@ under the License. ...@@ -121,9 +121,9 @@ under the License.
label: { label: {
alignTo: 'edge', alignTo: 'edge',
formatter: '{name|{b}}\n{time|{c} 小时}', formatter: '{name|{b}}\n{time|{c} 小时}',
margin: 10, minMargin: 5,
edgeDistance: 10,
lineHeight: 15, lineHeight: 15,
// color: 'inherit',
rich: { rich: {
time: { time: {
fontSize: 10, fontSize: 10,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册