提交 85559537 编写于 作者: P pah100

axis method retrieval

上级 75974d6c
......@@ -3,9 +3,10 @@ define(function (require) {
var zrUtil = require('zrender/core/util');
var graphic = require('../../util/graphic');
var Model = require('../../model/Model');
var numberUtil = require('../../util/number');
var remRadian = numberUtil.remRadian;
var isRadianAroundZero = numberUtil.isRadianAroundZero;
var EPSILON = 1e-4;
var PI2 = Math.PI * 2;
var PI = Math.PI;
/**
......@@ -40,8 +41,8 @@ define(function (require) {
* @param {number} [opt.labelDirection=1] 1 or -1
* @param {number} [opt.labelOffset=0] Usefull when onZero.
* @param {string} [opt.axisName] default get from axisModel.
* @param {number} [opt.lableRotation] by degree, default get from axisModel.
* @param {number} [opt.lableInterval] Default label interval when label
* @param {number} [opt.labelRotation] by degree, default get from axisModel.
* @param {number} [opt.labelInterval] Default label interval when label
* interval from model is null or 'auto'.
* @param {number} [opt.strokeContainThreshold] Default label interval when label
* @param {number} [opt.silent=true]
......@@ -311,11 +312,11 @@ define(function (require) {
var textAlign;
var textBaseline;
if (isAroundZero(rotationDiff)) { // Label is parallel with axis line.
if (isRadianAroundZero(rotationDiff)) { // Label is parallel with axis line.
textBaseline = direction > 0 ? 'top' : 'bottom';
textAlign = 'center';
}
else if (isAroundZero(rotationDiff - PI)) { // Label is inverse parallel with axis line.
else if (isRadianAroundZero(rotationDiff - PI)) { // Label is inverse parallel with axis line.
textBaseline = direction > 0 ? 'bottom' : 'top';
textAlign = 'center';
}
......@@ -348,11 +349,11 @@ define(function (require) {
var onLeft = (textPosition === 'start' && !inverse)
|| (textPosition !== 'start' && inverse);
if (isAroundZero(rotationDiff - PI / 2)) {
if (isRadianAroundZero(rotationDiff - PI / 2)) {
textBaseline = onLeft ? 'bottom' : 'top';
textAlign = 'center';
}
else if (isAroundZero(rotationDiff - PI * 1.5)) {
else if (isRadianAroundZero(rotationDiff - PI * 1.5)) {
textBaseline = onLeft ? 'top' : 'bottom';
textAlign = 'center';
}
......@@ -394,21 +395,6 @@ define(function (require) {
return interval;
};
/**
* @inner
*/
function isAroundZero(val) {
return val > -EPSILON && val < EPSILON;
}
/**
* @inner
*/
function remRadian(radian) {
// To 0 - 2 * PI, considering negative radian.
return (radian % PI2 + PI2) % PI2;
}
return AxisBuilder;
});
\ No newline at end of file
define(function (require) {
var zrUtil = require('zrender/core/util');
var axisHelper = require('./axisHelper');
function getName(obj) {
if (zrUtil.isObject(obj) && obj.value != null) {
......@@ -22,31 +23,11 @@ define(function (require) {
* Format labels
* @return {Array.<string>}
*/
function getFormattedLabels () {
var labelFormatter = this.get('axisLabel.formatter');
var axis = this.axis;
var scale = axis.scale;
var labels = scale.getTicksLabels();
var ticks = scale.getTicks();
if (typeof labelFormatter === 'string') {
labelFormatter = (function (tpl) {
return function (val) {
return tpl.replace('{value}', val);
};
})(labelFormatter);
return zrUtil.map(labels, labelFormatter);
}
else if (typeof labelFormatter === 'function') {
return zrUtil.map(ticks, function (tick, idx) {
return labelFormatter(
axis.type === 'category' ? scale.getLabel(tick) : tick,
idx
);
}, this);
}
else {
return labels;
}
function getFormattedLabels() {
return axisHelper.getFormattedLabels(
this.axis,
this.get('axisLabel.formatter')
);
}
return {
......
......@@ -5,7 +5,8 @@
define(function(require) {
'use strict';
var textContain = require('zrender/contain/text');
var zrUtil = require('zrender/core/util');
var axisHelper = require('../axisHelper');
return function (axis) {
var axisModel = axis.model;
......@@ -19,40 +20,11 @@ define(function(require) {
return labelInterval === 'auto' ? 0 : labelInterval;
}
var ticks = axis.scale.getTicks();
var labels = axisModel.getFormattedLabels();
var font = labelModel.getModel('textStyle').getFont();
var textSpaceTakenRect;
var autoLabelInterval = 0;
var accumulatedLabelInterval = 0;
var isAxisHorizontal = axis.isHorizontal();
for (var i = 0; i < ticks.length; i++) {
var tick = ticks[i];
var tickCoord = axis.dataToCoord(tick);
var rect = textContain.getBoundingRect(
labels[i], font, 'center', 'top'
);
rect[isAxisHorizontal ? 'x' : 'y'] += tickCoord;
rect[isAxisHorizontal ? 'width' : 'height'] *= 1.5;
if (!textSpaceTakenRect) {
textSpaceTakenRect = rect.clone();
}
// There is no space for current label;
else if (textSpaceTakenRect.intersect(rect)) {
accumulatedLabelInterval++;
autoLabelInterval = Math.max(autoLabelInterval, accumulatedLabelInterval);
}
else {
textSpaceTakenRect.union(rect);
// Reset
accumulatedLabelInterval = 0;
}
}
return autoLabelInterval;
return axisHelper.getAxisLabelInterval(
zrUtil.map(axis.scale.getTicks(), axis.dataToCoord, axis),
axisModel.getFormattedLabels(),
labelModel.getModel('textStyle').getFont(),
axis.isHorizontal()
);
};
});
\ No newline at end of file
......@@ -6,6 +6,7 @@
define(function (require) {
var zrUtil = require('zrender/core/util');
var numberUtil = require('../util/number');
var IntervalScale = require('./Interval');
......@@ -28,16 +29,6 @@ define(function (require) {
return lo;
};
/**
* @param {string|Date|number} value
* @inner
*/
var parseDate = function (value) {
return value instanceof Date
? value
: new Date(typeof value == 'string' ? value.replace(/-/g, '/') : value);
};
/**
* @param {string} str
* @return {string}
......@@ -63,7 +54,7 @@ define(function (require) {
tpl = 'MM-dd\nyyyy';
}
var date = parseDate(value);
var date = numberUtil.parseDate(value);
var y = date.getFullYear();
var M = date.getMonth() + 1;
var d = date.getDate();
......@@ -129,7 +120,7 @@ define(function (require) {
zrUtil.each(['contain', 'normalize'], function (methodName) {
TimeScale.prototype[methodName] = function (val) {
val = +parseDate(val);
val = +numberUtil.parseDate(val);
return intervalScaleProto[methodName].call(this, val);
};
});
......
......@@ -8,8 +8,11 @@ define(function (require) {
var zrUtil = require('zrender/core/util');
var number = {};
var RADIAN_EPSILON = 1e-4;
function _trim(str) {
return str.replace(/^\s+/, '').replace(/\s+$/, ''); }
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
/**
* Linear mapping a value from domain to range
......@@ -114,5 +117,33 @@ define(function (require) {
// Number.MAX_SAFE_INTEGER, ie do not support.
number.MAX_SAFE_INTEGER = 9007199254740991;
/**
* To 0 - 2 * PI, considering negative radian.
* @param {number} radian
* @return {number}
*/
number.remRadian = function (radian) {
var pi2 = Math.PI * 2;
return (radian % pi2 + pi2) % pi2;
};
/**
* @param {type} radian
* @return {boolean}
*/
number.isRadianAroundZero = function (val) {
return val > -RADIAN_EPSILON && val < RADIAN_EPSILON;
};
/**
* @param {string|Date|number} value
* @return {number} timestamp
*/
number.parseDate = function (value) {
return value instanceof Date
? value
: new Date(typeof value === 'string' ? value.replace(/-/g, '/') : value);
};
return number;
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册