2021SC@SDUSC
直角坐标轴 Axis
包括xAxis, yAxis
CartesianAxisView
CartesianAxisView扩展自AxisView,重写了render,定义了splitLine以及splitArea方法,并扩展了xAxis yAxis View。主要代码如下:
CartesianAxisView.prototype.render = function (axisModel, ecModel, api, payload) {
this.group.removeAll();
var oldAxisGroup = this._axisGroup;
this._axisGroup = new graphic.Group();
this.group.add(this._axisGroup);
if (!axisModel.get('show')) {
return;
}
var gridModel = axisModel.getCoordSysModel();
var layout = cartesianAxisHelper.layout(gridModel, axisModel);
var axisBuilder = new AxisBuilder(axisModel, zrUtil.extend({
handleAutoShown: function (elementType) {
var cartesians = gridModel.coordinateSystem.getCartesians();
for (var i = 0; i < cartesians.length; i++) {
var otherAxisType = cartesians[i].getOtherAxis(axisModel.axis).type;
if (otherAxisType === 'value' || otherAxisType === 'log') {
// Still show axis tick or axisLine if other axis is value / log
return true;
}
} // Not show axisTick or axisLine if other axis is category / time
return false;
}
}, layout));
zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
this._axisGroup.add(axisBuilder.getGroup());
zrUtil.each(selfBuilderAttrs, function (name) {
if (axisModel.get([name, 'show'])) {
axisElementBuilders[name](this, this._axisGroup, axisModel, gridModel);
}
}, this); // THIS is a special case for bar racing chart.
// Update the axis label from the natural initial layout to
// sorted layout should has no animation.
var isInitialSortFromBarRacing = payload && payload.type === 'changeAxisOrder' && payload.isInitSort;
if (!isInitialSortFromBarRacing) {
graphic.groupTransition(oldAxisGroup, this._axisGroup, axisModel);
}
_super.prototype.render.call(this, axisModel, ecModel, api, payload);
};
splitLine: function (axisView, axisGroup, axisModel, gridModel) {
var axis = axisModel.axis;
if (axis.scale.isBlank()) {
return;
}
var splitLineModel = axisModel.getModel('splitLine');
var lineStyleModel = splitLineModel.getModel('lineStyle');
var lineColors = lineStyleModel.get('color');
lineColors = zrUtil.isArray(lineColors) ? lineColors : [lineColors];
var gridRect = gridModel.coordinateSystem.getRect();
var isHorizontal = axis.isHorizontal();
var lineCount = 0;
var ticksCoords = axis.getTicksCoords({
tickModel: splitLineModel
});
var p1 = [];
var p2 = [];
var lineStyle = lineStyleModel.getLineStyle();
for (var i = 0; i < ticksCoords.length; i++) {
var tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);
if (isHorizontal) {
p1[0] = tickCoord;
p1[1] = gridRect.y;
p2[0] = tickCoord;
p2[1] = gridRect.y + gridRect.height;
} else {
p1[0] = gridRect.x;
p1[1] = tickCoord;
p2[0] = gridRect.x + gridRect.width;
p2[1] = tickCoord;
}
var colorIndex = lineCount++ % lineColors.length;
var tickValue = ticksCoords[i].tickValue;
axisGroup.add(new graphic.Line({
anid: tickValue != null ? 'line_' + ticksCoords[i].tickValue : null,
subPixelOptimize: true,
autoBatch: true,
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
style: zrUtil.defaults({
stroke: lineColors[colorIndex]
}, lineStyle),
silent: true
}));
}
},
splitArea: function (axisView, axisGroup, axisModel, gridModel) {
rectCoordAxisBuildSplitArea(axisView, axisGroup, axisModel, gridModel);
}
var CartesianXAxisView =
/** @class */
function (_super) {
__extends(CartesianXAxisView, _super);
function CartesianXAxisView() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.type = CartesianXAxisView.type;
return _this;
}
CartesianXAxisView.type = 'xAxis';
return CartesianXAxisView;
}(CartesianAxisView);
export { CartesianXAxisView };
var CartesianYAxisView =
/** @class */
function (_super) {
__extends(CartesianYAxisView, _super);
function CartesianYAxisView() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.type = CartesianXAxisView.type;
return _this;
}
CartesianYAxisView.type = 'yAxis';
return CartesianYAxisView;
}(CartesianAxisView);
极坐标轴?
每个极坐标轴都有一个角度轴AngleAxis和一个半径轴RadiusAxis
AngleAxisView
AngleAxis扩展自AxisView、AxisBuilder,重写了render,定义了axisLine、axisTick、axisLabel、splitLine以及splitArea方法。主要代码如下:
AngleAxisView.prototype.render = function (angleAxisModel, ecModel) {
this.group.removeAll();
if (!angleAxisModel.get('show')) {
return;
}
var angleAxis = angleAxisModel.axis;
var polar = angleAxis.polar;
var radiusExtent = polar.getRadiusAxis().getExtent();
var ticksAngles = angleAxis.getTicksCoords();
var minorTickAngles = angleAxis.getMinorTicksCoords();
var labels = zrUtil.map(angleAxis.getViewLabels(), function (labelItem) {
labelItem = zrUtil.clone(labelItem);
var scale = angleAxis.scale;
var tickValue = scale.type === 'ordinal' ? scale.getRawOrdinalNumber(labelItem.tickValue) : labelItem.tickValue;
labelItem.coord = angleAxis.dataToCoord(tickValue);
return labelItem;
});
fixAngleOverlap(labels);
fixAngleOverlap(ticksAngles);
zrUtil.each(elementList, function (name) {
if (angleAxisModel.get([name, 'show']) && (!angleAxis.scale.isBlank() || name === 'axisLine')) {
angelAxisElementsBuilders[name](this.group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent, labels);
}
}, this);
};
axisLine: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
var lineStyleModel = angleAxisModel.getModel(['axisLine', 'lineStyle']); // extent id of the axis radius (r0 and r)
var rId = getRadiusIdx(polar);
var r0Id = rId ? 0 : 1;
var shape;
if (radiusExtent[r0Id] === 0) {
shape = new graphic.Circle({
shape: {
cx: polar.cx,
cy: polar.cy,
r: radiusExtent[rId]
},
style: lineStyleModel.getLineStyle(),
z2: 1,
silent: true
});
} else {
shape = new graphic.Ring({
shape: {
cx: polar.cx,
cy: polar.cy,
r: radiusExtent[rId],
r0: radiusExtent[r0Id]
},
style: lineStyleModel.getLineStyle(),
z2: 1,
silent: true
});
}
shape.style.fill = null;
group.add(shape);
},
axisTick: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
var tickModel = angleAxisModel.getModel('axisTick');
var tickLen = (tickModel.get('inside') ? -1 : 1) * tickModel.get('length');
var radius = radiusExtent[getRadiusIdx(polar)];
var lines = zrUtil.map(ticksAngles, function (tickAngleItem) {
return new graphic.Line({
shape: getAxisLineShape(polar, [radius, radius + tickLen], tickAngleItem.coord)
});
});
group.add(graphic.mergePath(lines, {
style: zrUtil.defaults(tickModel.getModel('lineStyle').getLineStyle(), {
stroke: angleAxisModel.get(['axisLine', 'lineStyle', 'color'])
})
}));
},
axisLabel: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent, labels) {
var rawCategoryData = angleAxisModel.getCategories(true);
var commonLabelModel = angleAxisModel.getModel('axisLabel');
var labelMargin = commonLabelModel.get('margin');
var triggerEvent = angleAxisModel.get('triggerEvent'); // Use length of ticksAngles because it may remove the last tick to avoid overlapping
zrUtil.each(labels, function (labelItem, idx) {
var labelModel = commonLabelModel;
var tickValue = labelItem.tickValue;
var r = radiusExtent[getRadiusIdx(polar)];
var p = polar.coordToPoint([r + labelMargin, labelItem.coord]);
var cx = polar.cx;
var cy = polar.cy;
var labelTextAlign = Math.abs(p[0] - cx) / r < 0.3 ? 'center' : p[0] > cx ? 'left' : 'right';
var labelTextVerticalAlign = Math.abs(p[1] - cy) / r < 0.3 ? 'middle' : p[1] > cy ? 'top' : 'bottom';
if (rawCategoryData && rawCategoryData[tickValue]) {
var rawCategoryItem = rawCategoryData[tickValue];
if (zrUtil.isObject(rawCategoryItem) && rawCategoryItem.textStyle) {
labelModel = new Model(rawCategoryItem.textStyle, commonLabelModel, commonLabelModel.ecModel);
}
}
var textEl = new graphic.Text({
silent: AxisBuilder.isLabelSilent(angleAxisModel),
style: createTextStyle(labelModel, {
x: p[0],
y: p[1],
fill: labelModel.getTextColor() || angleAxisModel.get(['axisLine', 'lineStyle', 'color']),
text: labelItem.formattedLabel,
align: labelTextAlign,
verticalAlign: labelTextVerticalAlign
})
});
group.add(textEl); // Pack data for mouse event
if (triggerEvent) {
var eventData = AxisBuilder.makeAxisEventDataBase(angleAxisModel);
eventData.targetType = 'axisLabel';
eventData.value = labelItem.rawLabel;
getECData(textEl).eventData = eventData;
}
}, this);
},
splitLine: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
var splitLineModel = angleAxisModel.getModel('splitLine');
var lineStyleModel = splitLineModel.getModel('lineStyle');
var lineColors = lineStyleModel.get('color');
var lineCount = 0;
lineColors = lineColors instanceof Array ? lineColors : [lineColors];
var splitLines = [];
for (var i = 0; i < ticksAngles.length; i++) {
var colorIndex = lineCount++ % lineColors.length;
splitLines[colorIndex] = splitLines[colorIndex] || [];
splitLines[colorIndex].push(new graphic.Line({
shape: getAxisLineShape(polar, radiusExtent, ticksAngles[i].coord)
}));
} // Simple optimization
// Batching the lines if color are the same
for (var i = 0; i < splitLines.length; i++) {
group.add(graphic.mergePath(splitLines[i], {
style: zrUtil.defaults({
stroke: lineColors[i % lineColors.length]
}, lineStyleModel.getLineStyle()),
silent: true,
z: angleAxisModel.get('z')
}));
}
},
splitArea: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
if (!ticksAngles.length) {
return;
}
var splitAreaModel = angleAxisModel.getModel('splitArea');
var areaStyleModel = splitAreaModel.getModel('areaStyle');
var areaColors = areaStyleModel.get('color');
var lineCount = 0;
areaColors = areaColors instanceof Array ? areaColors : [areaColors];
var splitAreas = [];
var RADIAN = Math.PI / 180;
var prevAngle = -ticksAngles[0].coord * RADIAN;
var r0 = Math.min(radiusExtent[0], radiusExtent[1]);
var r1 = Math.max(radiusExtent[0], radiusExtent[1]);
var clockwise = angleAxisModel.get('clockwise');
for (var i = 1, len = ticksAngles.length; i <= len; i++) {
var coord = i === len ? ticksAngles[0].coord : ticksAngles[i].coord;
var colorIndex = lineCount++ % areaColors.length;
splitAreas[colorIndex] = splitAreas[colorIndex] || [];
splitAreas[colorIndex].push(new graphic.Sector({
shape: {
cx: polar.cx,
cy: polar.cy,
r0: r0,
r: r1,
startAngle: prevAngle,
endAngle: -coord * RADIAN,
clockwise: clockwise
},
silent: true
}));
prevAngle = -coord * RADIAN;
} // Simple optimization
// Batching the lines if color are the same
for (var i = 0; i < splitAreas.length; i++) {
group.add(graphic.mergePath(splitAreas[i], {
style: zrUtil.defaults({
fill: areaColors[i % areaColors.length]
}, areaStyleModel.getAreaStyle()),
silent: true
}));
}
}
RadiusAxisView
RadiusAxisView扩展自AxisView、AxisBuilder,重写了render,定义了splitLine以及splitArea方法。主要代码如下:
RadiusAxisView.prototype.render = function (radiusAxisModel, ecModel) {
this.group.removeAll();
if (!radiusAxisModel.get('show')) {
return;
}
var oldAxisGroup = this._axisGroup;
var newAxisGroup = this._axisGroup = new graphic.Group();
this.group.add(newAxisGroup);
var radiusAxis = radiusAxisModel.axis;
var polar = radiusAxis.polar;
var angleAxis = polar.getAngleAxis();
var ticksCoords = radiusAxis.getTicksCoords();
var minorTicksCoords = radiusAxis.getMinorTicksCoords();
var axisAngle = angleAxis.getExtent()[0];
var radiusExtent = radiusAxis.getExtent();
var layout = layoutAxis(polar, radiusAxisModel, axisAngle);
var axisBuilder = new AxisBuilder(radiusAxisModel, layout);
zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
newAxisGroup.add(axisBuilder.getGroup());
graphic.groupTransition(oldAxisGroup, newAxisGroup, radiusAxisModel);
zrUtil.each(selfBuilderAttrs, function (name) {
if (radiusAxisModel.get([name, 'show']) && !radiusAxis.scale.isBlank()) {
axisElementBuilders[name](this.group, radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords, minorTicksCoords);
}
}, this);
};
splitLine: function (group, radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords) {
var splitLineModel = radiusAxisModel.getModel('splitLine');
var lineStyleModel = splitLineModel.getModel('lineStyle');
var lineColors = lineStyleModel.get('color');
var lineCount = 0;
lineColors = lineColors instanceof Array ? lineColors : [lineColors];
var splitLines = [];
for (var i = 0; i < ticksCoords.length; i++) {
var colorIndex = lineCount++ % lineColors.length;
splitLines[colorIndex] = splitLines[colorIndex] || [];
splitLines[colorIndex].push(new graphic.Circle({
shape: {
cx: polar.cx,
cy: polar.cy,
r: ticksCoords[i].coord
}
}));
} // Simple optimization
// Batching the lines if color are the same
for (var i = 0; i < splitLines.length; i++) {
group.add(graphic.mergePath(splitLines[i], {
style: zrUtil.defaults({
stroke: lineColors[i % lineColors.length],
fill: null
}, lineStyleModel.getLineStyle()),
silent: true
}));
}
},
splitArea: function (group, radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords) {
if (!ticksCoords.length) {
return;
}
var splitAreaModel = radiusAxisModel.getModel('splitArea');
var areaStyleModel = splitAreaModel.getModel('areaStyle');
var areaColors = areaStyleModel.get('color');
var lineCount = 0;
areaColors = areaColors instanceof Array ? areaColors : [areaColors];
var splitAreas = [];
var prevRadius = ticksCoords[0].coord;
for (var i = 1; i < ticksCoords.length; i++) {
var colorIndex = lineCount++ % areaColors.length;
splitAreas[colorIndex] = splitAreas[colorIndex] || [];
splitAreas[colorIndex].push(new graphic.Sector({
shape: {
cx: polar.cx,
cy: polar.cy,
r0: prevRadius,
r: ticksCoords[i].coord,
startAngle: 0,
endAngle: Math.PI * 2
},
silent: true
}));
prevRadius = ticksCoords[i].coord;
} // Simple optimization
// Batching the lines if color are the same
for (var i = 0; i < splitAreas.length; i++) {
group.add(graphic.mergePath(splitAreas[i], {
style: zrUtil.defaults({
fill: areaColors[i % areaColors.length]
}, areaStyleModel.getAreaStyle()),
silent: true
}));
}
}
|