Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Xml.Linq;
using DevExpress.Xpf.Charts;
using DevExpress.Xpf.Gauges;
using DevExpress.Xpf.Map;
namespace MapDemo {
public partial class SalesDashboard : MapDemoModule {
SalesDataGenerator dataGenerator;
public SalesDashboard() {
InitializeComponent();
dataGenerator = new SalesDataGenerator(chart, circularGauge);
infoGrid.DataContext = dataGenerator;
scale.EndValue = dataGenerator.MaxSalesLevel + 10000;
scale.StartValue = dataGenerator.MinSalesLevel - 10000;
}
}
public class ProductGroupInfo : DependencyObject {
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name",
typeof(string), typeof(ProductGroupInfo), new PropertyMetadata(""));
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
typeof(double), typeof(ProductGroupInfo), new PropertyMetadata(0.0));
public String Name {
get { return (String)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public double Value {
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public ProductGroupInfo(double value, string productName) {
Value = value;
Name = productName;
}
}
public class ShopInfo : DependencyObject {
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name",
typeof(string), typeof(ShopInfo), new PropertyMetadata(""));
public static readonly DependencyProperty PhoneProperty = DependencyProperty.Register("Phone",
typeof(string), typeof(ShopInfo), new PropertyMetadata(""));
public static readonly DependencyProperty FaxProperty = DependencyProperty.Register("Fax",
typeof(string), typeof(ShopInfo), new PropertyMetadata(""));
public static readonly DependencyProperty AddressProperty = DependencyProperty.Register("Address",
typeof(string), typeof(ShopInfo), new PropertyMetadata(""));
public static readonly DependencyProperty SalesProperty = DependencyProperty.Register("Sales",
typeof(double), typeof(ShopInfo), new PropertyMetadata(0.0));
public static readonly DependencyProperty ShopLocationProperty = DependencyProperty.Register("ShopLocation",
typeof(GeoPoint), typeof(ShopInfo), new PropertyMetadata(new GeoPoint()));
public string Name {
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public string Phone {
get { return (string)GetValue(PhoneProperty); }
set { SetValue(PhoneProperty, value); }
}
public string Fax {
get { return (string)GetValue(FaxProperty); }
set { SetValue(FaxProperty, value); }
}
public string Address {
get { return (string)GetValue(AddressProperty); }
set { SetValue(AddressProperty, value); }
}
public double Sales {
get { return (double)GetValue(SalesProperty); }
set { SetValue(SalesProperty, value); }
}
public GeoPoint ShopLocation {
get { return (GeoPoint)GetValue(ShopLocationProperty); }
set { SetValue(ShopLocationProperty, value); }
}
static string ConvertShopNameToFilePath(string ShopName) {
string result = ShopName.Replace(" ", "");
result = "../Images/Shops/" + result.Replace("-", "") + ".png";
return result;
}
Dictionary<string, double> statistics = new Dictionary<string, double>();
readonly string imagePath;
public ShopInfo(string Name, string Address, string Phone, string Fax) {
this.Name = Name;
this.Address = Address;
this.Phone = Phone;
this.Fax = Fax;
this.imagePath = ConvertShopNameToFilePath(Name);
}
public string ImagePath { get { return imagePath; } }
public void AddProductGroup(string groupName, double sales) {
if (statistics.ContainsKey(groupName))
statistics[groupName] = sales;
else
statistics.Add(groupName, sales);
Sales += sales;
}
public double GetSalesByProductGroup(string groupName) {
return statistics.ContainsKey(groupName) ? statistics[groupName] : 0.0;
}
}
public class SalesDataGenerator : DependencyObject {
public static readonly DependencyProperty ShopsProperty = DependencyProperty.Register("Shops",
typeof(ObservableCollection<ShopInfo>), typeof(SalesDataGenerator), new PropertyMetadata(null));
public static readonly DependencyProperty ActualStatisticsProperty = DependencyProperty.Register("ActualStatistics",
typeof(ObservableCollection<ProductGroupInfo>), typeof(SalesDataGenerator), new PropertyMetadata(null));
public static readonly DependencyProperty SalesDescriptionProperty = DependencyProperty.Register("SalesDescription",
typeof(string), typeof(SalesDataGenerator), new PropertyMetadata(String.Empty));
public static readonly DependencyProperty SelectedShopProperty = DependencyProperty.Register("SelectedShop",
typeof(ShopInfo), typeof(SalesDataGenerator), new PropertyMetadata(null, new PropertyChangedCallback(SelectedShopPropertyChanged)));
public static readonly DependencyProperty MaxSalesLevelProperty = DependencyProperty.Register("MaxSalesLevel",
typeof(double), typeof(SalesDataGenerator), new PropertyMetadata(0.0));
public static readonly DependencyProperty MinSalesLevelProperty = DependencyProperty.Register("MinSalesLevel",
typeof(double), typeof(SalesDataGenerator), new PropertyMetadata(0.0));
public ObservableCollection<ProductGroupInfo> ActualStatistics {
get { return (ObservableCollection<ProductGroupInfo>)GetValue(ActualStatisticsProperty); }
}
public ObservableCollection<ShopInfo> Shops {
get { return (ObservableCollection<ShopInfo>)GetValue(ShopsProperty); }
}
public string SalesDescription {
get { return (string)GetValue(SalesDescriptionProperty); }
set { SetValue(SalesDescriptionProperty, value); }
}
public ShopInfo SelectedShop {
get { return (ShopInfo)GetValue(SelectedShopProperty); }
set { SetValue(SelectedShopProperty, value); }
}
public double MinSalesLevel {
get { return (double)GetValue(MinSalesLevelProperty); }
set { SetValue(MinSalesLevelProperty, value); }
}
public double MaxSalesLevel {
get { return (double)GetValue(MaxSalesLevelProperty); }
set { SetValue(MaxSalesLevelProperty, value); }
}
static void SelectedShopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
SalesDataGenerator dataGenerator = d as SalesDataGenerator;
ShopInfo shop = e.NewValue as ShopInfo;
if (dataGenerator != null) {
if (shop != null)
dataGenerator.UpdateStatistics(shop);
else
dataGenerator.UpdateTotalStatistics();
}
}
readonly ChartControl chart;
readonly CircularGaugeControl gaugeControl;
public SalesDataGenerator(ChartControl chart, CircularGaugeControl gaugeControl) {
this.chart = chart;
this.SetValue(ShopsProperty, new ObservableCollection<ShopInfo>());
this.SetValue(ActualStatisticsProperty, new ObservableCollection<ProductGroupInfo>());
this.gaugeControl = gaugeControl;
LoadDataFromXML();
UpdateMinMaxSales();
SelectedShop = Shops[0];
UpdateStatistics(SelectedShop);
UpdateTotalStatistics();
}
void LoadDataFromXML() {
List<string> productGroupNames = new List<string>();
XDocument document = DataLoader.LoadXmlFromResources("/Data/Sales.xml");
if (document != null) {
foreach (XElement element in document.Element("Sales").Elements()) {
string shopName = element.Element("ShopName").Value;
string shopAddress = element.Element("ShopAddr").Value;
string shopPhone = element.Element("ShopPhone").Value;
string shopFax = element.Element("ShopFax").Value;
ShopInfo info = new ShopInfo(shopName, shopAddress, shopPhone, shopFax);
foreach (XElement statElement in element.Element("ShopStatistics").Elements()) {
string groupName = statElement.Element("ProductsGroupName").Value;
if (!productGroupNames.Contains(groupName))
productGroupNames.Add(groupName);
double sales = Convert.ToDouble(statElement.Element("ProductGroupSales").Value, CultureInfo.InvariantCulture);
info.AddProductGroup(groupName, sales);
}
GeoPoint geoPoint = new GeoPoint(Convert.ToDouble(element.Element("Latitude").Value, CultureInfo.InvariantCulture), Convert.ToDouble(element.Element("Longitude").Value, CultureInfo.InvariantCulture));
info.ShopLocation = geoPoint;
Shops.Add(info);
}
}
foreach (string groupName in productGroupNames)
ActualStatistics.Add(new ProductGroupInfo(0.0, groupName));
UpdateTotalStatistics();
}
void UpdateStatistics(ShopInfo info) {
foreach (ProductGroupInfo productGroupInfo in ActualStatistics)
productGroupInfo.Value = info.GetSalesByProductGroup(productGroupInfo.Name);
SalesDescription = "Last Month Sales: " + info.Name;
chart.UpdateData();
chart.Animate();
gaugeControl.Visibility = Visibility.Visible;
}
void UpdateMinMaxSales() {
double minSales = Shops[0].Sales;
double maxSales = Shops[0].Sales;
foreach (ShopInfo info in Shops) {
if (info.Sales > maxSales)
maxSales = info.Sales;
if (info.Sales < minSales)
minSales = info.Sales;
}
MinSalesLevel = minSales;
MaxSalesLevel = maxSales;
}
public void UpdateTotalStatistics() {
foreach (ProductGroupInfo info in ActualStatistics) {
info.Value = 0.0;
foreach (ShopInfo shopInfo in Shops)
info.Value += shopInfo.GetSalesByProductGroup(info.Name);
}
gaugeControl.Visibility = Visibility.Collapsed;
SalesDescription = "Last Month Sales: All Shops";
chart.UpdateData();
chart.Animate();
}
}
}