Skip to content

Commit dfc7ac7

Browse files
Merge pull request #5 from ClockworkRecruiting/fix-tooltip-position
2 parents 2149d83 + 57212b1 commit dfc7ac7

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

react-c3js.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,52 @@ var C3Chart = function (_React$Component) {
4141
key: 'componentDidMount',
4242
value: function componentDidMount() {
4343
c3 = require('c3');
44+
45+
// function override
46+
c3.chart.internal.fn.tooltipPosition = function (dataToShow, tWidth, tHeight, element) {
47+
var $$ = this,
48+
config = $$.config,
49+
d3 = $$.d3;
50+
var svgLeft, tooltipLeft, tooltipRight, tooltipTop, chartRight;
51+
var forArc = $$.hasArcType(),
52+
mouse = d3.mouse(element);
53+
if (navigator.userAgent.indexOf("Firefox") != -1) {
54+
// this works in Firefox
55+
mouse = [d3.event.offsetX - 80, d3.event.offsetY - 80];
56+
}
57+
// Determin tooltip position
58+
if (forArc) {
59+
tooltipLeft = ($$.width - ($$.isLegendRight ? $$.getLegendWidth() : 0)) / 2 + mouse[0];
60+
tooltipTop = $$.height / 2 + mouse[1] + 20;
61+
} else {
62+
svgLeft = $$.getSvgLeft(true);
63+
if (config.axis_rotated) {
64+
tooltipLeft = svgLeft + mouse[0] + 100;
65+
tooltipRight = tooltipLeft + tWidth;
66+
chartRight = $$.currentWidth - $$.getCurrentPaddingRight();
67+
tooltipTop = $$.x(dataToShow[0].x) + 20;
68+
} else {
69+
tooltipLeft = svgLeft + $$.getCurrentPaddingLeft(true) + $$.x(dataToShow[0].x) + 20;
70+
tooltipRight = tooltipLeft + tWidth;
71+
chartRight = svgLeft + $$.currentWidth - $$.getCurrentPaddingRight();
72+
tooltipTop = mouse[1] + 15;
73+
}
74+
75+
if (tooltipRight > chartRight) {
76+
// 20 is needed for Firefox to keep tooltip width
77+
tooltipLeft -= tooltipRight - chartRight + 20;
78+
}
79+
if (tooltipTop + tHeight > $$.currentHeight) {
80+
tooltipTop -= tHeight + 30;
81+
}
82+
}
83+
if (tooltipTop < 0) {
84+
tooltipTop = 0;
85+
}
86+
87+
return { top: tooltipTop, left: tooltipLeft };
88+
};
89+
4490
this.updateChart(this.props);
4591
}
4692
}, {

src/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,52 @@ class C3Chart extends React.Component {
4646

4747
componentDidMount() {
4848
c3 = require('c3');
49+
50+
// function override
51+
c3.chart.internal.fn.tooltipPosition = function (dataToShow, tWidth, tHeight, element) {
52+
var $$ = this,
53+
config = $$.config,
54+
d3 = $$.d3;
55+
var svgLeft, tooltipLeft, tooltipRight, tooltipTop, chartRight;
56+
var forArc = $$.hasArcType(),
57+
mouse = d3.mouse(element);
58+
if (navigator.userAgent.indexOf("Firefox") != -1) {
59+
// this works in Firefox
60+
mouse = [d3.event.offsetX - 80, d3.event.offsetY - 80];
61+
}
62+
// Determin tooltip position
63+
if (forArc) {
64+
tooltipLeft = ($$.width - ($$.isLegendRight ? $$.getLegendWidth() : 0)) / 2 + mouse[0];
65+
tooltipTop = $$.height / 2 + mouse[1] + 20;
66+
} else {
67+
svgLeft = $$.getSvgLeft(true);
68+
if (config.axis_rotated) {
69+
tooltipLeft = svgLeft + mouse[0] + 100;
70+
tooltipRight = tooltipLeft + tWidth;
71+
chartRight = $$.currentWidth - $$.getCurrentPaddingRight();
72+
tooltipTop = $$.x(dataToShow[0].x) + 20;
73+
} else {
74+
tooltipLeft = svgLeft + $$.getCurrentPaddingLeft(true) + $$.x(dataToShow[0].x) + 20;
75+
tooltipRight = tooltipLeft + tWidth;
76+
chartRight = svgLeft + $$.currentWidth - $$.getCurrentPaddingRight();
77+
tooltipTop = mouse[1] + 15;
78+
}
79+
80+
if (tooltipRight > chartRight) {
81+
// 20 is needed for Firefox to keep tooltip width
82+
tooltipLeft -= tooltipRight - chartRight + 20;
83+
}
84+
if (tooltipTop + tHeight > $$.currentHeight) {
85+
tooltipTop -= tHeight + 30;
86+
}
87+
}
88+
if (tooltipTop < 0) {
89+
tooltipTop = 0;
90+
}
91+
92+
return { top: tooltipTop, left: tooltipLeft };
93+
};
94+
4995
this.updateChart(this.props);
5096
}
5197

0 commit comments

Comments
 (0)