Reputation: 2353
I'm a student that is still learning C# and I ran into a problem. I'm trying to make a graphic (pie chart) with 7 different fields and seven different legendas.
I've got this code:
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);
}
Could anybody explain me why I keep seeing SIX slices, but the legenda shows me seven? You can see the problem in this image: https://i.sstatic.net/910qq.png
Thank you very much, Yenthe.
Upvotes: 1
Views: 1170
Reputation: 48736
There are 7 slices. The first one is too small though and you only see a sliver. Try changing this line: prijsje += 50
to prijsje += 10
. his should allow you to better see that there are indeed 7 slices.
UPDATED:
I think you are using the Add
Method incorrectly. I think what you are thinking is that by calling series.Points.Add(teller);
, that C# is adding a point to teller
position in the collection. This is actually incorrect.
What its doing is its inserting a point with the value of teller
. The first one has a value of 0, which is why you aren't seeing it. Then the next one has a value of 1, which is the "100" label. Then the next one has a value of 2 (which is double the size of the one before it..1), which is the "150" label. If you were to cut out the "100" slice and overlay it on top of the "200" slice, you'd probably think by looking at the labels that two "100" slices could exactly fit into one "200" slice. In actuality, three can fit into one. The reason is because the value assigned to the "200" slice is 3.
To fix your problem, You should be using the Insert method instead of Add
. Try this:
for (teller = 0; teller < 7; teller++)
{
var dp = new DataPoint(prijsje, prijsje);
dp.AxisLabel = Convert.ToString(prijsje + "€");
dp.LegendText = Convert.ToString("legende " + teller);
series.Points.Insert(teller, dp);
prijsje += 50;
}
Upvotes: 2