locked
Additing Title Sub Title onto GraphView RRS feed

  • Question

  • I've a simple chart - no problem displaying it, etc. But when my users want to print it out it now starts looking rather bland.

    How can I add things like a title, subtitle - I do not want to make MSAGL into Crystal, but the drawn chart needs a title ?

    I've tried modfying the viewer _paint method with something like this

       public void viewer_OnPaint(object o, PaintEventArgs e)
       {
         .... lines ommitted
         e.Graphics.DrawString("Test"....);      
       }

    But no output.

    Any pointers ?

      Many thanks - it is still a great product.

         Tim

         
    Sunday, April 26, 2009 6:17 AM

Answers

  • Hi Tim,
    I have added this functionality.  Please replace the corresponding piece of code from GViewer.cs by the one below.
    The graph label will be displayed if DrawGraphLabel is set to true.
    Thanks,
    Lev

            bool drawGraphLabel = false;
            /// <summary>
            /// if set to true draws the graph label
            /// </summary>
            public bool DrawGraphLabel {
                get { return drawGraphLabel; }
                set { drawGraphLabel = value; }
            }


            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
            private void LayoutAndCreateDGraph() {
    #if DEBUGGLEE
                if (System.Environment.GetEnvironmentVariable("debugglee") == "on") {
                    Stream stream = File.Open("c:/tmp/currentGraph", FileMode.Create);
                    BinaryFormatter bformatter = new BinaryFormatter();
                    try {
                        Console.WriteLine("Writing current graph");
                        bformatter.Serialize(stream, Graph);
                    } catch (System.Runtime.Serialization.SerializationException e) {
                        Console.WriteLine(e.ToString());
                        MessageBox.Show(e.ToString());
                    }
                    stream.Close();
                }

    #endif

                bool needOpaqueColorsForNode = false;
                switch (this.CurrentLayoutMethod) {
                    case LayoutMethod.SugiyamaScheme:
                        if (!(OriginalGraph.LayoutAlgorithmSettings is SugiyamaLayoutSettings))
                            OriginalGraph.LayoutAlgorithmSettings = sugiyamaSettings;
                        break;
                    case LayoutMethod.MDS:
                        needOpaqueColorsForNode = true;
                        if (!(OriginalGraph.LayoutAlgorithmSettings is Microsoft.Msagl.Mds.MdsLayoutSettings))
                            OriginalGraph.LayoutAlgorithmSettings = mdsLayoutSettings;
                        break;
                    case LayoutMethod.Ranking:
                        needOpaqueColorsForNode = true;
                        if (!(OriginalGraph.LayoutAlgorithmSettings is Microsoft.Msagl.Mds.RankingLayoutSettings))
                            OriginalGraph.LayoutAlgorithmSettings = rankingSettings;
                        break;
                }

                OriginalGraph.CreateGeometryGraph();
                GeometryGraph gleeGraph = OriginalGraph.GeometryGraph;
                if (needOpaqueColorsForNode)
                    MakeNodesOpaque();
                DGraph = DGraph.CreateDGraphAndGeometryInfo(OriginalGraph, gleeGraph);
                gleeGraph.CalculateLayout();
                TransferGeometryFromMsaglGraphToGraph(gleeGraph);
                var graphLabel = OriginalGraph.Label;
                if (DrawGraphLabel && graphLabel != null && !String.IsNullOrEmpty(graphLabel.Text)) {
                    Microsoft.Msagl.Drawing.Node labelDrawingNode = new Microsoft.Msagl.Drawing.Node("labelId______#$");
                    labelDrawingNode.Label = graphLabel;
                    labelDrawingNode.Attr.Shape = Shape.Plaintext;

                    double width, height;
                    Microsoft.Msagl.GraphViewerGdi.StringMeasure.MeasureWithFont(labelDrawingNode.Label.Text, new Font(labelDrawingNode.Label.FontName, labelDrawingNode.Label.FontSize), out width, out height);
                    Point center = (gleeGraph.BoundingBox.LeftTop + gleeGraph.BoundingBox.RightTop) / 2 + new Point(0, height / 2);
                    CreateNodeGeometry(labelDrawingNode, center);
                    Microsoft.Msagl.Drawing.IViewerNode labelViewerNode = CreateNode(labelDrawingNode);
                    this.AddNode(labelViewerNode, false);
                    DGraph.DrawingGraph.GeometryGraph.ExtendBoundingBox(labelDrawingNode.Label.BoundingBox.LeftTop);
                }       
            }

            private void CreateNodeGeometry(Microsoft.Msagl.Drawing.Node node, Microsoft.Msagl.Point center) {
                Microsoft.Msagl.Node geomNode =
                    node.Attr.GeometryNode = Microsoft.Msagl.Drawing.CreateLayoutGraph.CreateGeometryNode(this.Graph.GeometryGraph, node, Connection.Disconnected);
                geomNode.Center = center;
            }


    Lev Nachmanson
    • Edited by Lev Nachmanson Tuesday, April 28, 2009 2:42 PM
    • Marked as answer by Tim Seed Sunday, May 3, 2009 7:48 AM
    Tuesday, April 28, 2009 12:44 AM

All replies

  • Hi Tim,
    I have added this functionality.  Please replace the corresponding piece of code from GViewer.cs by the one below.
    The graph label will be displayed if DrawGraphLabel is set to true.
    Thanks,
    Lev

            bool drawGraphLabel = false;
            /// <summary>
            /// if set to true draws the graph label
            /// </summary>
            public bool DrawGraphLabel {
                get { return drawGraphLabel; }
                set { drawGraphLabel = value; }
            }


            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
            private void LayoutAndCreateDGraph() {
    #if DEBUGGLEE
                if (System.Environment.GetEnvironmentVariable("debugglee") == "on") {
                    Stream stream = File.Open("c:/tmp/currentGraph", FileMode.Create);
                    BinaryFormatter bformatter = new BinaryFormatter();
                    try {
                        Console.WriteLine("Writing current graph");
                        bformatter.Serialize(stream, Graph);
                    } catch (System.Runtime.Serialization.SerializationException e) {
                        Console.WriteLine(e.ToString());
                        MessageBox.Show(e.ToString());
                    }
                    stream.Close();
                }

    #endif

                bool needOpaqueColorsForNode = false;
                switch (this.CurrentLayoutMethod) {
                    case LayoutMethod.SugiyamaScheme:
                        if (!(OriginalGraph.LayoutAlgorithmSettings is SugiyamaLayoutSettings))
                            OriginalGraph.LayoutAlgorithmSettings = sugiyamaSettings;
                        break;
                    case LayoutMethod.MDS:
                        needOpaqueColorsForNode = true;
                        if (!(OriginalGraph.LayoutAlgorithmSettings is Microsoft.Msagl.Mds.MdsLayoutSettings))
                            OriginalGraph.LayoutAlgorithmSettings = mdsLayoutSettings;
                        break;
                    case LayoutMethod.Ranking:
                        needOpaqueColorsForNode = true;
                        if (!(OriginalGraph.LayoutAlgorithmSettings is Microsoft.Msagl.Mds.RankingLayoutSettings))
                            OriginalGraph.LayoutAlgorithmSettings = rankingSettings;
                        break;
                }

                OriginalGraph.CreateGeometryGraph();
                GeometryGraph gleeGraph = OriginalGraph.GeometryGraph;
                if (needOpaqueColorsForNode)
                    MakeNodesOpaque();
                DGraph = DGraph.CreateDGraphAndGeometryInfo(OriginalGraph, gleeGraph);
                gleeGraph.CalculateLayout();
                TransferGeometryFromMsaglGraphToGraph(gleeGraph);
                var graphLabel = OriginalGraph.Label;
                if (DrawGraphLabel && graphLabel != null && !String.IsNullOrEmpty(graphLabel.Text)) {
                    Microsoft.Msagl.Drawing.Node labelDrawingNode = new Microsoft.Msagl.Drawing.Node("labelId______#$");
                    labelDrawingNode.Label = graphLabel;
                    labelDrawingNode.Attr.Shape = Shape.Plaintext;

                    double width, height;
                    Microsoft.Msagl.GraphViewerGdi.StringMeasure.MeasureWithFont(labelDrawingNode.Label.Text, new Font(labelDrawingNode.Label.FontName, labelDrawingNode.Label.FontSize), out width, out height);
                    Point center = (gleeGraph.BoundingBox.LeftTop + gleeGraph.BoundingBox.RightTop) / 2 + new Point(0, height / 2);
                    CreateNodeGeometry(labelDrawingNode, center);
                    Microsoft.Msagl.Drawing.IViewerNode labelViewerNode = CreateNode(labelDrawingNode);
                    this.AddNode(labelViewerNode, false);
                    DGraph.DrawingGraph.GeometryGraph.ExtendBoundingBox(labelDrawingNode.Label.BoundingBox.LeftTop);
                }       
            }

            private void CreateNodeGeometry(Microsoft.Msagl.Drawing.Node node, Microsoft.Msagl.Point center) {
                Microsoft.Msagl.Node geomNode =
                    node.Attr.GeometryNode = Microsoft.Msagl.Drawing.CreateLayoutGraph.CreateGeometryNode(this.Graph.GeometryGraph, node, Connection.Disconnected);
                geomNode.Center = center;
            }


    Lev Nachmanson
    • Edited by Lev Nachmanson Tuesday, April 28, 2009 2:42 PM
    • Marked as answer by Tim Seed Sunday, May 3, 2009 7:48 AM
    Tuesday, April 28, 2009 12:44 AM
  • Lev,
          Great - That gives me a starting point to add some extra things.

          I've added a CString object as well as your bool, to I can set the text to something slightly more meaningful (But I get the drift). I'll have a look an see if I can make the Title RTF formatted (which whould be very nice hint hint)

          Thanks for helping

               Tim
    Sunday, May 3, 2009 7:47 AM