Class that converts a vertex location between coordinates used in the Excel workbook and coordinates used in the NodeXL graph.
Inheritance: Object
Exemplo n.º 1
0
        //*************************************************************************
        //  Constructor: ReadWorkbookContext()
        //
        /// <summary>
        /// Initializes a new instance of the <see cref="ReadWorkbookContext" />
        /// class.
        /// </summary>
        //*************************************************************************

        public ReadWorkbookContext()
        {
            m_bIgnoreVertexLocations   = true;
            m_bFillIDColumns           = false;
            m_bPopulateVertexWorksheet = false;
            m_bReadEdgeWeights         = false;
            m_bReadGroups = false;
            m_bReadVertexColorFromGroups   = true;
            m_bReadVertexShapeFromGroups   = true;
            m_bReadVertexLabels            = false;
            m_bReadEdgeLabels              = false;
            m_bReadGroupLabels             = false;
            m_bReadVertexImages            = false;
            m_oDefaultVertexImageSize      = new Nullable <Single>();
            m_eDefaultVertexShape          = VertexShape.Disk;
            m_bReadAllEdgeAndVertexColumns = false;
            m_oGraphRectangle              = Rectangle.FromLTRB(0, 0, 100, 100);
            m_oColorConverter2             = new ColorConverter2();
            m_oBooleanConverter            = new BooleanConverter();
            m_oFontSizeConverter           = new FontSizeConverter();
            m_oEdgeWidthConverter          = new EdgeWidthConverter();
            m_oEdgeStyleConverter          = new EdgeStyleConverter();
            m_oVertexRadiusConverter       = new VertexRadiusConverter();

            m_oVertexLocationConverter =
                new VertexLocationConverter(m_oGraphRectangle);

            m_oVertexNameDictionary  = new Dictionary <String, IVertex>();
            m_oEdgeRowIDDictionary   = new Dictionary <Int32, IIdentityProvider>();
            m_oVertexRowIDDictionary = new Dictionary <Int32, IIdentityProvider>();

            AssertValid();
        }
    //*************************************************************************
    //  Constructor: ReadWorkbookContext()
    //
    /// <summary>
    /// Initializes a new instance of the <see cref="ReadWorkbookContext" />
    /// class.
    /// </summary>
    //*************************************************************************

    public ReadWorkbookContext()
    {
        m_bIgnoreVertexLocations = true;
        m_bFillIDColumns = false;
        m_bPopulateVertexWorksheet = false;
        m_bReadEdgeWeights = false;
        m_bReadGroups = false;
        m_bReadVertexColorFromGroups = true;
        m_bReadVertexShapeFromGroups = true;
        m_bReadVertexLabels = false;
        m_bReadEdgeLabels = false;
        m_bReadGroupLabels = false;
        m_bReadVertexImages = false;
        m_oDefaultVertexImageSize = new Nullable<Single>();
        m_eDefaultVertexShape = VertexShape.Disk;
        m_bReadAllEdgeAndVertexColumns = false;
        m_oGraphRectangle = Rectangle.FromLTRB(0, 0, 100, 100);
        m_oColorConverter2 = new ColorConverter2();
        m_oBooleanConverter = new BooleanConverter();
        m_oFontSizeConverter = new FontSizeConverter();
        m_oEdgeWidthConverter = new EdgeWidthConverter();
        m_oEdgeStyleConverter = new EdgeStyleConverter();
        m_oVertexRadiusConverter = new VertexRadiusConverter();

        m_oVertexLocationConverter =
            new VertexLocationConverter(m_oGraphRectangle);

        m_oVertexNameDictionary = new Dictionary<String, IVertex>();
        m_oEdgeRowIDDictionary = new Dictionary<Int32, IIdentityProvider>();
        m_oVertexRowIDDictionary = new Dictionary<Int32, IIdentityProvider>();

        AssertValid();
    }
Exemplo n.º 3
0
        TryGetLocation
        (
            ExcelTableReader.ExcelTableRow oRow,
            String sXColumnName,
            String sYColumnName,
            VertexLocationConverter oVertexLocationConverter,
            out PointF oLocation
        )
        {
            Debug.Assert(oRow != null);
            Debug.Assert(!String.IsNullOrEmpty(sXColumnName));
            Debug.Assert(!String.IsNullOrEmpty(sYColumnName));
            Debug.Assert(oVertexLocationConverter != null);
            AssertValid();

            oLocation = PointF.Empty;

            String sX, sY;

            Boolean bHasX = oRow.TryGetNonEmptyStringFromCell(
                sXColumnName, out sX);

            Boolean bHasY = oRow.TryGetNonEmptyStringFromCell(
                sYColumnName, out sY);

            if (bHasX != bHasY)
            {
                // X or Y alone won't do.

                goto Error;
            }

            if (!bHasX && !bHasY)
            {
                return(false);
            }

            Single fX, fY;

            if (!Single.TryParse(sX, out fX) || !Single.TryParse(sY, out fY))
            {
                goto Error;
            }

            // Transform the location from workbook coordinates to graph
            // coordinates.

            oLocation = oVertexLocationConverter.WorkbookToGraph(fX, fY);

            return(true);

Error:

            Range oInvalidCell = oRow.GetRangeForCell(sXColumnName);

            OnWorkbookFormatError(String.Format(

                                      "There is a problem with the location at {0}.  If you enter a"
                                      + " location, it must include both X and Y numbers.  Any"
                                      + " numbers are acceptable, although {1} is used for any"
                                      + " number less than {1} and and {2} is used for any number"
                                      + " greater than {2}."
                                      ,
                                      ExcelUtil.GetRangeAddress(oInvalidCell),

                                      VertexLocationConverter.MinimumXYWorkbook.ToString(
                                          ExcelTemplateForm.Int32Format),

                                      VertexLocationConverter.MaximumXYWorkbook.ToString(
                                          ExcelTemplateForm.Int32Format)
                                      ),

                                  oInvalidCell
                                  );

            // Make the compiler happy.

            return(false);
        }
Exemplo n.º 4
0
        SetLocations <TLocationInfo>
        (
            IEnumerable <TLocationInfo> oLocationInfo,
            System.Drawing.Rectangle oGraphRectangle,
            TryGetRowIDAndLocation <TLocationInfo> oTryGetRowIDAndLocation,
            Dictionary <Int32, Int32> oRowIDDictionary,
            Range oXColumnData,
            Range oYColumnData,
            Object [,] aoLockedColumnValues
        )
        {
            Debug.Assert(oLocationInfo != null);
            Debug.Assert(oTryGetRowIDAndLocation != null);
            Debug.Assert(oRowIDDictionary != null);
            Debug.Assert(oXColumnData != null);
            Debug.Assert(oYColumnData != null);
            AssertValid();

            Int32 iRowNumberOneBased;

            Object [,] aoXValues = ExcelUtil.GetRangeValues(oXColumnData);
            Object [,] aoYValues = ExcelUtil.GetRangeValues(oYColumnData);

            // This is the row number of the table's first data row.

            Int32 iDataBodyRangeRowOneBased = oXColumnData.Row;

            // Create an object that converts a location between coordinates used
            // in the NodeXL graph and coordinates used in the worksheet.

            VertexLocationConverter oVertexLocationConverter =
                new VertexLocationConverter(oGraphRectangle);

            foreach (TLocationInfo oOneLocationInfo in oLocationInfo)
            {
                Int32  iRowID;
                PointF oLocationToSet;

                if (
                    !oTryGetRowIDAndLocation(oOneLocationInfo, out iRowID,
                                             out oLocationToSet)
                    ||
                    !oRowIDDictionary.TryGetValue(iRowID, out iRowNumberOneBased)
                    )
                {
                    continue;
                }

                Int32 iRowToWriteOneBased =
                    iRowNumberOneBased - iDataBodyRangeRowOneBased + 1;

                if (
                    aoLockedColumnValues != null
                    &&
                    VertexIsLocked(aoLockedColumnValues, iRowToWriteOneBased)
                    )
                {
                    continue;
                }

                // Convert the location to workbook coordinates before writing it
                // to the X and Y column cells.

                Single fWorkbookX, fWorkbookY;

                oVertexLocationConverter.GraphToWorkbook(oLocationToSet,
                                                         out fWorkbookX, out fWorkbookY);

                aoXValues[iRowToWriteOneBased, 1] = fWorkbookX;
                aoYValues[iRowToWriteOneBased, 1] = fWorkbookY;
            }

            // Write the X and Y columns.

            oXColumnData.set_Value(Missing.Value, aoXValues);
            oYColumnData.set_Value(Missing.Value, aoYValues);
        }
 SetUp()
 {
     m_oVertexLocationConverter =
         new VertexLocationConverter(GraphRectangle);
 }
    TestGraphToWorkbook4()
    {
        // Empty rectangle.

        m_oVertexLocationConverter = new VertexLocationConverter(
            Rectangle.FromLTRB(100, 200, 100, 200) );

        Single fGraphX = 600;
        Single fGraphY = 1200;

        Single ExpectedWorkbookX = VertexLocationConverter.MinimumXYWorkbook;
        Single ExpectedWorkbookY = ExpectedWorkbookX;

        Single fWorkbookX, fWorkbookY;

        m_oVertexLocationConverter.GraphToWorkbook(
            new PointF(fGraphX, fGraphY), out fWorkbookX, out fWorkbookY);

        Assert.AreEqual(ExpectedWorkbookX, fWorkbookX);
        Assert.AreEqual(ExpectedWorkbookY, fWorkbookY);
    }
    TestWorkbookToGraph4()
    {
        // Empty rectangle.

        m_oVertexLocationConverter = new VertexLocationConverter(
            Rectangle.FromLTRB(100, 200, 100, 200) );

        Single WorkbookX = VertexLocationConverter.MaximumXYWorkbook;
        Single WorkbookY = VertexLocationConverter.MaximumXYWorkbook;
        Single ExpectedGraphX = 100;
        Single ExpectedGraphY = 200;

        PointF oGraphPointF = m_oVertexLocationConverter.WorkbookToGraph(
            WorkbookX, WorkbookY);

        Assert.AreEqual(ExpectedGraphX, oGraphPointF.X);
        Assert.AreEqual(ExpectedGraphY, oGraphPointF.Y);
    }
    TryGetLocation
    (
        ExcelTableReader.ExcelTableRow oRow,
        String sXColumnName,
        String sYColumnName,
        VertexLocationConverter oVertexLocationConverter,
        out PointF oLocation
    )
    {
        Debug.Assert(oRow != null);
        Debug.Assert( !String.IsNullOrEmpty(sXColumnName) );
        Debug.Assert( !String.IsNullOrEmpty(sYColumnName) );
        Debug.Assert(oVertexLocationConverter != null);
        AssertValid();

        oLocation = PointF.Empty;

        String sX, sY;

        Boolean bHasX = oRow.TryGetNonEmptyStringFromCell(
            sXColumnName, out sX);

        Boolean bHasY = oRow.TryGetNonEmptyStringFromCell(
            sYColumnName, out sY);

        if (bHasX != bHasY)
        {
            // X or Y alone won't do.

            goto Error;
        }

        if (!bHasX && !bHasY)
        {
            return (false);
        }

        Single fX, fY;

        if ( !Single.TryParse(sX, out fX) || !Single.TryParse(sY, out fY) )
        {
            goto Error;
        }

        // Transform the location from workbook coordinates to graph
        // coordinates.

        oLocation = oVertexLocationConverter.WorkbookToGraph(fX, fY);

        return (true);

        Error:

            Range oInvalidCell = oRow.GetRangeForCell(sXColumnName);

            OnWorkbookFormatError( String.Format(

                "There is a problem with the location at {0}.  If you enter a"
                + " location, it must include both X and Y numbers.  Any"
                + " numbers are acceptable, although {1} is used for any"
                + " number less than {1} and and {2} is used for any number"
                + " greater than {2}."
                ,
                ExcelUtil.GetRangeAddress(oInvalidCell),

                VertexLocationConverter.MinimumXYWorkbook.ToString(
                    ExcelTemplateForm.Int32Format),

                VertexLocationConverter.MaximumXYWorkbook.ToString(
                    ExcelTemplateForm.Int32Format)
                ),

                oInvalidCell
                );

            // Make the compiler happy.

            return (false);
    }