ThinkGeo.com    |     Documentation    |     Premium Support

Why I can't display the zedGraph in my map?

Yale,

  I want to  display the zedGraph in my map. the data of zedGraph is from a InMemoryFeatureLayer of thinkgeo.

I reference the sample  "DrawUsingZedGraphStyle" in  "CSharp VS2005 Winforms HowDoISamples"

the following is my codes:

            winformsMap1.MapUnit = GeographyUnit.DecimalDegree;

            InMemoryFeatureLayer memoryFeatureLayer = new InMemoryFeatureLayer();

            Dictionary<string, string> feature1Column = new Dictionary<string, string>();          

            feature1Column["AGE_UNDER5"] = "1500";

            feature1Column["AGE_5_17"] = "5000";

            feature1Column["AGE_18_21"] = "10000";

            Feature feature1 = new Feature("POINT(-92 38)", "1", feature1Column);

            memoryFeatureLayer.InternalFeatures.Add(feature1);

            string valu1 = memoryFeatureLayer.InternalFeatures[0].ColumnValues["AGE_UNDER5"];

            string valu2 = memoryFeatureLayer.InternalFeatures[0].ColumnValues["AGE_5_17"];

            string valu3 = memoryFeatureLayer.InternalFeatures[0].ColumnValues["AGE_18_21"];

            ZedGraphStyle zedGraphStyle = new ZedGraphStyle();

            foreach (string column in feature1Column.Keys)

            {

                zedGraphStyle.RequiredColumnNames.Add(column);

            }

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


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

            memoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            LayerOverlay Overlay = new LayerOverlay();

            Overlay.Layers.Add("Cities", memoryFeatureLayer);

            this.winformsMap1.Overlays.Add("CitiesOverlay", Overlay);

            winformsMap1.CurrentExtent = new RectangleShape(-126.4, 48.8, -67.0, 19.0);

            winformsMap1.Refresh(); 


      private void zedGraphStyle_ZedGraphDrawing1(object sender, ZedGraphDrawingEventArgs e)

        {

            e.Bitmap = (Bitmap)this.Invoke(new ToUIThreadDelegate(ZedGraphDrawingPie1), new object[] { e });

        }


       private Bitmap ZedGraphDrawingPie1(ZedGraphDrawingEventArgs e)

        {

            double scale = ExtentHelper.GetScale(winformsMap1.CurrentExtent, winformsMap1.Width,GeographyUnit.DecimalDegree);

            // Change the size of the graph based on the scale.  It will get bigger as you zoom in.   

            int graphHeight = Convert.ToInt32(1400000000 / scale);

            ZedGraphControl zedGraph = new ZedGraphControl();

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

            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 = true;

            zedGraph.GraphPane.Title.IsVisible = false;          

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

            pieItem1.LabelDetail.IsVisible = false;

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

            pieItem2.LabelDetail.IsVisible = false;

            PieItem pieItem3 = zedGraph.GraphPane.AddPieSlice(Convert.ToDouble(e.Feature.ColumnValues["AGE_18_21"]), GetColorFromGeoColor(GeoColor.StandardColors.LightCoral), 0, "AGE_18_21");

            pieItem3.LabelDetail.IsVisible = false;

            zedGraph.AxisChange();

            return zedGraph.GraphPane.GetImage();


        }


when I run the program,it throw a exception. Then I debug the program, I found the problem is the follwing lines

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

the value of "e.Feature.ColumnValues["AGE_UNDER5"]" is null,  ConvertToDouble generates a error.

At the same time ,I modify the value of "e.Feature.ColumnValues["AGE_UNDER5"]" to 1500,the value of "e.Feature.ColumnValues["AGE_5_17"]" to 5000, the value of "e.Feature.ColumnValues["AGE_18_21"]" to 10000. then the zedGraph style display well.

How can I resovle the problem? I'm excepting your reply ?

thank you very much.



Senlin, 
  
  I think you are going to need to check the value of what you are adding before you add it.  It is possible that the data you are using has some problems and my have blanks, nulls, or characters in the fields.  Instead of using Convert.ToDouble trying using the double.TryParse.  I made a little sample for you below.  This will clean the doubles to make sure they will pass.  I think this will handle a null. 
  
         private static double GetSafeDouble(string value, double defaultValue) 
         {            
             double doubleValue; 
             if (double.TryParse(value, out  doubleValue)) 
             { 
                 return doubleValue; 
             } 
             else 
             {                 
                 return defaultValue; 
             }             
         } 
  
         private static double GetSafeDouble(string value) 
         { 
             return GetSafeDouble(value, 0); 
         } 
  
 David

Yale,


I think you maybe not understand me. I test ed ZedGraphStyle in the nMemoryFeatureLayer. In the nMemoryFeatureLayer ,I only add one feature, look at the follwing codes:


          winformsMap1.MapUnit = GeographyUnit.DecimalDegree;

            InMemoryFeatureLayer memoryFeatureLayer = new InMemoryFeatureLayer();

            Dictionary feature1Column = new Dictionary();          

            feature1Column["AGE_UNDER5"] = "1500";

            feature1Column["AGE_5_17"] = "5000";

            feature1Column["AGE_18_21"] = "10000";

            Feature feature1 = new Feature("POINT(-92 38)", "1", feature1Column);

            memoryFeatureLayer.InternalFeatures.Add(feature1);




 I  think  the data I used  has no problems such as have blanks, nulls, or characters in the fields. there I only add only one feature as the above codes.  I Debug the program, I foud that the feature of InternalFeatures which  I added is ok, the value of  memoryFeatureLayer.InternalFeatures[0].ColumnValues["AGE_UNDER5"] is 1500, but  go to the lfunction ZedGraphDrawingPie1(ZedGraphDrawingEventArgs e),the value of e.Feature.ColumnValues["AGE_UNDER5"] is becomed to null. so the PieGraph can't draw.


I  hope you can I help me . thank you very much.


 


 



Yale,


thank you for your help. it is my wrong , I  forget to add the feature source column when you define the InMemoryFeatureLayer. the problem is resolved when I add the feature source column when you define the InMemoryFeatureLayer.


thanks very much! I


 

Senlin, 
  
 Thanks for your letting me know your status. 
  
 Any more questions just feel free to let me know. 
  
 Thanks. 
  
 Yale