How to draw vertex with its original position after loading graphml file?

Apr 20, 2013 at 4:37 AM
Dear tcap479! I have a graphml file and it contains the (X, Y)-axis coordinate. How could I draw these vertices with its coordinate(V-X, V-Y) after using LoadGraphFromFile function? Can you show me some example code?
Apr 22, 2013 at 5:20 AM
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.IO;
using System.Diagnostics;
using Smrf.NodeXL.Core;
using Smrf.NodeXL.Adapters;
using Smrf.NodeXL.Visualization.Wpf;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

    const Int32 GraphWidth = 600;
    const Int32 GraphHeight = 500;
    const Int32 Margin = 10;

    // It's assumed here that the vertices in the GraphML file have "X" and
    // "Y" GraphML-attributes.

    IGraph graph = ( new GraphMLGraphAdapter() ).LoadGraphFromFile(
        @"T:\Temp\TempGraphML.graphml");

    IVertexCollection vertices = graph.Vertices;

    // LoadGraphFromFile() stored all the edge and vertex
    // GraphML-attribute values as String metadata on the edges and
    // vertices.  It did NOT set the vertex locations.
    //
    // Retrieve the String metadata for the X- and Y-coordinates, convert
    // them to numbers, and use the numbers to set the vertex locations.

    foreach (IVertex vertex in vertices)
    {
        Single xCoordinateOriginal = Single.Parse(
            (String)vertex.GetRequiredValue( "X", typeof(String) ) );

        Single yCoordinateOriginal = Single.Parse(
            (String)vertex.GetRequiredValue( "Y", typeof(String) ) );

        vertex.Location =
            new PointF(xCoordinateOriginal, yCoordinateOriginal);
    }

    // It's assumed here that the full range of X-coordinates in the
    // GraphML should be linearly transformed to the full range of
    // X-coordinates in the graph, and ditto for the Y-coordinates.  Do
    // the linear transformations.
    //
    // (Note that a for-loop could be used to eliminate the redundant
    // loops in the following Linq expressions.)

    Double minimumXCoordinateOriginal = 
        ( from IVertex vertex in vertices
            select vertex.Location.Y ).Min();

    Double maximumXCoordinateOriginal = 
        ( from IVertex vertex in vertices
            select vertex.Location.Y ).Max();

    Double minimumYCoordinateOriginal = 
        (from IVertex vertex in vertices
            select vertex.Location.Y).Min();

    Double maximumYCoordinateOriginal = 
        (from IVertex vertex in vertices
            select vertex.Location.Y).Max();

    foreach (IVertex vertex in vertices)
    {
        Double xCoordinateOriginal = vertex.Location.X;
        Double yCoordinateOriginal = vertex.Location.Y;

        Double xCoordinateTransformed = TransformValueToRange(
            xCoordinateOriginal,
            minimumXCoordinateOriginal,
            maximumXCoordinateOriginal,
            0 + Margin,
            GraphWidth - Margin
            );

        Double yCoordinateTransformed = TransformValueToRange(
            yCoordinateOriginal,
            minimumYCoordinateOriginal,
            maximumYCoordinateOriginal,
            0 + Margin,
            GraphHeight - Margin
            );

        vertex.Location = new PointF(
            (Single)xCoordinateTransformed,
            (Single)yCoordinateTransformed
            );
    }

    // Create an object that can render a NodeXL graph as a Visual.

    NodeXLVisual nodeXLVisual = new NodeXLVisual();

    // Use the NodeXLVisual object's GraphDrawer to draw the graph onto the
    // Visual.

    GraphDrawingContext graphDrawingContext = new GraphDrawingContext(
        new Rect(0, 0, GraphWidth, GraphHeight), Margin,
        System.Windows.Media.Color.FromRgb(255, 255, 255));

    nodeXLVisual.GraphDrawer.DrawGraph(graph, graphDrawingContext);

    // Convert the Visual to a Bitmap.

    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
        GraphWidth, GraphHeight, 96, 96, PixelFormats.Default);

    renderTargetBitmap.Render(nodeXLVisual);
    BmpBitmapEncoder bmpBitmapEncoder = new BmpBitmapEncoder();
    bmpBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
    MemoryStream oMemoryStream = new MemoryStream();
    bmpBitmapEncoder.Save(oMemoryStream);
    Bitmap bitmap = new Bitmap(oMemoryStream);

    // Save the Bitmap to a file.

    bitmap.Save(@"T:\Temp\TempGraphImage.png", ImageFormat.Png);

}

//*************************************************************************
//  Method: TransformValueToRange()
//
/// <summary>
/// Linearly transforms a value from one range to another.
/// </summary>
///
/// <param name="valueRangeA">
/// Value to transform, in range A.
/// </param>
///
/// <param name="minimumValueRangeA">
/// Minimum value in range A.
/// </param>
///
/// <param name="maximumValueRangeA">
/// Maximum value in range A.  Can't be equal to <paramref
/// name="minimumValueRangeA" />.
/// </param>
///
/// <param name="minimumValueRangeB">
/// Minimum value in range B.
/// </param>
///
/// <param name="maximumValueRangeB">
/// Maximum value in range B.
/// </param>
///
/// <returns>
/// <paramref name="valueRangeA" /> transformed to range B, pinned to the
/// limits of range B.
/// </returns>
//*************************************************************************

private static Double
TransformValueToRange
(
    Double valueRangeA,
    Double minimumValueRangeA,
    Double maximumValueRangeA,
    Double minimumValueRangeB,
    Double maximumValueRangeB
)
{
    Debug.Assert(maximumValueRangeA != minimumValueRangeA);

    Double fValueRangeB =

        minimumValueRangeB + (valueRangeA - minimumValueRangeA) *

        (maximumValueRangeB - minimumValueRangeB) /
            (maximumValueRangeA - minimumValueRangeA);

    // Pin the value to the range B limits.

    fValueRangeB = Math.Max(fValueRangeB, minimumValueRangeB);
    fValueRangeB = Math.Min(fValueRangeB, maximumValueRangeB);

    return (fValueRangeB);
}

}
}
Apr 22, 2013 at 10:21 AM
Thank you very much! :P