简体   繁体   English

在jQuery工具提示中使用动态ID渲染Highchart

[英]Render Highchart with dynamic ID in jQuery tooltip

I am trying to render a highchart chart to a jquery tooltip. 我正在尝试将高图表图表呈现给jquery工具提示。 I also want to pass the ID of the element being hovered, so that it is passed directly to highcharts as a variable to the renderTo argument. 我还想传递要悬停的元素的ID,以便将其作为renderTo参数的变量直接传递给highcharts。 Right now I cannot get this to work. 现在,我无法使它正常工作。 Please click here for the JS fiddle: 请单击此处获取JS小提琴:

My code is as follows. 我的代码如下。

  $(document).tooltip({

    open: function() {

    var widget = $(this).data("ui-tooltip");
    var widget = $(widget.element[0]).attr("id")

      Highcharts.chart({
        chart: {
          renderTo: widget,
          type: 'bar'
        },

Edit 编辑

I need the ID of the hovered element. 我需要悬停的元素的ID。 At least I think I do. 至少我认为我愿意。 The reason is that I need to feed an array specific to this ID to the highchart function. 原因是我需要将特定于此ID的数组提供给highchart函数。 Also, I may want to hover a class and generate the graph to a div with a prefix. 另外,我可能希望将鼠标悬停在一个类上,并将图形生成到带有前缀的div中。 Here is another jsfiddle with pseudo code. 这是另一个带有伪代码的jsfiddle

HTML HTML

<p>
 <label for="age">Your age:</label>
 <input id="age" title="<div class='container' id='containerX'>
 <div id='chart_containerX' style='width:500px;height:500px;background:red'>
 </div>
</div>"/>
    </p>
    <p>Hover the field to see the tooltip.</p>

JScript 脚本

$(function() {
  $(".container").tooltip({

  <pseudo> get ID and generate array so that it can be passed to highcharts </pseudo>
  <pseudo> get ID and add 'chart_' prefix so that it can be passed to renderTo </pseudo>


    open: function(event, ui) {
      ui.tooltip.highcharts({
        data: { <<'array specific to this chart'>>},
        chart: {
          renderTo: <<chart_container (with prefix!)>>
          type: 'bar'
        },

This is a perfect example to learn to check the documentation carefully and also to debug. 这是学习仔细检查文档以及进行调试的完美示例。

First, you don't need the element ID to render Highcharts to an element. 首先,您不需要元素ID即可将Highcharts呈现为元素。 As documentation says , you can do this: 如文档所述 ,您可以执行以下操作:

element.highcharts({/* options */});

And second, have you tried to see if your widget variable actually contains something? 其次,您是否尝试过查看widget变量是否确实包含某些内容? If you do a console.log(widget) , you will see that the console outputs undefined, and if you do console.log($(this).data("ui-tooltip")) you see that the console logs the document, not the widget element. 如果执行console.log(widget) ,则会看到控制台输出未定义,如果执行console.log($(this).data("ui-tooltip"))则会看到控制台记录了文档,而不是小部件元素。 This is a perfect point to check the documentation and see that the open event receives a second ui argument with the actual tooltip element, being it a jQuery element so no need for $(ui.tooltip) but directly access ui.tooltip . 这是检查文档并查看open事件接收带有实际tooltip元素的第二个ui参数的理想点,因为它是jQuery元素,因此不需要$(ui.tooltip)而是直接访问ui.tooltip

Now with all this info you can do this: 现在,有了所有这些信息,您可以执行以下操作:

open: function(event, ui) {
  ui.tooltip.highcharts({

Which comes into this: https://jsfiddle.net/yx27xf7h/5/ 其中的内容是: https : //jsfiddle.net/yx27xf7h/5/

IMPORTANT: You need to destroy the Highchart when the widget closes or you will end with a memory leak (Insert dramatic ♪DUN DUN DUUUN♫ music here). 重要信息:小部件关闭时,您需要销毁Highchart,否则将导致内存泄漏 (在此处插入戏剧性的♪DUN DUN DUUUN♫音乐)。

EDIT: If you want to use a container element, is better to avoid adding IDs as IDs must be unique in the entire webpage so you must find a way to create a unique ID each time and that's adding extra code when you can avoid it. 编辑:如果要使用容器元素,最好避免添加ID,因为ID在整个网页中必须是唯一的,因此您必须每次都创建一个唯一ID的方法,并且在避免时添加了额外的代码。 The less code for the same task, the better. 用于同一任务的代码越少越好。

When working with containers that are created and destroyed on the fly is better to work with references and ways to gather them as references inside their parent containers, for example doing .class or using data attributes, like this: https://jsfiddle.net/yx27xf7h/6/ 当使用动态创建和销毁的容器时,最好使用引用和将其收集为父容器内的引用的方法,例如执行.class或使用数据属性,例如: https : //jsfiddle.net / yx27xf7h / 6 /

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

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