简体   繁体   English

highstocks-图例位置并在多个图表的鼠标移动时刷新图例值

[英]highstocks - legend position and refresh legend values on mousemove of multiple charts

Here is the scenario. 这是场景。 I've multiple highstocks say 10 charts on a single page. 我有多张高个highstocks说一页上有10张图表。 Currently I've written 500 lines of code to position the legend, show tooltip and refresh the legend values on mousemove. 目前,我已经编写了500行代码来定位图例,显示工具提示并在mousemove上刷新图例值。

No. of legends vary per chart. 每个图表的图例数量有所不同。 On mousemove values of all the legends are updated. 鼠标移动时,所有图例的值都会更新。 I need to optimize the code I am using highstocks v1.2.2. 我需要优化我使用highstocks的代码。 带有图例的多个图表

Above screenshot shows 2 charts. 上面的屏幕截图显示了2个图表。 Return , Basket , vs Basket Spread are legends and it's values are updated on every mousemove. ReturnBasketvs Basket Spread都是图例,其值在每次鼠标移动时都会更新。

Please find this fiddle for example . 请以这个小提琴为例 In my case legends are positioned and updated values on mouse move with hundreds of lines of code. 在我的案例中,图例是通过数百行代码在鼠标移动时定位和更新的值。 When I move the mouse the legend values of Return and Basket of first chart and the legend values of vs Basket Spread are updated. 当我移动鼠标时,第一张图表的ReturnBasket的图例值以及vs Basket Spread的图例值被更新。 It's working fine but with lots of javascript code. 它工作正常,但包含许多javascript代码。 So I need to optimize it less code or with highstocks built-in feature. 因此,我需要使用更少的代码或使用大量内置功能来对其进行优化。

Update 更新资料

User @wergeld has posted new fiddle . 用户@wergeld发布了新的小提琴 As I've shown in screenshot when cross-hair is being moved over any chart, the legend values of all the charts should be updated. 正如我在屏幕快照中显示的那样,当在任何图表上移动十字线时,所有图表的图例值都应更新。

Is there anyway to implement the same functionality with less code or is there built-in feature available in highstocks ??? 无论如何,是否需要用更少的代码来实现相同的功能,或者在highstocks中提供了内置功能?

Using this as a reference . 以此为参考

Basic example would be to use the events.mouseover methods: 基本示例是使用events.mouseover方法:

plotOptions: {
    series: {
        point: {
            events: {
                mouseOver: function () {
                    var theLegendList = $('#legend');
                    var theSeriesName = this.series.name;
                    var theYValue = this.y;
                    $('li', theLegendList).each(function (l) {
                        if (this.innerText.split(':')[0] == theSeriesName) {
                            this.innerText = theSeriesName + ': ' + theYValue;
                        }
                    });
                }
            }
        }
    }
}

This is assuming I have modded the <li> to be: 假设我将<li>为:

        $('<li>')
            .css('color', serie.color)
            .text(serie.name + ': NA')
            .click(function () {
            toggleSeries(i);
        })
            .appendTo($legend);

You would then need to handle the mouseout event but I do not know what you want to do there. 然后,您将需要处理mouseout事件,但我不知道您要在该事件中做什么。 Working example . 工作示例

EDIT: Here is a version using your reference OHLC chart to put the values in a different legend location when any point in the chart is hovered. 编辑:这是一个使用参考OHLC图表的版本,当图表中的任何点悬停时,将值放在不同的图例位置。

    plotOptions: {
        series: {
            point: {
                events: {
                    mouseOver: function () {
                        //using the ohlc and volumn data sets created at runtime.
                        var stockVal = ohlc[this.index][4]; // show close value
                        var stockVolume = volume[this.index][1];
                        var theChart = $('#container').highcharts();
                        var theLegendList = $('#legend');
                        $('li', theLegendList).each(function (l) {
                            var legendTitle = theChart.series[l].name;
                            if (l === 0) {
                                this.innerText = legendTitle + ': ' + stockVal;
                            }
                            if (l === 1) {
                                this.innerText = legendTitle + ': ' + stockVolume;
                            }
                        });
                    }
                }
            }
        }
    }

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

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