【发布时间】:2013-04-10 00:55:50
【问题描述】:
我是一名仍在学习 C# 的学生,但遇到了问题。 我正在尝试制作一个包含 7 个不同字段和 7 个不同图例的图形(饼图)。
我有这个代码:
private void InitializeChart()
{
this.components = new System.ComponentModel.Container();
ChartArea chartArea1 = new ChartArea();
Legend legend1 = new Legend()
{ BackColor = Color.FromArgb(97,97,97), //achtergrondkleur legende
ForeColor = Color.White, //kleur van tekst in legende
Title = "Legende grafiek", //titel legende
TitleForeColor = Color.White}; //kleur titel legende
pieChart = new Chart();
((ISupportInitialize)(pieChart)).BeginInit();
SuspendLayout();
//===Pie chart
chartArea1.Name = "PieChartArea";
pieChart.ChartAreas.Add(chartArea1);
pieChart.Height = 300;
pieChart.Width = 300;
pieChart.Dock = System.Windows.Forms.DockStyle.Fill;
legend1.Name = "Legend1";
pieChart.Legends.Add(legend1);
pieChart.Location = new System.Drawing.Point(0, 50);
//====Bar Chart
chartArea1 = new ChartArea();
chartArea1.Name = "BarChartArea";
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Load += new EventHandler(StatistiekenForm_Load);
((ISupportInitialize)(this.pieChart)).EndInit();
this.ResumeLayout(false);
}
private void LoadPieChart()
{
pieChart.Series.Clear();
pieChart.Width = 300;
pieChart.Height = 300;
pieChart.Palette = ChartColorPalette.Excel;
pieChart.BackColor = Color.Transparent;
//pieChart.Titles.Add("Overzicht uitgaven");
pieChart.ChartAreas[0].BackColor = Color.Transparent;
Series series = new Series
{
Name = "Overzicht",
IsVisibleInLegend = true,
Color = System.Drawing.Color.FromArgb(97,97,97),
ChartType = SeriesChartType.Pie
};
pieChart.Series.Add(series);
int teller, prijsje = 50;
for (teller = 0; teller < 7; teller++)
{
series.Points.Add(teller);
var p1 = series.Points[teller];
p1.AxisLabel = Convert.ToString(prijsje + "€");
p1.LegendText = Convert.ToString("legende " + teller);
prijsje += 50;
}
pieChart.Invalidate();
panelPie.Width = 400;
panelPie.Height = 400;
panelPie.Controls.Add(pieChart);
}
谁能解释一下为什么我一直看到六片,但传说中显示的是七片? 您可以在此图像中看到问题: http://i.imgur.com/4xciNUG.png?1
非常感谢, 延特。
【问题讨论】:
-
如何给每个饼片占整体的一定百分比?换句话说,我看到你的变量
prijsje被用来创建标签,但是它是如何被用来对饼图进行数字划分的呢? -
好吧,我已经通过为所有切片赋予相同的值来测试了这一点,它会自动将它们设置为正确的大小。我相信这是由 C# 正确处理的?
-
在下面查看我的更新答案
-
对于所有会遇到同样问题的人,我确实找到了解决方法,它不是 100% 合法的,但它可以完成工作。显然,轴的 0 与饼图部分的 0 不同。解决方案:int Teller, prijsje = 50;对于(柜员= 0;柜员
-
Yenthe - 请看下面的答案