Skip to content

Commit 0da06d8

Browse files
committed
tweak tooltip confine.
1 parent dae94c9 commit 0da06d8

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

src/component/tooltip/TooltipView.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,9 @@ define(function (require) {
699699
}
700700

701701
function refixTooltipPosition(x, y, el, viewWidth, viewHeight, gapH, gapV) {
702-
var width = el.offsetWidth;
703-
var height = el.offsetHeight;
702+
var size = getOuterSize(el);
703+
var width = size.width;
704+
var height = size.height;
704705

705706
if (gapH != null) {
706707
if (x + width + gapH > viewWidth) {
@@ -722,8 +723,9 @@ define(function (require) {
722723
}
723724

724725
function confineTooltipPosition(x, y, el, viewWidth, viewHeight) {
725-
var width = el.clientWidth;
726-
var height = el.clientHeight;
726+
var size = getOuterSize(el);
727+
var width = size.width;
728+
var height = size.height;
727729

728730
x = Math.min(x + width, viewWidth) - width;
729731
y = Math.min(y + height, viewHeight) - height;
@@ -733,6 +735,25 @@ define(function (require) {
733735
return [x, y];
734736
}
735737

738+
function getOuterSize(el) {
739+
var width = el.clientWidth;
740+
var height = el.clientHeight;
741+
742+
// Consider browser compatibility.
743+
// IE8 does not support getComputedStyle.
744+
if (document.defaultView.getComputedStyle) {
745+
var stl = document.defaultView.getComputedStyle(el);
746+
if (stl) {
747+
width += parseInt(stl.paddingLeft, 10) + parseInt(stl.paddingRight, 10)
748+
+ parseInt(stl.borderLeftWidth, 10) + parseInt(stl.borderRightWidth, 10);
749+
height += parseInt(stl.paddingTop, 10) + parseInt(stl.paddingBottom, 10)
750+
+ parseInt(stl.borderTopWidth, 10) + parseInt(stl.borderBottomWidth, 10);
751+
}
752+
}
753+
754+
return {width: width, height: height};
755+
}
756+
736757
function calcTooltipPosition(position, rect, contentSize) {
737758
var domWidth = contentSize[0];
738759
var domHeight = contentSize[1];

test/tooltip.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ <h1>showDelay: 50 | enterable | dynamic callback</h1>
3838
<div class="chart" id="main"></div>
3939
<h1>set position</h1>
4040
<div class="chart" id="position"></div>
41+
<h1>confine</h1>
42+
<div class="chart" id="confine"></div>
4143
<h1>triggerOn click</h1>
4244
<div class="chart" id="click"></div>
4345
<h1>dispatchAciton manually with `position` specified (postion: [10,10])</h1>
@@ -602,6 +604,69 @@ <h1>dispatchAciton manually with `position` specified (postion: [10,10])</h1>
602604

603605

604606

607+
608+
<script>
609+
610+
require([
611+
'echarts',
612+
'echarts/chart/pie',
613+
'echarts/component/legend',
614+
'echarts/component/grid',
615+
'echarts/component/tooltip',
616+
'zrender/vml/vml'
617+
], function (echarts) {
618+
619+
var option = {
620+
tooltip: {
621+
confine: true,
622+
borderWidth: 10,
623+
borderColor: 'green',
624+
padding: 20,
625+
formatter: "{a} <br/>{b} : {c} ({d}%)"
626+
},
627+
series: {
628+
name: '访问来源',
629+
type: 'pie',
630+
radius : 1180,
631+
data:[
632+
{
633+
value:335, name:'position: [300, 100]',
634+
},
635+
{
636+
value:310, name:'position: function [p[0] + 10, p[1] - 50]',
637+
},
638+
{
639+
value:234, name:'position: inside',
640+
},
641+
{
642+
value:135, name:'align: right; verticalAlign: bottom',
643+
tooltip: {
644+
align: 'right',
645+
verticalAlign: 'bottom'
646+
}
647+
},
648+
{
649+
value:548, name:'position: [30, 140], verticalAlign: bottom',
650+
}
651+
],
652+
itemStyle: {
653+
emphasis: {
654+
shadowBlur: 10,
655+
shadowOffsetX: 0,
656+
shadowColor: 'rgba(0, 0, 0, 0.5)'
657+
}
658+
}
659+
}
660+
};
661+
662+
createChart('confine', echarts, option, 500);
663+
})
664+
</script>
665+
666+
667+
668+
669+
605670
<script>
606671

607672
require([

0 commit comments

Comments
 (0)