/// <summary>
/// Public ctrl graph class
/// </summary>
public partial class CtrlBarGraph : System.Web.UI.UserControl
{
private SessionStateVar<BusAdvertisingCollection> m_AdvertisingCollection;
private SessionStateVar<BusRegionCollection> m_RegionCollection;
private SessionStateVar<List<Series>> m_seriesCollection;
/// <summary>
/// CtrlBarGraph shows any type of bar graph
/// </summary>
public CtrlBarGraph()
{
m_AdvertisingCollection = new SessionStateVar<BusAdvertisingCollection>("advertisingCollection");
m_RegionCollection = new SessionStateVar<BusRegionCollection>("regionCollection");
m_seriesCollection = new SessionStateVar<List<Series>>("seriesCollection", new List<Series>());
}
protected void Page_Init(object sender, EventArgs e)
{
chrAdvertising.EnableViewState = true;
}
/// <summary>
/// Page load event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//if (chrAdvertising.Series.Count == 0)
// chrAdvertising.Visible = false;
chrAdvertising.ViewStateContent = SerializationContents.Default;
lblError.Visible = false;
int i = 0;
while (i < ddlColorPicker.Items.Count)
{
ddlColorPicker.Items[i].Attributes.Add("style", "background-color:" + ddlColorPicker.Items[i].Value);
i++;
}
} /// <summary>
/// ibClicked happens when any image button is clicked.
/// Moves the listitems in the list box either up or down,
/// or moves them between shown and hidden
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ibClicked(object sender, EventArgs e)
{
ImageButton ibClicked = sender as ImageButton;
string command = ibClicked.CommandArgument;
if (command == "up")
lbSeriesCurrent.Items.ShiftSelectedUp();
else if (command == "down")
lbSeriesCurrent.Items.ShiftSelectedDown();
else if (command == "hide")
{
foreach (ListItem item in lbSeriesCurrent.Items.GetAllSelected())
{
chrAdvertising.Series.Remove(chrAdvertising.Series.FindByName(item.Value));
}
lbSeriesCurrent.Items.MoveSelectedTo(lbSeriesNotShown.Items, true);
}
else if (command == "show")
lbSeriesNotShown.Items.MoveSelectedTo(lbSeriesCurrent.Items, true);
showGraph();
}
/// <summary>
/// populates the advertising collection off the session variable
/// </summary>
protected void populate()
{
if (!m_AdvertisingCollection.IsSet())
{
m_AdvertisingCollection.Value = BusAdvertisingManager.GetALL("DateCreated");
}
}
/// <summary>
/// Populates the regions from the session variable
/// </summary>
protected void populateRegions()
{
if (!m_RegionCollection.IsSet())
{
m_RegionCollection.Value = BusRegionManager.GetRegionAll();
}
}
/// <summary>
/// Appends the current search to the list box
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAppend_Click(object sender, EventArgs e)
{
string legend = getLegendName();
ListItem liAdded = new ListItem();
liAdded.Text = legend;
liAdded.Value = legend;
foreach (ListItem item in lbSeriesCurrent.Items)
{
if (item.Text == liAdded.Text)
{
lblError.Visible = true;
return;
}
}
foreach (ListItem item in lbSeriesNotShown.Items)
{
if (item.Text == liAdded.Text)
{
lblError.Visible = true;
return;
}
}
populate();
Series addSeries = new Series(legend);
addSeries.ChartType = GetChartType();
addSeries.BorderWidth = 3;
addSeries.ShadowOffset = 2;
DateTime startDtm = tbDateBegin.Text.ToDateTime(m_AdvertisingCollection.Value[0].DateCreated);
DateTime EndDtm = tbDateEnd.Text.ToDateTime(m_AdvertisingCollection.Value.Last().DateCreated);
List<DateTime> Periods = new List<DateTime>();
List<List<double>> ListCollection = GetDateRange(startDtm, EndDtm, Periods);
List<double> Pages = new List<double>();
int count = 0;
BusAdvertisingCollection AdCollection = m_AdvertisingCollection.Value;
foreach (BusAdvertising item in AdCollection)
{
count = 0;
double itemGraphed = 0;
if (ddlSearchBy.SelectedItem.Value == "NumOfPieces")
itemGraphed = 1;
else if (ddlSearchBy.SelectedItem.Value == "NumOfPages")
itemGraphed = item.NumberOfPages;
else if (ddlSearchBy.SelectedItem.Value == "ComplexityScore")
itemGraphed = item.PageScoreID;
else if (ddlSearchBy.SelectedItem.Value == "TurnaroundTime")
itemGraphed = 1;////////TEMP, not working yet////////////////////////<----------FIX
if (!belongs(item, startDtm, EndDtm))
continue;
if (count + 1 > Periods.Count())
{
//count of 0, no items added within range
}
else if (count + 1 == Periods.Count())
{
//All datapoints fall within one time period
ListCollection[count].Add(itemGraphed);
}
else
{
//Item falls between two ranges of periods
while (count < Periods.Count && item.DateCreated >= Periods[count])
{
count++;
}
ListCollection[count - 1].Add(itemGraphed);
}
}
count = 0;
foreach (List<double> value in ListCollection)
{
string dateAxis = string.Empty;
if (Periods.Count == 1)
dateAxis = startDtm.ToShortDateString() + " - " + EndDtm.AddDays(-1).ToShortDateString();
else if ((count + 1 < Periods.Count))
dateAxis = Periods[count].ToShortDateString() + " - " + Periods[count + 1].AddDays(-1).ToShortDateString();
else
dateAxis = Periods[count].ToShortDateString() + " - " + EndDtm.ToShortDateString();
if (value.Count == 0)
addSeries.Points.AddXY(dateAxis, 0);
else if (cbAvg.Checked)
addSeries.Points.AddXY(dateAxis, value.Average());
else
addSeries.Points.AddXY(dateAxis, value.Sum());
count++;
}
addSeries.Color = System.Drawing.Color.FromName(ddlColorPicker.SelectedValue);
addSeries.LegendText = legend;
addSeries.ToolTip = legend;
lbSeriesCurrent.Items.Add(liAdded);
if (!m_seriesCollection.IsSet())
m_seriesCollection.Value = new List<Series>();
m_seriesCollection.Value.Add(addSeries);
showGraph();
}
/// <summary>
/// belongs checks to see if the collection matches the dropdownlist selected,
/// then checks to see if the date range is within the time frame.
/// </summary>
/// <param name="item"></param>
/// <param name="startDTM"></param>
/// <param name="endDTM"></param>
/// <returns></returns>
private bool belongs(BusAdvertising item, DateTime startDTM, DateTime endDTM)
{
if (checkRB(item))
if (checkTimeFrame(item, startDTM, endDTM))
return true;
return false;
}
private bool checkRB(BusAdvertising item)
{
if (rbBranch.Checked)
{
if (item.Branch == ddlSearchText.SelectedItem.Value)
return true;
return false;
}
else if (rbFANumber.Checked)
{
if (item.FANumber == ddlSearchText.SelectedItem.Value)
return true;
return false;
}
else if (rbRegion.Checked)
{
if (item.RegionId.ToString() == ddlSearchText.SelectedItem.Value)
return true;
return false;
}
return true;
}
private bool checkTimeFrame(BusAdvertising item, DateTime startDTM, DateTime endDTM)
{
if ((startDTM.AddDays(-1) < item.DateCreated)
&& (endDTM.AddDays(1) >= item.DateCreated))
return true;
return false;
}
private List<List<double>> GetDateRange(DateTime startDtm, DateTime EndDtm, List<DateTime> periods)
{
List<List<double>> range = new List<List<double>>();
switch (ddlBreakdown.SelectedValue.ToInt(0))
{
case 0:////////////All//////////////////
periods.Add(startDtm);
range.Add(new List<double>());
break;
case 1://////////Monthly////////////////
periods.Add(startDtm);
range.Add(new List<double>());
//Go to end of month
if (startDtm.Day == 1)
startDtm = startDtm.AddMonths(1);
while (startDtm.Day != 1)
startDtm = startDtm.AddDays(1);
while (startDtm < EndDtm)
{
periods.Add(startDtm);
range.Add(new List<double>());
startDtm = startDtm.AddMonths(1);
}
break;
case 3://////////Quarterly//////////////
periods.Add(startDtm);
range.Add(new List<double>());
//Go to end of Quarter
if (startDtm.Day == 1 && ((startDtm.Month == 10) || (startDtm.Month == 1) || (startDtm.Month == 4) || (startDtm.Month == 7)))
startDtm = startDtm.AddMonths(3);
while (startDtm.Day != 1)
startDtm = startDtm.AddDays(1);
while (!((startDtm.Month == 10) || (startDtm.Month == 1) || (startDtm.Month == 4) || (startDtm.Month == 7)))
startDtm = startDtm.AddMonths(1);
while (startDtm < EndDtm)
{
periods.Add(startDtm);
range.Add(new List<double>());
startDtm = startDtm.AddMonths(3);
}
break;
case 12://///Yearly (fiscal)////////////
periods.Add(startDtm);
range.Add(new List<double>());
if (startDtm.Day == 1 && startDtm.Month == 10)
startDtm = startDtm.AddYears(1);
while (startDtm.Day != 1)
startDtm = startDtm.AddDays(1);
while (!(startDtm.Month == 10))
startDtm = startDtm.AddMonths(1);
while (startDtm < EndDtm)
{
periods.Add(startDtm);
range.Add(new List<double>());
startDtm = startDtm.AddYears(1);
}
break;
case 36:////Tri-Yearly (3 fiscal years)//
periods.Add(startDtm);
range.Add(new List<double>());
if (startDtm.Day == 1 && startDtm.Month == 10)
startDtm = startDtm.AddYears(3);
while (startDtm.Day != 1)
startDtm = startDtm.AddDays(1);
while (!(startDtm.Month == 10))
startDtm = startDtm.AddMonths(1);
while (startDtm < EndDtm)
{
periods.Add(startDtm);
range.Add(new List<double>());
startDtm = startDtm.AddYears(3);
}
break;
default:
break;
}
return range;
}
private SeriesChartType GetChartType()
{
if (ddlChartType.SelectedValue == "Column")
return SeriesChartType.Column;
if (ddlChartType.SelectedValue == "Line")
return SeriesChartType.Line;
if (ddlChartType.SelectedValue == "Spline")
return SeriesChartType.Spline;
if (ddlChartType.SelectedValue == "StepLine")
return SeriesChartType.StepLine;
if (ddlChartType.SelectedValue == "Area")
return SeriesChartType.Area;
if (ddlChartType.SelectedValue == "SplineArea")
return SeriesChartType.SplineArea;
if (ddlChartType.SelectedValue == "Point")
return SeriesChartType.Point;
return SeriesChartType.Column;
}
private string getLegendName()
{
string legendName = string.Empty;
if (cbAvg.Checked)
legendName += "(Avg)";
legendName += ddlSearchBy.SelectedItem.Text;
if (rbBranch.Checked)
legendName += "-Branch-" + ddlSearchText.SelectedItem.Text;
else if (rbFANumber.Checked)
legendName += "-FA-" + ddlSearchText.SelectedItem.Text;
else if (rbRegion.Checked)
legendName += "-Region-" + ddlSearchText.SelectedItem.Text;
legendName += "~" + ddlChartType.SelectedItem.Text;
return legendName;
}
private void clearChartSeries(List<Series> seriesCollection)
{
foreach (Series series in seriesCollection)
if (chrAdvertising.Series.FindByName(series.Name) != null)
chrAdvertising.Series.Remove(chrAdvertising.Series.FindByName(series.Name));
}
/// <summary>
/// Occurs any time a radio button changes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rbCheckChanged(object sender, EventArgs e)
{
ddlSearchText.Items.Clear();
if (!rbAll.Checked)
{
ddlSearchText.Visible = true;
populate();
List<string> possibilities = new List<string>();
BusAdvertisingCollection ddlCollection = m_AdvertisingCollection.Value;
foreach (BusAdvertising item in ddlCollection)
{
if (rbBranch.Checked)
{
if (!possibilities.Contains(item.Branch))
possibilities.Add(item.Branch);
}
else if (rbFANumber.Checked)
{
if (!possibilities.Contains(item.FANumber))
possibilities.Add(item.FANumber);
}
else if (rbRegion.Checked)
{
if (!possibilities.Contains(item.RegionId.ToString()))
possibilities.Add(item.RegionId.ToString());
}
}
populateRegions();
possibilities.Sort();
foreach (string item in possibilities)
{
ListItem newItem = new ListItem();
newItem.Value = item;
if (rbRegion.Checked)
{
foreach (BusRegion region in m_RegionCollection.Value)
{
if (region.RjRegionId.ToString() == item)
{
newItem.Text = region.DescText;
break;
}
}
if (newItem.Text == "")
newItem.Text = item;
}
else
newItem.Text = item;
ddlSearchText.Items.Add(newItem);
}
}
else
ddlSearchText.Visible = false;
}
/// <summary>
/// showGraph happens when the user wants to display the items
/// in the listbox onto the chart.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void showGraph()
{
foreach (ListItem item in lbSeriesCurrent.Items)
foreach (Series SeriesItem in m_seriesCollection.Value)
if ((SeriesItem.Name == item.Text) && (chrAdvertising.Series.FindByName(SeriesItem.Name) == null))
{
//SeriesItem.Name = //----------------I tried to add by different name each time.
chrAdvertising.Series.Add(SeriesItem);
}
} /// <summary>
/// Removes all the graph information currently being pulled
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnRemoveAll_Click(object sender, EventArgs e)
{
chrAdvertising.Series.Clear();
m_seriesCollection.Clear();
m_RegionCollection.Clear();
m_AdvertisingCollection.Clear();
lbSeriesCurrent.Items.Clear();
lbSeriesNotShown.Items.Clear();
}
protected void btnSaveImage_Click(object sender, EventArgs e)
{
//chrAdvertising.SaveImage();
}
}
Here is the code that didnt past correctly to your reply