如何使用MS图表设置和显示饼图的标题

时间:2012-11-10 07:27:56

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

我想在饼图顶部显示标题。

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

我尝试将标题添加到图表的标题集合中,然后分配到图表区域标题。但它没有用。

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

3 个答案:

答案 0 :(得分:5)

以下内容应该有效:

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

答案 1 :(得分:1)

您应该使用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);

答案 2 :(得分:1)

这是以完全自定义方式添加标题的其他方式。

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

干杯