ThinkGeo.com    |     Documentation    |     Premium Support

Change ZedGraph size based on zoom level

 Hi,


I've just started using MapSuite webedition and zedgraphs in it and I am finally having some success, thanks to the support team here.


However I have a new query. How can I change the size of Zedgraphs with the based on the zoom level of the Map? Could anyone point me to a sample Web application. 


Also I tried doing it by add a ExtentChanged eventhandler of my own. But now the extent always goes back to the initial extent .. can anyone please point out why that is? You can get an idea from the code below.


Many Thanks,


Asim.



using System;


using System.Drawing;


using System.Web.UI;


using ThinkGeo.MapSuite.Core;


using ThinkGeo.MapSuite.WebEdition;


using ZedGraph;


 


namespace Oasis


{


    public partial class _Default : System.Web.UI.Page


    {


        protected void Page_Load(object sender, EventArgs e)


        {


           


                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));


                Map1.CurrentExtent = new RectangleShape(-123.41875, 41.96396484375, -107.158984375, 30.36240234375);


                Map1.MapUnit = GeographyUnit.DecimalDegree;


 


                WorldMapKitWmsWebOverlay worldMapKitOverlay = new WorldMapKitWmsWebOverlay();


                Map1.CustomOverlays.Add(worldMapKitOverlay);


 


                //Create our Zedgraph Sytle and wire up the event.  


                ZedGraphStyle zedGraphStyle = new ZedGraphStyle();


                zedGraphStyle.ZedGraphDrawing += new EventHandler<ZedGraphDrawingEventArgs>(zedGraphStyle_ZedGraphDrawing);


                


                // Map Extent change event.


                Map1.ExtentChanged += new EventHandler<ExtentChangedEventArgs>(Map1_ExtentChanged1);


 


                zedGraphStyle.RequiredColumnNames.Add("Reached");


                zedGraphStyle.RequiredColumnNames.Add("Remaining");


                zedGraphStyle.RequiredColumnNames.Add("AreaName");


 


                ShapeFileFeatureLayer citiesLayer = new ShapeFileFeatureLayer(@"F:\Oasis\Pak Admin Bndry\Prov_Centriods.shp");


                citiesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(zedGraphStyle);


                citiesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(TextStyles.City3("AreaName"));


                citiesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;


                citiesLayer.DrawingMarginPercentage = 100;


 


                LayerOverlay staticOverlay = new LayerOverlay();


                staticOverlay.Layers.Add("Cities", citiesLayer);


                staticOverlay.IsBaseOverlay = false;


                Map1.CustomOverlays.Add(staticOverlay);


            }


 


        private void Map1_ExtentChanged1(object sender, ExtentChangedEventArgs e)


        {


            Label1.Text = Label1.Text + "yeah baby!!!";


        }


 


        private void zedGraphStyle_ZedGraphDrawing(object sender, ZedGraphDrawingEventArgs e)


        {


            Label1.Text = Label1.Text + "yeah baby!!!";


            ChangeLabelPosition(((ShapeFileFeatureLayer)((LayerOverlay)Map1.CustomOverlays[1]).Layers["Cities"]), 100);


 


            ZedGraphControl zedGraph = new ZedGraphControl();


 


            zedGraph.Size = new Size(100, 100);


 


            zedGraph.GraphPane.Fill.Type = FillType.None;


            zedGraph.GraphPane.Chart.Fill.Type = FillType.None;


 


            zedGraph.GraphPane.Border.IsVisible = false;


            zedGraph.GraphPane.Chart.Border.IsVisible = false;


            zedGraph.GraphPane.XAxis.IsVisible = false;


            zedGraph.GraphPane.YAxis.IsVisible = false;


            zedGraph.GraphPane.Legend.IsVisible = false;


            zedGraph.GraphPane.Title.IsVisible = false;


                      


            PieItem pieItem1 = zedGraph.GraphPane.AddPieSlice(Convert.ToDouble(e.Feature.ColumnValues["Reached"]), GetColorFromGeoColor(GeoColor.StandardColors.LightBlue), 0, "White");


            pieItem1.LabelDetail.IsVisible = false;


 


            PieItem pieItem2 = zedGraph.GraphPane.AddPieSlice(Convert.ToDouble(e.Feature.ColumnValues["Remaining"]), GetColorFromGeoColor(GeoColor.StandardColors.LightGreen), 0, "Asian");


            pieItem2.LabelDetail.IsVisible = false;


            pieItem2.Displacement = 0.2;


 


            zedGraph.AxisChange();


 


            e.Bitmap = zedGraph.GraphPane.GetImage();


        }


 


        private void ChangeLabelPosition(ShapeFileFeatureLayer shapeFileLayer, int graphHeight)


        {


            ((TextStyle)shapeFileLayer.ZoomLevelSet.ZoomLevel01.CustomStyles[1]).XOffsetInPixel = -20;


            ((TextStyle)shapeFileLayer.ZoomLevelSet.ZoomLevel01.CustomStyles[1]).YOffsetInPixel = Convert.ToSingle(graphHeight * 0.4);


        }


 


        private Color GetColorFromGeoColor(GeoColor geoColor)


        {


            return Color.FromArgb(geoColor.AlphaComponent, geoColor.RedComponent, geoColor.GreenComponent, geoColor.BlueComponent);


        }


    }


}


 




 Hi Asim


Here are answers for your questions: 


1. How can I change the size of Zedgraphs with the based on the zoom level of the Map? 


We can use different ZedGraphStyles for different ZoomLevels to meet this requirement. Please check the following code for more information. 



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
        Map1.CurrentExtent = new RectangleShape(-123.41875, 41.96396484375, -107.158984375, 30.36240234375);
        Map1.MapUnit = GeographyUnit.DecimalDegree;

        WorldMapKitWmsWebOverlay worldMapKitOverlay = new WorldMapKitWmsWebOverlay();
        Map1.CustomOverlays.Add(worldMapKitOverlay);

        // Zedgraph Style (1-7)
        ZedGraphStyle zedGraphStyle = new ZedGraphStyle();
        zedGraphStyle.ZedGraphDrawing += new EventHandler<ZedGraphDrawingEventArgs>(zedGraphStyle_ZedGraphDrawing);
        zedGraphStyle.RequiredColumnNames.Add("Reached");
        zedGraphStyle.RequiredColumnNames.Add("Remaining");
        zedGraphStyle.RequiredColumnNames.Add("AreaName");

        // Another Zedgraph Style (8-20)
        ZedGraphStyle anotherZedGraphStyle = new ZedGraphStyle();
        anotherZedGraphStyle.RequiredColumnNames.Add("Reached");
        anotherZedGraphStyle.RequiredColumnNames.Add("Remaining");
        anotherZedGraphStyle.RequiredColumnNames.Add("AreaName");
        anotherZedGraphStyle.ZedGraphDrawing += new EventHandler<ZedGraphDrawingEventArgs>(anotherZedGraphStyle_ZedGraphDrawing);

        ShapeFileFeatureLayer citiesLayer = new ShapeFileFeatureLayer(@"F:\Oasis\Pak Admin Bndry\Prov_Centriods.shp");
        citiesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(zedGraphStyle);
        citiesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(TextStyles.City3("AreaName"));
        citiesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level07;
        citiesLayer.ZoomLevelSet.ZoomLevel08.CustomStyles.Add(anotherZedGraphStyle);
        citiesLayer.ZoomLevelSet.ZoomLevel08.CustomStyles.Add(TextStyles.City3("AreaName"));
        citiesLayer.ZoomLevelSet.ZoomLevel08.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
        citiesLayer.DrawingMarginPercentage = 100;

        LayerOverlay staticOverlay = new LayerOverlay();
        staticOverlay.Layers.Add("Cities", citiesLayer);
        staticOverlay.IsBaseOverlay = false;
        Map1.CustomOverlays.Add(staticOverlay);
    }
}

private void anotherZedGraphStyle_ZedGraphDrawing(object sender, ZedGraphDrawingEventArgs e)
{
    CustomizeZedGraph(e, 50);
}

private void zedGraphStyle_ZedGraphDrawing(object sender, ZedGraphDrawingEventArgs e)
{
    CustomizeZedGraph(e, 100);
}

private void CustomizeZedGraph(ZedGraphDrawingEventArgs e, int size)
{
    ShapeFileFeatureLayer layer = ((ShapeFileFeatureLayer)((LayerOverlay)Map1.CustomOverlays[1]).Layers["Cities"]);

    ChangeLabelPosition(layer, 100);

    ZedGraphControl zedGraph = new ZedGraphControl();

    zedGraph.Size = new Size(size, size);

    zedGraph.GraphPane.Fill.Type = FillType.None;
    zedGraph.GraphPane.Chart.Fill.Type = FillType.None;

    zedGraph.GraphPane.Border.IsVisible = false;
    zedGraph.GraphPane.Chart.Border.IsVisible = false;
    zedGraph.GraphPane.XAxis.IsVisible = false;
    zedGraph.GraphPane.YAxis.IsVisible = false;
    zedGraph.GraphPane.Legend.IsVisible = false;
    zedGraph.GraphPane.Title.IsVisible = false;

    PieItem pieItem1 = zedGraph.GraphPane.AddPieSlice(Convert.ToDouble(e.Feature.ColumnValues["WHITE"]), GetColorFromGeoColor(GeoColor.StandardColors.LightBlue), 0, "White");
    pieItem1.LabelDetail.IsVisible = false;

    PieItem pieItem2 = zedGraph.GraphPane.AddPieSlice(Convert.ToDouble(e.Feature.ColumnValues["ASIAN"]), GetColorFromGeoColor(GeoColor.StandardColors.LightGreen), 0, "Asian");
    pieItem2.LabelDetail.IsVisible = false;
    pieItem2.Displacement = 0.2;

    zedGraph.AxisChange();

    e.Bitmap = zedGraph.GraphPane.GetImage();
}

private void ChangeLabelPosition(ShapeFileFeatureLayer shapeFileLayer, int graphHeight)
{
    ((TextStyle)shapeFileLayer.ZoomLevelSet.ZoomLevel01.CustomStyles[1]).XOffsetInPixel = -20;
    ((TextStyle)shapeFileLayer.ZoomLevelSet.ZoomLevel01.CustomStyles[1]).YOffsetInPixel = Convert.ToSingle(graphHeight * 0.4);
}

private Color GetColorFromGeoColor(GeoColor geoColor)
{
    return Color.FromArgb(geoColor.AlphaComponent, geoColor.RedComponent, geoColor.GreenComponent, geoColor.BlueComponent);
}


2. Also I tried doing it by add a ExtentChanged eventhandler of my own. But now the extent always goes back to the initial extent .. can anyone please point out why that is? 


The “ExtentChanged” event is a post back event so it isn’t suitable for your scenario.


We can’t re-produce the issue (“always goes back to the initial extent”), could you please provide us more information to recreate this issue?


Regards,


Ivan