`
xianbin
  • 浏览: 214070 次
  • 来自: ...
社区版块
存档分类
最新评论

对jqplot的CategoryAxisRenderer插件进行扩展,底部Tick在鼠标在上时浮动显示名称。

阅读更多
在前一篇文章中介绍了如何让jqplot的柱形图tick下标出现省略号,参考:
《对jqplot的CategoryAxisRenderer插件进行扩展,让文字出现省略号》
http://xianbin.iteye.com/blog/1701667

下面介绍如何扩展jqplot,让其实现鼠标移动到下标tick的时候出现浮动提示框,显示完整的tick名称。

文件命名为:jqplot.categoryAxisRenderer.extend.js
在引入jqplot.categoryAxisRenderer.js之后,再引入该js,以便覆盖原有的实现。下面代码已经实现了将tick下标显示为带省略号的文字并出现浮动提示框。





/**
 * 本扩展插件是对jqplot插件jqplot.categoryAxisRenderer的扩展,主要修改了显示图形下标Tick的处理,
 * 在显示Tick之前,判断Tick标签的长度,当标题长度超过tickMaxLength定义的长度时,截获前
 * tickMaxLength - 1个字符,然后加上省略号。
 * 
 * 作者:suxianbin
 */
(function($) {
    $.jqplot.CategoryAxisRenderer.prototype.createTicks = function() {
        // we're are operating on an axis here
        var ticks = this._ticks;
        var userTicks = this.ticks;
        var name = this.name;
        // databounds were set on axis initialization.
        var db = this._dataBounds;
        var dim, interval;
        var min, max;
        var pos1, pos2;
        var tt, i;
        
        // X轴的标题文字最大长度
        var tickMaxLength = this.tickMaxLength;

        // if we already have ticks, use them.
        if (userTicks.length) {
            // adjust with blanks if we have groups
            if (this.groups > 1 && !this._grouped) {
                var l = userTicks.length;
                var skip = parseInt(l/this.groups, 10);
                var count = 0;
                for (var i=skip; i<l; i+=skip) {
                    userTicks.splice(i+count, 0, ' ');
                    count++;
                }
                this._grouped = true;
            }
            this.min = 0.5;
            this.max = userTicks.length + 0.5;
            var range = this.max - this.min;
            this.numberTicks = 2*userTicks.length + 1;
            for (i=0; i<userTicks.length; i++){
                tt = this.min + 2 * i * range / (this.numberTicks-1);
                // need a marker before and after the tick
                var t = new this.tickRenderer(this.tickOptions);
                t.showLabel = false;
                // t.showMark = true;
                t.setTick(tt, this.name);
                this._ticks.push(t);
                var t = new this.tickRenderer(this.tickOptions);
                
                // 当标题长度超过tickMaxLength定义的长度时,截获前tickMaxLength - 1个字符,
                // 然后加上省略号。
				if (tickMaxLength) {
					var userTick = userTicks[i].length <= tickMaxLength ? userTicks[i]
							: userTicks[i].substring(0, (tickMaxLength - 1)) + '...';
					t.label = userTick;
					t.userTick = userTicks[i];
				} else {
					t.label = userTicks[i];
				}
                
                // t.showLabel = true;
                t.showMark = false;
                t.showGridline = false;
                t.setTick(tt+0.5, this.name);
                this._ticks.push(t);
            }
            // now add the last tick at the end
            var t = new this.tickRenderer(this.tickOptions);
            t.showLabel = false;
            // t.showMark = true;
            t.setTick(tt+1, this.name);
            this._ticks.push(t);
        }

        // we don't have any ticks yet, let's make some!
        else {
            if (name == 'xaxis' || name == 'x2axis') {
                dim = this._plotDimensions.width;
            }
            else {
                dim = this._plotDimensions.height;
            }
            
            // if min, max and number of ticks specified, user can't specify interval.
            if (this.min != null && this.max != null && this.numberTicks != null) {
                this.tickInterval = null;
            }
            
            // if max, min, and interval specified and interval won't fit, ignore interval.
            if (this.min != null && this.max != null && this.tickInterval != null) {
                if (parseInt((this.max-this.min)/this.tickInterval, 10) != (this.max-this.min)/this.tickInterval) {
                    this.tickInterval = null;
                }
            }
        
            // find out how many categories are in the lines and collect labels
            var labels = [];
            var numcats = 0;
            var min = 0.5;
            var max, val;
            var isMerged = false;
            for (var i=0; i<this._series.length; i++) {
                var s = this._series[i];
                for (var j=0; j<s.data.length; j++) {
                    if (this.name == 'xaxis' || this.name == 'x2axis') {
                        val = s.data[j][0];
                    }
                    else {
                        val = s.data[j][1];
                    }
                    if ($.inArray(val, labels) == -1) {
                        isMerged = true;
                        numcats += 1;      
                        labels.push(val);
                    }
                }
            }
            
            if (isMerged && this.sortMergedLabels) {
                labels.sort(function(a,b) { return a - b; });
            }
            
            // keep a reference to these tick labels to use for redrawing plot (see bug #57)
            this.ticks = labels;
            
            // now bin the data values to the right lables.
            for (var i=0; i<this._series.length; i++) {
                var s = this._series[i];
                for (var j=0; j<s.data.length; j++) {
                    if (this.name == 'xaxis' || this.name == 'x2axis') {
                        val = s.data[j][0];
                    }
                    else {
                        val = s.data[j][1];
                    }
                    // for category axis, force the values into category bins.
                    // we should have the value in the label array now.
                    var idx = $.inArray(val, labels)+1;
                    if (this.name == 'xaxis' || this.name == 'x2axis') {
                        s.data[j][0] = idx;
                    }
                    else {
                        s.data[j][1] = idx;
                    }
                }
            }
            
            // adjust with blanks if we have groups
            if (this.groups > 1 && !this._grouped) {
                var l = labels.length;
                var skip = parseInt(l/this.groups, 10);
                var count = 0;
                for (var i=skip; i<l; i+=skip+1) {
                    labels[i] = ' ';
                }
                this._grouped = true;
            }
        
            max = numcats + 0.5;
            if (this.numberTicks == null) {
                this.numberTicks = 2*numcats + 1;
            }

            var range = max - min;
            this.min = min;
            this.max = max;
            var track = 0;
            
            // todo: adjust this so more ticks displayed.
            var maxVisibleTicks = parseInt(3+dim/10, 10);
            var skip = parseInt(numcats/maxVisibleTicks, 10);

            if (this.tickInterval == null) {

                this.tickInterval = range / (this.numberTicks-1);

            }
            // if tickInterval is specified, we will ignore any computed maximum.
            for (var i=0; i<this.numberTicks; i++){
                tt = this.min + i * this.tickInterval;
                var t = new this.tickRenderer(this.tickOptions);
                // if even tick, it isn't a category, it's a divider
                if (i/2 == parseInt(i/2, 10)) {
                    t.showLabel = false;
                    t.showMark = true;
                }
                else {
                    if (skip>0 && track<skip) {
                        t.showLabel = false;
                        track += 1;
                    }
                    else {
                        t.showLabel = true;
                        track = 0;
                    } 
                    t.label = t.formatter(t.formatString, labels[(i-1)/2]);
                    t.showMark = false;
                    t.showGridline = false;
                }
                t.setTick(tt, this.name);
                this._ticks.push(t);
            }
        }
    };
    
    
    // called with scope of axis
    $.jqplot.CategoryAxisRenderer.prototype.pack = function(pos, offsets) {
        var ticks = this._ticks;
        var max = this.max;
        var min = this.min;
        var offmax = offsets.max;
        var offmin = offsets.min;
        var lshow = (this._label == null) ? false : this._label.show;
        var i;
		
        for (var p in pos) {
            this._elem.css(p, pos[p]);
        }
        
        this._offsets = offsets;
        // pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
        var pixellength = offmax - offmin;
        var unitlength = max - min;
        
        // point to unit and unit to point conversions references to Plot DOM element top left corner.
        this.p2u = function(p){
            return (p - offmin) * unitlength / pixellength + min;
        };
        
        this.u2p = function(u){
            return (u - min) * pixellength / unitlength + offmin;
        };
                
        if (this.name == 'xaxis' || this.name == 'x2axis'){
            this.series_u2p = function(u){
                return (u - min) * pixellength / unitlength;
            };
            this.series_p2u = function(p){
                return p * unitlength / pixellength + min;
            };
        }
        
        else {
            this.series_u2p = function(u){
                return (u - max) * pixellength / unitlength;
            };
            this.series_p2u = function(p){
                return p * unitlength / pixellength + max;
            };
        }
        
        if (this.show) {
            if (this.name == 'xaxis' || this.name == 'x2axis') {
                for (i=0; i<ticks.length; i++) {
                    var t = ticks[i];
                    if (t.show && t.showLabel) {
                        var shim;
                        
                        if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
                            // will need to adjust auto positioning based on which axis this is.
                            var temp = (this.name == 'xaxis') ? 1 : -1;
                            switch (t.labelPosition) {
                                case 'auto':
                                    // position at end
                                    if (temp * t.angle < 0) {
                                        shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
                                    }
                                    // position at start
                                    else {
                                        shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
                                    }
                                    break;
                                case 'end':
                                    shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
                                    break;
                                case 'start':
                                    shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
                                    break;
                                case 'middle':
                                    shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
                                    break;
                                default:
                                    shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
                                    break;
                            }
                        }
                        else {
                            shim = -t.getWidth()/2;
                        }
                        var val = this.u2p(t.value) + shim + 'px';
                        t._elem.css('left', val);
                        t.pack();

                        /*
                         * 增加X轴Tick的弹出提醒框,内容为下标的全称。
                         */
                        t._elem.css('cursor', 'default');
                        t._elem.css('z-index', 888);
                        t._elem.attr('userTick', t.userTick);
                        
                        t._elem.mouseover(function (e) {
                        	var userTick = $(this).attr('usertick');
                        	_showUserTickTip(userTick, e);
                        });
                        
                        t._elem.mouseout(function () {
                        	_hideUserTickTip();
                        });
                    }
                }
                
                var labeledge=['bottom', 0];
                if (lshow) {
                    var w = this._label._elem.outerWidth(true);
                    this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
                    if (this.name == 'xaxis') {
                        this._label._elem.css('bottom', '0px');
                        labeledge = ['bottom', this._label._elem.outerHeight(true)];
                    }
                    else {
                        this._label._elem.css('top', '0px');
                        labeledge = ['top', this._label._elem.outerHeight(true)];
                    }
                    this._label.pack();
                }
                
                // draw the group labels
                var step = parseInt(this._ticks.length/this.groups, 10);
                for (i=0; i<this._groupLabels.length; i++) {
                    var mid = 0;
                    var count = 0;
                    for (var j=i*step; j<=(i+1)*step; j++) {
                        if (this._ticks[j]._elem && this._ticks[j].label != " ") {
                            var t = this._ticks[j]._elem;
                            var p = t.position();
                            mid += p.left + t.outerWidth(true)/2;
                            count++;
                        }
                    }
                    mid = mid/count;
                    this._groupLabels[i].css({'left':(mid - this._groupLabels[i].outerWidth(true)/2)});
                    this._groupLabels[i].css(labeledge[0], labeledge[1]);
                }
            }
            else {
                for (i=0; i<ticks.length; i++) {
                    var t = ticks[i];
                    if (t.show && t.showLabel) {                        
                        var shim;
                        if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
                            // will need to adjust auto positioning based on which axis this is.
                            var temp = (this.name == 'yaxis') ? 1 : -1;
                            switch (t.labelPosition) {
                                case 'auto':
                                    // position at end
                                case 'end':
                                    if (temp * t.angle < 0) {
                                        shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
                                    }
                                    else {
                                        shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
                                    }
                                    break;
                                case 'start':
                                    if (t.angle > 0) {
                                        shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
                                    }
                                    else {
                                        shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
                                    }
                                    break;
                                case 'middle':
                                    // if (t.angle > 0) {
                                    //     shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
                                    // }
                                    // else {
                                    //     shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
                                    // }
                                    shim = -t.getHeight()/2;
                                    break;
                                default:
                                    shim = -t.getHeight()/2;
                                    break;
                            }
                        }
                        else {
                            shim = -t.getHeight()/2;
                        }
                        
                        var val = this.u2p(t.value) + shim + 'px';
                        t._elem.css('top', val);
                        t.pack();
                    }
                }
                
                var labeledge=['left', 0];
                if (lshow) {
                    var h = this._label._elem.outerHeight(true);
                    this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
                    if (this.name == 'yaxis') {
                        this._label._elem.css('left', '0px');
                        labeledge = ['left', this._label._elem.outerWidth(true)];
                    }
                    else {
                        this._label._elem.css('right', '0px');
                        labeledge = ['right', this._label._elem.outerWidth(true)];
                    }   
                    this._label.pack();
                }
                
                // draw the group labels, position top here, do left after label position.
                var step = parseInt(this._ticks.length/this.groups, 10);
                for (i=0; i<this._groupLabels.length; i++) {
                    var mid = 0;
                    var count = 0;
                    for (var j=i*step; j<=(i+1)*step; j++) {
                        if (this._ticks[j]._elem && this._ticks[j].label != " ") {
                            var t = this._ticks[j]._elem;
                            var p = t.position();
                            mid += p.top + t.outerHeight()/2;
                            count++;
                        }
                    }
                    mid = mid/count;
                    this._groupLabels[i].css({'top':mid - this._groupLabels[i].outerHeight()/2});
                    this._groupLabels[i].css(labeledge[0], labeledge[1]);
                    
                }
            }
        }
    };

    // 显示Tick下标全称的提示框
    function _showUserTickTip(userTick, event) {
		var containerId = 'userTickTipDiv';
		var container = $('#' + containerId);
		if (container == undefined || container == null || container.length == 0) {
			container = $('<div class="jqplot-cursor-tooltip chartDetailDiv"></div>');
			container.attr('id', containerId);

			var table = '<table class="jqplot-highlighter">'
					+ '<tr><td><span id="userTickValue"></span></td></tr>'
					+ '</table>';
			$(table).appendTo(container);
			container.appendTo('body');
		}
		
		$('#userTickValue').text(userTick);
		
		// 计算显示框的位置,如果超过右边边界则在左边显示
		var left = event.pageX;
		var detailPopDivWidth = container.width();
		var docWidth = $(window).width();
		var leftX = left + detailPopDivWidth + 10; // 10 表示与边界的间隔
		if (leftX > docWidth) {
			left = event.pageX - detailPopDivWidth;
		}

		container.css({
			display : 'block',
			left : left,
			top : event.pageY - 25
		});
	}
    
    // 隐藏Tick下标全称的提示框
    function _hideUserTickTip() {
		$('#userTickTipDiv').css({
			display : 'none'
		});
    }

})(jQuery);
  • 大小: 6.2 KB
分享到:
评论

相关推荐

    使用Timer插件 实现鼠标坐标显示

    在C# Winform应用程序中,实现鼠标坐标的动态显示,我们可以利用Timer控件来定期获取鼠标的位置信息,并将其更新到界面上。Timer插件在Windows Forms应用程序中是一种非常实用的工具,它可以在指定的时间间隔内触发...

    jQuery插件 Jqplot图表实例

    通过以上介绍,我们了解了如何使用 Jqplot 创建一个基本的柱状图,并对图表进行了一些基本的配置。此外,还介绍了如何进一步扩展图表的功能。Jqplot 是一个非常强大的图表绘制工具,适合于需要复杂图表展示的项目。...

    winfrom Timer控件实现时间浮动显示【源码】

    在Windows Forms(WinForm)开发中,`Timer`控件...总结,WinForm的Timer控件在实时更新UI元素方面非常有用,如在本例中用于显示浮动时间。通过理解和实践这样的源码,开发者可以更好地掌握WinForm应用的动态交互设计。

    VB实现的浮动窗体,mousemove触发窗体出现

    - 为了实现在鼠标移动时显示窗体,我们需要处理窗体的`Visible`属性。默认情况下,窗体是可见的,但我们可以在程序启动时将其设置为不可见,如`Me.Visible = False`。 3. **鼠标事件处理**: - 我们关注的是`...

    c#窗体编程(显示鼠标位置;菜单程序;时钟程序)

    在本项目中,我们将探讨如何利用C#进行窗体编程,实现显示鼠标位置、创建菜单程序以及构建时钟程序。 首先,我们要了解窗体(Form)的基本概念。窗体是用户与应用程序交互的窗口,它是所有控件的容器。在C#中,我们...

    仿QQ屏幕右上角鼠标移动显示离开隐藏效果

    QQ作为一款流行的即时通讯软件,其用户界面设计和交互体验是业界的典范,尤其是其窗口边缘的动态效果,当鼠标靠近时显示,离开时自动隐藏,这种设计既美观又实用。 首先,我们需要理解Winform中的基本元素和事件...

    vb显示鼠标指针位置.rar

    在VB(Visual Basic)编程中,显示鼠标指针的位置是一项基本功能,这通常涉及到与Windows API(应用程序接口)的交互,因为VB的标准库并不直接提供获取鼠标实时位置的方法。本教程将详细介绍如何在VB中实现这一功能...

    winows mobile 浮动窗体

    浮动窗体,也称为对话框或者浮动对话框,允许开发者创建可以在主应用程序界面之上独立显示的小窗口,通常用于展示重要信息、进行用户交互或提供特定功能。 在Windows Mobile开发中,浮动窗体的实现主要依赖于.NET ...

    matlab开发-datetick2

    在MATLAB编程环境中,"datetick2" 是一个用于改进日期刻度显示的自定义函数,它扩展了MATLAB内置的`datetick`功能。`datetick`是MATLAB提供的一种命令,用于在图形窗口的轴上用日期格式显示刻度标签,而`datetick2`...

    Tick数据收集器.zip

    Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick数据收集器 Tick...

    tick:Tick是一个JavaScript插件,可让您轻松直观地看到动态变化的数字

    打钩Tick是一个JavaScript插件,可轻松实现动态可视化数字变化。实例方法注意: $().ticker始终返回Tick实例的数组,因此请始终在该数组的元素上调用这些方法! start渲染并启动Tick实例。 返回实例的计时器;如果...

    Tick-Tock Clock-crx插件

    【Tick-Tock Clock-crx插件】是一款专为Chrome浏览器设计的扩展程序,它提供了独特的计时功能,帮助用户以“滴答-TOCK”的形式跟踪时间流逝。这款插件以其简洁的界面和实用的特性,深受那些需要精确管理时间或者喜欢...

    VB.NET获取全局鼠标坐标

    在VB.NET编程环境中,获取全局鼠标坐标是一项常见的需求,特别是在开发需要实时监控鼠标位置或进行特定位置判断的应用程序时。全局鼠标坐标指的是鼠标在屏幕上的绝对位置,不受当前活动窗口的影响。下面我们将深入...

    Tick History Services – FTP Tick Data Supplement(美股tick)

    tick历史服务(Tick History Services)是一种提供股票交易历史数据的服务,特别为美股市场提供基于FTP(File Transfer Protocol,文件传输协议)的tick数据补充。tick数据是一种非常频繁的交易数据,它记录了股票等...

    小 窗体在桌面上浮动的控制

    在编程领域,特别是GUI(图形用户界面)设计中,"小窗体在桌面上浮动的控制"是一个常见的需求,尤其适用于工具提示、弹出通知或者监控应用等场景。本示例实现了一个窗体,该窗体可以在用户的桌面上自由移动,并在...

    tick指标 - MetaTrader 4脚本.zip

    用户可能需要对脚本进行一些配置,例如设置数据保存路径、显示的tick数量或特定的市场时段。 **注意事项** 在使用自定义脚本时,确保你了解脚本的工作原理和可能的风险。不当使用或未经测试的脚本可能导致数据错误...

    股票tick数据集.zip

    集中展示了2种不同的情况:一是分时tick在穿越昨日收盘价时,正好在昨收盘价位处有成交,另外一种情况是,在穿越昨日收盘价时,没有成交,直接跳过了这个价位,此时就需要用程序进行判断。根据分别对应的情况,就...

    20230214日期货市场所有品种的所有tick数据

    Tick数据是指在期货市场中,交易发生时最细粒度的价格变动记录,它包含了每一个交易瞬间的完整信息,如开盘价、最高价、最低价、收盘价、成交量等。 每个文件名如sc2304.csv、au2304.csv等,代表不同期货品种的tick...

    新浪、网易、腾讯实时tick接口_python股票_股票接口_新浪股票_tick股票_股票接口

    最后,对于获取的Tick数据,我们可以进行各种统计分析,如计算平均价、绘制K线图、计算波动率等,这些都是基于Tick数据的常见金融分析任务。在Python中,matplotlib和seaborn库可用于绘制图表,statsmodels和scipy则...

    linux tick timer clock笔记

    Linux中的tick timer(也称为时钟滴答或调度滴答)是操作系统内核的一个关键组件,它负责在系统中提供时间基准和定时功能。在早期的Linux版本中,tick timer每秒会产生一次中断(通常为100Hz,即每毫秒一次),用于...

Global site tag (gtag.js) - Google Analytics