使用 Microsoft Chart 控件在图表上添加标签

发布于 2024-08-29 09:20:11 字数 2200 浏览 7 评论 0原文

我正在使用 Microsoft Chart 控件创建 3d 图表。这是图像:

替代文字
(来源:highoncoding.com

我想展示每个条形图顶部的点。就像条形图顶部的考试 1 一样,它应该显示 2(如 2 点)等。

这是代码:

private void BindData() {

            var exams = new List<Exam>()
            {
                new Exam() { Name = "Exam 1", Point = 10 }, 
                new Exam() { Name = "Exam 2", Point = 12 }, 
                new Exam() { Name = "Exam 3", Point = 15 }, 
                new Exam() { Name = "Exam 4", Point = 2 }
            };

            var series = ExamsChart.Series["ExamSeries"];         



            series.YValueMembers = "Point"; 
            series.XValueMember = "Name"; 

            //series.MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Circle;
            //series.MarkerSize = 20;
            //series.LegendText = "hellow";
            //series.Label = "something";            


            var chartAreas = ExamsChart.ChartAreas["ChartArea1"];           


            ExamsChart.DataSource = exams;
            ExamsChart.DataBind(); 
        }

这是 html 代码:

<asp:Chart ID="ExamsChart" Width="600" Height="320" runat="server">
      <Titles>
      <asp:Title Text="Exam Report" />
      </Titles>
        <Series>
          <asp:Series Name="ExamSeries" ChartType="Column">
          </asp:Series>
        </Series>
        <ChartAreas>

          <asp:ChartArea Name="ChartArea1">
          <Area3DStyle Enable3D="true"  WallWidth="10" />
          </asp:ChartArea>
        </ChartAreas>
      </asp:Chart>

更新:

这是答案:

 foreach (var exam in exams) {

                var point = new DataPoint();
                point.SetValueXY(exam.Name, exam.Point);

                point.Label = exam.Name;

                series.Points.Add(point); 
            }  

I am creating a 3d chart using Microsoft Chart controls. Here is the image:

alt text
(source: highoncoding.com)

I want to show the point on the top of each bar graph. Like for Exam 1 on top of bar chart it should show 2 (as in 2 points) etc.

Here is the code:

private void BindData() {

            var exams = new List<Exam>()
            {
                new Exam() { Name = "Exam 1", Point = 10 }, 
                new Exam() { Name = "Exam 2", Point = 12 }, 
                new Exam() { Name = "Exam 3", Point = 15 }, 
                new Exam() { Name = "Exam 4", Point = 2 }
            };

            var series = ExamsChart.Series["ExamSeries"];         



            series.YValueMembers = "Point"; 
            series.XValueMember = "Name"; 

            //series.MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Circle;
            //series.MarkerSize = 20;
            //series.LegendText = "hellow";
            //series.Label = "something";            


            var chartAreas = ExamsChart.ChartAreas["ChartArea1"];           


            ExamsChart.DataSource = exams;
            ExamsChart.DataBind(); 
        }

and here is the html code:

<asp:Chart ID="ExamsChart" Width="600" Height="320" runat="server">
      <Titles>
      <asp:Title Text="Exam Report" />
      </Titles>
        <Series>
          <asp:Series Name="ExamSeries" ChartType="Column">
          </asp:Series>
        </Series>
        <ChartAreas>

          <asp:ChartArea Name="ChartArea1">
          <Area3DStyle Enable3D="true"  WallWidth="10" />
          </asp:ChartArea>
        </ChartAreas>
      </asp:Chart>

UPDATE:

Here is the answer:

 foreach (var exam in exams) {

                var point = new DataPoint();
                point.SetValueXY(exam.Name, exam.Point);

                point.Label = exam.Name;

                series.Points.Add(point); 
            }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2024-09-05 09:20:11

直接来自 MS 图样本:

// Show data points values as labels
chart1.Series["Series1"].IsValueShownAsLabel = true;

// Set data point label
chart1.Series["Series1"].Points[2].Label = "My Point Label\nLabel Line #2";

Directly from the MS chart samples:

// Show data points values as labels
chart1.Series["Series1"].IsValueShownAsLabel = true;

// Set data point label
chart1.Series["Series1"].Points[2].Label = "My Point Label\nLabel Line #2";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文