Example of Zooming And Panning in ASP.NET MVC Diagram Control
Zooming And Panning: zoom with the buttons, with Ctrl and the mouse wheel (at the pointer) or with Ctrl and + / - / 0 when a node has the focus. Turn the Pan tool on and drag the canvas to move the view; the scroll bars move it too.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Zooming And Panning: zoom with the buttons, with Ctrl and the mouse wheel (at the pointer) or with Ctrl and + / - / 0 when a node has
the focus. Turn the Pan tool on and drag the canvas to move the view; the scroll bars move it too.
</p>
<div class="mb-2" role="toolbar" aria-label="Zoom and pan">
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-zoom="in">Zoom in</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-zoom="out">Zoom out</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-zoom="reset">100%</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-zoom="fit">Fit to page</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-zoom="pan" aria-pressed="false">Pan tool</button>
</div>
@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; }
}
function diagram() { return Lumen.get('zoomDiagram'); }
/** OnScrollChange: the zoom and where the view is. */
function scrolled(args) {
var v = args.newValue;
status('Zoom ' + Math.round(v.zoom * 100) + '%; the view starts at ' + Math.round(v.horizontalOffset) + ', ' + Math.round(v.verticalOffset) + '.');
}
document.addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-zoom]') : null;
if (!b) { return; }
var d = diagram();
var what = b.getAttribute('data-zoom');
if (what === 'in') { d.zoomTo({ type: 'ZoomIn', zoomFactor: 0.2 }); }
else if (what === 'out') { d.zoomTo({ type: 'ZoomOut', zoomFactor: 0.2 }); }
else if (what === 'reset') { d.resetZoom(); }
else if (what === 'fit') { d.fitToPage({ margin: 20 }); }
else if (what === 'pan') {
var on = b.getAttribute('aria-pressed') !== 'true';
b.setAttribute('aria-pressed', on ? 'true' : 'false');
d.setTool(on ? 'ZoomPan' : 'Default');
status(on ? 'Pan tool on: drag the canvas to move the view.' : 'Pan tool off: a drag on the canvas selects.');
}
});
return { 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/ZoomingAndPanning.cshtml; it draws what ViewData holds.
public ActionResult ZoomingAndPanning()
{
UiDiagramOptions o = Spread("zoomDiagram", 6, 4, 200, 150);
o.AriaLabel = "Diagram to zoom and pan";
o.OnScrollChange = "DiagramSamples.scrolled";
ViewData["diagram"] = o;
return View();
}
private static UiDiagramNode Node(string id, double x, double y, string text, UiDiagramShapeType type = UiDiagramShapeType.Flow, string shape = "Process",
double width = 120, double height = 54, string fill = null)
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = type, Shape = shape },
Style = new UiDiagramStyle { Fill = fill }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text }); }
return node;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
/// <summary>A flow of n steps across and down, for the samples that need room (zoom, scrolling, overview).</summary>
private static UiDiagramOptions Spread(string id, int across, int down, double gapX, double gapY)
{
var nodes = new List<UiDiagramNode>();
var links = new List<UiDiagramConnector>();
string[] fills = { "#e8f0fe", "#e6f4ea", "#fff4e5", "#f3e5f5" };
for (int r = 0; r < down; r++)
{
for (int c = 0; c < across; c++)
{
int i = r * across + c;
string nid = "step" + (i + 1);
nodes.Add(Node(nid, 100 + c * gapX, 80 + r * gapY, "Step " + (i + 1), UiDiagramShapeType.Flow, i % 5 == 4 ? "Decision" : "Process",
120, i % 5 == 4 ? 70 : 54, fills[r % fills.Length]));
if (c > 0) { links.Add(Link("l" + i, "step" + i, nid)); }
else if (r > 0) { links.Add(Link("l" + i, "step" + ((r - 1) * across + 1), nid)); }
}
}
return new UiDiagramOptions { Id = id, Height = "480px", Nodes = nodes, Connectors = links };
}
}
}
@* 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>