Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm new to C# and .Net and am facing a problem within an MS chart I've created. I've searched and found answers to this question already, but although I have implemented the replies correctly, so I thought, I still have not been able to get it to work. The chart displays only the final series of the array (groups[3]). My code is as follows:

C#
string[] groups = new string[5];
        groups[0] = "MD";
        groups[1] = "SR";
        groups[2] = "TS";
        groups[3] = "WS";
        
        System.Drawing.Color[] clrs = new System.Drawing.Color[5];
        clrs[0] = System.Drawing.Color.Red;
    clrs[1] = System.Drawing.Color.Green;
        clrs[2] = System.Drawing.Color.MediumPurple;
        clrs[3] = System.Drawing.Color.Blue;
        clrs[4] = System.Drawing.Color.Orange;

        Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
        Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 96;
        Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
        
        Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
        Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 400;
        Chart1.ChartAreas["ChartArea1"].AxisY.Interval = 50;
        
        for (int cnt = 0; cnt < 4; cnt++)
        {
            com2.CommandText = "";
            com2.CommandText = "SELECT left(convert(varchar,Date,106),6) + char(10) as Date, occupancy FROM occupancy_projection WHERE specialtygroup = '" + groups[cnt] + "' order by convert(date,Date)";
            SqlDataReader reader = com2.ExecuteReader();


           Series aSeries = new Series();
           aSeries.Name = groups[cnt];


            Chart1.Series.Add(aSeries[cnt]);
            Chart1.Series[cnt].ChartType = SeriesChartType.Line;
            Chart1.Series[cnt].BorderWidth = 2;
            Chart1.Series[cnt].MarkerStyle = MarkerStyle.Circle;
            Chart1.Series[cnt].XValueMember = "Date";
            Chart1.Series[cnt].YValueMembers = "occupancy";
            Chart1.Series[cnt].Color = clrs[cnt];
            //Chart1.Series[cnt].Legend = "Legend1";

            Chart1.DataSource = reader;
            Chart1.DataBind();
            reader.Close();
            
            
                       
        }
        
        com.Dispose();
        con.Close();  
    }


I wonder if anyone can point out my error(s) here?

Thanks

Steve
Posted
Updated 9-Apr-13 4:47am
v2

1 solution

Answering my own question, this is not a solution to the problem as stated, but it solved the issue by means of a workaround: I altered the configuration of the data by getting the stored procedure to pivot it so that I was able to assign a column from the table to each series. I'm still curious to know why the above code didn't work though!

I now have another question about Chart Series which I will post separately.


Regards

Steve
 
Share this answer
 
Comments
Member 8066974 11-Nov-13 11:11am    
Can you post the full code solution?
Steve I 12-Nov-13 2:02am    
The sp now produced a table of 6 cols (theDate, M, SR,TO, WS, Ad), the first being the date, the rest containing integer values assigned in each category to that date. The sp that produces this table is quite long and involved, so it's probably not practicable to reproduce it here.

The relevant c# code is this procedure:

protected void BindChartData()
{
groups[0] = "M";
groups[1] = "SR";
groups[2] = "TO";
groups[3] = "WS";
groups[4] = "Ad";

clrs[0] = Color.Red;
clrs[1] = Color.Green;
clrs[2] = Color.MediumPurple;
clrs[3] = Color.Blue;
clrs[4] = Color.SandyBrown;

if (Page.IsPostBack)
{
if (targetIt.Value == "Ok")
BindTargetChartData();
}
else
{
initialiseChartData(); // Binds data to a Gridview
}
com.Dispose();

Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 94;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 400;
Chart1.ChartAreas["ChartArea1"].AxisY.Interval = 50;

Chart1.Series.Clear();

sqlStr = "SELECT left(convert(varchar,theDate,106),6) + char(10) as theDate, M, S, [TO], WS, Ad FROM occ_proj_tmp order by convert(date,theDate)";
com2.CommandText = sqlStr;
SqlDataReader reader = com2.ExecuteReader();

for (int cnt = 0; cnt < 5; cnt++)
{
addSeries( groups[cnt], clrs[cnt]);
}

Chart1.DataSource = reader;
Chart1.DataBind();

reader.Close();
com2.Dispose();
con.Close();
}

I hope these essentials make some sense and are of help. As I said, to post the whole code, including the stored procedure is probably not feasible.

Regards

Steve

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900