3

I want to add text(say, annotations) in MS chart(winforms) like (10, 20) , (30, 40) where I already have a scroll bar.

I can able to draw strings(graphics.drawstring) in Chart, but on scrolling the horizontal scroll bar, the text which I have drawn remains static and immovable.

On scrolling the scrollbar, the text which I have drawn also should move along with my horizontal scrolling.

My code follows:

 chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
 chart2.BorderlineColor      = System.Drawing.Color.FromArgb(26, 59, 105);
 chart2.BorderlineWidth = 3;
 chart2.BackColor       = Color.White;

 chart2.ChartAreas.Add("chtArea");
 chart2.ChartAreas[0].AxisX.Title = "Category Name";
 chart2.ChartAreas[0].AxisX.TitleFont = 
        new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
 chart2.ChartAreas[0].AxisY.Title = "UnitPrice";
 chart2.ChartAreas[0].AxisY.TitleFont = 
        new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
 chart2.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;
 chart2.ChartAreas[0].BorderWidth = 2;

 chart2.ChartAreas["chtArea"].AxisX.ScrollBar.Enabled = true;
 chart2.ChartAreas["chtArea"].CursorX.IsUserEnabled = true;
 chart2.ChartAreas["chtArea"].CursorX.IsUserSelectionEnabled = true;
 chart2.ChartAreas["chtArea"].AxisX.ScaleView.Zoomable = false;
 chart2.ChartAreas["chtArea"].AxisX.ScrollBar.IsPositionedInside = true;
 chart2.ChartAreas["chtArea"].AxisX.ScaleView.Size = 20;
 chart2.ChartAreas[0].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Seconds;
 chart2.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 1;

 chart2.Legends.Add("UnitPrice");
 chart2.Series.Add("UnitPrice");
 chart2.Series[0].ChartType = SeriesChartType.Line;

 Random rand = new Random();
 var valuesArray = Enumerable.Range(0, 500).Select(x => rand.Next(0, 100)).ToArray();

 for (int i = 0; i < 500; i++)
 {                         
      chart2.Series["UnitPrice"].Points.AddXY(i+10, valuesArray[i]);               
 }

I tried TextAnnotaions, Line annotations, etc Nothing helped me.

Then I tried drawing dynamic labels inside MS chart also. Labels remain immovable while scrolling horizontal scroll bar.

This code works perfectly in your machine also.

TaW
  • 53,122
  • 8
  • 69
  • 111
user1089345
  • 109
  • 2
  • 11

1 Answers1

9

Sounds a lot as if you want to add TextAnnotations.

If you want them to stick with your data points you should anchor them to the points they shall stay with.

Here are a few examples:

enter image description here

    // directly anchored to a point
    TextAnnotation TA1 = new TextAnnotation();
    TA1.Text = "DataPoint 222";
    TA1.SetAnchor(chart2.Series["UnitPrice"].Points[222]);
    chart2.Annotations.Add(TA1);

    // anchored to a point but shifted down
    TextAnnotation TA2 = new TextAnnotation();
    TA2.Text = "DataPoint 111";
    TA2.AnchorDataPoint = chart2.Series["UnitPrice"].Points[111];
    TA2.AnchorY = 0;   

    chart2.Annotations.Add(TA2);

    // this one is not anchored on a point:
    TextAnnotation TA3 = new TextAnnotation();
    TA3.Text = "At 50% width BC";
    TA3.AnchorX = 50;  // 50% of chart width
    TA3.AnchorY = 20;  // 20% of chart height, from top!
    TA3.Alignment = ContentAlignment.BottomCenter;  // try a few!

    chart2.Annotations.Add(TA3);

By default they either anchor to DataPoints or are positioned in % of the chart size.

It is also possible to set the positions according to pixel coordinates, but for this you need to calculate the positions each time the chart changes its view!

See here for an example how to transform chart data positions to chart control coordinates and vice versa.. (Not really recommended, though)

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Dear sir, Thanks 1000 times for your precious reply or help. I have to plot 10, 000 points. I have put your code in for loop as below, and taking too much of time. – user1089345 Jul 02 '15 at 11:43
  • did I understand that right: You added 10 or 10k annotations?? 10 should not take any extra time. But 10k is a little too much, I'd say. Wouldn't be visible either, would they? – TaW Jul 02 '15 at 12:12
  • 1
    So you should ask your boss just what the annotation should look like, what they should show and where. Also 60 Series sounds rather big. Ask him to show you an example or to draw a sketch.. – TaW Jul 02 '15 at 13:01
  • Actually, we use IOCOMP (third party tool) to draw charts for our aviation domain product. We need to show 20 channels in IOcomp. We can able to show all data in IOCOMP, but, it has become slow. On dragging horizontal scroll bar in IOCOmp chart, the chart hangs for a while to show data. That is why I am trying to show data in MS chart control. – user1089345 Jul 03 '15 at 05:31