gpt4 book ai didi

javascript - 如何将 svg 标题粘贴/卡住到顶部

转载 作者:行者123 更新时间:2023-11-28 16:58:12 24 4
gpt4 key购买 nike

在谷歌时间线图表中,有一个技巧可以将标题(日期)置于顶部。如何卡住该标题,使其在垂直向下滚动时保持可见。类似于excel中的卡住行。

 function bring_date_header_up() {
var g = document.getElementsByTagName("svg")[0].getElementsByTagName("g")[1];
document.getElementsByTagName("svg")[0].parentNode.style.top = '80px';
document.getElementsByTagName("svg")[0].style.overflow = 'visible';
var height = Number(g.getElementsByTagName("text")[0].getAttribute('y')) + 25;
g.setAttribute('transform','translate(38,-'+height+')');
g = null;
}

举个例子: https://codepen.io/anon/pen/jJzxzj

最佳答案

您可以尝试克隆日期行并添加到图表上方的元素。

<g>在原始示例中捕获并移动了元素,
在这里,我们复制它并移动到另一个元素......

google.charts.load('current', {
packages:['timeline']
}).then(function () {
var data = new google.visualization.DataTable({
cols: [
{id: 'team', label: 'Team', type: 'string'},
{id: 'start', label: 'Season Start Date', type: 'date'},
{id: 'end', label: 'Season End Date', type: 'date'}
],
rows: [
{c: [{v: 'Baltimore Ravens'}, {v: 'Date(2000, 8, 5)'}, {v: 'Date(2001, 1, 5)'}]},
{c: [{v: 'New England Patriots'}, {v: 'Date(2001, 8, 5)'}, {v: 'Date(2002, 1, 5)'}]},
{c: [{v: 'Tampa Bay Buccaneers'}, {v: 'Date(2002, 8, 5)'}, {v: 'Date(2003, 1, 5)'}]},
{c: [{v: 'New England Patriots'}, {v: 'Date(2003, 8, 5)'}, {v: 'Date(2004, 1, 5)'}]},
{c: [{v: 'New England Patriots'}, {v: 'Date(2004, 8, 5)'}, {v: 'Date(2005, 1, 5)'}]},
{c: [{v: 'Pittsburgh Steelers'}, {v: 'Date(2005, 8, 5)'}, {v: 'Date(2006, 1, 5)'}]},
{c: [{v: 'Indianapolis Colts'}, {v: 'Date(2006, 8, 5)'}, {v: 'Date(2007, 1, 5)'}]},
{c: [{v: 'New York Giants'}, {v: 'Date(2007, 8, 5)'}, {v: 'Date(2008, 1, 5)'}]},
{c: [{v: 'Pittsburgh Steelers'}, {v: 'Date(2008, 8, 5)'}, {v: 'Date(2009, 1, 5)'}]},
{c: [{v: 'New Orleans Saints'}, {v: 'Date(2009, 8, 5)'}, {v: 'Date(2010, 1, 5)'}]},
{c: [{v: 'Green Bay Packers'}, {v: 'Date(2010, 8, 5)'}, {v: 'Date(2011, 1, 5)'}]},
{c: [{v: 'Green Bay Packers'}, {v: 'Date(2010, 8, 5)'}, {v: 'Date(2011, 1, 5)'}]},
{c: [{v: 'New England Patriots'}, {v: 'Date(2001, 8, 5)'}, {v: 'Date(2002, 1, 5)'}]},
{c: [{v: 'Tampa Bay Buccaneers'}, {v: 'Date(2002, 8, 5)'}, {v: 'Date(2003, 1, 5)'}]},
{c: [{v: 'New England Patriots'}, {v: 'Date(2003, 8, 5)'}, {v: 'Date(2004, 1, 5)'}]},
{c: [{v: 'New England Patriots'}, {v: 'Date(2004, 8, 5)'}, {v: 'Date(2005, 1, 5)'}]},
{c: [{v: 'Pittsburgh Steelers'}, {v: 'Date(2005, 8, 5)'}, {v: 'Date(2006, 1, 5)'}]},
]
});

var options = {
height: 650,
timeline: {
groupByRowLabel: true
}
};

var chart = new google.visualization.Timeline(document.getElementById('chart'));
google.visualization.events.addListener(chart, 'ready', afterDraw);
chart.draw(data, options);
window.addEventListener('resize', function () {
chart.draw(data, options);
});
});

function afterDraw() {
var header = document.getElementById('header');
header.innerHTML = '';
var svg = document.getElementsByTagName('svg')[0];
var g = svg.getElementsByTagName('g')[1];
var svgNew = header.appendChild(svg.cloneNode());
var gNew = svgNew.appendChild(g.cloneNode(true));
var height = parseFloat(gNew.getElementsByTagName('text')[0].getAttribute('y')) - 25;
gNew.setAttribute('transform','translate(0,-'+height+')');
g.parentNode.removeChild(g);
}
#header {
height: 56px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="header"></div>
<div id="chart"></div>

关于javascript - 如何将 svg 标题粘贴/卡住到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55185413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com