提交 ff93e3e7 编写于 作者: L lang

tooltip optimize

上级 47910327
...@@ -55,7 +55,8 @@ define(function (require) { ...@@ -55,7 +55,8 @@ define(function (require) {
r: radiusExtent[1] r: radiusExtent[1]
}, },
style: lineStyleModel.getLineStyle(), style: lineStyleModel.getLineStyle(),
z2: 1 z2: 1,
silent: true
}); });
circle.style.fill = null; circle.style.fill = null;
......
...@@ -550,10 +550,7 @@ define(function (require) { ...@@ -550,10 +550,7 @@ define(function (require) {
var labelPrecision = dataZoomModel.get('labelPrecision'); var labelPrecision = dataZoomModel.get('labelPrecision');
if (labelPrecision == null) { if (labelPrecision == null) {
var dataExtent = axis.scale.getExtent(); labelPrecision = axis.getFormatPrecision();
labelPrecision = Math.abs(
Math.floor(Math.log(dataExtent[1] - dataExtent[0]) / Math.LN10)
);
} }
return (value == null && isNaN(value)) return (value == null && isNaN(value))
......
...@@ -4,6 +4,9 @@ define(function (require) { ...@@ -4,6 +4,9 @@ define(function (require) {
var TooltipContent = require('./tooltip/TooltipContent'); var TooltipContent = require('./tooltip/TooltipContent');
var graphic = require('../util/graphic'); var graphic = require('../util/graphic');
var zrUtil = require('zrender/core/util'); var zrUtil = require('zrender/core/util');
var formatUtil = require('../util/format');
require('./tooltip/TooltipModel');
function getAxisPointerKey(coordName, axisType) { function getAxisPointerKey(coordName, axisType) {
return coordName + axisType; return coordName + axisType;
...@@ -39,8 +42,6 @@ define(function (require) { ...@@ -39,8 +42,6 @@ define(function (require) {
}; };
} }
require('./tooltip/TooltipModel');
require('../echarts').extendComponentView({ require('../echarts').extendComponentView({
type: 'tooltip', type: 'tooltip',
...@@ -83,6 +84,7 @@ define(function (require) { ...@@ -83,6 +84,7 @@ define(function (require) {
// Only cartesian2d and polar support axis trigger // Only cartesian2d and polar support axis trigger
if (coordSysType === 'cartesian2d') { if (coordSysType === 'cartesian2d') {
// FIXME `axisPointer.axis` is not baseAxis
baseAxis = coordSys.getBaseAxis(); baseAxis = coordSys.getBaseAxis();
var baseDim = baseAxis.dim; var baseDim = baseAxis.dim;
var axisIndex = seriesModel.get(baseDim + 'AxisIndex'); var axisIndex = seriesModel.get(baseDim + 'AxisIndex');
...@@ -108,6 +110,11 @@ define(function (require) { ...@@ -108,6 +110,11 @@ define(function (require) {
}, this); }, this);
this._seriesGroupByAxis = seriesGroupByAxis; this._seriesGroupByAxis = seriesGroupByAxis;
var crossText = this._crossText;
if (crossText) {
this.group.add(crossText);
}
}, },
/** /**
...@@ -132,15 +139,19 @@ define(function (require) { ...@@ -132,15 +139,19 @@ define(function (require) {
return; return;
} }
var seriesModel = ecModel.getSeriesByIndex(el.seriesIndex);
this._showItemTooltip(seriesModel, el.dataIndex, e);
} }
else { else {
this._showAxisTooltip(e); this._showAxisTooltip(e);
} }
// Always show item tooltip if mouse is on the element with dataIndex
if (el && el.dataIndex) {
var seriesModel = ecModel.getSeriesByIndex(el.seriesIndex);
this._showItemTooltip(seriesModel, el.dataIndex, e);
}
}, },
/** /**
...@@ -179,7 +190,9 @@ define(function (require) { ...@@ -179,7 +190,9 @@ define(function (require) {
this._showPolarPointer(axisPointerModel, allCoordSys, point); this._showPolarPointer(axisPointerModel, allCoordSys, point);
} }
this._showSeriesTooltip(coordSys, item.series, point, value); if (axisPointerModel.get('type') !== 'cross') {
this._showSeriesTooltip(coordSys, item.series, point, value);
}
}, this); }, this);
}, },
...@@ -201,6 +214,8 @@ define(function (require) { ...@@ -201,6 +214,8 @@ define(function (require) {
if (axisPointerType === 'cross') { if (axisPointerType === 'cross') {
moveGridLine('x', point, cartesian.getAxis('y').getExtent()); moveGridLine('x', point, cartesian.getAxis('y').getExtent());
moveGridLine('y', point, cartesian.getAxis('x').getExtent()); moveGridLine('y', point, cartesian.getAxis('x').getExtent());
this._updateCrossText(cartesian, point, axisPointerModel);
} }
else { else {
// Use the first cartesian // Use the first cartesian
...@@ -277,8 +292,10 @@ define(function (require) { ...@@ -277,8 +292,10 @@ define(function (require) {
var radiusAxis = polar.getRadiusAxis(); var radiusAxis = polar.getRadiusAxis();
if (axisPointerType === 'cross') { if (axisPointerType === 'cross') {
movePolarLine('angle', point, angleAxis.getExtent()); movePolarLine('angle', point, radiusAxis.getExtent());
movePolarLine('radius', point, radiusAxis.getExtent()); movePolarLine('radius', point, angleAxis.getExtent());
this._updateCrossText(polar, point, axisPointerModel);
} }
else { else {
var axisType = axisPointerModel.get('axis'); var axisType = axisPointerModel.get('axis');
...@@ -358,6 +375,50 @@ define(function (require) { ...@@ -358,6 +375,50 @@ define(function (require) {
} }
}, },
_updateCrossText: function (coordSys, point, axisPointerModel) {
var crossStyleModel = axisPointerModel.getModel('crossStyle');
var textStyleModel = crossStyleModel.getModel('textStyle');
var tooltipModel = this._tooltipModel;
var text = this._crossText;
if (!text) {
text = this._crossText = new graphic.Text({
style: {
textAlign: 'left',
textBaseline: 'bottom'
}
});
this.group.add(text);
}
var value = coordSys.pointToData(point);
var dims = coordSys.dimensions;
value = zrUtil.map(value, function (val, idx) {
var axis = coordSys.getAxis(dims[idx]);
if (axis.type === 'category') {
val = axis.scale.getLabel(val);
}
else {
val = formatUtil.addCommas(
val.toFixed(axis.getFormatPrecision())
);
}
return val;
});
text.setStyle({
fill: textStyleModel.get('color') || crossStyleModel.get('color'),
textFont: textStyleModel.getFont(),
text: value.join(', '),
x: point[0] + 5,
y: point[1] - 5
});
text.z = tooltipModel.get('z');
text.zlevel = tooltipModel.get('zlevel');
},
/** /**
* Hide axis tooltip * Hide axis tooltip
*/ */
...@@ -366,7 +427,9 @@ define(function (require) { ...@@ -366,7 +427,9 @@ define(function (require) {
}, },
_getPointerElement: function (coordSys, pointerModel, axisType) { _getPointerElement: function (coordSys, pointerModel, axisType) {
var z = this._tooltipModel.get('z'); var tooltipModel = this._tooltipModel;
var z = tooltipModel.get('z');
var zlevel = tooltipModel.get('zlevel');
var axisPointers = this._axisPointers; var axisPointers = this._axisPointers;
var key = getAxisPointerKey(coordSys.name, axisType); var key = getAxisPointerKey(coordSys.name, axisType);
if (axisPointers[key]) { if (axisPointers[key]) {
...@@ -380,7 +443,7 @@ define(function (require) { ...@@ -380,7 +443,7 @@ define(function (require) {
var style = styleModel[isShadow ? 'getAreaStyle' : 'getLineStyle'](); var style = styleModel[isShadow ? 'getAreaStyle' : 'getLineStyle']();
var elementType = coordSys.type === 'polar' var elementType = coordSys.type === 'polar'
? (isShadow ? 'Sector' : 'Circle') ? (isShadow ? 'Sector' : (axisType === 'radius' ? 'Circle' : 'Line'))
: (isShadow ? 'Rect' : 'Line'); : (isShadow ? 'Rect' : 'Line');
isShadow ? (style.stroke = null) : (style.fill = null); isShadow ? (style.stroke = null) : (style.fill = null);
...@@ -388,6 +451,7 @@ define(function (require) { ...@@ -388,6 +451,7 @@ define(function (require) {
var el = axisPointers[key] = new graphic[elementType]({ var el = axisPointers[key] = new graphic[elementType]({
style: style, style: style,
z: z, z: z,
zlevel: zlevel,
silent: true silent: true
}); });
......
...@@ -80,7 +80,11 @@ define(function (require) { ...@@ -80,7 +80,11 @@ define(function (require) {
crossStyle: { crossStyle: {
color: '#1e90ff', color: '#1e90ff',
width: 1, width: 1,
type: 'dashed' type: 'dashed',
// TODO formatter
textStyle: {}
}, },
// 阴影指示器样式设置 // 阴影指示器样式设置
......
define(function (require) { define(function (require) {
var linearMap = require('../util/number').linearMap; var numberUtil = require('../util/number')
var linearMap = numberUtil.linearMap;
var zrUtil = require('zrender/core/util'); var zrUtil = require('zrender/core/util');
function fixExtentWithBands(extent, nTick) { function fixExtentWithBands(extent, nTick) {
...@@ -82,6 +83,23 @@ define(function (require) { ...@@ -82,6 +83,23 @@ define(function (require) {
return ret; return ret;
}, },
/**
* Get precision used for formatting
* @return {number}
*/
getFormatPrecision: function () {
var log = Math.log;
var LN10 = Math.LN10;
var dataExtent = this.scale.getExtent();
var extent = this._extent;
var dataQuantity = Math.floor(log(dataExtent[1] - dataExtent[0]) / LN10);
var sizeQuantity = Math.ceil(log(Math.abs(extent[1] - extent[0])) / LN10);
return Math.max(
-dataQuantity + sizeQuantity,
0
);
},
/** /**
* Set coord extent * Set coord extent
* @param {number} min * @param {number} min
......
...@@ -118,11 +118,15 @@ define(function(require) { ...@@ -118,11 +118,15 @@ define(function(require) {
var data = this._data; var data = this._data;
var value = data.getRawValue(dataIndex); var value = data.getRawValue(dataIndex);
var formattedValue = zrUtil.isArray(value) var formattedValue = zrUtil.isArray(value)
? zrUtil.map(value, addCommas) : addCommas(value); ? zrUtil.map(value, addCommas).join(', ') : addCommas(value);
var name = data.getName(dataIndex); var name = data.getName(dataIndex, true);
return !mutipleSeries ? (encodeHTML(this.name) + '<br />' return !mutipleSeries
+ encodeHTML(name) + ' : ' + formattedValue) ? (encodeHTML(this.name) + '<br />'
+ (name
? encodeHTML(name) + ' : ' + formattedValue
: formattedValue)
)
: (encodeHTML(this.name) + ' : ' + formattedValue); : (encodeHTML(this.name) + ' : ' + formattedValue);
}, },
......
...@@ -93,10 +93,10 @@ define(function (require) { ...@@ -93,10 +93,10 @@ define(function (require) {
*/ */
setExtent: function (start, end) { setExtent: function (start, end) {
var thisExtent = this._extent; var thisExtent = this._extent;
if (! isNaN(start)) { if (!isNaN(start)) {
thisExtent[0] = start; thisExtent[0] = start;
} }
if (! isNaN(end)) { if (!isNaN(end)) {
thisExtent[1] = end; thisExtent[1] = end;
} }
}, },
...@@ -158,6 +158,15 @@ define(function (require) { ...@@ -158,6 +158,15 @@ define(function (require) {
return labels; return labels;
}, },
/**
* @param {number} n
* @return {number}
*/
// FIXME addCommas
getLabel: function (data) {
return data;
},
/** /**
* Update interval and extent of intervals for nice ticks * Update interval and extent of intervals for nice ticks
* Algorithm from d3.js * Algorithm from d3.js
......
...@@ -142,6 +142,8 @@ define(function (require) { ...@@ -142,6 +142,8 @@ define(function (require) {
/** /**
* Get item on rank n * Get item on rank n
* @param {number} n
* @return {string}
*/ */
getLabel: function (n) { getLabel: function (n) {
return this._data[n]; return this._data[n];
......
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
'echarts', 'echarts',
'echarts/chart/line', 'echarts/chart/line',
'echarts/component/legend', 'echarts/component/legend',
'echarts/component/polar' 'echarts/component/polar',
'echarts/component/tooltip'
], function (echarts) { ], function (echarts) {
var chart = echarts.init(document.getElementById('main'), null, { var chart = echarts.init(document.getElementById('main'), null, {
...@@ -38,6 +39,12 @@ ...@@ -38,6 +39,12 @@
data: ['line'] data: ['line']
}, },
polar: {}, polar: {},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
}
},
angleAxis: { angleAxis: {
type: 'value' type: 'value'
}, },
......
...@@ -30,10 +30,14 @@ ...@@ -30,10 +30,14 @@
var data2 = []; var data2 = [];
var data3 = []; var data3 = [];
for (var i = 0; i < 100; i++) { var random = function (max) {
data1.push([Math.random() * 5, Math.random() * 4, Math.random().toFixed(2)]); return (Math.random() * max).toFixed(3);
data2.push([Math.random() * 10, Math.random() * 5, (Math.random() + 0.3).toFixed(2)]); }
data3.push([Math.random() * 15, Math.random() * 10, Math.random().toFixed(2)]);
for (var i = 0; i < 200; i++) {
data1.push([random(5), random(5), random(1)]);
data2.push([random(10), random(10), random(1)]);
data3.push([random(15), random(10), random(1)]);
} }
chart.setOption({ chart.setOption({
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册