Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I added an MS Chart component to my Win Forms application and for the most part it works and looks pretty good. I am having two issues:

#1 - whenever I select a new element, some of the StripLine values are still showing up. This only happens for some elements and I can't find a common denominator why this happens. When ever the event is triggered , the first thing I do is Element.Series.Clear(); which should remove any data from the previous event.

#2 - I need to place the actual value of the point being added to the Y axis. Currently I show the coordinate as a circle using MarkerStyle.Circle. I would like to place the value above the circle. I attempted to use the AxisLabel property but it is replacing the column header property on the bottom portion of the chart area. I suspect I should be using a different property.

Here's the code

C#
       int blockSize = 30;
       // clear the chart
       Element.Series.Clear();

       // fill the chart
       var series = Element.Series.Add("My Series");
       series.ChartType = SeriesChartType.Line;
       series.XValueType = ChartValueType.Int32;

       int counter = 0;
       int cnt = 0;

       var chartArea = Element.ChartAreas[series.ChartArea];

       Element.Series["My Series"].BorderWidth = 3;
       Element.Series["My Series"].MarkerStyle = MarkerStyle.Circle;
       Element.Series["My Series"].MarkerSize = 10;
       Element.Series["My Series"].SmartLabelStyle.Enabled = true;
       series.Points.Clear();

       foreach( DataGridViewColumn col in dataGridView1.Columns)
       {
           if (row.Cells[counter].Value != null && counter > 0)
           {
               series.Points.AddXY(col.HeaderText, row.Cells[counter].Value.ToString());
               // the following line overwrites the column Header text
             //  series.Points[cnt].AxisLabel = row.Cells[counter].Value.ToString();


                if (Convert.ToDouble(row.Cells[counter].Value.ToString()) < minVal)
                {
                    minVal = Convert.ToDouble(row.Cells[counter].Value.ToString());
                }

                if (Convert.ToDouble(row.Cells[counter].Value.ToString()) > maxVal)
                {
                    maxVal = Convert.ToDouble(row.Cells[counter].Value.ToString());
                }

                cnt += 1;
           }
               counter += 1;
       }



       // set view range to [0,max]
       chartArea.AxisX.Minimum = 1;
       chartArea.AxisX.Maximum = cnt;


       chartArea.AxisY.Minimum = minVal;
       chartArea.AxisY.Maximum = maxVal;

       // enable autoscroll
       chartArea.CursorX.AutoScroll = true;

       // let's zoom to [0,blockSize] (e.g. [0,100])
       chartArea.AxisX.ScaleView.Zoomable = true;
//       chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
       int position = 0;
       //  int size = blockSize;
       int size = 10;
       chartArea.AxisX.ScaleView.Zoom(position, size);

       // disable zoom-reset button (only scrollbar's arrows are available)
       chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;

       // set scrollbar small change to blockSize (e.g. 100)
       chartArea.AxisX.ScaleView.SmallScrollSize = blockSize;

 StripLine stripLine = new StripLine();
// chartArea.AxisY.StripLines.Remove(stripLine);
 stripLine.IntervalOffset = mminVal;
 stripLine.StripWidth = stripLine.StripWidth = (mmaxVal - mminVal);
 stripLine.TextAlignment = StringAlignment.Near;
 stripLine.ForeColor = Color.Red;
 stripLine.Text = "Normal Range";
 stripLine.BackColor = Color.LightPink;
 stripLine.BorderColor = Color.Red;
 stripLine.BorderDashStyle = ChartDashStyle.Dash;
 stripLine.BorderWidth = (int)1.5;
 stripLine.BackHatchStyle = ChartHatchStyle.LightDownwardDiagonal;
 chartArea.AxisY.StripLines.Add(stripLine);
Posted
Comments
George Jonsson 26-Jun-14 23:29pm    
For #1
Have you tried to add an Update after clearing the series?
Element.Series.Clear();
Element.Update();
stevenandler 27-Jun-14 9:09am    
Hi George

I added the Element.Update(), but I still get multiple strip lines showing up for certain elements. Can you recommend a good text source which explains the MS Chart component in detail, how it works, clear explanation of all of its properties and and some examples? It has been very frustrating getting any organized documentation about MS Chart even from Microsoft.
George Jonsson 27-Jun-14 9:28am    
Sorry, I am in the same situation. Have not found any really good documentation so it is trial and error or try to find info on forums.
Another thing you can try is Element.DataBind() after the Element.Clear().
That is the best I can offer.
stevenandler 27-Jun-14 11:04am    
Databind() didn't help me out. I did find two resources of some help, Microsoft has a forum dedicated to MS Chart. I also found Alex Gorev's blog to be of help. Since my boss wants me convert this app to a WPF, I'm thinking it doesn't pay for me to spend any more effort on this project. I hope the charting component available with WPF will be easier and have better documentation.

Thanks for your assistance!

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