Consuming WebServices with Charting, part II
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.
In any case, In this experiment we will try out one chart and feed it dynamic
data. This data will be retrieved by WebserviceX.NET
that gets Stock quotes for a company symbol.
I have hard-coded some random stock symbols for the experiment to look for,
invoke the service, retrieve the data and draw the graphic.
| ID | Symbol | Price | Date | Time | Change | Open | High | Low |
| 0 | INTC | $26.15 | 5/21/2012 | 4:00pm | +0.08 | 26.04 | 26.19 | 25.68 |
| 1 | CSCO | $16.67 | 5/21/2012 | 4:00pm | +0.20 | 16.50 | 16.70 | 16.37 |
| 2 | DELL | $14.97 | 5/21/2012 | 4:00pm | +0.23 | 14.80 | 15.00 | 14.60 |
| 3 | MSFT | $29.75 | 5/21/2012 | 4:00pm | +0.48 | 29.09 | 29.79 | 29.06 |
| 4 | YHOO | $15.58 | 5/21/2012 | 4:00pm | +0.16 | 15.98 | 16.00 | 15.10 |
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 System.IO;
using Xceed.Chart.Standard;
using Xceed.Chart.GLCore;
using Xceed.Chart;
using Xceed.Chart.Core;
using Xceed.Chart.Server;
public partial class sandbox_ws_stocks_Default:System.Web.UI.Page
{
private Xceed.Chart.Core.Chart chart;
private Xceed.Chart.Core.BarSeries bar;
private string arrow;
private System.Drawing.Color[] barColors = new Color[]{
Color.Gray,
Color.IndianRed,
Color.SteelBlue,
Color.Olive,
Color.MediumSlateBlue,
Color.Salmon,
Color.SeaGreen,
Color.SandyBrown,
Color.AliceBlue
};
protected void Page_Load(object sender, EventArgs e)
{
//chart control--------------------------------------
Xceed.Chart.Server.GeneralSettings generalSettings =
this.ChartServerControl1.ServerConfiguration.GeneralSettings;
generalSettings.TempDirectory = "/sandbox/ws_stocks/temp/";
XmlDocument xmlDoc = new XmlDocument();
StockQuote stock = new StockQuote();
xmlDoc.LoadXml(stock.GetQuote(
"CAPA,
INTC,
CSCO,
NSANY,
DELL,
MSFT,
CNET,
YHOO"));
this.chart = (Chart) ChartServerControl1.Charts[0];
this.chart.Width = 280;
this.bar = (BarSeries) chart.Series.Add(SeriesType.Bar);
this.bar.Appearance.FillMode = AppearanceFillMode.DataPoints;
this.bar.WidthPercent = 90;
this.bar.DepthPercent = 150;
this.ChartServerControl1.Settings.EnableAntialiasing = true;
this.ChartServerControl1.Settings.DoublePassBlending = true;
this.ChartServerControl1.BackColor = Color.Aquamarine;
LegendCollection legendsCollection =
ChartServerControl1.Legends;
Legend legend = (Legend) legendsCollection[0];
legend.Mode = Xceed.Chart.Core.LegendMode.Disabled;
this.bar.DataLabels.Format = "<label>";
this.bar.BarStyle = BarStyle.SmoothEdgeBar;
this.bar.Interactivity.TooltipMode =
SeriesTooltipMode.DataPoints;
chart.LightModel.EnableLighting = true;
chart.LightModel.SetPredefinedScheme(LightScheme.ShinyTopLeft);
this.ChartServerControl1.Background.FillEffect.Color =
Color.FromArgb(237, 241, 242);
//table section here-----------------------------------------
this.Table1.BorderWidth = 1;
this.Table1.BorderStyle = BorderStyle.Inset;
this.Table1.Width = new Unit(280);
this.Table1.BackColor = Color.Beige;
TableRow hr = new TableRow();
TableHeaderCell hc_color = new TableHeaderCell();
TableHeaderCell hc_symbol = new TableHeaderCell();
TableHeaderCell hc_price = new TableHeaderCell();
TableHeaderCell hc_date = new TableHeaderCell();
TableHeaderCell hc_time = new TableHeaderCell();
TableHeaderCell hc_change = new TableHeaderCell();
TableHeaderCell hc_open = new TableHeaderCell();
TableHeaderCell hc_high = new TableHeaderCell();
TableHeaderCell hc_low = new TableHeaderCell();
hc_color.Text = "ID";
hc_color.BackColor = Color.Lavender;
hc_color.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_symbol.Text = "Symbol";
hc_symbol.BackColor = Color.Lavender;
hc_symbol.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_price.Text = "Price";
hc_price.BackColor = Color.Lavender;
hc_price.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_date.Text = "Date";
hc_date.BackColor = Color.Lavender;
hc_date.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_time.Text = "Time";
hc_time.BackColor = Color.Lavender;
hc_time.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_change.Text = "Change";
hc_change.BackColor = Color.Lavender;
hc_change.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_open.Text = "Open";
hc_open.BackColor = Color.Lavender;
hc_open.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_high.Text = "High";
hc_high.BackColor = Color.Lavender;
hc_high.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hc_low.Text = "Low";
hc_low.BackColor = Color.Lavender;
hc_low.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Left;
hr.Cells.Add(hc_color);
hr.Cells.Add(hc_symbol);
hr.Cells.Add(hc_price);
hr.Cells.Add(hc_date);
hr.Cells.Add(hc_time);
hr.Cells.Add(hc_change);
hr.Cells.Add(hc_open);
hr.Cells.Add(hc_high);
hr.Cells.Add(hc_low);
Table1.Rows.AddAt(0, hr);
for(int i = 0;i < xmlDoc.FirstChild.ChildNodes.Count;i++) {
this.bar.Add(
Double.Parse(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[1].InnerText,
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[0].InnerText,
new FillEffect((Color) this.barColors[i])
);
this.bar.Interactivity.Tooltips.Add(
"Symbol: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[0].InnerText+"\n"+
"Price: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[1].InnerText+"\n"+
"Date: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[2].InnerText+"\n"+
"Time: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[3].InnerText+"\n"+
"Change: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[4].InnerText+"\n"+
"Open: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[5].InnerText+"\n"+
"High: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[6].InnerText+"\n"+
"Low: " +
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[7].InnerText+"\n"+
);
TableRow r = new TableRow();
TableCell c_color = new TableCell();
TableCell c_symbol = new TableCell();
TableCell c_price = new TableCell();
TableCell c_date = new TableCell();
TableCell c_time = new TableCell();
TableCell c_change = new TableCell();
TableCell c_open = new TableCell();
TableCell c_high = new TableCell();
TableCell c_low = new TableCell();
c_color.Controls.Add(new LiteralControl(" " + i.ToString()));
c_color.BackColor = barColors[i];
c_symbol.Controls.Add(
new LiteralControl(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[0].InnerText)
);
c_price.Controls.Add(
new LiteralControl($" +
"xmlDoc.FirstChild.ChildNodes[i].ChildNodes[1].InnerText)
);
c_date.Controls.Add(
new LiteralControl(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[2].InnerText)
);
c_time.Controls.Add(
new LiteralControl(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[3].InnerText)
);
if(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[4].InnerText=="+"){
this.arrow = "<img src='../../images/arrow_up.gif' /> ";
}else if(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[4].InnerText=="-"){
this.arrow = "<img src='../../images/arrow_down.gif' /> ";
}
c_change.Controls.Add(
new LiteralControl(
arrow+xmlDoc.FirstChild.ChildNodes[i].ChildNodes[4].InnerText)
);
c_open.Controls.Add(
new LiteralControl(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[5].InnerText)
);
c_high.Controls.Add(
new LiteralControl(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[6].InnerText)
);
c_low.Controls.Add(
new LiteralControl(
xmlDoc.FirstChild.ChildNodes[i].ChildNodes[7].InnerText)
);
r.Cells.Add(c_color);
r.Cells.Add(c_symbol);
r.Cells.Add(c_price);
r.Cells.Add(c_date);
r.Cells.Add(c_time);
r.Cells.Add(c_change);
r.Cells.Add(c_open);
r.Cells.Add(c_high);
r.Cells.Add(c_low);
Table1.Rows.Add(r);
}
}