Smrf.NodeXL.Visualization.Wpf.GraphImageScaler.SetGraphScale C# (CSharp) Method

SetGraphScale() public method

public SetGraphScale ( Int32 bitmapWidthPx, Int32 bitmapHeightPx, Double screenDpi ) : void
bitmapWidthPx System.Int32
bitmapHeightPx System.Int32
screenDpi Double
return void
    SetGraphScale
    (
        Int32 bitmapWidthPx,
        Int32 bitmapHeightPx,
        Double screenDpi
    )
    {
        Debug.Assert(screenDpi > 0);
        Debug.Assert(bitmapWidthPx > 0);
        Debug.Assert(bitmapHeightPx > 0);
        AssertValid();

        GraphDrawer oGraphDrawer = m_oNodeXLControl.GraphDrawer;
        GroupDrawer oGroupDrawer = oGraphDrawer.GroupDrawer;

        // Note that group labels have their own scale, distinct from the scale
        // used by the other graph elements.

        m_dOriginalGraphScale = oGraphDrawer.GraphScale;
        m_dOriginalGroupLabelScale = oGroupDrawer.LabelScale;

        // Get the actual control size, in units of 1/96".

        Double dActualWidthWpf = m_oNodeXLControl.ActualWidth;
        Double dActualHeightWpf = m_oNodeXLControl.ActualHeight;

        // Convert this to pixels.

        Double dActualWidthPx = WpfGraphicsUtil.WpfToPx(
            dActualWidthWpf, screenDpi);

        Double dActualHeightPx = WpfGraphicsUtil.WpfToPx(
            dActualHeightWpf, screenDpi);

        // If the image is smaller (or larger) than the control, adjust the
        // GraphScale property to shrink (or enlarge) the graph's vertices and
        // edges accordingly.  Use the smaller of the two bitmap/actual ratio.

        Double dGraphScaleFactor = Math.Min(
            bitmapWidthPx / dActualWidthPx,
            bitmapHeightPx / dActualHeightPx
            );

        Double dGraphScale =
            PinScale(m_dOriginalGraphScale * dGraphScaleFactor);

        Double dGroupLabelScale =
            PinScale(m_dOriginalGroupLabelScale * dGraphScaleFactor);

        // Don't set the NodeXLControl.GraphScale property, which would cause
        // the NodeXLControl.GraphScaleChanged event to fire.  Set the property
        // on the GraphDrawer instead.

        oGraphDrawer.GraphScale = dGraphScale;

        oGroupDrawer.LabelScale = dGroupLabelScale;

        AssertValid();
    }

Usage Example

    Composite
    (
        Double compositeWidth,
        Double compositeHeight,
        String headerText,
        String footerText,
        System.Drawing.Font headerFooterFont,
        IEnumerable<LegendControlBase> legendControls
    )
    {
        Debug.Assert(compositeWidth > 0);
        Debug.Assert(compositeHeight > 0);
        Debug.Assert(headerFooterFont != null);
        Debug.Assert(legendControls != null);
        AssertValid();

        // Note:
        //
        // Don't try taking a shortcut by using
        // NodeXLControl.CopyGraphToBitmap() to get an image of the graph and
        // then compositing the image with the other elements.  That would work
        // if the caller were creating an image, but if it were creating an XPS
        // document, the graph would no longer be scalable.

        Double dScreenDpi =
            WpfGraphicsUtil.GetScreenDpi(m_oNodeXLControl).Width;

        // The NodeXLControl can't be a child of two logical trees, so
        // disconnect it from its parent after saving the current vertex
        // locations.

        m_oLayoutSaver = new LayoutSaver(m_oNodeXLControl.Graph);

        Debug.Assert(m_oNodeXLControl.Parent is Panel);
        m_oParentPanel = (Panel)m_oNodeXLControl.Parent;
        UIElementCollection oParentChildren = m_oParentPanel.Children;
        m_iChildIndex = oParentChildren.IndexOf(m_oNodeXLControl);
        oParentChildren.Remove(m_oNodeXLControl);

        m_oGraphImageScaler = new GraphImageScaler(m_oNodeXLControl);

        m_oGraphImageCenterer = new NodeXLControl.GraphImageCenterer(
            m_oNodeXLControl);

        // The header and footer are rendered as Label controls.  The legend is
        // rendered as a set of Image controls.

        Label oHeaderLabel, oFooterLabel;
        IEnumerable<Image> oLegendImages;
        Double dHeaderHeight, dTotalLegendHeight, dFooterHeight;

        CreateHeaderOrFooterLabel(headerText, compositeWidth, headerFooterFont,
            out oHeaderLabel, out dHeaderHeight);

        CreateLegendImages(legendControls, compositeWidth, out oLegendImages,
            out dTotalLegendHeight);

        CreateHeaderOrFooterLabel(footerText, compositeWidth, headerFooterFont,
            out oFooterLabel, out dFooterHeight);

        m_oNodeXLControl.Width = compositeWidth;

        m_oNodeXLControl.Height = Math.Max(10,
            compositeHeight - dHeaderHeight - dTotalLegendHeight
            - dFooterHeight);

        // Adjust the control's graph scale so that the graph's vertices and
        // edges will be the same relative size in the composite that they are
        // in the control.

        m_oGraphImageScaler.SetGraphScale(
            (Int32)WpfGraphicsUtil.WpfToPx(compositeWidth, dScreenDpi),
            (Int32)WpfGraphicsUtil.WpfToPx(m_oNodeXLControl.Height, dScreenDpi),
            dScreenDpi);

        // Adjust the NodeXLControl's translate transforms so that the
        // composite will be centered on the same point on the graph that the
        // NodeXLControl is centered on.

        m_oGraphImageCenterer.CenterGraphImage( new Size(compositeWidth,
            m_oNodeXLControl.Height) );

        StackPanel oStackPanel = new StackPanel();
        UIElementCollection oStackPanelChildren = oStackPanel.Children;

        // To avoid a solid black line at the bottom of the header or the top
        // of the footer, which is caused by rounding errors, make the
        // StackPanel background color the same as the header and footer.

        oStackPanel.Background = HeaderFooterBackgroundBrush;

        if (oHeaderLabel != null)
        {
            oStackPanelChildren.Add(oHeaderLabel);
        }

        // Wrap the NodeXLControl in a Grid to clip it.

        m_oGrid = new Grid();
        m_oGrid.Width = m_oNodeXLControl.Width;
        m_oGrid.Height = m_oNodeXLControl.Height;
        m_oGrid.ClipToBounds = true;
        m_oGrid.Children.Add(m_oNodeXLControl);

        oStackPanelChildren.Add(m_oGrid);

        foreach (Image oLegendImage in oLegendImages)
        {
            oStackPanelChildren.Add(oLegendImage);
        }

        if (oFooterLabel != null)
        {
            oStackPanelChildren.Add(oFooterLabel);
        }

        Size oCompositeSize = new Size(compositeWidth, compositeHeight);
        Rect oCompositeRectangle = new Rect(new Point(), oCompositeSize);

        oStackPanel.Measure(oCompositeSize);
        oStackPanel.Arrange(oCompositeRectangle);
        oStackPanel.UpdateLayout();

        return (oStackPanel);
    }
All Usage Examples Of Smrf.NodeXL.Visualization.Wpf.GraphImageScaler::SetGraphScale