[5 of the Web chart series] 5. Implementation of Figure tooltip in draw2d

Source: Internet
Author: User
Preface

The first four articles in Series 5 are based on the development of draw2d version 2.3.0.

Currently (2013/05), the latest version of draw2d is version 2.6.1.

The tooltip function is not provided in version 2.3.0, but the tooltip implementation method is provided in version 2.6.1.

Tooltip in version 2.6.1
Before we can see the source code, we thought that the implementation method should be to add a tooltip attribute to the shape in draw2d. JS, and provide some set or hide methods for calling.
This is not the case. First, directly paste the source code:
MyFigure = draw2d.shape.basic.Rectangle.extend({    NAME : "MyFigure",        init : function()    {        this._super();                this.createPort("input");        this.createPort("output");        this.setDimension(50,50);                this.tooltip = null;    },    /**     * @method     * Change the color and the internal value of the figure.     * Post the new value to related input ports.     *      */    onMouseEnter: function(){        this.showTooltip();    },        onMouseLeave: function(){        this.hideTooltip();    },        setPosition: function(x,y){        this._super(x,y);        this.positionTooltip();    },        hideTooltip:function(){                  this.tooltip.remove();           this.tooltip = null;    },            showTooltip:function(){                  this.tooltip= $('<div class="tooltip">Tooltip</div>').appendTo('body');        this.positionTooltip();            },        positionTooltip: function(){        if( this.tooltip===null){            return;        }                var width =  this.tooltip.outerWidth(true);        var tPosX = this.getAbsoluteX()+this.getWidth()/2-width/2+8;        var tPosY = this.getAbsoluteY()+this.getHeight() + 20;        this.tooltip.css({'top': tPosY, 'left': tPosX});    }    });

It can be found that the implementation method is actually stacked and is not modified in the original definition, but provided to developers for expansion.
Inherit a shape class -- when mouse enter and mouse leave are used, the tooltip. tooltip implementation is to add a div on the body page and set the CSS style and position of the div.
In this way, you can add tooltip in the old version based on this method.
Failed. The cause is that the onmouseenter and onmouseleave event trigger methods are not executed. It seems that to achieve this effect, you need to modify draw2d. js so that the mouse enter and mouse leave event methods of the shape can be executed. In addition, you need to add the CSS of tooltip.

Other solutions of earlier versions

If the old version of draw2d is used, is there any other way to implement the tooltip function?
The answer is yes.
The implementation principle should be similar to that of the new version.
Before introducing the solution, let's first introduce jqueyui
Jquery UI-jquery user interface is an interface element implemented by jquery.
The tooltip component is available in jqueryui 1.9 and later versions. (Neither version 1.8 nor earlier)
Let's take a look at the implementation:

<!-- by oscar999 --><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

The implementation method is: Find whether the page element has the title attribute. If so, add the DIV Of The tooltip.
Therefore, if we add the title attribute to the SVG figure of the page, we can display the beautiful tootip.

If you want to add tooltip to a shape like rectangle

Modify the settings as follows:

1. repaint definition of draw2d. Shape. Basic. rectangle

Add the following section (the reason for defining attributes in repaint instead of init is that attributes is used in repaint)

attributes.title = stitle;

2. createshapeelement definition section of draw2d. Shape. Basic. Rectangle. (This section uses Raphael to create a shape)

    createShapeElement: function () {            return this.canvas.paper.rect(this.getAbsoluteX(), this.getAbsoluteY(), this.getWidth(), this.getHeight(), null, this.getTitle());    }

3. Add the title attribute to the shape created by Raphael.

    //add title    paperproto.rect = function (x, y, w, h, r ,t) {            var out = R._engine.rect(this, x || 0, y || 0, w || 0, h || 0, r || 0, t || "");        this.__set__ && this.__set__.push(out);        return out;    };

    //add title    R._engine.rect = function (svg, x, y, w, h, r, t) {        var el = $("rect");        svg.canvas && svg.canvas.appendChild(el);        var res = new Element(el, svg);                res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000", title: t || ""};        res.type = "rect";        $(el, res.attrs);        return res;    };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.