October
2004




Consuming WebServices with Charting, part I

I wanted to experiment a bit more with web services and also apply the Charting feature of the Xceed Charts for .NET. I have used the Charting components for Windows Forms and have been quite impressed with the flexibility and capabilities of this component. And not just this particular component...most components from the Xceed Suite are truly solid and easy to work with. I would blindly recommend any of the components. More at www.xceedsoft.com. In any case, In this experiment we will try out a different type of chart and feed it dynamic data. This data will be retrieved by a public webservice (US Census Bureau Population) that delivers the data of population from any of 227 countries. I have tried with other services like stock quotes, but they keep returning empty data objects and the service is intermittent, so I decided to implement this with a less popular service. In any case, I just need any type of dynamic data.



aspx.cs code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using System.Xml;
using Xceed.Chart.Standard;
using Xceed.Chart.GLCore;
using Xceed.Chart;
using Xceed.Chart.Core;
using Xceed.Chart.Server;

using sandbox.ws_population;

public partial class sandbox_ws_population_Default : System.Web.UI.Page
{
  private Xceed.Chart.Core.Chart chart;
  private Xceed.Chart.Core.PieSeries pie;

  private PopulationWS countryPop = new PopulationWS();

  private double country;
  private double world;

  protected void Page_Load(object sender, EventArgs e)
  {
    Xceed.Chart.Server.Licenser.LicenseKey = "XXXXX-XXXXX-XXXXX-XXXX";
	Xceed.Chart.Server.GeneralSettings generalSettings = 
		ChartServerControl1.ServerConfiguration.GeneralSettings;

	generalSettings.TempDirectory = "directory";

	foreach (string countryname in countryPop.getCountries()){
		this.DropDownList1.Items.Add(countryname);
	}

	this.chart = (Chart) ChartServerControl1.Charts[0];
	this.pie = (PieSeries) chart.Series.Add(SeriesType.Pie);

	this.pie.Appearance.FillMode = AppearanceFillMode.DataPoints;

	this.ChartServerControl1.Settings.EnableJittering = true;
	this.ChartServerControl1.Settings.EnableAntialiasing = false;
	this.ChartServerControl1.Settings.DoublePassBlending = false;

	this.pie.DataLabels.Format = "<label> (<value>)";
	this.pie.DataLabels.Mode = DataLabelsMode.Every;
	this.pie.LabelMode = PieLabelMode.Spider;
	this.pie.PieStyle = PieStyle.SmoothEdgePie;

	this.pie.PieFillEffect.SetSolidColor(Color.Green);
	this.pie.PieBorder.Color = Color.Blue;

	this.pie.PieEdgePercent = 15;

	this.chart.Depth = 20;

	this.pie.Interactivity.TooltipMode = SeriesTooltipMode.DataPoints;

	if(((
		Population)countryPop.getWorldPopulation()).Pop.GetType()
		==typeof(System.String)){
          this.world=Double.Parse(((
          Population)countryPop.getWorldPopulation()).Pop.ToString());
	} else {
          this.world = 6396438938;
	}

	this.country=Double.Parse(countryPop.getPopulation(
		this.DropDownList1.SelectedItem.Text.ToString()).Pop);

	this.pie.Add(
	world - country, 
	"Rest of the world", 
	new FillEffect(Color.Red));
	
	this.pie.Interactivity.Tooltips.Add("World: " + world.ToString());

	this.pie.AddPie( country, 
		3, 
		this.DropDownList1.SelectedItem.Text.ToString(), 
		new FillEffect(Color.RoyalBlue));
						
	this.pie.Interactivity.Tooltips.Add(
		this.DropDownList1.SelectedItem.Text.ToString() + 
		": " + country.ToString());

	chart.LightModel.EnableLighting = true;
	chart.LightModel.SetPredefinedScheme(LightScheme.MetallicLustre);

	BackgroundFrame frame = ChartServerControl1.Background.Frame;
	frame.FrameType = BackgroundFrameType.ImageBorder;
	frame.ImageFrameType = ImageFrameType.Common;

	LegendCollection legendsCollection = ChartServerControl1.Legends;
	Legend legend = (Legend) legendsCollection[0];

	legend.Mode = Xceed.Chart.Core.LegendMode.Manual;

	LegendDataItem item1 = new LegendDataItem();
	item1.Text = "World population: " + world.ToString();
	item1.MarkFillEffect.SetGradient(
		GradientStyle.Horizontal, 
		GradientVariant.Variant1, 
		Color.RosyBrown, 
		Color.Red);
		
	item1.MarkShape = LegendMarkShape.Rectangle;
	legend.Data.Items.Add(item1);

	LegendDataItem item = new LegendDataItem();
	item.Text = this.DropDownList1.SelectedItem.Text.ToString() + " +/- " + 
		((int) ((country / world) * 100) + 1).ToString() + "%";
		
	item.MarkFillEffect.SetGradient(
		GradientStyle.Horizontal, 
		GradientVariant.Variant1, 
		Color.Azure, 
		Color.Blue);
	
	item.MarkShape = LegendMarkShape.Rectangle;
	legend.Data.Items.Add(item);

	legend.Header.Text = "US Census Bureau (2004)";

	ChartServerControl1.Legends.Add(legend);
  }
}