Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
I want the effect like this picture https://www.yousendit.com/download/UW15WmdrdGpsUi9OUjhUQw[^],My code is :
C#
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

Hello. See the problem was, your Annotation style was LineAnnotation which does not display the text.

Now look the new snippet for annotation.

C#
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.
 
Share this answer
 
Comments
PEIYANGXINQU 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?
Just add another line before your foreach

chart1.Series.ElementAt(0).ChartType = SeriesChartType.Column;
 
Share this answer
 
Comments
PEIYANGXINQU 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 21-Jan-13 20:47pm    
You have set SeriesChartType.Point, it should be SeriesChartType.Column
PEIYANGXINQU 21-Jan-13 21:59pm    
Thank you! I have change the type,but it still does not work.Can you give me a example??
[no name] 30-Apr-14 16:50pm    
Thank you! I have change the type,
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<datapoint> Data = new List<datapoint>();
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];
}
C#

 
Share this answer
 
Comments
PEIYANGXINQU 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)



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