Quite good looking graph derived from d3.js http://c3js.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
7.9 KiB

import CLASS from './class';
import {
ChartInternal
} from './core';
import {
isValue,
isFunction,
isArray,
isString,
sanitise
} from './util';
ChartInternal.prototype.initTooltip = function () {
var $$ = this,
config = $$.config,
i;
$$.tooltip = $$.selectChart
.style("position", "relative")
.append("div")
.attr('class', CLASS.tooltipContainer)
.style("position", "absolute")
.style("pointer-events", "none")
.style("display", "none");
// Show tooltip if needed
if (config.tooltip_init_show) {
if ($$.isTimeSeries() && isString(config.tooltip_init_x)) {
config.tooltip_init_x = $$.parseDate(config.tooltip_init_x);
for (i = 0; i < $$.data.targets[0].values.length; i++) {
if (($$.data.targets[0].values[i].x - config.tooltip_init_x) === 0) {
break;
}
}
config.tooltip_init_x = i;
}
$$.tooltip.html(config.tooltip_contents.call($$, $$.data.targets.map(function (d) {
return $$.addName(d.values[config.tooltip_init_x]);
}), $$.axis.getXAxisTickFormat(), $$.getYFormat($$.hasArcType()), $$.color));
$$.tooltip.style("top", config.tooltip_init_position.top)
.style("left", config.tooltip_init_position.left)
.style("display", "block");
}
};
ChartInternal.prototype.getTooltipSortFunction = function () {
var $$ = this,
config = $$.config;
if (config.data_groups.length === 0 || config.tooltip_order !== undefined) {
// if data are not grouped or if an order is specified
// for the tooltip values we sort them by their values
var order = config.tooltip_order;
if (order === undefined) {
order = config.data_order;
}
var valueOf = function (obj) {
return obj ? obj.value : null;
};
// if data are not grouped, we sort them by their value
if (isString(order) && order.toLowerCase() === 'asc') {
return function (a, b) {
return valueOf(a) - valueOf(b);
};
} else if (isString(order) && order.toLowerCase() === 'desc') {
return function (a, b) {
return valueOf(b) - valueOf(a);
};
} else if (isFunction(order)) {
// if the function is from data_order we need
// to wrap the returned function in order to format
// the sorted value to the expected format
var sortFunction = order;
if (config.tooltip_order === undefined) {
sortFunction = function (a, b) {
return order(a ? {
id: a.id,
values: [a]
} : null, b ? {
id: b.id,
values: [b]
} : null);
};
}
return sortFunction;
} else if (isArray(order)) {
return function (a, b) {
return order.indexOf(a.id) - order.indexOf(b.id);
};
}
} else {
// if data are grouped, we follow the order of grouped targets
var ids = $$.orderTargets($$.data.targets).map(function (i) {
return i.id;
});
// if it was either asc or desc we need to invert the order
// returned by orderTargets
if ($$.isOrderAsc() || $$.isOrderDesc()) {
ids = ids.reverse();
}
return function (a, b) {
return ids.indexOf(a.id) - ids.indexOf(b.id);
};
}
};
ChartInternal.prototype.getTooltipContent = function (d, defaultTitleFormat, defaultValueFormat, color) {
var $$ = this,
config = $$.config,
titleFormat = config.tooltip_format_title || defaultTitleFormat,
nameFormat = config.tooltip_format_name || function (name) {
return name;
},
valueFormat = config.tooltip_format_value || defaultValueFormat,
text, i, title, value, name, bgcolor;
var tooltipSortFunction = this.getTooltipSortFunction();
if (tooltipSortFunction) {
d.sort(tooltipSortFunction);
}
for (i = 0; i < d.length; i++) {
if (!(d[i] && (d[i].value || d[i].value === 0))) {
continue;
}
if (!text) {
title = sanitise(titleFormat ? titleFormat(d[i].x) : d[i].x);
text = "<table class='" + $$.CLASS.tooltip + "'>" + (title || title === 0 ? "<tr><th colspan='2'>" + title + "</th></tr>" : "");
}
value = sanitise(valueFormat(d[i].value, d[i].ratio, d[i].id, d[i].index, d));
if (value !== undefined) {
// Skip elements when their name is set to null
if (d[i].name === null) {
continue;
}
name = sanitise(nameFormat(d[i].name, d[i].ratio, d[i].id, d[i].index));
bgcolor = $$.levelColor ? $$.levelColor(d[i].value) : color(d[i].id);
text += "<tr class='" + $$.CLASS.tooltipName + "-" + $$.getTargetSelectorSuffix(d[i].id) + "'>";
text += "<td class='name'><span style='background-color:" + bgcolor + "'></span>" + name + "</td>";
text += "<td class='value'>" + value + "</td>";
text += "</tr>";
}
}
return text + "</table>";
};
ChartInternal.prototype.tooltipPosition = function (dataToShow, tWidth, tHeight, element) {
var $$ = this,
config = $$.config,
d3 = $$.d3;
var svgLeft, tooltipLeft, tooltipRight, tooltipTop, chartRight;
var forArc = $$.hasArcType(),
mouse = d3.mouse(element);
// Determin tooltip position
if (forArc) {
tooltipLeft = (($$.width - ($$.isLegendRight ? $$.getLegendWidth() : 0)) / 2) + mouse[0];
Squashed commit of the following: commit 547311d30ca334c4bcd1c51cf13fa20ae28eb740 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Jun 23 14:35:09 2017 +0200 for multi-arc-gauge the radius has changed to let the label fit in the chart commit 43ba49dd154995bdfac4ac5d756076616824ae8f Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Jun 23 13:00:29 2017 +0200 fixed warnings of travis check commit 7f5a025de45b06683f08866da2e9680dd149422f Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Thu Feb 4 15:14:34 2016 +0100 rebased, conflict resolved, bugfixed, refactored and tests added commit 013f0666a7e0583a698667a0b8a6702490804ea0 Merge: ff175d1 c343243 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Jun 23 12:40:39 2017 +0200 Merge remote-tracking branch 'upstream/master' into feature/multi-arc-gauge # Conflicts: # src/arc.js # src/data.js commit ff175d15ea04786b72303d76d2b74ed28d5731f6 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Thu Feb 4 15:14:34 2016 +0100 optimized position of arc label line commit 0bf2cefff3644852119f5f739208b46779d0db42 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Thu Feb 4 14:44:50 2016 +0100 wrong tooltip position for gauges fixed commit 155c08c045421775a9b2c378c3e1e3d83c651091 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Thu Feb 4 14:49:25 2016 +0100 also hide arc label line when gauge_label_show is false commit e145b5c2a5878abe60d2b5a68aeca060778917bd Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Aug 28 09:36:31 2015 +0200 draw line from arc end to its label to highlight the relation commit 28616bf8104ec261ae8c7ee76a9240ef84bc9688 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Aug 28 09:36:31 2015 +0200 add background for every single arc commit cb2aaf16721d2e32ce7aec0a3213d382d7842f72 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Aug 28 09:36:31 2015 +0200 label placement for more than one arc commit 6ecc60987826c2d095406a5d26dfdb914b90d5df Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Fri Aug 28 09:36:31 2015 +0200 gauge_arcs_minWidth property to determine the minimal width of gaugearcs until the innerRadius disappears commit 5689dee384e02faf35f5de085a404d0970fdb9dc Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Thu Aug 27 10:24:18 2015 +0200 visibleTargetCount for cleaner arc calculation commit 9a2686c37f6dcae833fe32f793280bd77479e564 Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Thu Aug 27 10:21:13 2015 +0200 more than one arc are possible for gauges commit 0866234001c25ce81a4d7d25804b74e0a9e5d83f Author: Gökhan Özen <Goekhan.Oezen@hksinformatik.de> Date: Mon Aug 10 17:07:48 2015 +0200 show legend for gauges which have more than one arc
7 years ago
tooltipTop = ($$.hasType('gauge') ? $$.height : $$.height / 2) + mouse[1] + 20;
} else {
svgLeft = $$.getSvgLeft(true);
if (config.axis_rotated) {
tooltipLeft = svgLeft + mouse[0] + 100;
tooltipRight = tooltipLeft + tWidth;
chartRight = $$.currentWidth - $$.getCurrentPaddingRight();
tooltipTop = $$.x(dataToShow[0].x) + 20;
} else {
tooltipLeft = svgLeft + $$.getCurrentPaddingLeft(true) + $$.x(dataToShow[0].x) + 20;
tooltipRight = tooltipLeft + tWidth;
chartRight = svgLeft + $$.currentWidth - $$.getCurrentPaddingRight();
tooltipTop = mouse[1] + 15;
}
if (tooltipRight > chartRight) {
10 years ago
// 20 is needed for Firefox to keep tooltip width
tooltipLeft -= tooltipRight - chartRight + 20;
}
if (tooltipTop + tHeight > $$.currentHeight) {
tooltipTop -= tHeight + 30;
}
}
if (tooltipTop < 0) {
tooltipTop = 0;
}
return {
top: tooltipTop,
left: tooltipLeft
};
};
ChartInternal.prototype.showTooltip = function (selectedData, element) {
var $$ = this,
config = $$.config;
var tWidth, tHeight, position;
var forArc = $$.hasArcType(),
dataToShow = selectedData.filter(function (d) {
return d && isValue(d.value);
}),
positionFunction = config.tooltip_position || ChartInternal.prototype.tooltipPosition;
if (dataToShow.length === 0 || !config.tooltip_show) {
return;
}
$$.tooltip.html(config.tooltip_contents.call($$, selectedData, $$.axis.getXAxisTickFormat(), $$.getYFormat(forArc), $$.color)).style("display", "block");
// Get tooltip dimensions
tWidth = $$.tooltip.property('offsetWidth');
tHeight = $$.tooltip.property('offsetHeight');
position = positionFunction.call(this, dataToShow, tWidth, tHeight, element);
// Set tooltip
$$.tooltip
.style("top", position.top + "px")
.style("left", position.left + 'px');
};
ChartInternal.prototype.hideTooltip = function () {
this.tooltip.style("display", "none");
};