using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
using ZedGraph;
namespace ZedGraph
{
public partial class TestForm : Form
{
private delegate Bitmap ToUIThreadDelegate(ZedGraphDrawingEventArgs e);
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
ShapeFileFeatureLayer worldLayer = null;
ShapeFileFeatureLayer cityFlueLayer = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the full extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-99.83,34.27,-89.28,27.15), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
// Add the static layers to the MapEngine
worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\Countries02.shp", ShapeFileReadWriteMode.ReadOnly);
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1;
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("WorldLayer", worldLayer);
//Create our Zedgraph Sytle and wire up the event.
ZedGraphStyle zedGraphStyle = new ZedGraphStyle();
zedGraphStyle.ZedGraphDrawing += new EventHandler<ZedGraphDrawingEventArgs>(zedGraphStyle_ZedGraphDrawing);
//Adds the columns used by the ZedGraph
zedGraphStyle.RequiredColumnNames.Add("WHITE");
zedGraphStyle.RequiredColumnNames.Add("BLACK");
zedGraphStyle.RequiredColumnNames.Add("ASIAN");
zedGraphStyle.RequiredColumnNames.Add("HISPANIC");
zedGraphStyle.RequiredColumnNames.Add("AREANAME");
cityFlueLayer = new ShapeFileFeatureLayer(@"..\..\Data\majorcities.shp", ShapeFileReadWriteMode.ReadOnly);
cityFlueLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(zedGraphStyle);
cityFlueLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("CityLayer", cityFlueLayer);
DrawImage();
}
private void zedGraphStyle_ZedGraphDrawing(object sender, ZedGraphDrawingEventArgs e)
{
e.Bitmap = (Bitmap)this.Invoke(new ToUIThreadDelegate(ZedGraphDrawing), new object[] { e });
}
private Bitmap ZedGraphDrawing(ZedGraphDrawingEventArgs e)
{
double scale = ExtentHelper.GetScale(mapEngine.CurrentExtent, mapEngine.Canvas.Width, GeographyUnit.DecimalDegree);
//Sets the size of the chart proportional to the total population
//Note that here the values are hard coded. This could be extended to be customizable.
int maximumGraphSize = 135;
int minimumGraphSize = 300;
int maximumValue = 8000000;
int minimumValue = 100000;
//Gets the different values for the current feature and calculates the total value and the
//size of the chart.
int value1 = Convert.ToInt32(e.Feature.ColumnValues["WHITE"]);
int value2 = Convert.ToInt32(e.Feature.ColumnValues["BLACK"]);
int value3 = Convert.ToInt32(e.Feature.ColumnValues["ASIAN"]);
int value4 = Convert.ToInt32(e.Feature.ColumnValues["HISPANIC"]);
string areaName = e.Feature.ColumnValues["AREANAME"];
int totalValue = value1 + value2 + value3 + value4;
int perc1 = (value1 * 100) / totalValue;
int perc2 = (value2 * 100) / totalValue;
int perc3 = (value3 * 100) / totalValue;
int perc4 = (value4 * 100) / totalValue;
int graphHeight = (int)(maximumGraphSize - ((totalValue - minimumValue) * (maximumGraphSize - minimumGraphSize)) /
(maximumValue - minimumValue));
//Sets the properties of the ZedGraphControl for the desired appearance.
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 = false;
zedGraph.GraphPane.Title.IsVisible = false;
zedGraph.GraphPane.IsFontsScaled = false;
//Adds the different pie slices with the percentage label.
if (value1 > 0)
{
PieItem pieItem1 = null;
pieItem1 = zedGraph.GraphPane.AddPieSlice(value1, Color.FromArgb(150, Color.Blue), Color.White, 45f, 0,Convert.ToString(perc1));
pieItem1.LabelDetail.IsVisible = true;
}
if (value2 > 0)
{
PieItem pieItem2 = null;
pieItem2 = zedGraph.GraphPane.AddPieSlice(value2, Color.FromArgb(150, Color.Brown), Color.White, 45f, 0, Convert.ToString(perc2));
pieItem2.LabelDetail.IsVisible = true;
}
if (value3 > 0)
{
PieItem pieItem3 = null;
pieItem3 = zedGraph.GraphPane.AddPieSlice(value3, Color.FromArgb(150, Color.Yellow), Color.White, 45f, 0, Convert.ToString(perc3));
pieItem3.LabelDetail.IsVisible = true;
}
if (value4 > 0)
{
PieItem pieItem4 = null;
pieItem4 = zedGraph.GraphPane.AddPieSlice(value4, Color.FromArgb(150, Color.Red), Color.White, 45f, 0, Convert.ToString(perc4));
pieItem4.LabelDetail.IsVisible = true;
}
//Makes a text label to highlight the city name and the total value.
TextObj text = new TextObj(areaName + ": " + totalValue,0.1, 0.15, CoordType.PaneFraction);
text.Location.AlignH = AlignH.Left;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
text.FontSpec.StringAlignment = StringAlignment.Center;
zedGraph.GraphPane.GraphObjList.Add(text);
return zedGraph.GraphPane.GetImage();
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.CloseAllLayers();
Map.Image = bitmap;
}
private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Zoom In":
mapEngine.CurrentExtent.ScaleDown(50);
break;
case "Zoom Out":
mapEngine.CurrentExtent.ScaleUp(50);
break;
case "Full Extent":
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
break;
case "Pan Left":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);
break;
case "Pan Right":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);
break;
case "Pan Up":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);
break;
case "Pan Down":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);
break;
default:
break;
}
DrawImage();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}