C# 隐藏饼图中的标签(用于 .Net 的 MS 图表)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2147930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:55:00  来源:igfitidea点击:

Hide labels in pie charts (MS Chart for .Net)

c#asp.netdata-visualizationmschart

提问by grenade

ugly pie chart

丑陋的饼图

I can't seem to find the property that controls visibility of labels in pie charts. I need to turn the labels off as the information is available in the legend.

我似乎找不到控制饼图中标签可见性的属性。我需要关闭标签,因为图例中的信息可用。

Anyone know what property I can use in code behind?

任何人都知道我可以在后面的代码中使用什么属性?

I tried setting the series labels to nothing Chart1.Series[i].Label = string.Empty;but the labels seem to show up anyway.

我尝试将系列标签设置为空,Chart1.Series[i].Label = string.Empty;但标签似乎无论如何都会出现。

采纳答案by Ben

Chart1.Series[i]["PieLabelStyle"] = "Disabled";

works too, and doesn't need to be set for each datapoint.

也可以工作,不需要为每个数据点设置。

回答by grenade

Found the answer here: http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/32ccd993-5f43-47a3-bcbc-e772a13a87fe

在这里找到答案:http: //social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/32ccd993-5f43-47a3-bcbc-e772a13a87fe

It turns out there is an obscure DataPointCustomProperty called PieLabelStyle that governs label visibility in pie charts. Worse still, the property must be set on each data point.

事实证明,有一个名为 PieLabelStyle 的晦涩的 DataPointCustomProperty 管理饼图中的标签可见性。更糟糕的是,必须在每个数据点上设置该属性。

for (var i = 0; i < chart.Series.Count; i++) 
    for (var j = 0; j < chart.Series[i].Points.Count; j++)
        chart.Series[i].Points[j]["PieLabelStyle"] = "Disabled";

回答by sumanmodi

objChart.ChartAreas[0].AxisY.LabelStyle.Enabled = false;

回答by Juan C

Changing chart custom properties will do the trick as well and no coding is needed

更改图表自定义属性也可以解决问题,无需编码

<asp:Series Name="Series1" ChartType="Pie" CustomProperties="PieLabelStyle=Disabled">

回答by Raheel

May be this website solve your problem

也许这个网站可以解决你的问题

protected void Page_Load(object sender, EventArgs e) {
// Insert code to create basic pie chart // See my blog post entitled "Pie Charts in ASP.NET" for full source code

protected void Page_Load(object sender, EventArgs e) {
// 插入代码以创建基本饼图 // 请参阅我的博客文章“ASP.NET 中的饼图”以获取完整源代码

     // Set pie labels to be outside the pie chart
     this.Chart2.Series[0]["PieLabelStyle"] = "Outside";

     // Set border width so that labels are shown on the outside
     this.Chart2.Series[0].BorderWidth = 1;
     this.Chart2.Series[0].BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);

     // Add a legend to the chart and dock it to the bottom-center
     this.Chart2.Legends.Add("Legend1");
     this.Chart2.Legends[0].Enabled = true;
     this.Chart2.Legends[0].Docking = Docking.Bottom;
     this.Chart2.Legends[0].Alignment = System.Drawing.StringAlignment.Center;

     // Set the legend to display pie chart values as percentages
     // Again, the P2 indicates a precision of 2 decimals
     this.Chart2.Series[0].LegendText = "#PERCENT{P2}";

     // By sorting the data points, they show up in proper ascending order in the legend
     this.Chart2.DataManipulator.Sort(PointSortOrder.Descending, Chart2.Series[0]);
 }

Also visit this website i also take this code from that website very nice tutorial on mscharts http://betterdashboards.wordpress.com/2009/02/04/display-percentages-on-a-pie-char

也访问这个网站我也从那个网站上获取这个代码 mscharts 上非常好的教程 http://betterdashboards.wordpress.com/2009/02/04/display-percentages-on-a-pie-char

回答by Kristian

...and Ben's answer in VB.NET format:

...和 ​​Ben 的 VB.NET 格式的回答:

Chart1.Series(0)("PieLabelStyle") = "Disabled"

works fine for setting whole series

适用于设置整个系列

回答by bkqc

This can also be done in the UI by

这也可以在 UI 中通过

  1. Opening the Series editor window (ellipsis button in the main properties panel)
  2. Selecting the wanted series
  3. Expanding the CustomPropertiesproperty
  4. Choosing Disabled
  1. 打开系列编辑器窗口(主属性面板中的省略号按钮)
  2. 选择想要的系列
  3. 扩大CustomProperties财产
  4. 选择 Disabled

Example

例子