Example of Virtualization in ASP.NET MVC Diagram Control
Virtualization: the page makes a diagram of 5,000 nodes and 4,900 connectors; only what is in view (and near it) is drawn, so it opens and scrolls quickly. Scroll, zoom or press Alt and an arrow key to go through it; the status says how much is drawn.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Virtualization: the page makes a diagram of 5,000 nodes and 4,900 connectors; only what is in view (and near it) is drawn, so it
opens and scrolls quickly. Scroll, zoom or press Alt and an arrow key to go through it; the status says how much is drawn.
</p>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
var TOTAL = 5000;
var ACROSS = 50;
var fills = ['#e8f0fe', '#e6f4ea', '#fff4e5', '#f3e5f5'];
function drawn() { return document.querySelectorAll('#virtualDiagram .ui-dg-node').length; }
/** OnCreated: 5,000 nodes in rows of 50, each connected to the one before it. */
function created(args, id) {
var nodes = [];
var connectors = [];
for (var i = 0; i < TOTAL; i++) {
var row = Math.floor(i / ACROSS);
var col = i % ACROSS;
nodes.push({
id: 'n' + i, offsetX: 80 + col * 160, offsetY: 60 + row * 110, width: 110, height: 50,
shape: { type: 'Basic', shape: i % 7 === 0 ? 'Ellipse' : 'Rectangle' }, style: { fill: fills[row % fills.length] },
annotations: [{ content: 'Node ' + (i + 1) }]
});
if (col > 0) { connectors.push({ id: 'c' + i, sourceID: 'n' + (i - 1), targetID: 'n' + i, type: 'Straight' }); }
}
var start = window.performance.now();
Lumen.get(id).loadDiagram({ nodes: nodes, connectors: connectors });
window.DiagramSamples.loadTime = Math.round(window.performance.now() - start);
status('5,000 nodes loaded in ' + window.DiagramSamples.loadTime + ' ms; ' + drawn() + ' drawn now.');
}
/** OnScrollChange: how many nodes are drawn now. */
function scrolled() {
status('5,000 nodes; ' + drawn() + ' drawn now.');
}
return { created: created, scrolled: scrolled };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/Virtualization.cshtml; it draws what ViewData holds.
public ActionResult Virtualization()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "virtualDiagram",
Height = "520px",
AriaLabel = "Diagram of 5,000 nodes",
Virtualization = true,
HistoryLimit = 10,
OnCreated = "DiagramSamples.created",
OnScrollChange = "DiagramSamples.scrolled"
};
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>