using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
using System.Drawing;
namespace Novacode
{
///
/// Represents every Chart in this document.
///
public abstract class Chart
{
protected XElement ChartXml { get; private set; }
protected XElement ChartRootXml { get; private set; }
///
/// The xml representation of this chart
///
public XDocument Xml { get; private set; }
#region Series
///
/// Chart's series
///
public List Series
{
get
{
List series = new List();
foreach (XElement element in ChartXml.Elements(XName.Get("ser", DocX.c.NamespaceName)))
{
series.Add(new Series(element));
}
return series;
}
}
///
/// Return maximum count of series
///
public virtual Int16 MaxSeriesCount { get { return Int16.MaxValue; } }
///
/// Add a new series to this chart
///
public void AddSeries(Series series)
{
if (ChartXml.Elements(XName.Get("ser", DocX.c.NamespaceName)).Count() == MaxSeriesCount)
throw new InvalidOperationException("Maximum series for this chart is" + MaxSeriesCount.ToString() + "and have exceeded!");
ChartXml.Add(series.Xml);
}
#endregion
#region Legend
///
/// Chart's legend.
/// If legend doesn't exist property is null.
///
public ChartLegend Legend { get; private set; }
///
/// Add standart legend to the chart.
///
public void AddLegend()
{
AddLegend(ChartLegendPosition.Right, false);
}
///
/// Add a legend with parameters to the chart.
///
public void AddLegend(ChartLegendPosition position, Boolean overlay)
{
if (Legend != null)
RemoveLegend();
Legend = new ChartLegend(position, overlay);
ChartRootXml.Add(Legend.Xml);
}
///
/// Remove the legend from the chart.
///
public void RemoveLegend()
{
Legend.Xml.Remove();
Legend = null;
}
#endregion
#region Axis
///
/// Represents the category axis
///
public CategoryAxis CategoryAxis { get; private set; }
///
/// Represents the values axis
///
public ValueAxis ValueAxis { get; private set; }
///
/// Represents existing the axis
///
public virtual Boolean IsAxisExist { get { return true; } }
#endregion
///
/// Get or set 3D view for this chart
///
public Boolean View3D
{
get
{
return ChartXml.Name.LocalName.Contains("3D");
}
set
{
if (value)
{
if (!View3D)
{
String currentName = ChartXml.Name.LocalName;
ChartXml.Name = XName.Get(currentName.Replace("Chart", "3DChart"), DocX.c.NamespaceName);
}
}
else
{
if (View3D)
{
String currentName = ChartXml.Name.LocalName;
ChartXml.Name = XName.Get(currentName.Replace("3DChart", "Chart"), DocX.c.NamespaceName);
}
}
}
}
///
/// Specifies how blank cells shall be plotted on a chart
///
public DisplayBlanksAs DisplayBlanksAs
{
get
{
return XElementHelpers.GetValueToEnum(
ChartRootXml.Element(XName.Get("dispBlanksAs", DocX.c.NamespaceName)));
}
set
{
XElementHelpers.SetValueFromEnum(
ChartRootXml.Element(XName.Get("dispBlanksAs", DocX.c.NamespaceName)), value);
}
}
///
/// Create an Chart for this document
///
public Chart()
{
// Create global xml
Xml = XDocument.Parse
(@"
");
// Create a real chart xml in an inheritor
ChartXml = CreateChartXml();
// Create result plotarea element
XElement plotAreaXml = new XElement(
XName.Get("plotArea", DocX.c.NamespaceName),
new XElement(XName.Get("layout", DocX.c.NamespaceName)),
ChartXml);
// Set labels
XElement dLblsXml = XElement.Parse(
@"
");
ChartXml.Add(dLblsXml);
// if axes exists, create their
if (IsAxisExist)
{
CategoryAxis = new CategoryAxis("148921728");
ValueAxis = new ValueAxis("154227840");
XElement axIDcatXml = XElement.Parse(String.Format(
@"", CategoryAxis.Id));
XElement axIDvalXml = XElement.Parse(String.Format(
@"", ValueAxis.Id));
ChartXml.Add(axIDcatXml);
ChartXml.Add(axIDvalXml);
plotAreaXml.Add(CategoryAxis.Xml);
plotAreaXml.Add(ValueAxis.Xml);
}
ChartRootXml = Xml.Root.Element(XName.Get("chart", DocX.c.NamespaceName));
ChartRootXml.Add(plotAreaXml);
}
///
/// An abstract method which creates the current chart xml
///
protected abstract XElement CreateChartXml();
}
///
/// Represents a chart series
///
public class Series
{
private XElement strCache;
private XElement numCache;
///
/// Series xml element
///
internal XElement Xml { get; private set; }
public Color Color
{
get
{
XElement colorElement = Xml.Element(XName.Get("spPr", DocX.c.NamespaceName));
if (colorElement == null)
return Color.Transparent;
else
return Color.FromArgb(Int32.Parse(
colorElement.Element(XName.Get("solidFill", DocX.a.NamespaceName)).Element(XName.Get("srgbClr", DocX.a.NamespaceName)).Attribute(XName.Get("val")).Value,
System.Globalization.NumberStyles.HexNumber));
}
set
{
XElement colorElement = Xml.Element(XName.Get("spPr", DocX.c.NamespaceName));
if (colorElement != null)
colorElement.Remove();
colorElement = new XElement(
XName.Get("spPr", DocX.c.NamespaceName),
new XElement(
XName.Get("solidFill", DocX.a.NamespaceName),
new XElement(
XName.Get("srgbClr", DocX.a.NamespaceName),
new XAttribute(XName.Get("val"), value.ToHex()))));
Xml.Add(colorElement);
}
}
internal Series(XElement xml)
{
Xml = xml;
strCache = xml.Element(XName.Get("cat", DocX.c.NamespaceName)).Element(XName.Get("strRef", DocX.c.NamespaceName)).Element(XName.Get("strCache", DocX.c.NamespaceName));
numCache = xml.Element(XName.Get("val", DocX.c.NamespaceName)).Element(XName.Get("numRef", DocX.c.NamespaceName)).Element(XName.Get("numCache", DocX.c.NamespaceName));
}
public Series(String name)
{
strCache = new XElement(XName.Get("strCache", DocX.c.NamespaceName));
numCache = new XElement(XName.Get("numCache", DocX.c.NamespaceName));
Xml = new XElement(
XName.Get("ser", DocX.c.NamespaceName),
new XElement(
XName.Get("tx", DocX.c.NamespaceName),
new XElement(
XName.Get("strRef", DocX.c.NamespaceName),
new XElement(
XName.Get("strCache", DocX.c.NamespaceName),
new XElement(
XName.Get("pt", DocX.c.NamespaceName),
new XAttribute(XName.Get("idx"), "0"),
new XElement(XName.Get("v", DocX.c.NamespaceName), name)
)))),
new XElement(XName.Get("invertIfNegative", DocX.c.NamespaceName), "0"),
new XElement(
XName.Get("cat", DocX.c.NamespaceName),
new XElement(XName.Get("strRef", DocX.c.NamespaceName), strCache)),
new XElement(
XName.Get("val", DocX.c.NamespaceName),
new XElement(XName.Get("numRef", DocX.c.NamespaceName), numCache))
);
}
public void Bind(ICollection list, String categoryPropertyName, String valuePropertyName)
{
XElement ptCount = new XElement(XName.Get("ptCount", DocX.c.NamespaceName), new XAttribute(XName.Get("val"), list.Count));
XElement formatCode = new XElement(XName.Get("formatCode", DocX.c.NamespaceName), "General");
strCache.RemoveAll();
numCache.RemoveAll();
strCache.Add(ptCount);
numCache.Add(ptCount);
numCache.Add(formatCode);
Int32 index = 0;
XElement pt;
foreach (var item in list)
{
pt = new XElement(XName.Get("pt", DocX.c.NamespaceName), new XAttribute(XName.Get("idx"), index),
new XElement(XName.Get("v", DocX.c.NamespaceName), item.GetType().GetProperty(categoryPropertyName).GetValue(item, null)));
strCache.Add(pt);
pt = new XElement(XName.Get("pt", DocX.c.NamespaceName), new XAttribute(XName.Get("idx"), index),
new XElement(XName.Get("v", DocX.c.NamespaceName), item.GetType().GetProperty(valuePropertyName).GetValue(item, null)));
numCache.Add(pt);
index++;
}
}
public void Bind(IList categories, IList values)
{
if (categories.Count != values.Count)
throw new ArgumentException("Categories count must equal to Values count");
XElement ptCount = new XElement(XName.Get("ptCount", DocX.c.NamespaceName), new XAttribute(XName.Get("val"), categories.Count));
XElement formatCode = new XElement(XName.Get("formatCode", DocX.c.NamespaceName), "General");
strCache.RemoveAll();
numCache.RemoveAll();
strCache.Add(ptCount);
numCache.Add(ptCount);
numCache.Add(formatCode);
XElement pt;
for (int index = 0; index < categories.Count; index++)
{
pt = new XElement(XName.Get("pt", DocX.c.NamespaceName), new XAttribute(XName.Get("idx"), index),
new XElement(XName.Get("v", DocX.c.NamespaceName), categories[index].ToString()));
strCache.Add(pt);
pt = new XElement(XName.Get("pt", DocX.c.NamespaceName), new XAttribute(XName.Get("idx"), index),
new XElement(XName.Get("v", DocX.c.NamespaceName), values[index].ToString()));
numCache.Add(pt);
}
}
}
///
/// Represents a chart legend
/// More: http://msdn.microsoft.com/ru-ru/library/cc845123.aspx
///
public class ChartLegend
{
///
/// Legend xml element
///
internal XElement Xml { get; private set; }
///
/// Specifies that other chart elements shall be allowed to overlap this chart element
///
public Boolean Overlay
{
get { return Xml.Element(XName.Get("overlay", DocX.c.NamespaceName)).Attribute("val").Value == "1"; }
set { Xml.Element(XName.Get("overlay", DocX.c.NamespaceName)).Attribute("val").Value = GetOverlayValue(value); }
}
///
/// Specifies the possible positions for a legend
///
public ChartLegendPosition Position
{
get
{
return XElementHelpers.GetValueToEnum(
Xml.Element(XName.Get("legendPos", DocX.c.NamespaceName)));
}
set
{
XElementHelpers.SetValueFromEnum(
Xml.Element(XName.Get("legendPos", DocX.c.NamespaceName)), value);
}
}
internal ChartLegend(ChartLegendPosition position, Boolean overlay)
{
Xml = new XElement(
XName.Get("legend", DocX.c.NamespaceName),
new XElement(XName.Get("legendPos", DocX.c.NamespaceName), new XAttribute("val", XElementHelpers.GetXmlNameFromEnum(position))),
new XElement(XName.Get("overlay", DocX.c.NamespaceName), new XAttribute("val", GetOverlayValue(overlay)))
);
}
///
/// ECMA-376, page 3840
/// 21.2.2.132 overlay (Overlay)
///
private String GetOverlayValue(Boolean overlay)
{
if (overlay)
return "1";
else
return "0";
}
}
///
/// Specifies the possible positions for a legend.
/// 21.2.3.24 ST_LegendPos (Legend Position)
///
public enum ChartLegendPosition
{
[XmlName("t")]
Top,
[XmlName("b")]
Bottom,
[XmlName("l")]
Left,
[XmlName("r")]
Right,
[XmlName("tr")]
TopRight
}
///
/// Specifies the possible ways to display blanks.
/// 21.2.3.10 ST_DispBlanksAs (Display Blanks As)
///
public enum DisplayBlanksAs
{
[XmlName("gap")]
Gap,
[XmlName("span")]
Span,
[XmlName("zero")]
Zero
}
}