将滚动条添加到 svg 容器
Add scrollbar to svg container
我正在使用 GetOrgChart 为我的公司制作组织结构图,但我 运行 遇到了一个小问题。
如果 SVG
比容器大,我们希望添加一个滚动条,这样您就可以使用它来滚动,因为这比用鼠标拖动要快得多。
我试过这个 example 但没能成功。
有什么方法可以实现我想要的吗?
下面的示例比我们的实际图表小得多,但它应该足以说明问题。
var peopleElement = document.getElementById("people");
var orgChart = new getOrgChart(peopleElement, {
primaryFields: ["name", "title", "phone", "mail"],
photoFields: ["image"],
scale: 0.4,
dataSource: [{
id: 1,
parentId: null,
name: "Amber McKenzie",
title: "CEO",
phone: "678-772-470",
mail: "lemmons@jourrapide.com",
adress: "Atlanta, GA 30303",
image: "images/f-11.jpg"
},
{
id: 2,
parentId: 1,
name: "Ava Field",
title: "Paper goods machine setter",
phone: "937-912-4971",
mail: "anderson@jourrapide.com",
image: "images/f-10.jpg"
},
{
id: 3,
parentId: 1,
name: "Evie Johnson",
title: "Employer relations representative",
phone: "314-722-6164",
mail: "thornton@armyspy.com",
image: "images/f-9.jpg"
},
{
id: 4,
parentId: 1,
name: "Paul Shetler",
title: "Teaching assistant",
phone: "330-263-6439",
mail: "shetler@rhyta.com",
image: "images/f-5.jpg"
},
{
id: 11,
parentId: 1,
name: "Paul Shetler",
title: "Teaching assistant",
phone: "330-263-6439",
mail: "shetler@rhyta.com",
image: "images/f-5.jpg"
},
{
id: 12,
parentId: 1,
name: "Paul Shetler",
title: "Teaching assistant",
phone: "330-263-6439",
mail: "shetler@rhyta.com",
image: "images/f-5.jpg"
},
{
id: 5,
parentId: 2,
name: "Rebecca Francis",
title: "Welding machine setter",
phone: "408-460-0589",
image: "images/f-4.jpg"
},
{
id: 6,
parentId: 2,
name: "Rebecca Randall",
title: "Optometrist",
phone: "801-920-9842",
mail: "JasonWGoodman@armyspy.com",
image: "images/f-8.jpg"
},
{
id: 7,
parentId: 2,
name: "Spencer May",
title: "System operator",
phone: "Conservation scientist",
mail: "hodges@teleworm.us",
image: "images/f-7.jpg"
},
{
id: 8,
parentId: 6,
name: "Max Ford",
title: "Budget manager",
phone: "989-474-8325",
mail: "hunter@teleworm.us",
image: "images/f-6.jpg"
},
{
id: 9,
parentId: 7,
name: "Riley Bray",
title: "Structural metal fabricator",
phone: "479-359-2159",
image: "images/f-3.jpg"
},
{
id: 10,
parentId: 7,
name: "Callum Whitehouse",
title: "Radar controller",
phone: "847-474-8775",
image: "images/f-2.jpg"
}
]
});
$('.get-left,.get-down,.get-up,.get-right').remove();
$(document).ready(function() {
$(".get-oc-c").css("overflow","scroll");
})
#people {
width: 90%;
height: 90%;
border:1px solid #000;
}
<link href="http://www.getorgchart.com/GetOrgChart/getorgchart/getorgchart.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.getorgchart.com/GetOrgChart/getorgchart/getorgchart.js"></script>
<div id="people"></div>
你可以看看this jsfiddle。 .get-oc-c
容器在需要时显示滚动条:
.get-oc-c {
overflow: auto !important;
}
并且 SVG 图表元素被包裹在一个 div 中,它被调整大小以容纳整个图表:
function wrapChart() {
...
$("svg").wrap("<div id='svgContainer'></div>");
...
}
#svgContainer {
overflow: visible;
}
在updatedEvent
中调用了wrapChart
方法。禁用图表移动选项以删除两侧的箭头并防止平移:
var orgChart = new getOrgChart(peopleElement, {
enableMove: false,
...
});
原始显示似乎可以工作,但很难获得包装元素的正确大小值(jsfiddle 中使用的表达式非常靠经验),并且在 window 调整大小时变得更加复杂,当链接为 expanded/collapsed 且图表缩放时。一些调整大小使用动画,因此计算必须考虑到获得最终值之前的延迟。
jsfiddle 显示了一些简单的代码来恢复 expanding/collapsing 节点后的滚动位置,但它需要改进。我还没有编写代码来说明 window 调整大小和缩放。
考虑到使滚动条正确运行所需的工作量,最好使用组件提供的平移和移动功能。也可以联系组件的作者,让他们添加滚动条选项。
这里缺少的重要 css 规则是:
max-height
和 overflow-y
overflow-y
应设置为自动,最大高度应设置为 SVG 不应越过的高度(这是我们将触发滚动条的高度)
'static' 方法是使用媒体查询为不同的屏幕设置预期大小,这同样会在这些大小上适当地触发滚动条。当然,如果客户的尺寸你没有想到,它可能效果不佳。
我个人会推荐使用滚动插件来处理 responsiveness/auto-resizing 以在需要时显示滚动条。 CSS 传统上是为支持静态 HTML 内容而构建的,但如今,我们有很多动态内容(严格来说并非来自服务器端),发现并解决所有问题总是一件令人头疼的事情。例如滚动条的出现可能会导致 DOM 的宽度发生变化,但无法通过 CSS 观察它。同样在某些浏览器中,传统的滚动条不必要地胖。
一些示例插件包括:
我正在使用 GetOrgChart 为我的公司制作组织结构图,但我 运行 遇到了一个小问题。
如果 SVG
比容器大,我们希望添加一个滚动条,这样您就可以使用它来滚动,因为这比用鼠标拖动要快得多。
我试过这个 example 但没能成功。
有什么方法可以实现我想要的吗?
下面的示例比我们的实际图表小得多,但它应该足以说明问题。
var peopleElement = document.getElementById("people");
var orgChart = new getOrgChart(peopleElement, {
primaryFields: ["name", "title", "phone", "mail"],
photoFields: ["image"],
scale: 0.4,
dataSource: [{
id: 1,
parentId: null,
name: "Amber McKenzie",
title: "CEO",
phone: "678-772-470",
mail: "lemmons@jourrapide.com",
adress: "Atlanta, GA 30303",
image: "images/f-11.jpg"
},
{
id: 2,
parentId: 1,
name: "Ava Field",
title: "Paper goods machine setter",
phone: "937-912-4971",
mail: "anderson@jourrapide.com",
image: "images/f-10.jpg"
},
{
id: 3,
parentId: 1,
name: "Evie Johnson",
title: "Employer relations representative",
phone: "314-722-6164",
mail: "thornton@armyspy.com",
image: "images/f-9.jpg"
},
{
id: 4,
parentId: 1,
name: "Paul Shetler",
title: "Teaching assistant",
phone: "330-263-6439",
mail: "shetler@rhyta.com",
image: "images/f-5.jpg"
},
{
id: 11,
parentId: 1,
name: "Paul Shetler",
title: "Teaching assistant",
phone: "330-263-6439",
mail: "shetler@rhyta.com",
image: "images/f-5.jpg"
},
{
id: 12,
parentId: 1,
name: "Paul Shetler",
title: "Teaching assistant",
phone: "330-263-6439",
mail: "shetler@rhyta.com",
image: "images/f-5.jpg"
},
{
id: 5,
parentId: 2,
name: "Rebecca Francis",
title: "Welding machine setter",
phone: "408-460-0589",
image: "images/f-4.jpg"
},
{
id: 6,
parentId: 2,
name: "Rebecca Randall",
title: "Optometrist",
phone: "801-920-9842",
mail: "JasonWGoodman@armyspy.com",
image: "images/f-8.jpg"
},
{
id: 7,
parentId: 2,
name: "Spencer May",
title: "System operator",
phone: "Conservation scientist",
mail: "hodges@teleworm.us",
image: "images/f-7.jpg"
},
{
id: 8,
parentId: 6,
name: "Max Ford",
title: "Budget manager",
phone: "989-474-8325",
mail: "hunter@teleworm.us",
image: "images/f-6.jpg"
},
{
id: 9,
parentId: 7,
name: "Riley Bray",
title: "Structural metal fabricator",
phone: "479-359-2159",
image: "images/f-3.jpg"
},
{
id: 10,
parentId: 7,
name: "Callum Whitehouse",
title: "Radar controller",
phone: "847-474-8775",
image: "images/f-2.jpg"
}
]
});
$('.get-left,.get-down,.get-up,.get-right').remove();
$(document).ready(function() {
$(".get-oc-c").css("overflow","scroll");
})
#people {
width: 90%;
height: 90%;
border:1px solid #000;
}
<link href="http://www.getorgchart.com/GetOrgChart/getorgchart/getorgchart.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.getorgchart.com/GetOrgChart/getorgchart/getorgchart.js"></script>
<div id="people"></div>
你可以看看this jsfiddle。 .get-oc-c
容器在需要时显示滚动条:
.get-oc-c {
overflow: auto !important;
}
并且 SVG 图表元素被包裹在一个 div 中,它被调整大小以容纳整个图表:
function wrapChart() {
...
$("svg").wrap("<div id='svgContainer'></div>");
...
}
#svgContainer {
overflow: visible;
}
在updatedEvent
中调用了wrapChart
方法。禁用图表移动选项以删除两侧的箭头并防止平移:
var orgChart = new getOrgChart(peopleElement, {
enableMove: false,
...
});
原始显示似乎可以工作,但很难获得包装元素的正确大小值(jsfiddle 中使用的表达式非常靠经验),并且在 window 调整大小时变得更加复杂,当链接为 expanded/collapsed 且图表缩放时。一些调整大小使用动画,因此计算必须考虑到获得最终值之前的延迟。
jsfiddle 显示了一些简单的代码来恢复 expanding/collapsing 节点后的滚动位置,但它需要改进。我还没有编写代码来说明 window 调整大小和缩放。
考虑到使滚动条正确运行所需的工作量,最好使用组件提供的平移和移动功能。也可以联系组件的作者,让他们添加滚动条选项。
这里缺少的重要 css 规则是:
max-height
和 overflow-y
overflow-y
应设置为自动,最大高度应设置为 SVG 不应越过的高度(这是我们将触发滚动条的高度)
'static' 方法是使用媒体查询为不同的屏幕设置预期大小,这同样会在这些大小上适当地触发滚动条。当然,如果客户的尺寸你没有想到,它可能效果不佳。
我个人会推荐使用滚动插件来处理 responsiveness/auto-resizing 以在需要时显示滚动条。 CSS 传统上是为支持静态 HTML 内容而构建的,但如今,我们有很多动态内容(严格来说并非来自服务器端),发现并解决所有问题总是一件令人头疼的事情。例如滚动条的出现可能会导致 DOM 的宽度发生变化,但无法通过 CSS 观察它。同样在某些浏览器中,传统的滚动条不必要地胖。
一些示例插件包括: