如何使用mschart为winform添加注释?我的代码不起作用 [英] How to add Annotations for winform using mschart?My codes doesn't work

查看:186
本文介绍了如何使用mschart为winform添加注释?我的代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望像这张图片的效果 https://www.yousendit.com/download/UW15WmdrdGpsUi9OUjhUQw [ ^ ],我的代码是:

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?

推荐答案

您好。看问题是,您的注释样式是LineAnnotation,它不显示文本。



现在查看新的注释片段。



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.


在你的foreach之前添加另一行



Just add another line before your foreach

chart1.Series.ElementAt(0).ChartType = SeriesChartType.Column;


点击按钮检查此代码。



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项)

{

// 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 =check;

aX = 1;

aY = 10;

a.Visible = true ;

chart1.Annotations.Add(a);

//放批注

chart1.Annotations [0] .AnchorDataPoint = chart1.Series [0] .Points [1];

}
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];
}


这篇关于如何使用mschart为winform添加注释?我的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆