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

对jqplot的CategoryAxisRenderer插件进行扩展,让文字出现省略号

阅读更多
喜欢用jqplot的朋友一定碰到图形下标文字很长,会出现相互之间重叠的情况,如下图所示:


这是时候,我们就必须在显示底部文字的时候进行处理,比如限定文字的长度等,如:今年收入增...

下面给出扩展的源码:
jqplot.categoryAxisRenderer.extend.js
/**
 * 本扩展插件是对jqplot插件jqplot.categoryAxisRenderer的扩展,主要修改了显示图形下标Tick的处理,
 * 在显示Tick之前,判断Tick标签的长度,当标题长度超过tickMaxLength定义的长度时,截获前
 * tickMaxLength - 1个字符,然后加上省略号。
 * 
 * 作者:苏显斌
 */
(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;
				} 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);
            }
        }
    };
})(jQuery);


大家注意红色字体的代码,这是对原有的源码做了一些修改,增加了设置Tick最大长度的属性字段以及对Tick超过长度时候的控制。

使用方法:
在HTML或JSP中引入jqplot的插件:
<script  type="text/javascript" src="../jqplot/jqplot.categoryAxisRenderer.min.js'}"></script>
然后在其后面引入扩展插件:
<script  type="text/javascript" src="../jqplot/jqplot.categoryAxisRenderer.extend.js'}"></script>

并在创建jqplot的时候设置属性:
$.jqplot(chartContainerId, chartData, {
	...
	axes : {
		xaxis : {
			renderer : $.jqplot.CategoryAxisRenderer,
			rendererOptions : {
				fillToZero : true,
				tickMaxLength : 6  // 设置该属性后,将自动限制标题长度
			},
			ticks : ticks
		}

		...
	}
});


效果如下图所示:
  • 大小: 9.2 KB
  • 大小: 2.9 KB
分享到:
评论

相关推荐

    jQuery文字溢出显示省略号插件.zip

    "jQuery文字溢出显示省略号插件"就是为了解决这个问题而诞生的。它基于一款名为"dotdotdot.js"的JavaScript插件,旨在帮助开发者优雅地处理文字溢出的情况,使长文本在指定区域内以省略号的形式简洁展示。 jQuery是...

    CSS实现文字多行省略效果.zip_朋友圈文字最后变成省略号

    当我们需要展示大量文本但受限于有限的空间时,可以利用CSS来实现文字的多行省略效果,让超出部分以省略号(...)的形式呈现。在给定的“CSS实现多行文字显示省略号效果.zip”压缩包中,包含了一个名为“2.css”的...

    网页文字溢出显示省略号jQuery插件代码

    网页文字溢出显示省略号是一个常见的需求,尤其是在有限的空间内展示大量文本时。jQuery的dotdotdot.js插件提供了一种优雅的解决方案,能够帮助开发者处理这种情况。这个插件适用于单行和多行文本,当内容超出指定...

    js文字超出省略号特效

    在网页设计中,为了美观和用户体验,经常需要对过长的文字进行省略处理,尤其是在有限的空间内展示内容。"js文字超出省略号特效"就是一种利用JavaScript实现文本溢出时添加省略号的技术,它可以帮助我们在不改变HTML...

    多格式多行文字显示省略号插件 兼容ie6

    多格式多行文字显示省略号插件,总共有两种方法,一个是直接用jq写的简单代码。另一个是jq的插件,支持多种显示省略号的方式,例如在省略号后面加“查看详情”等,为了明显效果,把overflow注释了,在IE6下查看的话...

    网页文字溢出显示省略号jQuery插件代码.zip

    源代码对于学习和定制插件非常有价值,如果你需要调整插件的行为,比如改变省略号的位置或者添加自定义动画,就可以在此基础上进行修改。 使用这个插件,开发者可以轻松地将文字溢出显示省略号的功能集成到自己的...

    wpf Textblock 文字过长时,中间用省略号代替。

    这种效果可以通过多种方式实现,下面将详细介绍如何在WPF中使用TextBlock实现文字过长时中间用省略号代替的效果。 1. 使用TextTrimming属性: TextTrimming属性是TextBlock的一个内置特性,可以用来控制当文本内容...

    文字溢出显示省略号小插件

    文件为jquery简单封装方法,需要先引入jquery,调用方法: $(function(){ $('.test').wordLimit(30) })

    HTML CSS 省略号代码 超过长度省略号显示点击全部可以显示等 超出范围自动变成省略号,但鼠标可以选择文字,酷吧……

    HTML CSS 省略号代码 超过长度省略号显示点击全部可以显示等 超出范围自动变成省略号,但鼠标可以选择文字,酷吧……

    Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    视频与票的图标跟在标题后面显示,当标题过长时icon显示到省略号…后(textview省略号显示,图标自动靠后)。 二、问题解决 TextView可以通过 android:ellipsize=end android:singleLine=true实现单行省略, 但是...

    jquery省略号插件

    jquery实现超出部分省略号显示

    Android动态点点省略号闪烁效果的等待控件

    "Android动态点点省略号闪烁效果的等待控件"是一种常见的设计,它通过连续显示“...”来表示程序正在进行后台操作,同时通过动态闪烁增加用户的交互感,提高用户体验。本篇将详细介绍如何实现这样的控件。 首先,...

    jquery带省略号的分页

    这些插件通常具有高度自定义性,可以通过配置参数来调整分页样式、每页显示的条目数、是否显示省略号等。 在实际应用中,我们可能需要设置以下关键属性: 1. `totalPages`:定义总的页数,这是分页的基础。 2. `...

    带省略号显示的CStatic控件

    为了解决这个问题,我们可以创建一个扩展的CStatic控件,使其支持使用省略号来表示被截断的部分。 首先,让我们深入了解CStatic控件。CStatic是CWnd类的派生类,它在用户界面中表现为一个简单的文本框,可以显示...

    分页中间带有省略号的分页插件

    该分页插件可以设置前后显示的页码数,中间带有类似省略号的分割符。使用时,注意要引入jquery。

    一个支持火狐浏览器的自动显示省略号插件.rar

    标题中的“一个支持火狐浏览器的自动显示省略号插件.rar”指的是一个专为Firefox浏览器设计的插件,它的主要功能是在网页内容过长时自动添加省略号,以保持页面整洁并优化布局。这样的插件对于网页设计师和开发者来...

    前端样式 分页中间是省略号

    在前端开发中,分页设计是一项常见的任务,用于在大量数据中提供良好的用户体验,让用户能够按需加载或...通过分析和学习这个文件,开发者可以理解如何在自己的项目中实现类似的功能,或者对现有的分页设计进行改进。

    js分页带省略号

    而"js分页带省略号"指的是在分页插件中,为了简洁地展示页码,通常会采用省略号来表示中间的页码,只显示首尾部分和当前页附近的页码,这种设计既节省了空间,又保持了用户体验。 首先,我们来理解一下分页的基本...

    DIV 上下居中 多行 省略号

    在网页设计中,让`&lt;div&gt;`元素内的多行文本实现上下居中并添加省略号功能是一项常见的需求。这通常涉及到CSS布局、文本对齐和溢出隐藏等多个技术点。接下来,我们将深入探讨如何实现这个效果。 首先,我们要解决的是...

    微信小程序中的canvas 文字断行和省略号显示功能的处理方法

    以下是对微信小程序中Canvas文字断行和省略号显示功能的详细解析: 1. **文字测量**: - `measureText()` 方法是用于测量文本尺寸的关键,它返回一个对象,其中的 `width` 属性表示文本的宽度。在微信小程序中,这...

Global site tag (gtag.js) - Google Analytics