public Plots PrepareData(string xValue, string yValue, GroupAction action)
{
// We first check that xValue and yValue are correct and throw an exception if needed
if (!_items.List.Fields.ContainsField(xValue))
{
string message = "The column \"" + xValue + "\" does not exist in the list \"" + _items.List.Title + "\" or in the specified view \"" + _view.Title + "\".<br /><br />Check the view you specified and make sure that the column exists.";
message += ListValidColumns();
throw (new Exception(message));
}
if (yValue != String.Empty && !_items.List.Fields.ContainsField(yValue))
{
string message = "The column \"" + yValue + "\" does not exist in the list \"" + _items.List.Title + "\" or in the specified view \"" + _view.Title + "\".<br /><br />Check the view you specified and make sure that the column exists.";
message += ListValidColumns();
throw (new Exception(message));
}
// We prepare our output
Plots plots = new Plots();
// We loop through our SPList
foreach (SPListItem item in _items)
{
// We get the X and Y from
string x = "";
try
{
if (item[xValue] == null)
continue;
x = item[xValue].ToString();
}
catch
{
throw (new Exception("The column \"" + xValue + "\" does not exist in the view"));
}
double y = 0;
if (yValue != String.Empty) Double.TryParse(item[yValue].ToString(), out y);
// The value is already in our array
if (plots.Contains(x))
{
int pos = plots.XIndexOf(x);
if (action == GroupAction.COUNT)
{
// Count, we add +1 to the existing value
plots.SetY(pos, Double.Parse(plots.GetY(pos).ToString()) + 1);
}
if (action == GroupAction.SUM)
{
// Sum, we add the value to the existing value
plots.SetY(pos, Double.Parse(plots.GetY(pos).ToString()) + y);
}
if (action == GroupAction.NOTHING)
{
// No action, we simply add the value
plots.Add(x, y);
}
}
else // The value is a new one in our array
{
if (action == GroupAction.COUNT)
{
// Count, the first yValue is 1.
plots.Add(x, 1);
}
else
{
// anything else than Count, we just add the plot
plots.Add(x, y);
}
}
}
return plots;
}