I am using the Chart helper (7-displaying-data-in-a-chart) to build some charts for a report. This report is derived from a survey. At one part of the survey there are some questions that could havee anywhere from 2 to 10 different responses so I can not know at build time what the number is. I have some stored procedures in the datbase that return the pivoted values for the report.
My class reflects the dynamics of this
public class Demographics
{
public string Xvalue { get; set; }
public double YValue1 { get; set; }
public double YValue2 { get; set; }
public double YValue3 { get; set; }
public double YValue4 { get; set; }
public double YValue5 { get; set; }
public double YValue6 { get; set; }
public double YValue7 { get; set; }
public double YValue8 { get; set; }
public double YValue9 { get; set; }
public double YValue10 { get; set; }
}
to display the values iIhave the following method in the Demographics class which is then called in my controller
public List<Demographics> GetPivotedDemographics(int GroupNumber, int SurveyNumber, int Demographic, int NumResponses)
{
using (SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (SqlCommand oCmd = new SqlCommand())
{
try
{
oCmd.CommandText = "PivotedDemographicGroupAverage";
oCmd.CommandType = CommandType.StoredProcedure;
oCmd.Parameters.Add("@GroupID", SqlDbType.Int).Value = GroupNumber;
oCmd.Parameters.Add("@Survey", SqlDbType.Int).Value = SurveyNumber;
oCmd.Parameters.Add("@Demographic", SqlDbType.Int).Value = Demographic;
oCmd.Connection = oConn;
oConn.Open();
using (var reader = oCmd.ExecuteReader())
{
List<Demographics> myList = new List<Demographics>();
while (reader.Read())
{
Demographics data = new Demographics();
switch (NumResponses)
{
case 3:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
break;
case 4:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
break;
case 5:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
data.YValue5 = Convert.ToDouble(reader[5]);
break;
case 6:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
data.YValue5 = Convert.ToDouble(reader[5]);
data.YValue6 = Convert.ToDouble(reader[6]);
break;
case 7:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
data.YValue5 = Convert.ToDouble(reader[5]);
data.YValue6 = Convert.ToDouble(reader[6]);
data.YValue7 = Convert.ToDouble(reader[7]);
break;
case 8:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
data.YValue5 = Convert.ToDouble(reader[5]);
data.YValue6 = Convert.ToDouble(reader[6]);
data.YValue7 = Convert.ToDouble(reader[7]);
data.YValue8 = Convert.ToDouble(reader[8]);
break;
case 9:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
data.YValue5 = Convert.ToDouble(reader[5]);
data.YValue6 = Convert.ToDouble(reader[6]);
data.YValue7 = Convert.ToDouble(reader[7]);
data.YValue8 = Convert.ToDouble(reader[8]);
data.YValue9 = Convert.ToDouble(reader[9]);
break;
case 10:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
data.YValue3 = Convert.ToDouble(reader[3]);
data.YValue4 = Convert.ToDouble(reader[4]);
data.YValue5 = Convert.ToDouble(reader[5]);
data.YValue6 = Convert.ToDouble(reader[6]);
data.YValue7 = Convert.ToDouble(reader[7]);
data.YValue8 = Convert.ToDouble(reader[8]);
data.YValue9 = Convert.ToDouble(reader[9]);
data.YValue10 = Convert.ToDouble(reader[10]);
break;
default:
data.Xvalue = reader[0].ToString();
data.YValue1 = Convert.ToDouble(reader[1]);
data.YValue2 = Convert.ToDouble(reader[2]);
break;
}
myList.Add(data);
}
return myList;
}
}
finally
{
oConn.Close();
oCmd.Dispose();
}
}
}
}
And my controller code
//Get the number of possible responses for this question
int NumberPossibleResponses = survey.NumberDemographicResponses(Demographic);
//Get the data as a list
var Group1Data = demo.GetPivotedDemographics(1, SurveyNumber, Demographic, NumberPossibleResponses);
//Build the chart
int c = Group1Data.Count();
ChartHeight = DetermineHeight(c);
FilePathName = "/ChartFiles/" + SurveyNumber.ToString() + "_Demographic_1.jpg";
if (!System.IO.File.Exists(Server.MapPath(FilePathName)))
{
System.IO.File.Delete(Server.MapPath(FilePathName));
}
var Chart1 = new Chart(width: ChartWidth, height: ChartHeight)
.AddTitle("Average Answers")
.DataBindTable(dataSource:Group1Data, xField: "Xvalue")
//.AddLegend(null,Legend)
//.AddSeries("Chart1", chartType: "Bar")
.Save(path: Server.MapPath(FilePathName));
ViewBag.Chart1 = FilePathName;
In the example I am showing there are 4 possible responses being used for the question "how long employed at company"
- Less than 1 year
- At least 1 year but less than 3 years
- At least 3 years but less than 5 years
- More than 5 years
other examples are
What is your gender?
Which location do you work at?
What department do you work in?
Here is the graph this code creates
And this is where I am needing some help as I would like to make 2 changes
- Use a bar chart instead of a column chart
- Have the legend say what the various colors stand for based on the possible responses
How do I go about these two things? Any help would be greatly appreciated.
disclaimer this a purely fictional company with a small amount of data used for testing.