Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Chart
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:
 
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 9-Apr-13 4:57am
Steve I376
Edited 9-Apr-13 5:47am
_duDE_8K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Member 8066974 at 11-Nov-13 11:11am
   
Can you post the full code solution?
Steve I at 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)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 295
2 Afzaal Ahmad Zeeshan 224
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 160
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100