【问题标题】:How to set and show title to pie chart using MS chart如何使用 MS 图表设置和显示饼图的标题
【发布时间】:2012-10-30 11:42:11
【问题描述】:

我正在尝试在饼图顶部显示标题。

chartArea1.AxisX.Title = "Product1";                 
chartArea1.AxisX.TitleFont = new Font("Arial Bold", 15, FontStyle.Bold);    
chartArea1.AxisX.TitleAlignment = StringAlignment.Center;

我尝试将标题添加到图表的标题集合,然后分配给图表区域标题。但它不起作用。

我还能做些什么来在饼图顶部显示标题?

【问题讨论】:

    标签: winforms visual-studio-2010 c#-4.0 mschart


    【解决方案1】:

    类似下面的东西应该可以工作:

    Chart1.Titles.Clear()
    Dim newTitle As New Title("Title Here", Docking.Top, New Font("Verdana", 12), Color.Black)
    Chart1.Titles.Add(newTitle)
    

    已编辑:

    为了将不同的标题应用到不同的图表区域,您需要设置标题的DockedToChartArea 属性。例如,如果我有一个包含 2 个 ChartAreas 的图表,则可以像这样添加标题:

        Title Area1Title = new Title("Title1", Docking.Top, new Font("Verdana", 12), Color.Black);
        Title Area2Title = new Title("Title2", Docking.Top, new Font("Verdana", 12), Color.Black);
        Area1Title.DockedToChartArea = Chart1.ChartAreas[0].Name;
        Area2Title.DockedToChartArea = Chart1.ChartAreas[1].Name;
        Chart1.Titles.Add(Area1Title);
        Chart1.Titles.Add(Area2Title);
    

    【讨论】:

    • 谢谢。现在我得到了顶部的标题。还有一件事,我的图表中有两个饼图(Chartarea)。我想分别将标题分配给图表区域。
    【解决方案2】:

    您应该尝试使用 yourChart 对象的 Titles.Add 方法,而不是像下面这样的 chartArea

    yourChart.Titles.Clear();   // Unnecessary if you have already clear
    Title yourTitle = new Title("Title", Docking.Top, new Font("Verdana", 12), Color.Black);
    yourChart.Titles.Add(yourTitle);
    

    【讨论】:

    • 感谢您翻译我的 VB 代码。下次发表评论,我可以快速编辑。将我的答案复制/粘贴为您自己的答案似乎很粗鲁。
    • @ChrisZeh 好的。这样更好。如果我是你,不会介意的。但我和你一样道歉。我不能投票给你,因为问题是用 c# 标记的,所以这就像未来的小课 :) 仍然很抱歉,因为你介意。请立即翻译,我将删除我的答案并为您投票:)
    • 没问题萨米,你的回答很优秀,对我来说是一个教训,我应该尽可能清晰和正确。下次您可能会考虑在您的答案中添加类似:“扩展 Chris 所说的内容......”,这将有助于消除负面情绪 :-)
    • 兄弟说得好。在这种情况下,我通常不会错过参考,您可以查看我的答案:),我以后会避免这样做。谢谢你的好态度。
    【解决方案3】:

    这是以完全自定义的方式添加标题的另一种方式。

    Title testTitle = new Title();
    testTitle.text = "Total";
    testTitle.Font = new Font(FontFamily.GenericSansSerif, 14F, FontStyle.Bold);
    testTitle.IsDockedInsideChartArea = true;
    testTitle.Docking = Docking.Left;
    testTitle.TextOrientation = TextOrientation.Rotated270;
    testTitle.DockedToChartArea = TrunkChart.ChartAreas[0].Name;
    TestChart.Title.Add(testTitle);
    

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多