2016-04-10 25 views
3

鑑於我的數據名稱可能超長,我希望在用戶懸停數據名稱時實現工具提示,但是,我找不到任何有關將自定義標記添加到生成的數據名稱的文檔HTML。可以添加一個類,但不能添加數據標籤。在這種情況下數據名稱添加數據html標記

enter image description here

方式太長數據名稱。一個工具提示將是完美的。

+0

你的意思是你想有一個簡短的數據名稱,但顯示其全長爲工具提示,當鼠標懸停? – Aziz

+0

不一定,工具提示內容可能與初始值有所不同。意思是,我不想裁剪文字並在懸停時顯示全長。數據名稱可能是「hello」,然後懸停「今天真是美好的一天!」 – delmalki

+0

好的,但你不想有更短的價值觀? – Aziz

回答

1

好吧,我明白你想要的東西:)

我們可以只使用在我們的c3.generate調用中有一些額外的功能,並擴展了庫,這會給你一些更靈活ibility。只需在oninit函數中定義長標籤即可。

這裏是的jsfiddle,懸停在傳說中看到: https://jsfiddle.net/abacaj90/6v2tpft2/14/

function insertAfter(referenceNode, newNode) { 
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); 
} 

function legendFollowMouse(e) { 
    var x = e[0]; 
    var y = e[1]; 
    return { 
    x: x - 50 + 'px', 
    y: y + 20 + 'px' 
    } 
} 

function createLegendTooltip() { 
    var svg = this.svg[0][0]; 
    var frag = document.createDocumentFragment(); 
    var div = document.createElement('div'); 
    var span = document.createElement('span'); 
    div.className = 'c3-legend-tooltip-container'; 
    span.className = 'c3-legend-tooltip'; 

    div.appendChild(span); 
    frag.appendChild(div); 
    insertAfter(svg, frag); 

    this.legendHoverNode = span; 
} 

function generateLegendHoverLabels(labels) { 
    createLegendTooltip.call(this); 
    var obj = {}; 
    this.data.targets.map(function(data, i) { 
    if(typeof labels[i] !== 'undefined') { 
     obj[data.id] = data.id + ': ' + labels[i]; 
    } 
    }) 
    return obj; 
} 


var chart = c3.generate({ 
    data: { 
    columns: [ 
     ['data1', 30, 200, 100, 400, 150, 250], 
     ['data2', 20, 180, 240, 100, 190, 250], 
     ['data3', 20, 180, 240, 100, 190, 250] 
    ], 
    }, 
    oninit: function() { 
    // declare your extra long labels here 
    var legendLongLabels = ['long content here, data3 doesnt have a tooltip!','even longer content here, you can style me with css!']; 

    this.legendHoverContent = generateLegendHoverLabels.call(this, legendLongLabels); 
    }, 
    legend: { 
    item: { 
     onmouseover: function (id) { 
     // keep default behavior as well as our tooltip 
     d3.select(this.svg[0][0]).classed('c3-legend-item-focused', true); 

     if (!this.transiting && this.isTargetToShow(id)) { 
      this.api.focus(id); 
     } 

     // if we defined the long labels, display them 
     if (this.legendHoverContent.hasOwnProperty(id)) { 
      var coords = legendFollowMouse(d3.mouse(this.svg[0][0])) 
      this.legendHoverNode.parentNode.style.display = 'block'; 
      this.legendHoverNode.parentNode.style.top = coords.y; 
      this.legendHoverNode.parentNode.style.left = coords.x; 
      this.legendHoverNode.innerHTML = this.legendHoverContent[id]; 
     } 
     }, 
     onmouseout: function (id) { 
     // keep default behavior as well 
     d3.select(this.svg[0][0]).classed('c3-legend-item-focused', false); 
     this.api.revert(); 

     // just hide the tooltips 
     this.legendHoverNode.parentNode.style.display = 'none'; 
     } 
    } 
    } 
}); 
+1

這真的很好,做得很好。如果將它整合到c3核心上,那將是完美的!我會延長創建一個包裝,以隱藏繁重的工作,並保持onInit。謝謝 ! – delmalki

+0

謝謝:) - 是的,可以添加這樣的東西,如果它被集成,大部分的幫助函數甚至會被刪除。我想如果有足夠的人需要這個我可以做一個拉請求。 – AntonB

0

臨時應答在等待更好的東西:

這是數據Im使用輸入。

var data = { 
"data": [{ 
    "A": "1075.000000", 
    "date": "01-03-2016" 
}, { 
    "A": "878.571429", 
    "date": "01-04-2016" 
}, { 
    "A": "485.000000", 
    "date": "04-03-2016" 
}, { 
    "A": "795.000000", 
    "date": "05-03-2016" 
}, { 
    "A": "620.000000", 
    "date": "06-03-2016" 
}, { 
    "A": "957.500000", 
    "date": "07-03-2016" 
}, { 
    "name": "H1W", 
    "A": "990.000000" 
}, { 
    "A": "950.000000", 
    "date": "09-03-2016" 
}, { 
    "A": "680.000000", 
    "date": "10-03-2016" 
}, { 
    "A": "1000.000000", 
    "date": "17-03-2016" 
}, { 
    "A": "535.000000", 
    "date": "18-02-2016" 
}], 
"name": "A", 
"namePlus": { 
    "location": "B or C or D or E", 
    "vars": { 
     "sizes": "Sizes: 2 or 3 or 5", 
     "unitSizes": "Units Amount: (0 to 2) or (3 to 5)" 
    } 
}} 

這是我如何處理輸入

var tooltip = "<ul><li>" + data.name + "</li><li>" + data.namePlus.location + "</li>"; 
$.each(data.namePlus.vars, function (key, value) { 
    tooltip += "<li>" + value + "</li>"; 
}); 
tooltip += "</ul>"; 

c3.generate({ 
      bindto: '#lineChart', 
      data: { 
       json: data.data, 
       keys: { 
        x: 'date', 
        value: [data.name] 
       }, 
       xFormat: '%d-%m-%Y', 
       classes: { 
        value: 'HEY' 
       } 
      }); 

      $(".c3-legend-item-" + data.name).mouseover(function (e) { 
       $(".myTooltip").html(tooltip).css({visibility: "visible"}); 
      }); 
      $(".c3-legend-item-" + data.name).mouseleave(function (e) { 
       $(".myTooltip").html(tooltip).css({visibility: "hidden"}); 
      }); 

這是實際的CSS

.myTooltip { 
background-color: black; 
border-radius: 6px; 
color: #fff; 
font-size: 12px; 
height: 25px; 
margin: auto; 
opacity: 0.68; 
padding: 5px 0; 
position: relative; 
text-align: center; 
top: 14px; 
visibility: hidden; 
width: 50%;}