Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0 WinForm
I want the effect like this picture https://www.yousendit.com/download/UW15WmdrdGpsUi9OUjhUQw[^],My code is :
   
 chart1.Series.ElementAt(0).LegendText = "日期数量";
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd HH" + "时";
            chart1.Series[0].XValueType = ChartValueType.DateTime;
            chart1.Series.ElementAt(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
            foreach (var item in Data)
            {
                DataPoint dp = new DataPoint();
                dp.SetValueXY(item.Key, item.Value);
                chart1.Series.ElementAt(0).Points.Add(dp);
            }
 
            LineAnnotation a = new LineAnnotation();
          a.Name = "lineAnnotation";
            a.ToolTip = "放批注";
            a.X = 1;
            a.Y = 1;
            a.Visible = true;
            chart1.Annotations.Add(a);
            //放批注
            chart1.Annotations[0].AnchorDataPoint = chart1.Series[0].Points[1];
I draw a line ,and want to give the second point annotations,but is seems failed?Can you tell me what is wrong?
Posted 21-Jan-13 5:47am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello. See the problem was, your Annotation style was LineAnnotation which does not display the text.
 
Now look the new snippet for annotation.
 
CalloutAnnotation a = new CalloutAnnotation();
           a.Text = "My Annotation";
           a.ToolTip = "Annotation tool tip";
           a.ForeColor = Color.Green;
           //a.
           a.AnchorDataPoint = chart1.Series[0].Points[0];
           a.Visible = true;
 
           chart1.Annotations.Add(a);
           //放批注

           a.LineWidth = 2;
 
Now it's perfectly annotating, drawing Bar chart and also upon mouse hover over annotation point displaying the tooltip. Do let me know if it is working now. Should work.
  Permalink  
Comments
PEIYANGXINQU at 22-Jan-13 7:33am
   
Thank you very much,it works well.Can I ask the last question?You say LineAnnotation which does not display the text,then where is
LineAnnotation used in mainly?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just add another line before your foreach
 
chart1.Series.ElementAt(0).ChartType = SeriesChartType.Column;
  Permalink  
Comments
PEIYANGXINQU at 21-Jan-13 20:45pm
   
I have do this in the code up at line four,but it do not work
chart1.Series.ElementAt(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
Grasshopper.iics at 21-Jan-13 20:47pm
   
You have set SeriesChartType.Point, it should be SeriesChartType.Column
PEIYANGXINQU at 21-Jan-13 21:59pm
   
Thank you! I have change the type,but it still does not work.Can you give me a example??
Member 10786704 at 30-Apr-14 16:50pm
   
Thank you! I have change the type,
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check this code with a button click.
 
private void button1_Click(object sender, EventArgs e)
{
 
chart1.Series.ElementAt(0).LegendText = "Something";
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd HH" + "extra";
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.Series.ElementAt(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
List Data = new List();
Data.Add(new DataPoint(1, 10));
Data.Add(new DataPoint(2, 20));
Data.Add(new DataPoint(3, 30));
 
chart1.Series.ElementAt(0).ChartType = SeriesChartType.Column;
foreach (var item in Data)
{
//DataPoint dp = new DataPoint();
//dp.SetValueXY(item.Key, item.Value);
chart1.Series.ElementAt(0).Points.Add(item);
chart1.Series.ElementAt(0).MarkerColor = Color.Red;
chart1.Series.ElementAt(0).ShadowColor = Color.Green;

chart1.Series.ElementAt(0).LabelBackColor = Color.Green;
}
 
LineAnnotation a = new LineAnnotation();
a.Name = "lineAnnotation";
a.ToolTip = "checking";
a.X = 1;
a.Y = 10;
a.Visible = true;
chart1.Annotations.Add(a);
//放批注
chart1.Annotations[0].AnchorDataPoint = chart1.Series[0].Points[1];
}
  Permalink  
Comments
PEIYANGXINQU at 22-Jan-13 0:24am
   
Do you have run the code?I run,but it still do not work.This is the result:https://www.yousendit.com/download/UW13blR1Z2pWRC80WjlVag

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 22 Jan 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