private void InitializeGraphDefaults(IGraph graph)
        {
            graph.NodeDefaults.Style = new ShinyPlateNodeStyle {
                Brush = Brushes.Orange
            };
            graph.NodeDefaults.Size = new SizeD(100, 50);

            // create a label style that shows the labels bounds
            DefaultLabelStyle labelStyle = new DefaultLabelStyle {
                BackgroundPen   = Pens.LightGray,
                BackgroundBrush = new SolidBrush(Color.FromArgb(0x77, 0xCC, 0xFF, 0xFF)),
                StringFormat    = new StringFormat()
                {
                    Alignment = StringAlignment.Center
                }
            };

            graph.NodeDefaults.Labels.Style = labelStyle;
            //Our resize logic does not work together with all label models resp. label model parameters
            //for simplicity, we just use a centered label for nodes
            graph.NodeDefaults.Labels.LayoutParameter =
                new GenericLabelModel(InteriorLabelModel.Center).CreateDefaultParameter();
            graph.EdgeDefaults.Labels.Style = labelStyle;

            var labelModel = new EdgeSegmentLabelModel {
                Distance = 10
            };

            graph.EdgeDefaults.Labels.LayoutParameter = labelModel.CreateParameterFromSource(0, 0.5, EdgeSides.RightOfEdge);

            graph.SetUndoEngineEnabled(true);
        }
        /// <summary>
        /// Initializes the graph instance setting default styles
        /// and creating a small sample graph.
        /// </summary>
        protected virtual async Task InitializeGraph()
        {
            IGraph graph = graphControl.Graph;

            // set the style as the default for all new nodes
            graph.NodeDefaults.Style = defaultStyle;
            graph.NodeDefaults.Size  = new SizeD(90, 65);

            // create a simple label style
            DefaultLabelStyle labelStyle = new DefaultLabelStyle
            {
                BackgroundBrush = Brushes.White,
                AutoFlip        = true
            };

            // set the style as the default for all new node labels
            graph.NodeDefaults.Labels.Style = labelStyle;
            graph.EdgeDefaults.Labels.Style = labelStyle;

            graph.EdgeDefaults.Labels.LayoutParameter = new EdgeSegmentLabelModel().CreateDefaultParameter();

            // create the graph and perform a layout operation
            CreateNewGraph();
            await DoLayout();
        }
Exemple #3
0
        /// <summary>
        /// Creates a deep-copy clone of this instance.
        /// </summary>
        public Style Clone()
        {
            Style style = (Style)MemberwiseClone();

            style.DefaultLabelStyle = DefaultLabelStyle.Clone();
            return(style);
        }
Exemple #4
0
        /// <summary>
        /// Initializes the graph instance, setting default styles
        /// and creating a small sample graph.
        /// </summary>
        protected virtual void InitializeGraph()
        {
            // Create the graph instance that will hold the complete graph.
            fullGraph = new DefaultGraph();

            // Create a nice default style for the nodes
            fullGraph.NodeDefaults.Style = new NodeControlNodeStyle("InnerNodeStyleTemplate");
            fullGraph.NodeDefaults.Size  = new SizeD(60, 30);
            fullGraph.NodeDefaults.ShareStyleInstance = false;


            // and a style for the labels
            DefaultLabelStyle labelStyle = new DefaultLabelStyle();

            fullGraph.NodeDefaults.Labels.Style = labelStyle;


            // now build a simple sample tree
            BuildTree(fullGraph, 3, 3, 3);

            // create a view of the graph that contains only non-collapsed subtrees.
            // use a predicate method to decide what nodes should be part of the graph.
            filteredGraph = new FilteredGraphWrapper(fullGraph, NodePredicate);

            // display the filtered graph in our control.
            graphControl.Graph = filteredGraph;
            // center the graph to prevent the initial layout fading in from the top left corner
            graphControl.FitGraphBounds();

            // create layout algorithms
            SetupLayouts();
        }
Exemple #5
0
        /// <summary>
        /// Initializes the graph instance setting default styles
        /// and creating a small sample graph.
        /// </summary>
        protected virtual async Task InitializeGraph()
        {
            IGraph graph = graphControl.Graph;

            // set the style as the default for all new nodes
            graph.NodeDefaults.Style = defaultStyle;
            // let the node decide how much space it needs and make sure it doesn't get any smaller.
            graph.NodeDefaults.Size = defaultStyle.GetPreferredSize(graphControl.CreateRenderContext(), new SimpleNode {
                Tag = new LayerConstraintsInfo()
            });
            defaultStyle.MinimumSize = graph.NodeDefaults.Size;

            // create a simple label style
            DefaultLabelStyle labelStyle = new DefaultLabelStyle
            {
                Typeface        = new Typeface("Arial"),
                BackgroundBrush = Brushes.White,
                AutoFlip        = true
            };

            // set the style as the default for all new node labels
            graph.NodeDefaults.Labels.Style = labelStyle;
            graph.EdgeDefaults.Labels.Style = labelStyle;

            graph.EdgeDefaults.Labels.LayoutParameter = new EdgeSegmentLabelModel().CreateDefaultParameter();

            // create the graph and perform a layout operation
            CreateNewGraph();
            await DoLayout();
        }
        /// <summary>Creates the label for the page header.</summary>
        /// <remarks>
        /// The page header is a dummy labeled that is anchored at a fixed location. FreeLabelModel allows arbitrary positioning.
        /// In this case, we anchor the label to a dynamic point that always corresponds to the upper left corner of the viewport.
        /// We use the ZoomInvariantLabelStyle from the tutorial to ensure we always look the same
        /// </remarks>
        public static SimpleLabel CreatePageHeader(GraphControl graphControl)
        {
            var innerLabelStyle = new DefaultLabelStyle
            {
                Typeface        = new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
                TextSize        = 20,
                BackgroundPen   = Pens.Green,
                TextBrush       = Brushes.White,
                BackgroundBrush = Brushes.Green
            };
            var headerLabel = new SimpleLabel(null, "Page Header", FreeLabelModel.Instance.CreateAnchored(
                                                  new DynamicViewPoint(graphControl, 5, 30)))
            {
                Style = new ZoomInvariantLabelStyle(innerLabelStyle)
            };

            // Adjust the size so that the text fits
            headerLabel.AdoptPreferredSizeFromStyle();

            // Since we don't have a model item for the label, we add the label's visual creator directly
            // to the scene graph
            var headerCanvasObject = graphControl.RootGroup.AddChild(new PageHeaderVisualCreator(headerLabel));

            var pageHeaderLabelEditHelper = new PageHeaderEditLabelHelper(headerLabel, headerCanvasObject);

            headerLabel.LookupImplementation = Lookups.Single <IEditLabelHelper>(pageHeaderLabelEditHelper);

            return(headerLabel);
        }
        /// <summary>
        /// Sets up default styles for graph elements.
        /// </summary>
        /// <remarks>
        /// Default styles apply only to elements created after the default style has been set,
        /// so typically, you'd set these as early as possible in your application.
        /// </remarks>
        private void SetDefaultStyles()
        {
            ///////////////// New in this Sample /////////////////

            #region Default Node Style
            // Sets the default style for nodes
            // Creates a nice ShinyPlateNodeStyle instance, using an orange Brush.
            INodeStyle defaultNodeStyle = new ShinyPlateNodeStyle {
                Brush = new SolidBrush(Color.FromArgb(255, 255, 140, 0))
            };

            // Sets this style as the default for all nodes that don't have another
            // style assigned explicitly
            Graph.NodeDefaults.Style = defaultNodeStyle;

            #endregion

            #region Default Edge Style
            // Sets the default style for edges:
            // Creates an edge style that will apply a gray pen with thickness 1
            // to the entire line using PolyLineEdgeStyle,
            // which draws a polyline determined by the edge's control points (bends)
            var defaultEdgeStyle = new PolylineEdgeStyle {
                Pen = Pens.Gray
            };

            // Sets the source and target arrows on the edge style instance
            // (Actually: no source arrow)
            // Note that IEdgeStyle itself does not have these properties
            // Also note that by default there are no arrows
            defaultEdgeStyle.TargetArrow = Arrows.Default;

            // Sets the defined edge style as the default for all edges that don't have
            // another style assigned explicitly:
            Graph.EdgeDefaults.Style = defaultEdgeStyle;
            #endregion

            #region Default Label Styles
            // Sets the default style for labels
            // Creates a label style with the label text color set to dark red
            ILabelStyle defaultLabelStyle = new DefaultLabelStyle {
                Font = new Font("Tahoma", 12), TextBrush = Brushes.DarkRed
            };

            // Sets the defined style as the default for both edge and node labels:
            Graph.EdgeDefaults.Labels.Style = Graph.NodeDefaults.Labels.Style = defaultLabelStyle;

            #endregion

            #region Default Node size
            // Sets the default size explicitly to 40x40
            Graph.NodeDefaults.Size = new SizeD(40, 40);

            #endregion

            ///////////////////////////////////////////////////
        }
        private static DefaultLabelStyle CreateInnerStyle()
        {
            var defaultLabelStyle = new DefaultLabelStyle {
                AutoFlip = true, ClipText = true, TextBrush = Brushes.Black,
                Font     = new System.Drawing.Font("Arial", 10)
            };

            defaultLabelStyle.StringFormat.LineAlignment = StringAlignment.Center;
            return(defaultLabelStyle);
        }
Exemple #9
0
        /// <summary>
        /// Creates a sample node for this demo.
        /// </summary>
        private static void CreateNode(IGraph graph, double x, double y, double w, double h, Color fillColor, Color textColor, string labelText)
        {
            var whiteTextLabelStyle = new DefaultLabelStyle {
                TextBrush = new SolidBrush(textColor)
            };
            INode node = graph.CreateNode(new RectD(x, y, w, h), new ShinyPlateNodeStyle {
                Brush = new SolidBrush(fillColor)
            }, fillColor);

            graph.SetStyle(graph.AddLabel(node, labelText), whiteTextLabelStyle);
        }
        /// <summary>
        /// Creates a sample node for this demo.
        /// </summary>
        private static INode CreateNode(IGraph graph, double x, double y, double w, double h, Color color, string labelText)
        {
            var whiteTextLabelStyle = new DefaultLabelStyle {
                TextBrush = Brushes.White
            };
            INode node = graph.CreateNode(new RectD(x, y, w, h), new ShinyPlateNodeStyle {
                Brush = new SolidBrush(color)
            }, color);

            graph.SetStyle(graph.AddLabel(node, labelText), whiteTextLabelStyle);
            return(node);
        }
Exemple #11
0
        /// <summary>
        /// Creates a sample file that uses an alternating stripe style
        /// </summary>
        private void CreateAlternatingSampleTable()
        {
            var table = new Table {
                Insets = new InsetsD(5, 30, 5, 5)
            };

            //Declare the defaults for the columns
            table.ColumnDefaults.MinimumSize = table.ColumnDefaults.Size = 200;
            var defaultLabelStyle = new DefaultLabelStyle {
                StringFormat = { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }
            };

            table.ColumnDefaults.Labels.Style = defaultLabelStyle;
            table.ColumnDefaults.Insets       = new InsetsD(5, 30, 5, 0);
            table.ColumnDefaults.Style        = new AlternatingStripeStyle
            {
                EvenStripeDescriptor =
                    new StripeDescriptor
                {
                    BackgroundBrush = new SolidBrush(Color.FromArgb(255, 153, 164, 187)),
                    InsetBrush      = new SolidBrush(Color.FromArgb(255, 204, 196, 168))
                },
                OddStripeDescriptor =
                    new StripeDescriptor
                {
                    BackgroundBrush = new SolidBrush(Color.FromArgb(255, 139, 162, 220)),
                    InsetBrush      = new SolidBrush(Color.FromArgb(255, 186, 194, 212))
                }
            };
            for (int i = 0; i < 4; ++i)
            {
                //Create four columns
                IColumn column = table.CreateColumn();
                table.AddLabel(column, "Lane " + i);
            }
            //Create a single row
            table.CreateRow(table.RootRow, 300, null, InsetsD.Empty, new NodeStyleStripeStyleAdapter(new ShapeNodeStyle {
                Brush = Brushes.Transparent
            }));

            //Create a single node and bind the table to this node
            Graph.Clear();
            Graph.CreateGroupNode(null, table.Layout.ToRectD(),
                                  new yWorks.Graph.Styles.TableNodeStyle(table)
            {
                BackgroundStyle =
                    new ShapeNodeStyle {
                    Brush = new SolidBrush(Color.FromArgb(255, 248, 236, 201))
                }
            });
            graphControl.FitGraphBounds();
        }
Exemple #12
0
        /// <summary>
        /// The default label Style
        /// </summary>
        /// <returns></returns>
        public static DefaultLabelStyle NewDefaultInstance()
        {
            var defaultLabelStyle = new DefaultLabelStyle();

            // Set font
            const string font = "Arial";

            var fontStyle = FontStyle.Regular;

            defaultLabelStyle.Font = new Font(font, LabelTextSize, fontStyle, GraphicsUnit.Pixel);
            defaultLabelStyle.StringFormat.Alignment     = StringAlignment.Center;
            defaultLabelStyle.StringFormat.LineAlignment = StringAlignment.Center;
            defaultLabelStyle.StringFormat.FormatFlags  &= ~StringFormatFlags.NoWrap;
            return(defaultLabelStyle);
        }
        /// <summary>
        /// Register custom label helpers for nodes and node labels
        /// </summary>
        private void RegisterCustomEditLabelHelper()
        {
            var firstLabelStyle = new DefaultLabelStyle {
                TextBrush = Brushes.Firebrick
            };

            // Register the helper for both nodes and edges, but only when the global flag ist set
            // We can use more or less the same implementation for both items, so we just change the item to which the helper is bound
            // The decorator on nodes is called when a label should be added or the label does not provide its own label helper
            GraphControl.Graph.GetDecorator().NodeDecorator.EditLabelHelperDecorator.SetFactory(
                node => customHelperEnabled,
                node => new MyEditLabelHelper(node, null, ExteriorLabelModel.North, firstLabelStyle));
            // The decorator on labels is called when a label is edited
            GraphControl.Graph.GetDecorator().LabelDecorator.EditLabelHelperDecorator.SetFactory(
                label => customHelperEnabled,
                label => new MyEditLabelHelper(null, label, ExteriorLabelModel.North, firstLabelStyle));
        }
        protected virtual void InitializeGraphDefaults()
        {
            var graph = GraphControl.Graph;

            graph.NodeDefaults.Style = new BevelNodeStyle();
            graph.NodeDefaults.Size  = new SizeD(50, 50);

            var labelStyle = new DefaultLabelStyle {
                BackgroundPen = Pens.Black
            };

            graph.NodeDefaults.Labels.Style           = labelStyle;
            graph.NodeDefaults.Labels.LayoutParameter = FreeNodeLabelModel.Instance.CreateParameter(
                new PointD(0.5, 0.0), new PointD(0, -10), new PointD(0.5, 1.0), PointD.Origin, 0.0);

            graph.EdgeDefaults.Labels.Style           = labelStyle;
            graph.EdgeDefaults.Labels.LayoutParameter = new SmartEdgeLabelModel().CreateParameterFromSource(0, 0, 0.5);
        }
        /// <summary>
        /// Initializes the graph instance setting default styles
        /// and creating a small sample graph.
        /// </summary>
        protected virtual void InitializeGraph()
        {
            IGraph graph = graphControl.Graph;

            // set the style as the default for all new nodes
            graph.NodeDefaults.Style = defaultStyle;
            graph.NodeDefaults.Size  = new SizeD(100, 60);

            DefaultLabelStyle labelStyle = new DefaultLabelStyle();

            labelStyle.Typeface        = new Typeface("Arial");
            labelStyle.BackgroundBrush = Brushes.LightBlue;

            // set the style as the default for all new node labels
            graph.NodeDefaults.Labels.Style = labelStyle;

            graph.CreateNode(RectD.FromCenter(new PointD(200, 100), graph.NodeDefaults.Size), graph.NodeDefaults.GetStyleInstance(), new MyBusinessObject(0.3));
        }
        private void InitializeStyles()
        {
            graphControl.Graph.NodeDefaults.Style = new ShinyPlateNodeStyle {
                Brush = Brushes.Orange, DrawShadow = false, Radius = 1
            };
            graphControl.Graph.NodeDefaults.Size = new SizeD(10, 10);

            var innerLabelStyle = new DefaultLabelStyle {
                TextSize = 8
            };
            var labelStyle = new CityLabelStyle(innerLabelStyle)
            {
                InnerLabelStyle = innerLabelStyle
            };

            graphControl.Graph.NodeDefaults.Labels.Style           = labelStyle;
            graphControl.Graph.NodeDefaults.Labels.LayoutParameter = ExteriorLabelModel.North;
        }
Exemple #17
0
        /// <summary>
        /// Initializes the graph instance setting default styles
        /// and creating a small sample graph.
        /// </summary>
        protected virtual void InitializeGraph()
        {
            IGraph graph = graphControl.Graph;

            // set the style as the default for all new nodes
            graph.NodeDefaults.Style = defaultStyle;
            graph.NodeDefaults.Size  = new SizeD(100, 60);

            DefaultLabelStyle labelStyle = new DefaultLabelStyle();
            Font font = new Font(FontFamily.GenericSansSerif, 12, GraphicsUnit.Pixel);

            labelStyle.Font            = font;
            labelStyle.BackgroundBrush = Brushes.LightBlue;

            // set the style as the default for all new node labels
            graph.NodeDefaults.Labels.Style = labelStyle;

            graph.CreateNode(RectD.FromCenter(new PointD(200, 100), graph.NodeDefaults.Size), graph.NodeDefaults.GetStyleInstance(), new MyBusinessObject(0.3));
        }
        /// <summary>
        /// Generates custom edge labels with a random value which denotes the edge weight.
        /// </summary>
        private async Task GenerateEdgeLabels()
        {
            var graph = graphControl.Graph;

            DeleteCustomEdgeLabels();

            foreach (var edge in graph.Edges)
            {
                var weightLabelStyle = new DefaultLabelStyle {
                    TextSize  = 10,
                    TextBrush = Brushes.Gray
                };

                // select a weight from 1 to 20
                var weight = useUniformWeights ? 1 : random.Next(1, 21);
                graph.AddLabel(edge, weight.ToString(), FreeEdgeLabelModel.Instance.CreateDefaultParameter(), weightLabelStyle, tag: "Weight");
            }

            await RunLayout(true, false, true);
        }
        /// <summary>
        /// Creates a group node for the sample graph with a specific styling.
        /// </summary>
        private static INode CreateGroupNode(IGraph graph, double x, double y, Color fillColor, string labelText)
        {
            var groupNode = graph.CreateGroupNode();

            graph.SetStyle(groupNode, new PanelNodeStyle {
                Color = fillColor, LabelInsetsColor = fillColor, Insets = new InsetsD(5, 20, 5, 5)
            });
            graph.SetNodeLayout(groupNode, new RectD(x, y, 130, 100));

            // The label style and placement
            var labelStyle = new DefaultLabelStyle();

            labelStyle.TextBrush = Brushes.White;
            var labelModel = new InteriorStretchLabelModel();

            labelModel.Insets = new InsetsD(5, 2, 5, 4);
            var modelParameter = labelModel.CreateParameter(InteriorStretchLabelModel.Position.North);

            graph.AddLabel(groupNode, labelText, modelParameter, labelStyle);

            groupNode.Tag = fillColor;
            return(groupNode);
        }
        /// <summary>
        /// The default label Style
        /// </summary>
        /// <returns></returns>
        public static DefaultLabelStyle NewDefaultInstance()
        {
            var defaultLabelStyle = new DefaultLabelStyle();

            // Set font
            const string font = "Arial";

            // Set text size
            defaultLabelStyle.TextSize = LabelTextSize;

            // Set font style
            var fontStyle = FontStyles.Normal;

            // Set font weight
            var fontWeight = FontWeights.Normal;

            // Set Typeface
            defaultLabelStyle.Typeface              = new Typeface(new FontFamily(font), fontStyle, fontWeight, FontStretches.Normal);
            defaultLabelStyle.TextWrapping          = TextWrapping.Wrap;
            defaultLabelStyle.TextAlignment         = TextAlignment.Center;
            defaultLabelStyle.VerticalTextAlignment = VerticalAlignment.Center;
            return(defaultLabelStyle);
        }
        /// <summary>
        /// Runs the algorithm.
        /// </summary>
        public override void RunAlgorithm(IGraph graph)
        {
            ResetGraph(graph);

            double maximumNodeCentrality;
            double minimumNodeCentrality;
            ResultItemMapping <INode, double> normalizedNodeCentrality;

            switch (algorithmType)
            {
            case CentralityMode.Weight: {
                var result = new WeightCentrality {
                    Weights = { Delegate = GetEdgeWeight }, ConsiderOutgoingEdges = true, ConsiderIncomingEdges = true
                }.Run(graph);
                maximumNodeCentrality    = result.MaximumNodeCentrality;
                minimumNodeCentrality    = result.MinimumNodeCentrality;
                normalizedNodeCentrality = result.NormalizedNodeCentrality;
                break;
            }

            case CentralityMode.Graph: {
                var result = new GraphCentrality {
                    Weights = { Delegate = GetEdgeWeight }, Directed = Directed
                }.Run(graph);
                maximumNodeCentrality    = result.MaximumNodeCentrality;
                minimumNodeCentrality    = result.MinimumNodeCentrality;
                normalizedNodeCentrality = result.NormalizedNodeCentrality;
                break;
            }

            case CentralityMode.NodeEdgeBetweenness: {
                var result = new BetweennessCentrality {
                    Directed = Directed, Weights = { Delegate = GetEdgeWeight }
                }.Run(graph);
                maximumNodeCentrality    = result.MaximumNodeCentrality;
                minimumNodeCentrality    = result.MinimumNodeCentrality;
                normalizedNodeCentrality = result.NormalizedNodeCentrality;
                result.NormalizedEdgeCentrality.ForEach((edge, centralityId) => {
                        edge.Tag = Math.Round(centralityId * 100.0) / 100;
                        graph.AddLabel(edge, edge.Tag.ToString(), tag: "Centrality");
                    });
                break;
            }

            case CentralityMode.Closeness: {
                var result = new ClosenessCentrality {
                    Directed = Directed, Weights = { Delegate = GetEdgeWeight }
                }.Run(graph);
                maximumNodeCentrality    = result.MaximumNodeCentrality;
                minimumNodeCentrality    = result.MinimumNodeCentrality;
                normalizedNodeCentrality = result.NormalizedNodeCentrality;
                break;
            }

            case CentralityMode.Degree:
            default: {
                var result = new DegreeCentrality {
                    ConsiderOutgoingEdges = true, ConsiderIncomingEdges = true
                }.Run(graph);
                maximumNodeCentrality    = result.MaximumNodeCentrality;
                minimumNodeCentrality    = result.MinimumNodeCentrality;
                normalizedNodeCentrality = result.NormalizedNodeCentrality;
                break;
            }
            }

            var min  = minimumNodeCentrality / maximumNodeCentrality;
            var diff = 1 - min;

            var mostCentralValue  = 100;
            var leastCentralValue = 30;
            var colorNumber       = 50;

            normalizedNodeCentrality.ForEach((node, centralityId) => {
                var textLabelStyle = new DefaultLabelStyle {
                    TextBrush = Brushes.White
                };
                node.Tag = Math.Round(centralityId * 100.0) / 100;

                if (double.IsNaN((double)node.Tag) || double.IsNaN(diff))
                {
                    node.Tag = "Inf";
                }
                graph.AddLabel(node, node.Tag.ToString(), style: textLabelStyle, tag: "Centrality");

                if (diff == 0 || double.IsNaN(diff))
                {
                    graph.SetStyle(node, GetMarkedNodeStyle(0, colorNumber));
                    graph.SetNodeLayout(node, new RectD(node.Layout.X, node.Layout.Y, leastCentralValue, leastCentralValue));
                }
                else
                {
                    // adjust gradient color
                    var colorScale = (colorNumber - 1) / diff;
                    var index      = Math.Max(0, Math.Min(colorNumber, (int)Math.Floor((centralityId - min) * colorScale)));
                    graph.SetStyle(node, GetMarkedNodeStyle(index, colorNumber));
                    // adjust size
                    var sizeScale = (mostCentralValue - leastCentralValue) / diff;
                    var size      = leastCentralValue + (centralityId - min) * sizeScale;
                    graph.SetNodeLayout(node, new RectD(node.Layout.X, node.Layout.Y, size, size));
                }
            });
        }
Exemple #22
0
 public LabelStyle()
 {
     style = CreateInnerStyle();
 }
Exemple #23
0
        /// <summary>
        /// Constructs an instance of <see cref="DefaultLabelStyle"/> representing this Style
        /// </summary>
        /// <param name="xStyle">The XML Element to be converted into this style</param>
        public BpmnLabelStyle(XElement xStyle)
        {
            Id              = null;
            Font            = "Arial";
            Size            = 0;
            IsBold          = false;
            IsItalic        = false;
            IsUnderline     = false;
            IsStrikeThrough = false;

            Id = BpmnNM.GetAttributeValue(xStyle, BpmnNM.Dc, BpmnDiConstants.IdAttribute);

            // Parse Values of the Label Style
            var xFont = BpmnNM.GetElement(xStyle, BpmnNM.Dc, BpmnDiConstants.FontElement);

            if (xFont != null)
            {
                Font = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.NameAttribute);

                var attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.SizeAttribute);
                if (attr != null)
                {
                    Size = float.Parse(attr, CultureInfo.InvariantCulture);
                }

                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsBoldAttribute);
                if (attr != null)
                {
                    IsBold = bool.Parse(attr);
                }
                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsItalicAttribute);
                if (attr != null)
                {
                    IsItalic = bool.Parse(attr);
                }

                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsUnderlineAttribute);
                if (attr != null)
                {
                    IsUnderline = bool.Parse(attr);
                }

                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsStrikeThroughAttribute);
                if (attr != null)
                {
                    IsStrikeThrough = bool.Parse(attr);
                }
            }

            LabelStyle = new DefaultLabelStyle {
                StringFormat =
                {
                    Alignment     = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                }
            };

            var fontStyle = FontStyle.Regular;

            // Set Boldness
            if (IsBold)
            {
                fontStyle |= FontStyle.Bold;
            }

            // Set Italic
            if (IsItalic)
            {
                fontStyle |= FontStyle.Italic;
            }

            // Set Underline
            if (IsUnderline)
            {
                fontStyle |= FontStyle.Underline;
            }

            // Set StrikeThrough
            if (IsStrikeThrough)
            {
                fontStyle |= FontStyle.Strikeout;
            }

            LabelStyle.Font = new Font(Font, Size > 0 ? Size : LabelTextSize, fontStyle, GraphicsUnit.Pixel);
            LabelStyle.StringFormat.FormatFlags &= ~StringFormatFlags.NoWrap;
        }
Exemple #24
0
        /// <summary>
        /// Creates a sample graph that uses a simple custom style for the stripes
        /// </summary>
        /// <remarks>This sample also shows how to created nested stripes</remarks>
        private void CreateSimpleNodeStyleSampleTable()
        {
            var table = new Table {
                Insets = new InsetsD(0, 30, 0, 0)
            };

            //Declare the defaults for the columns
            table.ColumnDefaults.MinimumSize = table.ColumnDefaults.Size = 500;
            table.ColumnDefaults.Insets      = new InsetsD(5, 30, 5, 30);
            var defaultLabelStyle = new DefaultLabelStyle {
                StringFormat = { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }
            };

            table.ColumnDefaults.Labels.Style = defaultLabelStyle;
            table.ColumnDefaults.Style        = new NodeStyleStripeStyleAdapter(new ShapeNodeStyle {
                Shape = ShapeNodeShape.Rectangle, Pen = Pens.Black, Brush = Brushes.Transparent
            });

            for (int i = 0; i < 2; ++i)
            {
                //Create three columns
                IColumn column = table.CreateColumn();
                table.AddLabel(column, "Milestone " + i);
            }
            //Declare the defaults for the rows
            table.RowDefaults.MinimumSize  = 50;
            table.RowDefaults.Insets       = new InsetsD(30, 0, 0, 0);
            table.RowDefaults.Labels.Style = defaultLabelStyle;
            table.RowDefaults.Style        = new NodeStyleStripeStyleAdapter(new ShapeNodeStyle {
                Shape = ShapeNodeShape.Rectangle, Pen = Pens.Black, Brush = Brushes.LightBlue
            });
            var nestedStyle1 = new NodeStyleStripeStyleAdapter(new ShapeNodeStyle {
                Shape = ShapeNodeShape.Rectangle, Pen = Pens.Black, Brush = Brushes.LightCyan
            });
            var rootRowStyle = new NodeStyleStripeStyleAdapter(new ShapeNodeStyle {
                Shape = ShapeNodeShape.Rectangle, Pen = Pens.Black, Brush = Brushes.LightCyan
            });

            IRow row_1 = table.CreateRow();

            table.SetStripeInsets(row_1, new InsetsD(30, 30, 0, 30));
            table.AddLabel(row_1, "Lane 1");
            //Nested rows for the first lane, level 1
            IRow row_1_1 = table.CreateRow(row_1, 150);

            table.SetStyle(row_1_1, nestedStyle1);
            table.AddLabel(row_1_1, "Lane 1.1");

            //Nested rows for the first lane, level 2
            IRow row_1_1_1 = table.CreateRow(row_1_1, 150);

            table.AddLabel(row_1_1_1, "Lane 1.1.1");

            IRow row_1_1_2 = table.CreateRow(row_1_1, 70);

            table.AddLabel(row_1_1_2, "Lane 1.1.2");

            IRow row_1_1_3 = table.CreateRow(row_1_1, 70);

            table.AddLabel(row_1_1_3, "Lane 1.1.3");

            //Another nested row on the first level
            IRow row_1_2 = table.CreateRow(row_1, 200);

            table.AddLabel(row_1_2, "Lane 1.2");

            IRow row_2 = table.CreateRow();

            table.SetStyle(row_2, rootRowStyle);
            table.AddLabel(row_2, "Lane 2");
            //Another nested row on the first level
            IRow row_2_1 = table.CreateRow(row_2, 150);

            table.AddLabel(row_2_1, "Lane 2.2");
            //Another nested row on the first level
            IRow row_2_2 = table.CreateRow(row_2, 150);

            table.AddLabel(row_2_2, "Lane 2.2");

            //Create a single node and bind the table to this node
            Graph.Clear();
            var node = Graph.CreateGroupNode(null, table.Layout.ToRectD(),
                                             new yWorks.Graph.Styles.TableNodeStyle(table)
            {
                BackgroundStyle =
                    new ShapeNodeStyle {
                    Brush = new SolidBrush(Color.FromArgb(236, 245, 255))
                },
                TableRenderingOrder = TableRenderingOrder.RowsFirst
            });

            Graph.AddLabel(node, "Pool 1", InteriorLabelModel.North);
            graphControl.FitGraphBounds();
        }
        /// <summary>
        /// Constructs an instance of <see cref="DefaultLabelStyle"/> representing this Style
        /// </summary>
        /// <param name="xStyle">The XML Element to be converted into this style</param>
        public BpmnLabelStyle(XElement xStyle)
        {
            Id              = null;
            Font            = "Arial";
            Size            = 0;
            IsBold          = false;
            IsItalic        = false;
            IsUnderline     = false;
            IsStrikeThrough = false;

            Id = BpmnNM.GetAttributeValue(xStyle, BpmnNM.Dc, BpmnDiConstants.IdAttribute);

            // Parse Values of the Label Style
            var xFont = BpmnNM.GetElement(xStyle, BpmnNM.Dc, BpmnDiConstants.FontElement);

            if (xFont != null)
            {
                Font = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.NameAttribute);

                var attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.SizeAttribute);
                if (attr != null)
                {
                    Size = double.Parse(attr, CultureInfo.InvariantCulture);
                }

                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsBoldAttribute);
                if (attr != null)
                {
                    IsBold = bool.Parse(attr);
                }
                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsItalicAttribute);
                if (attr != null)
                {
                    IsItalic = bool.Parse(attr);
                }

                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsUnderlineAttribute);
                if (attr != null)
                {
                    IsUnderline = bool.Parse(attr);
                }

                attr = BpmnNM.GetAttributeValue(xFont, BpmnNM.Dc, BpmnDiConstants.IsStrikeThroughAttribute);
                if (attr != null)
                {
                    IsStrikeThrough = bool.Parse(attr);
                }
            }

            LabelStyle = new DefaultLabelStyle {
                TextAlignment = TextAlignment.Center, VerticalTextAlignment = VerticalAlignment.Center
            };

            var fontStyle  = FontStyles.Normal;
            var fontWeight = FontWeights.Normal;

            // Set text size
            if (Size > 0)
            {
                LabelStyle.TextSize = Size;
            }
            else
            {
                LabelStyle.TextSize = LabelTextSize;
            }

            // Set Boldness
            if (IsBold)
            {
                fontWeight = FontWeights.Bold;
            }

            // Set Italic
            if (IsItalic)
            {
                fontStyle = FontStyles.Italic;
            }
            LabelStyle.Typeface        = new Typeface(new FontFamily(Font), fontStyle, fontWeight, FontStretches.Normal);
            LabelStyle.TextDecorations = new TextDecorationCollection();

            // Set Underline
            if (IsUnderline)
            {
                LabelStyle.TextDecorations.Add(TextDecorations.Underline);
            }

            // Set StrikeThrough
            if (IsStrikeThrough)
            {
                LabelStyle.TextDecorations.Add(TextDecorations.Strikethrough);
            }

            LabelStyle.TextWrapping = TextWrapping.Wrap;
        }
        /// <summary>
        /// Creates a sample graph and introduces all important graph elements present in
        /// yFiles.NET. Additionally, this method now overrides the label placement for some specific labels.
        /// </summary>
        private void PopulateGraph()
        {
            #region Sample Graph creation

            //////////// Sample node creation ///////////////////

            // Creates two nodes with the default node size
            // The location is specified for the _center_
            INode node1 = Graph.CreateNode(new PointD(50, 50));
            INode node2 = Graph.CreateNode(new PointD(150, 50));
            // Creates a third node with a different size of 80x40
            // In this case, the location of (360,380) describes the _upper left_
            // corner of the node bounds
            INode node3 = Graph.CreateNode(new RectD(360, 380, 80, 40));

            /////////////////////////////////////////////////////

            //////////// Sample edge creation ///////////////////

            // Creates some edges between the nodes
            IEdge edge1 = Graph.CreateEdge(node1, node2);
            IEdge edge2 = Graph.CreateEdge(node2, node3);

            /////////////////////////////////////////////////////

            //////////// Using Bends ////////////////////////////

            // Creates the first bend for edge2 at (400, 50)
            IBend bend1 = Graph.AddBend(edge2, new PointD(400, 50));

            /////////////////////////////////////////////////////

            //////////// Using Ports ////////////////////////////

            // Actually, edges connect "ports", not nodes directly.
            // If necessary, you can manually create ports at nodes
            // and let the edges connect to these.
            // Creates a port in the center of the node layout
            IPort port1AtNode1 = Graph.AddPort(node1, FreeNodePortLocationModel.NodeCenterAnchored);

            // Creates a port at the middle of the left border
            // Note to use absolute locations when placing ports using PointD.
            IPort port1AtNode3 = Graph.AddPort(node3, new PointD(node3.Layout.X, node3.Layout.GetCenter().Y));

            // Creates an edge that connects these specific ports
            IEdge edgeAtPorts = Graph.CreateEdge(port1AtNode1, port1AtNode3);

            /////////////////////////////////////////////////////

            //////////// Sample label creation ///////////////////

            // Adds labels to several graph elements
            Graph.AddLabel(node1, "N 1");
            Graph.AddLabel(node2, "N 2");
            Graph.AddLabel(node3, "N 3");
            var edgeLabel = Graph.AddLabel(edgeAtPorts, "Edge at Ports");

            /////////////////////////////////////////////////////
            /////////////////////////////////////////////////////

            #endregion

            ///////////////// New in this Sample /////////////////

            // Override default styles

            // Changes the style for the second node
            // Creates a new node style, this time a ShapeNodeStyle:
            ShapeNodeStyle sns = new ShapeNodeStyle {
                Shape = ShapeNodeShape.Ellipse, Pen = Pens.Black, Brush = Brushes.OrangeRed
            };

            // Sets the node's style property to the new style through its owning graph,
            // since you can't set the style property of an INode directly
            // (you'll set most other graph object properties this way as well)
            Graph.SetStyle(node2, sns);

            // Creates a different style for the label with black text and a red border
            // and intransparent white background
            DefaultLabelStyle sls = new DefaultLabelStyle {
                Font = new Font("Arial Black", 12), TextBrush = Brushes.Black
            };
            sls.BackgroundPen   = Pens.Red;
            sls.BackgroundBrush = Brushes.White;

            // And sets the style for the edge label, again through its owning graph.
            Graph.SetStyle(edgeLabel, sls);

            // Override the style for the "Edge at Ports" edge:
            // Uses a dashed red Pen with thickness 2.
            Pen defaultPen = new Pen(Brushes.Red, 2)
            {
                DashStyle = DashStyle.Dash
            };
            // Creates an edge style that will apply the new default pen
            // to the entire line using PolyLineEdgeStyle,
            // which draws a polyline determined by the edge's control points (bends)
            var edgeStyle = new PolylineEdgeStyle {
                Pen = defaultPen
            };

            // Sets the source and target arrows on the edge style instance
            // Note that IEdgeStyle itself does not have these properties
            // also note: by default the arrows have a default brush and pen
            edgeStyle.SourceArrow = Arrows.Circle;
            // set color and size to match the thick red line
            edgeStyle.TargetArrow = new Arrow(Color.Red)
            {
                Type = ArrowType.Short, Scale = 2
            };

            // Sets the style for the "Edge at Ports" edge, again through its owning graph.
            Graph.SetStyle(edge2, edgeStyle);

            //////////////////////////////////////////////////////
        }