Automatic Layouts (Soccer Tournament) Example for Blazor

Automatic Layouts (Soccer Tournament) Example for Blazor

Nevron NOV Diagram for Blazor ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All Blazor Diagram examples are also available as a stand-alone Visual Studio solution in C#, which you can download from the links below.

Automatic Layouts (Soccer Tournament) Example for Blazor Description

Demonstrates how to create a soccer tournament scoreboard using the following NOV Diagram features:

  • Theme-based colors for the title - change the page theme from the "Design" tab to see how the style of the title will change, too.
  • Table blocks - used for the match shapes.
  • Layered Graph Layout - used to arrange the shapes.

Soccer Tournament Example

C# Automatic Layouts (Soccer Tournament) Example Code for Nevron Diagram for Blazor
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Layout;
using Nevron.Nov.Diagram.Themes;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NSoccerTournamentExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create a simple drawing
			NDrawingViewWithRibbon drawingViewWithRibbon = new NDrawingViewWithRibbon();
			m_DrawingView = drawingViewWithRibbon.View;

            // hide grid and ports
            m_DrawingView.Content.ScreenVisibility.ShowGrid = false;
            m_DrawingView.Content.ScreenVisibility.ShowPorts = false;

            m_DrawingView.Document.HistoryService.Pause();
			try
			{
				InitDiagram(m_DrawingView.Document);
			}
			finally
			{
				m_DrawingView.Document.HistoryService.Resume();
			}

			return drawingViewWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			return null;
		}
		public string GetExampleDescription()
		{
			return @"

Demonstrates how to create a soccer tournament scoreboard using the following NOV Diagram features:

  • Theme-based colors for the title - change the page theme from the ""Design"" tab to see how the style of the title will change, too.
  • Table blocks - used for the match shapes.
  • Layered Graph Layout - used to arrange the shapes.
"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"), ("Text Block", "https://www.nevron.com/docs/openvision/diagram-text-block.html"), ("Routable Connectors", "https://www.nevron.com/docs/openvision/diagram-routable-connectors.html"), }; } private void InitDiagram(NDrawingDocument drawingDocument) { NPage page = drawingDocument.Content.ActivePage; // Create a title shape NShape titleShape = CreateTitleShape("FIFA World Cup 2022"); page.Items.Add(titleShape); // Create the match shapes NShape[] roundOf16Shapes = CreateMatchShapes(page, RoundOf16); NShape[] quarterFinalsShapes = CreateMatchShapes(page, QuarterFinals); NShape[] semiFinalsShapes = CreateMatchShapes(page, SemiFinals); NShape finalShape = CreateMatchShape(Final); page.Items.Add(finalShape); // Connect the shapes for (int i = 0; i < quarterFinalsShapes.Length; i++) { ConnectShapes(roundOf16Shapes[i * 2], roundOf16Shapes[i * 2 + 1], quarterFinalsShapes[i]); } ConnectShapes(quarterFinalsShapes[0], quarterFinalsShapes[1], semiFinalsShapes[0]); ConnectShapes(quarterFinalsShapes[2], quarterFinalsShapes[3], semiFinalsShapes[1]); ConnectShapes(semiFinalsShapes[0], semiFinalsShapes[1], finalShape); // Arrange the shapes using a Layered Graph Layout NLayeredGraphLayout layout = new NLayeredGraphLayout(); layout.Direction = ENHVDirection.LeftToRight; layout.UseSingleBus = true; layout.PlugSpacing.Mode = ENPlugSpacingMode.None; layout.RegionLayout.VerticalSpacing = 25; // Controls the spacing between the title and the diagram NList shapes = page.GetShapes(false, NDiagramFilters.ShapeType2D); layout.Arrange(shapes.CastAll(), new NDrawingLayoutContext(page)); page.SizeToContent(); } #endregion #region Implementation private NShape CreateTitleShape(string title) { NDrawingTheme theme = NDrawingTheme.MyDrawNature; NShape titleShape = new NShape(); titleShape.SetBounds(0, 0, 500, 50); titleShape.Text = title; titleShape.SetProtectionMask(ENDiagramItemOperationMask.All); NTextBlock titleTextBlock = (NTextBlock)titleShape.TextBlock; titleTextBlock.ResizeMode = ENTextBlockResizeMode.ShapeSize; titleTextBlock.FontSize = 28; titleTextBlock.FontStyleBold = true; // Set theme-based colors to the title text, so that it changes when the user changes the theme NColor strokeColor = theme.ColorPalette.Variants[0][0]; strokeColor.Tag = new NThemeVariantColorInfo(0); titleTextBlock.Stroke = new NStroke(strokeColor); NColor fillColor = theme.ColorPalette.Variants[0][4]; fillColor.Tag = new NThemeVariantColorInfo(4); titleTextBlock.Fill = new NColorFill(fillColor); // Alternatively, you can also use fixed colors (uncomment the 2 lines below) //titleTextBlock.Stroke = new NStroke(NColor.DarkBlue); //titleTextBlock.Fill = new NColorFill(NColor.LightBlue); // Set an expression to center the title horizontally in the page titleShape.SetFx(NShape.PinXProperty, new NFormulaFx("$ParentSheet.X + $ParentSheet.Width / 2", true)); return titleShape; } private NShape[] CreateMatchShapes(NPage page, Match[] matches) { NShape[] matchShapes = new NShape[matches.Length]; for (int i = 0; i < matches.Length; i++) { NShape matchShape = CreateMatchShape(matches[i]); page.Items.Add(matchShape); matchShapes[i] = matchShape; } return matchShapes; } private NShape CreateMatchShape(Match match) { NShape shape = new NShape(); shape.SetBounds(0, 0, MatchShapeSize.Width, MatchShapeSize.Height); // Create a table block NTableBlock tableBlock = new NTableBlock(2, 2, NBorder.CreateFilledBorder(MatchBorderColor), MatchBorderThickness); tableBlock.PortsDistributionMode = ENPortsDistributionMode.None; tableBlock.ResizeMode = ENTableBlockResizeMode.FitToShape; shape.TextBlock = tableBlock; // Configure the table block content NTableBlockContent table = tableBlock.Content; table.AllowSpacingBetweenCells = false; table.Columns[1].PreferredWidth = NMultiLength.NewFixed(MatchShapeSize.Width / 4); // Add the match info to the table table.Rows[0].Cells[0].Blocks[0] = new NParagraph(match.Team1); table.Rows[0].Cells[1].Blocks[0] = new NParagraph(match.Score1String); table.Rows[1].Cells[0].Blocks[0] = new NParagraph(match.Team2); table.Rows[1].Cells[1].Blocks[0] = new NParagraph(match.Score2String); // Apply center vertical alignment to all cells for (int i = 0; i < table.Rows.Count; i++) { for (int j = 0; j < table.Columns.Count; j++) { table.Rows[i].Cells[j].VerticalAlignment = ENVAlign.Center; } } // Make the winner's row bold table.Rows[match.WinnerTeamIndex - 1].FontStyleBold = true; return shape; } private void ConnectShapes(NShape matchShape1, NShape matchShape2, NShape nextMatchShape) { NPage page = matchShape1.OwnerPage; NRoutableConnector con1 = new NRoutableConnector(); page.Items.Add(con1); con1.GlueBeginToShape(matchShape1); con1.GlueEndToShape(nextMatchShape); NRoutableConnector con2 = new NRoutableConnector(); page.Items.Add(con2); con2.GlueBeginToShape(matchShape2); con2.GlueEndToShape(nextMatchShape); } #endregion #region Event Handlers #endregion #region Fields private NDrawingView m_DrawingView; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types private class Match { public Match(string team1, int goals1, string team2, int goals2) : this (team1, goals1, -1, team2, goals2, -1) { } public Match(string team1, int goals1, int penalty1, string team2, int goals2, int penalty2) { Team1 = team1; m_Goals1 = goals1; m_Penalty1 = penalty1; Team2 = team2; m_Goals2 = goals2; m_Penalty2 = penalty2; } /// /// Returns the index of the winner team - 1 or 2. /// public int WinnerTeamIndex { get { if (m_Goals1 > m_Goals2) return 1; else if (m_Goals1 < m_Goals2) return 2; else return m_Penalty1 > m_Penalty2 ? 1 : 2; } } public string Score1String { get { string score = m_Goals1.ToString(); if (m_Penalty1 != -1) { score += $" ({m_Penalty1})"; } return score; } } public string Score2String { get { string score = m_Goals2.ToString(); if (m_Penalty2 != -1) { score += $" ({m_Penalty2})"; } return score; } } public string Team1; public string Team2; private int m_Goals1; private int m_Goals2; private int m_Penalty1; private int m_Penalty2; } #endregion } }

Download a fully functional C# (.cs) Automatic Layouts (Soccer Tournament) Example for Blazor Solution

Related Documentation Links