简体   繁体   English

在 Google 甘特图的 rect 元素中嵌套跨度文本

[英]Nest Span Text inside a rect element for a Google Gantt Chart

I have a google gantt chart that im trying to add text to inside the bars (as seen below) however it seems im adding them to the master container as oppose to inside the rect element.我有一个谷歌甘特图,我试图在条形内部添加文本(如下所示),但似乎我将它们添加到主容器中,与 rect 元素内部相反。 I have attempted to appendchild on the rect node but this causes it to nest improperly as well and never shows up.我试图在 rect 节点上追加子节点,但这也导致它嵌套不正确并且永远不会出现。 Attached is my chart with the test values and how it looks , I feel im super close, help is appreciated!附件是我的测试值图表及其外观,我觉得我非常接近,感谢帮助! 图表

Here is my code snippet (which derives from another users solution):这是我的代码片段(源自另一个用户解决方案):

      google.visualization.events.addOneTimeListener(chart, 'ready', function () {


    var rectangles = container.getElementsByTagName('rect');
    var adjustY = 10;
    var adjustX = 15;
    for(var i=0; i<rectangles.length; i++){
        if (rectangles[i].getAttribute('x') !== '0') {
            var barLabel = container.appendChild(document.createElement('span'));
            barLabel.innerHTML = 'test';
            barLabel.style.color = '#000000';
            barLabel.style.position = 'absolute';
            barLabel.style.overflow = 'hidden';
            barLabel.style.width = (parseInt(rectangles[i].getAttribute('width') - adjustX)) + 'px';
            barLabel.style.top = (parseInt(rectangles[i].getAttribute('y')) + adjustY) + 'px';
            barLabel.style.left = (parseInt(rectangles[i].getAttribute('x')) + adjustX) + 'px';
        }
    }

first, <rect> elements are svg elements, and they shouldn't really have children.首先, <rect>元素是 svg 元素,它们不应该真的有孩子。
you could append a <text> element (rather than <span> ) to the <rect> element's parent <g> ,您可以将<text>元素(而不是<span> )附加到<rect>元素的父元素<g>
but that wouldn't really help much, because you would still have to set the x, y coordinates.但这并没有多大帮助,因为您仍然需要设置 x、y 坐标。
also, when creating an svg element, you have to use its namespace, using --> createElementNS此外,在创建 svg 元素时,您必须使用其命名空间,使用 --> createElementNS

document.createElementNS(svgNS, 'text'); 

which is the reason nothing appeared这就是什么都没有出现的原因


appending a <span> to the chart container is fine.<span>附加到图表容器很好。
however, the reason your labels are not positioned properly,但是,您的标签未正确定位的原因,
is because you have to account for the container's position on the page.是因为您必须考虑容器在页面上的位置。

if it were at 0, 0 on the page, your code would work.如果它在页面上的 0, 0 处,您的代码将起作用。
but since it has elements above, your y coordinate is off.但因为它有上面的元素,你的 y 坐标是关闭的。

to resolve, get the bounding box of the container,解决,获取容器的边界框,
and add the top position of the container to your label's y coordinate.并将容器的顶部位置添加到标签的 y 坐标。

var containerBounds = container.getBoundingClientRect();

...

barLabel.style.top = (parseFloat(rectangles[i].getAttribute('y')) + adjustY + containerBounds.top) + 'px';

see following working snippet...请参阅以下工作片段...

 google.charts.load('current', { packages:['gantt'] }).then(function () { var data = new google.visualization.DataTable(); data.addColumn('string', 'Task ID'); data.addColumn('string', 'Task Name'); data.addColumn('string', 'Resource'); data.addColumn('date', 'Start Date'); data.addColumn('date', 'End Date'); data.addColumn('number', 'Duration'); data.addColumn('number', 'Percent Complete'); data.addColumn('string', 'Dependencies'); data.addRows([ ['2014Spring', 'Spring 2014', 'spring', new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null], ['2014Summer', 'Summer 2014', 'summer', new Date(2014, 5, 21), new Date(2014, 8, 20), null, 100, null], ['2014Autumn', 'Autumn 2014', 'autumn', new Date(2014, 8, 21), new Date(2014, 11, 20), null, 100, null], ['2014Winter', 'Winter 2014', 'winter', new Date(2014, 11, 21), new Date(2015, 2, 21), null, 100, null], ['2015Spring', 'Spring 2015', 'spring', new Date(2015, 2, 22), new Date(2015, 5, 20), null, 50, null], ['2015Summer', 'Summer 2015', 'summer', new Date(2015, 5, 21), new Date(2015, 8, 20), null, 0, null], ['2015Autumn', 'Autumn 2015', 'autumn', new Date(2015, 8, 21), new Date(2015, 11, 20), null, 0, null], ['2015Winter', 'Winter 2015', 'winter', new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null], ['Football', 'Football Season', 'sports', new Date(2014, 8, 4), new Date(2015, 1, 1), null, 100, null], ['Baseball', 'Baseball Season', 'sports', new Date(2015, 2, 31), new Date(2015, 9, 20), null, 14, null], ['Basketball', 'Basketball Season', 'sports', new Date(2014, 9, 28), new Date(2015, 5, 20), null, 86, null], ['Hockey', 'Hockey Season', 'sports', new Date(2014, 9, 8), new Date(2015, 5, 21), null, 89, null] ]); var options = { height: 400, gantt: { trackHeight: 30 } }; var container = document.getElementById('chart_div'); var containerBounds = container.getBoundingClientRect(); var chart = new google.visualization.Gantt(container); google.visualization.events.addOneTimeListener(chart, 'ready', function () { var rectangles = container.getElementsByTagName('rect'); var adjustY = 0; var adjustX = 15; for(var i=0; i<rectangles.length; i++){ if (rectangles[i].getAttribute('x') !== '0') { var barLabel = container.appendChild(document.createElement('span')); barLabel.innerHTML = 'test'; barLabel.style.color = '#ffffff'; barLabel.style.fontWeight = 'bold'; barLabel.style.position = 'absolute'; barLabel.style.overflow = 'hidden'; barLabel.style.width = (parseFloat(rectangles[i].getAttribute('width') - adjustX)) + 'px'; barLabel.style.top = (parseFloat(rectangles[i].getAttribute('y')) + adjustY + containerBounds.top) + 'px'; barLabel.style.left = (parseFloat(rectangles[i].getAttribute('x')) + adjustX + containerBounds.left) + 'px'; } } }); chart.draw(data, options); function daysToMilliseconds(days) { return days * 24 * 60 * 60 * 1000; } });
 <script src="https://www.gstatic.com/charts/loader.js"></script> <h1>TEST CUSTOMER 1</h1> <div id="chart_div"></div>

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

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