Echarts Legend icon Change

Source: Internet
Author: User

Recently there is a need to look at the echarts of the relationship Network diagram can be achieved, but the figure out after the legend shows the icon display is the same, after reading the document, feel that does not meet my requirements, changed the code, the final chart as follows (each kind of an icon):

The changes are as follows:

Legend.js inside the _builditem method.

ItemType = Data[i].icon | | This._getsomethingbyname (itemname). Type;

This is the icon type to get the legend, you can look at the

/** *  returns a series data by name or data */_getsomethingbyname: function  (name)  {     var series = this.option.series;    var data;     for  (var i = 0, l = series.length; i < l;  i++)  {        if  (series[i].name === name)  {            //  Series name Priority              return {                 type: series[i].type,                 series: series[i],                 seriesIndex: i,                 data: null,                 dataIndex: -1             };        }         if  (             series[i].type === ecConfig.CHART_TYPE_PIE              | |  series[i].type === ecConfig.CHART_TYPE_RADAR             | |  series[i].type === ecConfig.CHART_TYPE_CHORD             | |  series[i].type === ecConfig.CHART_TYPE_FORCE             | |  series[i].type === ecconfig.chart_type_funnel             | |  series[i].type === ecConfig.CHART_TYPE_TREEMAP         )  {            data = series[i]. categories | |  series[i].data | |  series[i].nodes;            for  (Var  j = 0, k = data.length; j < k; j++)  {                 if  (data[j].name  === name)  {                     return {                         type: series[i].type,                         series: series[i],                          seriesIndex: i,                         data: data[j],                          dataIndex: j                     };                 }            }         }    }    return {         type:  ' Bar ',         series: null,         seriesIndex: -1,         data: null,        dataIndex: -1     };},

As can be seen, get the icon type, first obtained from the series (the first series of type in the above types), not get the default is bar, but force only one series, so the legend all the icons are force type.

See this, I know how to change, I have defined a method (_genlegendtypebyname) from the category, each category is an object, which defines the name and the type attribute (I wrapped in the background), the code after the change is as follows:

ItemType = Data[i].icon | | This._getlegendtypebyname (ItemName). Type;if (!itemtype | | itemType = ") {ItemType = Data[i].icon | | this._getsomethin Gbyname (itemname). Type;}

This way, if the category does not have a definition, it also uses the _getsomethingbyname method to get it, without error, and after getting to type, there is a problem

Graphics Itemshape = This._getitemshapebytype (Lastx, Lasty, Itemwidth, ItemHeight, (This._selectedmap[itemname] & ;& This._hasdatamap[itemname]? Color: ' #ccc '), itemType, color);

The Shape object is encapsulated here based on the type previously obtained

_getitemshapebytype: function  (x, y, width, height, color, itemtype,  DefaultColor)  {    var highlightColor = color ===  ' #ccc '  ? defaultColor : color;    var itemShape = {         zlevel: this.getzlevelbase (),         z: this.getzbase (),        style: {             iconType:  ' Legendicon '  +  itemtype ,            x: x,             y: y,             width: width,             height: height,            color: color,             strokeColor: color,             lineWidth: 2         },        highlightStyle: {             color: highlightColor,             strokeColor: highlightColor,             lineWidth: 1        },         hoverable: this.legendOption.selectedMode,         clickable: this.legendOption.selectedMode    };

The above method icon type has become the ' Legendicon ' +type, the final method of generating the icon in Icon.js inside

/**         *  Creating a rectangular path           *  @param  {context2d} ctx canvas 2d Context           *  @param  {Object} style  Styles           */        buildPath : function  (CTX,  style, refreshnextframe)  {             if  (This.iconlibrary[style.icontype])  {                 this.iconlibrary[style.icontype].call (This, ctx, style,  refreshnextframe);            }             else {                  ctx.moveto (STYLE.X,&NBSP;STYLE.Y);                 ctx.lineto (style.x + style.width, style.y );                 ctx.lineto ( Style.x + style.width, style.y + style.height);                 ctx.lineto (style.x, style.y +  Style.height);                 Ctx.lineto (STYLE.X,&NBSP;STYLE.Y);                 ctx.closepath ();             }            return;         },

But here the iconlibrary content is as follows:

See no, Legendicon beginning is so several, so you want to use triangle these, put _getitemshapebytype inside Icontype change can

Echarts Legend icon Change

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.