提交 6fb4079a 编写于 作者: S sushuang

Support auto label and some refactor about label.

上级 8840e95b
import * as graphic from '../../util/graphic';
import {getDefaultLabel} from '../helper/labelHelper';
export function setLabel(
normalStyle, hoverStyle, itemModel, color, seriesModel, dataIndex, labelPositionOutside
......@@ -11,7 +12,7 @@ export function setLabel(
{
labelFetcher: seriesModel,
labelDataIndex: dataIndex,
defaultText: seriesModel.getRawValue(dataIndex),
defaultText: getDefaultLabel(seriesModel.getData(), dataIndex),
isRectText: true,
autoColor: color
}
......
......@@ -2,7 +2,7 @@ import {__DEV__} from '../config';
import * as echarts from '../echarts';
import * as zrUtil from 'zrender/src/core/util';
import * as graphicUtil from '../util/graphic';
import {findLabelValueDim} from './helper/labelHelper';
import {getDefaultLabel} from './helper/labelHelper';
import createListFromArray from './helper/createListFromArray';
import { getLayoutOnAxis } from '../layout/barGrid';
import DataDiffer from '../data/DataDiffer';
......@@ -309,7 +309,6 @@ function makeRenderItem(customSeries, data, ecModel, api) {
var currItemModel;
var currLabelNormalModel;
var currLabelEmphasisModel;
var currLabelValueDim;
var currVisualColor;
return function (dataIndexInside) {
......@@ -331,7 +330,6 @@ function makeRenderItem(customSeries, data, ecModel, api) {
currItemModel = data.getItemModel(dataIndexInside);
currLabelNormalModel = currItemModel.getModel(LABEL_NORMAL);
currLabelEmphasisModel = currItemModel.getModel(LABEL_EMPHASIS);
currLabelValueDim = findLabelValueDim(data);
currVisualColor = data.getItemVisual(dataIndexInside, 'color');
currDirty = false;
......@@ -368,7 +366,7 @@ function makeRenderItem(customSeries, data, ecModel, api) {
var opacity = data.getItemVisual(dataIndexInside, 'opacity');
opacity != null && (itemStyle.opacity = opacity);
if (currLabelValueDim != null) {
if (!data.dimensionsSummary.noDefaultLabel) {
graphicUtil.setTextStyle(itemStyle, currLabelNormalModel, null, {
autoColor: currVisualColor,
isRectText: true
......@@ -377,7 +375,7 @@ function makeRenderItem(customSeries, data, ecModel, api) {
itemStyle.text = currLabelNormalModel.getShallow('show')
? zrUtil.retrieve2(
customSeries.getFormattedLabel(dataIndexInside, 'normal'),
data.get(currLabelValueDim, dataIndexInside)
getDefaultLabel(data, dataIndexInside)
)
: null;
}
......
......@@ -6,7 +6,7 @@ import * as zrUtil from 'zrender/src/core/util';
import {createSymbol} from '../../util/symbol';
import * as graphic from '../../util/graphic';
import {parsePercent} from '../../util/number';
import {findLabelValueDim} from './labelHelper';
import {getDefaultLabel} from './labelHelper';
function getSymbolSize(data, idx) {
......@@ -259,15 +259,14 @@ symbolProto._updateCommon = function (data, idx, symbolSize, seriesScope) {
}
var useNameLabel = seriesScope && seriesScope.useNameLabel;
var valueDim = !useNameLabel && findLabelValueDim(data);
if (useNameLabel || valueDim != null) {
if (useNameLabel || !data.dimensionsSummary.noDefaultLabel) {
graphic.setLabelStyle(
elStyle, hoverItemStyle, labelModel, hoverLabelModel,
{
labelFetcher: seriesModel,
labelDataIndex: idx,
defaultText: useNameLabel ? data.getName(idx) : data.get(valueDim, idx),
defaultText: useNameLabel ? data.getName(idx) : getDefaultLabel(data, idx),
isRectText: true,
autoColor: color
}
......
import * as zrUtil from 'zrender/src/core/util';
import List from '../../data/List';
import createDimensions from '../../data/helper/createDimensions';
import {getDimTypeByAxis, SOURCE_FORMAT_ORIGINAL} from '../../data/helper/sourceHelper';
import {SOURCE_FORMAT_ORIGINAL} from '../../data/helper/sourceHelper';
import {getValueTypeByAxis} from '../../data/helper/dimensionHelper';
import {getDataItemValue} from '../../util/model';
import CoordinateSystem from '../../CoordinateSystem';
import {getCoordSysDefineBySeries} from '../../model/referHelper';
......@@ -24,7 +25,7 @@ function createListFromArray(source, seriesModel) {
var axisModel = coordSysDefine.axisMap.get(dim);
if (axisModel) {
var axisType = axisModel.get('type');
dimInfo.type = getDimTypeByAxis(axisType);
dimInfo.type = getValueTypeByAxis(axisType);
dimInfo.stackable = isStackable(axisType);
}
return dimInfo;
......
/**
* @module echarts/chart/helper/Symbol
*/
import {otherDimToDataDim} from '../../util/model';
import {SOURCE_FORMAT_ORIGINAL} from '../../data/helper/sourceHelper';
export function findLabelValueDim(data) {
var valueDim;
var labelDims = otherDimToDataDim(data, 'label');
// Get concrete dim.
function getLabelValueDim(data) {
var dimensionsSummary = data.dimensionsSummary;
var labelDims = dimensionsSummary.label;
if (labelDims.length) {
valueDim = labelDims[0];
}
else {
// Get last value dim
var dimensions = data.dimensions.slice();
var dataType;
while (dimensions.length && (
valueDim = dimensions.pop(),
dataType = data.getDimensionInfo(valueDim).type,
dataType === 'ordinal' || dataType === 'time'
)) {} // jshint ignore:line
}
return labelDims[0]
? labelDims[0]
// Only if the source is own to a series, we can use the last.
// If the source is from dataset, it probably be shared by
// different series.
: data.getProvider().getSource().sourceFormat === SOURCE_FORMAT_ORIGINAL
? dimensionsSummary.lastValueDimension
: null;
}
return valueDim;
/**
* @param {module:echarts/data/List} data
* @param {number} dataIndex
*/
export function getDefaultLabel(data, dataIndex) {
var val = data.get(getLabelValueDim(data), dataIndex);
return (val == null || isNaN(val)) ? '' : val;
}
......@@ -9,6 +9,7 @@ import Model from '../model/Model';
import DataDiffer from './DataDiffer';
import Source from './Source';
import {defaultDimValueGetters, DefaultDataProvider} from './helper/dataProvider';
import {summarizeDimensions} from './helper/dimensionHelper';
var isObject = zrUtil.isObject;
......@@ -250,6 +251,13 @@ var List = function (dimensions, hostModel) {
* @private
*/
this._approximateExtent = {};
/**
* Cache summary info for fast visit. See "dimensionHelper".
* @readOnly
* @type {Object}
*/
this.dimensionsSummary = summarizeDimensions(this);
};
var listProto = List.prototype;
......
......@@ -8,15 +8,12 @@ import * as zrUtil from 'zrender/src/core/util';
import {normalizeToArray} from '../../util/model';
import {guessOrdinal} from './sourceHelper';
import Source from '../Source';
import {SPECIAL_DIMENSIONS} from './dimensionHelper';
var each = zrUtil.each;
var isString = zrUtil.isString;
var defaults = zrUtil.defaults;
var OTHER_DIMS = zrUtil.createHashMap({
tooltip: 1, label: 1, itemName: 1, seriesName: 1
});
/**
* @see {module:echarts/test/ut/spec/data/completeDimensions}
*
......@@ -146,7 +143,7 @@ function completeDimensions(sysDims, source, opt) {
});
function applyDim(resultItem, coordDim, coordDimIndex) {
if (OTHER_DIMS.get(coordDim)) {
if (SPECIAL_DIMENSIONS.get(coordDim) != null) {
resultItem.otherDims[coordDim] = coordDimIndex;
}
else {
......
import {each, createHashMap} from 'zrender/src/core/util';
export var SPECIAL_DIMENSIONS = createHashMap([
'tooltip', 'label', 'itemName', 'seriesName'
]);
/**
* Summarize and cache for avoiding repeat calculation while travel data.
* @return {Object}
* {
* each of SPECIAL_DIMENSIONS in concrete dim. Array not null/undefined.
* lastValueDimension: a set of DIMENSION_TYPES in concrete dim. Array not null/undefined.
* }
*/
export function summarizeDimensions(data) {
var summary = {};
SPECIAL_DIMENSIONS.each(function (v, dimType) {
summary[dimType] = otherDimToDataDim(data, dimType);
});
summary.lastValueDimension = findTheLastValueDimensions(data);
summary.noDefaultLabel = !(summary.label[0] || summary.lastValueDimension);
return summary;
}
/**
* @see {module:echarts/data/helper/createDimensions}
* @param {module:echarts/data/List} data
* @param {string} otherDim See OTHER_DIMS
* @return {Array.<string>} data dimensions on the otherDim.
*/
export function otherDimToDataDim(data, otherDim) {
var dataDim = [];
each(data.dimensions, function (dimName) {
var dimItem = data.getDimensionInfo(dimName);
var otherDims = dimItem.otherDims;
var dimIndex = otherDims[otherDim];
if (dimIndex != null && dimIndex !== false) {
dataDim[dimIndex] = dimItem.name;
}
});
return dataDim;
}
export function getValueTypeByAxis(axisType) {
return axisType === 'category'
? 'ordinal'
: axisType === 'time'
? 'time'
: 'float';
}
function findTheLastValueDimensions(data) {
// Get last value dim
var dimensions = data.dimensions.slice();
var valueType;
var valueDim;
while (dimensions.length && (
valueDim = dimensions.pop(),
valueType = data.getDimensionInfo(valueDim).type,
valueType === 'ordinal' || valueType === 'time'
)) {} // jshint ignore:line
return valueDim;
}
......@@ -416,11 +416,3 @@ export function guessOrdinal(source, dimIndex, dimName) {
return false;
}
export function getDimTypeByAxis(axisType) {
return axisType === 'category'
? 'ordinal'
: axisType === 'time'
? 'time'
: 'float';
}
......@@ -19,6 +19,7 @@ import {
prepareSource,
getSource
} from '../data/helper/sourceHelper';
import {otherDimToDataDim} from '../data/helper/dimensionHelper';
var inner = modelUtil.makeInner();
......@@ -280,7 +281,7 @@ var SeriesModel = ComponentModel.extend({
}, 0);
var result = [];
var tooltipDims = modelUtil.otherDimToDataDim(data, 'tooltip');
var tooltipDims = otherDimToDataDim(data, 'tooltip');
tooltipDims.length
? zrUtil.each(tooltipDims, function (dimIdx) {
......@@ -431,7 +432,7 @@ function autoSeriesName(seriesModel) {
function getSeriesAutoName(seriesModel) {
var data = seriesModel.getRawData();
var dataDims = modelUtil.otherDimToDataDim(data, 'seriesName');
var dataDims = otherDimToDataDim(data, 'seriesName');
var nameArr = [];
zrUtil.each(dataDims, function (dataDim) {
var dimInfo = data.getDimensionInfo(dataDim);
......
......@@ -636,26 +636,6 @@ export function coordDimToDataDim(data, coordDim) {
return dataDim;
}
/**
* @see {module:echarts/data/helper/createDimensions}
* @param {module:echarts/data/List} data
* @param {string} otherDim Can be `otherDims`
* like 'label' or 'tooltip'.
* @return {Array.<string>} data dimensions on the otherDim.
*/
export function otherDimToDataDim(data, otherDim) {
var dataDim = [];
each(data.dimensions, function (dimName) {
var dimItem = data.getDimensionInfo(dimName);
var otherDims = dimItem.otherDims;
var dimIndex = otherDims[otherDim];
if (dimIndex != null && dimIndex !== false) {
dataDim[dimIndex] = dimItem.name;
}
});
return dataDim;
}
function has(obj, prop) {
return obj && obj.hasOwnProperty(prop);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册