简体   繁体   English

在HIghcharts中设置自定义图例项目符号(或图标)

[英]Set custom legend item symbol (or icon) in HIghcharts

I'm trying to change the default highcharts legend symbol to my own custom one. 我正在尝试将默认的highcharts图例符号更改为我自己的自定义符号。 I wish to have a font-awesome icon next to the legend label. 我希望图例标签旁边有一个字体很棒的图标。 for that, Iv'e thought about labelformatter: 为此,我想过labelformatter:

labelFormatter: function () {
                    var on  = '<g><text x="0" y="0" style="color:'+this.color+';font-family:FontAwesome">&#xf14a;</text></g> ' + this.name;
                    var off   = '<g><text x="0" y="0" style="color:'+this.color+';font-family:FontAwesome">&#xf0c8;</text></g> ' + this.name;

                    return  this.iconState ? on : off;
                }

that actually let me add the icon as I wanted, but now my probelm is the following: when clicking on the legend items, the icons remain in the original color and do not become gray like the labels. 实际上我可以按照自己的意愿添加图标,但现在我的问题如下:点击图例项目时,图标保持原始颜色,不会像标签一样变灰。 Iv'e thought about re-rendering the legend when a click event is fired (using legendItemClicked), but I havnn't found anything that works out. 我想过在点击事件被触发时重新渲染图例(使用legendItemClicked),但是我找不到任何有效的东西。

Does anyone know how can I manage re-rendering the legend? 有谁知道如何管理重新渲染图例?

OR 要么

How Its even possible to set the symbol to my own? 如何将符号设置为我自己的符号?

Thanks! 谢谢!

You can define your custom symbol by adding this information to Highcharts symbol arrays, like in the example: 您可以通过将此信息添加到Highcharts符号数组来定义自定义符号,如示例中所示:

 Highcharts.SVGRenderer.prototype.symbols.cross = function (x, y, w, h) {
    return ['M', x, y, 'L', x + w, y, 'z'];
};
if (Highcharts.VMLRenderer) {
    Highcharts.VMLRenderer.prototype.symbols.cross = Highcharts.SVGRenderer.prototype.symbols.cross;
}

http://jsfiddle.net/nhx59/2/ http://jsfiddle.net/nhx59/2/

or use a image http://www.highcharts.com/demo/spline-symbols 或使用图片http://www.highcharts.com/demo/spline-symbols

There is another way. 还有另一种方式。 In my labelFormatter, i return a html string which includes my image, with a unique class name or id. 在我的labelFormatter中,我返回一个html字符串,其中包含我的图像,具有唯一的类名或id。

Now, I listen for legendItemClick event under plotOptions.column.events or plotOptions.line.events and then find my element and add a css class which has opacity in it like below: .dullImage { opacity: 0.4; filter: alpha(opacity=40); /* msie */ } 现在,我在plotOptions.column.events或plotOptions.line.events下监听legendItemClick事件,然后找到我的元素并添加一个具有不透明度的css类,如下所示: .dullImage { opacity: 0.4; filter: alpha(opacity=40); /* msie */ } .dullImage { opacity: 0.4; filter: alpha(opacity=40); /* msie */ }

In the legendItemClick, event object is passed and you can look at event.currentTarget.name (to find legend name) and event.currentTarget.isDirty to determine if this was click to disable or enable legend. 在legendItemClick中,传递事件对象,您可以查看event.currentTarget.name(查找图例名称)和event.currentTarget.isDirty以确定是否单击以禁用或启用图例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM