Skip to content

Commit

Permalink
Code cleanup and consistency changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Roundaround committed Aug 4, 2014
1 parent 54d865a commit 1d47e15
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions js/jquery.flot.tooltip.source.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@
},

// callbacks
onHover: function(flotItem, $tooltipEl) {},
onHover: function (flotItem, $tooltipEl) {},

$compat: false
}
};

// object
var FlotTooltip = function(plot) {

var FlotTooltip = function (plot) {
// variables
this.tipPosition = {x: 0, y: 0};

this.init(plot);
};

// main plugin function
FlotTooltip.prototype.init = function(plot) {

FlotTooltip.prototype.init = function (plot) {
var that = this;

// detect other flot plugins
Expand Down Expand Up @@ -130,7 +128,7 @@
};

// Quick little function for showing the tooltip.
function showTooltip(item) {
var showTooltip = function (item) {
var $tip = that.getDomElement();

// convert tooltip content template to real tipText
Expand All @@ -150,7 +148,7 @@
}

// Quick little function for hiding the tooltip.
function hideTooltip() {
var hideTooltip = function () {
that.getDomElement().hide().html('');
}

Expand Down Expand Up @@ -227,7 +225,7 @@
* get or create tooltip DOM element
* @return jQuery object
*/
FlotTooltip.prototype.getDomElement = function() {
FlotTooltip.prototype.getDomElement = function () {
var $tip = $('#' + this.tooltipOptions.id);

if( $tip.length === 0 ){
Expand All @@ -252,7 +250,7 @@
};

// as the name says
FlotTooltip.prototype.updateTooltipPosition = function(pos) {
FlotTooltip.prototype.updateTooltipPosition = function (pos) {
var $tip = $('#' + this.tooltipOptions.id);

var totalTipWidth = $tip.outerWidth() + this.tooltipOptions.shifts.x;
Expand All @@ -273,7 +271,7 @@
* @param {object} item - Flot item
* @return {string} real tooltip content for current item
*/
FlotTooltip.prototype.stringFormat = function(content, item) {
FlotTooltip.prototype.stringFormat = function (content, item) {

var percentPattern = /%p\.{0,1}(\d{0,})/;
var seriesPattern = /%s/;
Expand Down Expand Up @@ -309,80 +307,75 @@
}

// if it is a function callback get the content string
if( typeof(content) === 'function' ) {
if (typeof(content) === 'function') {
content = content(item.series.label, x, y, item);
}

// percent match for pie charts and stacked percent
if (typeof (item.series.percent) !== 'undefined'){
if (typeof (item.series.percent) !== 'undefined') {
p = item.series.percent;
} else if (typeof (item.series.percents) !== 'undefined'){
} else if (typeof (item.series.percents) !== 'undefined') {
p = item.series.percents[item.dataIndex];
}
if( typeof p === 'number' ) {
if (typeof p === 'number') {
content = this.adjustValPrecision(percentPattern, content, p);
}

// series match
if( typeof(item.series.label) !== 'undefined' ) {
if (typeof(item.series.label) !== 'undefined') {
content = content.replace(seriesPattern, item.series.label);
}
else {
} else {
//remove %s if label is undefined
content = content.replace(seriesPattern, "");
}

// x axis label match
if( this.hasAxisLabel('xaxis', item) ) {
if (this.hasAxisLabel('xaxis', item)) {
content = content.replace(xLabelPattern, item.series.xaxis.options.axisLabel);
}
else {
} else {
//remove %lx if axis label is undefined or axislabels plugin not present
content = content.replace(xLabelPattern, "");
}

// y axis label match
if( this.hasAxisLabel('yaxis', item) ) {
if (this.hasAxisLabel('yaxis', item)) {
content = content.replace(yLabelPattern, item.series.yaxis.options.axisLabel);
}
else {
} else {
//remove %ly if axis label is undefined or axislabels plugin not present
content = content.replace(yLabelPattern, "");
}

// time mode axes with custom dateFormat
if(this.isTimeMode('xaxis', item) && this.isXDateFormat(item)) {
if (this.isTimeMode('xaxis', item) && this.isXDateFormat(item)) {
content = content.replace(xPattern, this.timestampToDate(x, this.tooltipOptions.xDateFormat, item.series.xaxis.options));
}

if(this.isTimeMode('yaxis', item) && this.isYDateFormat(item)) {
if (this.isTimeMode('yaxis', item) && this.isYDateFormat(item)) {
content = content.replace(yPattern, this.timestampToDate(y, this.tooltipOptions.yDateFormat, item.series.yaxis.options));
}

// set precision if defined
if(typeof x === 'number') {
if (typeof x === 'number') {
content = this.adjustValPrecision(xPattern, content, x);
}
if(typeof y === 'number') {
if (typeof y === 'number') {
content = this.adjustValPrecision(yPattern, content, y);
}

// change x from number to given label, if given
if(typeof item.series.xaxis.ticks !== 'undefined') {
if (typeof item.series.xaxis.ticks !== 'undefined') {

var ticks;
if(this.hasRotatedXAxisTicks(item)) {
if (this.hasRotatedXAxisTicks(item)) {
// xaxis.ticks will be an empty array if tickRotor is being used, but the values are available in rotatedTicks
ticks = 'rotatedTicks';
}
else {
} else {
ticks = 'ticks';
}

// see https://github.com/krzysu/flot.tooltip/issues/65
var tickIndex = item.dataIndex + item.seriesIndex;

if(item.series.xaxis[ticks].length > tickIndex && !this.isTimeMode('xaxis', item)) {
if (item.series.xaxis[ticks].length > tickIndex && !this.isTimeMode('xaxis', item)) {
var valueX = (this.isCategoriesMode('xaxis', item)) ? item.series.xaxis[ticks][tickIndex].label : item.series.xaxis[ticks][tickIndex].v;
if (valueX === x) {
content = content.replace(xPattern, item.series.xaxis[ticks][tickIndex].label);
Expand All @@ -391,7 +384,7 @@
}

// change y from number to given label, if given
if(typeof item.series.yaxis.ticks !== 'undefined') {
if (typeof item.series.yaxis.ticks !== 'undefined') {
for (var index in item.series.yaxis.ticks) {
if (item.series.yaxis.ticks.hasOwnProperty(index)) {
var valueY = (this.isCategoriesMode('yaxis', item)) ? item.series.yaxis.ticks[index].label : item.series.yaxis.ticks[index].v;
Expand All @@ -403,47 +396,46 @@
}

// if no value customization, use tickFormatter by default
if(typeof item.series.xaxis.tickFormatter !== 'undefined') {
if (typeof item.series.xaxis.tickFormatter !== 'undefined') {
//escape dollar
content = content.replace(xPatternWithoutPrecision, item.series.xaxis.tickFormatter(x, item.series.xaxis).replace(/\$/g, '$$'));
}
if(typeof item.series.yaxis.tickFormatter !== 'undefined') {
if (typeof item.series.yaxis.tickFormatter !== 'undefined') {
//escape dollar
content = content.replace(yPatternWithoutPrecision, item.series.yaxis.tickFormatter(y, item.series.yaxis).replace(/\$/g, '$$'));
}

if(customText) {
if (customText)
content = content.replace(customTextPattern, customText);
}

return content;
};

// helpers just for readability
FlotTooltip.prototype.isTimeMode = function(axisName, item) {
FlotTooltip.prototype.isTimeMode = function (axisName, item) {
return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'time');
};

FlotTooltip.prototype.isXDateFormat = function(item) {
FlotTooltip.prototype.isXDateFormat = function (item) {
return (typeof this.tooltipOptions.xDateFormat !== 'undefined' && this.tooltipOptions.xDateFormat !== null);
};

FlotTooltip.prototype.isYDateFormat = function(item) {
FlotTooltip.prototype.isYDateFormat = function (item) {
return (typeof this.tooltipOptions.yDateFormat !== 'undefined' && this.tooltipOptions.yDateFormat !== null);
};

FlotTooltip.prototype.isCategoriesMode = function(axisName, item) {
FlotTooltip.prototype.isCategoriesMode = function (axisName, item) {
return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'categories');
};

//
FlotTooltip.prototype.timestampToDate = function(tmst, dateFormat, options) {
FlotTooltip.prototype.timestampToDate = function (tmst, dateFormat, options) {
var theDate = $.plot.dateGenerator(tmst, options);
return $.plot.formatDate(theDate, dateFormat, this.tooltipOptions.monthNames, this.tooltipOptions.dayNames);
};

//
FlotTooltip.prototype.adjustValPrecision = function(pattern, content, value) {
FlotTooltip.prototype.adjustValPrecision = function (pattern, content, value) {

var precision;
var matchResult = content.match(pattern);
Expand All @@ -462,17 +454,17 @@
// other plugins detection below

// check if flot-axislabels plugin (https://github.com/markrcote/flot-axislabels) is used and that an axis label is given
FlotTooltip.prototype.hasAxisLabel = function(axisName, item) {
FlotTooltip.prototype.hasAxisLabel = function (axisName, item) {
return ($.inArray(this.plotPlugins, 'axisLabels') !== -1 && typeof item.series[axisName].options.axisLabel !== 'undefined' && item.series[axisName].options.axisLabel.length > 0);
};

// check whether flot-tickRotor, a plugin which allows rotation of X-axis ticks, is being used
FlotTooltip.prototype.hasRotatedXAxisTicks = function(item) {
return ($.grep($.plot.plugins, function(p){ return p.name === "tickRotor"; }).length === 1 && typeof item.series.xaxis.rotatedTicks !== 'undefined');
FlotTooltip.prototype.hasRotatedXAxisTicks = function (item) {
return ($.inArray(this.plotPlugins, 'tickRotor') !== -1 && typeof item.series.xaxis.rotatedTicks !== 'undefined');
};

//
var init = function(plot) {
var init = function (plot) {
new FlotTooltip(plot);
};

Expand Down

0 comments on commit 1d47e15

Please sign in to comment.