using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
namespace XAMLFinance.DataSource
{
/// <summary>
/// A heatmap, which groups instruments by sector.
/// </summary>
public class HeatmapData
{
internal HeatmapData(XDocument element)
{
// find all stocks with a sector, change % and market cap.
var stocks = element.Descendants("stock")
.Where(el => el.Attribute("sector") != null && el.Attribute("changePercent") != null && el.Attribute("marketCap") != null)
.Select(el => new InstrumentPriceData(el));
// group stocks by sector
var stocksBySector = stocks.GroupBy
(s => s.Sector, s => s.MarketCap.Value,
(sector, marketCaps) =>
new SectorData()
{
Name = sector,
MarketCap = marketCaps.Aggregate(0.0, (total, val) => total + val),
ChangePercent = stocks.Where(s => s.Sector == sector).Select(s => s.ChangePercent.Value).Average(),
Children = stocks.Where(s => s.Sector == sector).ToList()
}
);
Sectors = stocksBySector.ToList();
}
/// <summary>
/// Gets the sectors within the heatmap
/// </summary>
public List<SectorData> Sectors { get; private set; }
}
}