I had Gantt chart like this. I'd like to show AxisX2 with value is percentage that I prepared formula for it. I had challenges to show AxisX2 and set series for it.
Here is the Gantt chart I captured click here.
I expect to one more axis like this.
Please help, Thank you .
Here are some basic function to render that chart
public void setUpGantt(Chart chart)
{
chart.Series.Clear();
Series s = chart.Series.Add("sszs");
s.ChartType = SeriesChartType.RangeBar;
s.YValueType = ChartValueType.DateTime;
s.ResetIsVisibleInLegend () ;
s.IsVisibleInLegend = true;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ax.MajorGrid.Enabled = false;
ay.IntervalType = DateTimeIntervalType.Minutes;
ay.Interval = 60;
ay.LabelStyle.Format = "HH:mm";
ay.Minimum = 0;
ay.Maximum = 0.2;
limitGantt(chart, "0:00", "24:00");
s.ToolTip = "#VALY1{HH:mm}~#VALY2{HH:mm}";
}
public void limitGantt(Chart chart, string start, string end)
{
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ay.Minimum = fromTimeString(start).ToOADate();
ay.Maximum = fromTimeString(end).ToOADate();
}
DateTime fromTimeString(string time)
{
var p = time.Split(':');
int sec = p.Length == 3 ? Convert.ToInt16(p[2]) : 0;
TimeSpan t = new TimeSpan(Convert.ToInt16(p[0]), Convert.ToInt16(p[1]), sec);
return DateTime.Today.Add(t);
}
public void addGanttTask(Series s, string start, string end, Color c, int slot, string [] array)
{
DateTime start_ = fromTimeString(start);
DateTime end_ = fromTimeString(end);
int pt = s.Points.AddXY(slot, start_, end_);
s.Points[pt].Color = c;
s.IsVisibleInLegend = true;
if (array != null)
{
for (int i = 0; i < array.Length; i++)
{
if (slot == i + 1)
{ s.Points[pt].AxisLabel = array[i];
}
}
}
}
Taw's comment
public void setUpGantt(Chart chart)
{
chart.Series.Clear();
Series s = chart.Series.Add("sszs");
s.ChartType = SeriesChartType.RangeBar;
s.YValueType = ChartValueType.DateTime;
s.ResetIsVisibleInLegend () ;
s.IsVisibleInLegend = true;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ax.MajorGrid.Enabled = false;
Axis ax2 = chart.ChartAreas[0].AxisX2;
ax2.Enabled = AxisEnabled.True;
ax2.Maximum = 100;
ax2.MajorGrid.Enabled = false;
ay.IntervalType = DateTimeIntervalType.Minutes;
ay.Interval = 60;
ay.LabelStyle.Format = "HH:mm";
ay.Minimum = 0;
ay.Maximum = 0.2;
limitGantt(chart, "0:00", "24:00");
s.ToolTip = "#VALY1{HH:mm}~#VALY2{HH:mm}";
}
I'd like to have percentage = Green Time/Total time (from 00:
00 to Current time) example
Thank you for TaW's suggestion. It worked for me.
I post here to share who want to know
public void setUpGantt(Chart chart)
{
chart.Series.Clear();
Series s = chart.Series.Add("sszs");
s.ChartType = SeriesChartType.RangeBar;
s.YValueType = ChartValueType.DateTime;
s.ResetIsVisibleInLegend () ;
s.IsVisibleInLegend = true;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ax.MajorGrid.Enabled = false;
Axis ax2 = chart.ChartAreas[0].AxisX2;
ax2.Enabled = AxisEnabled.True;
ax2.MajorGrid.Enabled = true;
ax2.CustomLabels.Clear();// clear previous value when switch another data
for (int i = 0; i < 12; i++)
{
CustomLabel cl = new CustomLabel();
cl.FromPosition = i+0.5;
cl.ToPosition = i+1.5;
cl.Text = i+" %"; // example value to show on CustomLabel
ax2.CustomLabels.Add(cl);
}
ay.IntervalType = DateTimeIntervalType.Minutes;
ay.Interval = 60;
ay.LabelStyle.Format = "HH:mm";
ay.Minimum = 0;
ay.Maximum = 0.2;
limitGantt(chart, "0:00", "24:00");
s.ToolTip = "#VALY1{HH:mm}~#VALY2{HH:mm}";
}
The main script to show one more Axis with CustomLabel
Axis ax2 = chart.ChartAreas[0].AxisX2;
ax2.Enabled = AxisEnabled.True;
ax2.MajorGrid.Enabled = true;
ax2.CustomLabels.Clear();// clear previous value when switch another data
for (int i = 0; i < 12; i++)
{
CustomLabel cl = new CustomLabel();
cl.FromPosition = i+0.5;
cl.ToPosition = i+1.5;
cl.Text = i+" %"; // example value to show on CustomLabel
ax2.CustomLabels.Add(cl);
}
Related
Intro:
This pseudocode is based on Multiple chart areas in one column, however, when I enable many chart areas and all of them are aligned one above the other, the graphic height is getting lower while the axis X labels are getting away from the chart.
Let's see this in a deep way:
I have a panel which contains inside itself the chart.
Every chart area makes reference to a selected resource, so in this case, we have 5 chart areas as 5 resources selected on the checked list box.
From the third resource, I add 300 as the minimum scroll value of the panel, so the size of the chart increases, and the chart areas do not look excessively different in height every time we add a chart area.
Chart view for 13 Resources:
Chart view for 18 Resources:
Chart view for 21 resources:
And finally, chart view for 26 or more resoures:
Code:
Auxiliar method to clone a chart area
private ChartArea CloneChartArea(ChartArea chartArea, string resultName)
{
var result = new ChartArea();
result.Name = resultName;
result.AxisX.MajorTickMark.LineColor = chartArea.AxisX.MajorTickMark.LineColor;
result.AxisY.MajorTickMark.LineColor = chartArea.AxisY.MajorTickMark.LineColor;
result.AxisX.LabelStyle.Font = chartArea.AxisX.LabelStyle.Font;
result.AxisY.LabelStyle.Font = chartArea.AxisY.LabelStyle.Font;
result.AxisX.LabelStyle.ForeColor = chartArea.AxisX.LabelStyle.ForeColor;
result.AxisY.LabelStyle.ForeColor = chartArea.AxisY.LabelStyle.ForeColor;
/// The following lines allow us to paint multiple chart areas in one column
result.AlignWithChartArea = chartArea.Name;
result.AlignmentStyle = AreaAlignmentStyles.Position;
result.AlignmentOrientation = AreaAlignmentOrientations.Vertical;
return result;
}
Auxiliar method to set axis y label
private ChartArea SetAxisLabelY(ChartArea chartArea, string ylabel, Color foreColor, float fontSize)
{
chartArea.AxisY.Title = ylabel;
chartArea.AxisY.TitleAlignment = StringAlignment.Center;
chartArea.AxisY.TitleFont = new Font("Century Gothic", fontSize, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
chartArea.AxisY.TitleForeColor = foreColor;
return chartArea;
}
Main method to set chart values:
private void SetChartValues(IEnumerable<IGrouping<int, ResourceDailyCalendar>> data)
{
while (chart1.Series.Any())
{
chart1.Series.RemoveAt(0);
if (chart1.ChartAreas.Count > 1)
{
chart1.ChartAreas.RemoveAt(1);
}
}
panel5.AutoScrollMinSize = new Size(0, 0);
if (data != null && data.Count() > 0)
{
var resourcesNames = data.SelectMany(group => group).Select(element => element.Resource).Distinct().ToList();
var chartAreaIndex = 1;
foreach (var resourceName in resourcesNames)
{
var chartAreaName = string.Format("ChartArea{0}", chartAreaIndex);
ChartArea chartArea = chart1.ChartAreas.Last();
if (!chart1.ChartAreas.Any(ca => ca.Name.Equals(chartAreaName)))
{
chartArea = CloneChartArea(chartArea, resultName: chartAreaName);
chart1.ChartAreas.Add(chartArea);
if (chartAreaIndex > 3)
{
var minScroll = panel5.AutoScrollMinSize;
if (minScroll.IsEmpty)
{
minScroll.Height = panel5.Size.Height;
}
minScroll.Height += 300;
panel5.AutoScrollMinSize = minScroll;
}
}
chartArea = SetAxisLabelY(chartArea, resourceName, Color.Yellow, 10F);
var totalSerie = new Series()
{
Name = $"{resourceName} - Total",
ChartArea = chartArea.Name,
ChartType = SeriesChartType.StackedColumn,
Color = Color.Black,
BorderWidth = 1,
BorderDashStyle = ChartDashStyle.Solid,
IsVisibleInLegend = false
};
var allowedSerie = new Series()
{
Name = $"{resourceName} - Allowed",
ChartArea = chartArea.Name,
ChartType = SeriesChartType.StackedColumn,
Color = Color.Gray,
BorderWidth = 1,
BorderDashStyle = ChartDashStyle.Solid,
IsVisibleInLegend = false
};
var currentSerie = new Series()
{
Name = $"{resourceName} - Current",
ChartArea = chartArea.Name,
ChartType = SeriesChartType.StackedColumn,
BorderWidth = 1,
BorderDashStyle = ChartDashStyle.Solid,
IsVisibleInLegend = false
};
var x = 0;
var maxValue = 0.0;
foreach (var group in data)
{
string axisLabel;
if (group.Count() == 1) /// Agrupados por día
{
if (group.First().Resource != resourceName) continue;
axisLabel = group.First().Date.Value.ToShortDateString();
}
else
{
if (group.Count() > 1 && group.Count() < 8) /// Agrupados por semanas
{
axisLabel = group.First().YYYYWW.ToString();
}
else /// Agrupados por meses
{
axisLabel = group.First().YYYYMM.ToString();
}
}
var effectiveWorkingHours = group.Where(i => i.Resource == resourceName).Sum(i => i.EffectiveWorkingHours);
currentSerie.Points.AddXY(x, effectiveWorkingHours);
currentSerie.Points[x].AxisLabel = axisLabel;
maxValue = effectiveWorkingHours > maxValue ? effectiveWorkingHours : maxValue;
var workingHours = group.Where(i => i.Resource == resourceName).Sum(i => i.WorkingHours);
maxValue = workingHours > maxValue ? workingHours : maxValue;
workingHours -= effectiveWorkingHours;
allowedSerie.Points.AddXY(x, workingHours);
allowedSerie.Points[x].AxisLabel = axisLabel;
var periodTotalHours = group.Where(i => i.Resource == resourceName).Sum(i => i.PeriodTotalHours);
maxValue = periodTotalHours > maxValue ? periodTotalHours : maxValue;
periodTotalHours -= (workingHours + effectiveWorkingHours);
totalSerie.Points.AddXY(x, periodTotalHours);
totalSerie.Points[x].AxisLabel = axisLabel;
x++;
}
chart1.Series.Add(currentSerie);
chart1.Series.Add(allowedSerie);
chart1.Series.Add(totalSerie);
chart1.ChartAreas.First(ca => ca.Name == chartArea.Name).AxisY.Maximum = maxValue * 1.1;
chartAreaIndex++;
}
/// Resize de chart
var currentHeight = 0;
var chartAreasCounter = chart1.ChartAreas.Count();
foreach (ChartArea ca in chart1.ChartAreas)
{
ca.AxisY.Minimum = 0;
ca.Position.Height = 100 / chartAreasCounter;
ca.Position.Y = currentHeight;
ca.Position.X = 0;
ca.Position.Width = 100;
ca.AxisX.MaximumAutoSize = 5;
currentHeight += 100 / chartAreasCounter;
}
chart1.ApplyPaletteColors();
chart1.Update();
System.Windows.Forms.Application.DoEvents();
}
}
Could anyone help me how to prevent this please?
I have a list of signals in a listview. When the user checks one, the values of the signals are being plotted on the chart. Moreover there is a vertical annotation which the user can drag across the graph and see the values for every x value. Each signal has one rectangle annotation that shows the Y value.
My problem is that when the user checks a new signal then the old rectangle annotations do not disappear.
Here is what I mean :
enter image description here
Here is my code so far :
List<RectangleAnnotation> anno = new List<RectangleAnnotation>();
List<Series> checkedItems = new List<Series>();
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (listView1.FocusedItem != null)
{
double xFactor = 0.03;
double yFactor = 0.02;
CA = chart1.ChartAreas[0];
if (e.NewValue == CheckState.Checked)
{
anno.Clear();
Series s12 = new Series();
s12 = chart1.Series.Add((listView1.Items[e.Index].Text).ToString());
s12.ChartType = SeriesChartType.Line;
s12.MarkerStyle = MarkerStyle.Circle; // make the points stand out
s12.MarkerSize = 3;
checkedItems.Add(s12);
for (int i = 0; i < chart1.Series.Count - 1; i++)
{
anno.Add(new RectangleAnnotation());
anno[i].AxisX = CA.AxisX;
anno[i].IsSizeAlwaysRelative = false;
anno[i].Width = 20 * xFactor;
anno[i].Height = 8 * yFactor;
// VA.Name = "myRect";
anno[i].LineColor = Color.Black;
anno[i].BackColor = Color.Black;
anno[i].AxisY = CA.AxisY;
anno[i].Y = -anno[i].Height;
// RA[i].X = VA.X - RA[i].Width / 2;
anno[i].Text = "Hello";
anno[i].ForeColor = Color.Black;
anno[i].Font = new System.Drawing.Font("Arial", 8f);
anno[i].Text = String.Format("{0:0.00}", 0);
chart1.Annotations.Add(anno[i]);
}
for (int r = 0; r < num_rows; r++)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
// chart1.Series[checkedListBox1.Items[e.Index].ToString()].Points.Add(dp);
s12.Points.AddXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
}
}
}
}
private void chart1_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
{
int pt1 = (int)e.NewLocationX;
int i = 0;
foreach (var signal in checkedItems) {
double val = signal.Points[pt1].YValues[0];
foreach (var sim in signal.Points[pt1].YValues)
{
anno[i].Y = sim + 0.5;
}
if (sender == VA) anno[i].X = VA.X - anno[i].Width / 2;
anno[i].Text = String.Format("{0:0.00}", val);
i++;
Console.WriteLine(anno.Count);
}
I have thought of adding
chart1.Annotations.clear();
But it deletes all Annotations including the vertical. I only want to delete the rectangle annotations.
I want to create a bar graph, like it scans something. In addition I want to change the color of the bar according to the x-axis interval at that time.
So far, I can managed the scanning function but when I try to change the colour of the bar, the whole bar is changing.
Here is my code :
namespace UDP
{
public partial class ScanGraph : Form
{
int boardCounter = 0;
int trackValue;
public ScanGraph()
{
InitializeComponent();
}
private void ScanGraph_Load(object sender, EventArgs e)
{
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new Chart();
ChartArea chartArea1 = new ChartArea("foobar");
chartArea1.Name = "ChartArea1";
Chart1.Series.Add("Head1");
Chart1.Series[1].ChartType = SeriesChartType.Bar;
Chart1.Series.Add("Head2");
Chart1.Series[2].ChartType = SeriesChartType.Bar;
Chart1.Series.Add("Head3");
Chart1.Series[3].ChartType = SeriesChartType.Bar;
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
Chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 1;
Chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
Chart1.ChartAreas[0].AxisX.IsMarginVisible = false;
Chart1.ChartAreas[0].AxisY.Maximum = 100;
Chart1.ChartAreas[0].AxisY.Minimum = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Chart1.Series.Clear();
Chart1.Series.Add("Head1");
Chart1.Series[0].ChartType = SeriesChartType.Bar;
Chart1.Series.Add("Head2");
Chart1.Series[1].ChartType = SeriesChartType.Bar;
Chart1.Series.Add("Head3");
Chart1.Series[2].ChartType = SeriesChartType.Bar;
/*string black = "#000000";
string red = "#FF0000";
string blue = "#4981CE";
Color _color = System.Drawing.ColorTranslator.FromHtml(red);
if (boardCounter > 0 && boardCounter < 30)
{
_color = System.Drawing.ColorTranslator.FromHtml(black);
}
else if (boardCounter > 30 && boardCounter < 65)
{
_color = System.Drawing.ColorTranslator.FromHtml(red);
}
else if (boardCounter > 65 && boardCounter < 100)
{
_color = System.Drawing.ColorTranslator.FromHtml(blue);
}*/
Chart1.Series["Head1"].Points.AddY(boardCounter);
//Chart1.Series["Series2"].Color = _color;
Chart1.Series["Head2"].Points.AddY(boardCounter);
//Chart1.Series["Series3"].Color = _color;
Chart1.Series["Head3"].Points.AddY(boardCounter);
//Chart1.Series["Series4"].Color = _color;
boardCounter += 1;
if (boardCounter > 100) boardCounter = 1;
}
}
}
If you are changing the Color of the Series all bars/columns will change to that new Color. Instead you probably want to change only the Color of the Point(s) you want to change!
This will add a few Points to a Series and give each one a slightly different Color:
Series S1 = chart1.Series[0];
S1.ChartType = SeriesChartType.Column;
S1["PixelPointWidth"] = "3";
ChartArea CA = chart1.ChartAreas[0];
CA.AxisX.Minimum = 0;
CA.AxisX.Maximum = 100;
for (int d = 0; d <= 100; d ++)
{
S1.Points.AddXY(d, 100 + d * 3);
S1.Points[d].Color = Color.FromArgb(255, d, d * 2, 255 - d * 2);
}
I am working with a RangeBar Chart in a c# form app. When I add the series to the chart they do no line up correctly. When I change the "DrawSideBySide=false" it works fine, but I need some of the series to be side by side.
Any help on this would be greatly appreciated.
My code is just a List of Series being populated and then those series being added to a chart.
I loop through this populating series and adding them to a series list.
Populating-
double yplot1 = (double)user.Projects[i].StartDate.ToOADate();
double yplot2 = (double)user.Projects[i].EndDate.ToOADate();
// Use a different series for each datapoint
Series seriesInstance = new Series();
//seriesInstance.Name = user.Name;
seriesInstance.Label = user.Projects[i].Name + " - " + (user.Projects[i].AllocationPercent * 100).ToString() + "%";
seriesInstance.AxisLabel = user.Name;
seriesInstance.ChartType = SeriesChartType.RangeBar;
// Have a start and end date so plotting 2 points on the y-axis
seriesInstance.YValuesPerPoint = 2;
//seriesInstance.CustomProperties = "DrawSideBySide=true";
//seriesInstance["PixelPointWidth"] = "200";
seriesInstance["MinPixelPointWidth"] = "150";
int xordinal = j;
seriesInstance.IsXValueIndexed = false;
seriesInstance.Points.AddXY(xordinal, yplot1, yplot2);
/*seriesInstance.Points[0].ToolTip = someTipText;
seriesInstance.Points[0].Color = resourceColor;
seriesInstance.Points[0].AxisLabel = xlabel;*/
seriesList.Add(seriesInstance);
Then I add all the series in the list to the chart
foreach (Series plotSeries in seriesList)
{
chart1.Series.Add(plotSeries);
}
// Force x-axis to show each task or resource
chart1.ChartAreas[0].AxisX.Interval = 1;
// Set y-axis to show each day of the month
chart1.ChartAreas[0].AxisY.Interval = 1;
ChartArea chartArea1 = chart1.ChartAreas[0];
chartArea1.AxisX.IsReversed = true;
// Set other y-axis properties
chartArea1.AxisX.ScrollBar.Enabled = true;
chartArea1.AxisX.IsLabelAutoFit = true;
chartArea1.AxisX.ScaleView.Size = 5;
chartArea1.AxisY.IsStartedFromZero = false;
chartArea1.AxisY.IsMarginVisible = false;
if ((lastDate - firstDate).TotalDays < 60)
{
chartArea1.AxisY.IntervalType = DateTimeIntervalType.Days;
}
else if (((lastDate - firstDate).TotalDays > 60) && ((lastDate - firstDate).TotalDays < 365))
{
chartArea1.AxisY.IntervalType = DateTimeIntervalType.Weeks;
}
else
{
chartArea1.AxisY.IntervalType = DateTimeIntervalType.Months;
}
// Set the y-axis labels
chart1.ChartAreas[0].AxisY.Minimum = firstDate.AddDays(-1).ToOADate();
chart1.ChartAreas[0].AxisY.Maximum = lastDate.AddDays(+1).ToOADate();
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "ddd M/d";
// Force redraw of chart
chart1.Update();
I have a function that I need to call every once in a while. But, every time I do, the graph (a pie chart) resizes itself and becomes smaller than the previously obtained piechart.
void LoadPieChart(DateTime lower, DateTime higher)
{
splitContainer1.Panel1.Controls.Remove(pieChart);
pieChart.Series.Clear();
pieChart.Palette = ChartColorPalette.Fire;
pieChart.BackColor = Color.Black;
pieChart.Titles.Add("LOST OPPORTUNITY");
pieChart.ChartAreas[0].BackColor = Color.Transparent;
Series series1 = new Series
{
Name = "series1",
IsVisibleInLegend = true,
Color = System.Drawing.Color.White,
ChartType = SeriesChartType.Pie
};
pieChart.Series.Add(series1);
int num = A.CountLO();
List<string>[] lista = new List<string>[7];
lista = A.SelectLO();
float counter_manpower = 0;
float counter_spares = 0;
float counter_tools = 0;
float counter_other = 0;
string[] reason = lista[6].ToArray();
string[] low = lista[4].ToArray();
string[] up = lista[5].ToArray();
string[] collection = new string[366];
for (int j = num-LO; j < num; j++)
{
DateTime x = DateTime.Parse(low[j]);
DateTime y = DateTime.Parse(up[j]);
if (x.Date>=lower.Date && y.Date<=higher.Date)
{
if (reason[j].Equals("LACK OF MANPOWER"))
counter_manpower++;
if (reason[j].Equals("LACK OF SPARES"))
counter_spares++;
if (reason[j].Equals("LACK OF TOOLS"))
counter_tools++;
if (!reason[j].Equals("LACK OF MANPOWER") && !reason[j].Equals("LACK OF SPARES") && !reason[j].Equals("LACK OF TOOLS"))
{
counter_other++;
}
}
}
float a = ((counter_manpower/(counter_manpower+counter_spares+counter_tools+counter_other))*100);
float b = ((counter_spares / (counter_manpower + counter_spares + counter_tools+counter_other)) * 100);
float c = ((counter_tools / (counter_manpower + counter_spares + counter_tools+counter_other)) * 100);
float d = ((counter_other / (counter_manpower + counter_spares + counter_tools + counter_other)) * 100);
double aa = Math.Truncate(100 * a) / 100;
double bb = Math.Truncate(100 * b) / 100;
double cc = Math.Truncate(100 * c) / 100;
double dd = Math.Truncate(100 * d) / 100;
series1.Points.Add(counter_manpower);
var p1 = series1.Points[0];
Math.Round(a, 1);
Math.Round(b, 1);
Math.Round(c, 1);
if (counter_manpower!=0)
p1.AxisLabel = (aa.ToString() + "%");
p1.LegendText = "LACK OF MANPOWER";
p1.Color = Color.Red;
series1.Points.Add(counter_spares);
p1 = series1.Points[1];
if (counter_spares!=0)
p1.AxisLabel = (bb.ToString() + "%");
p1.LegendText = "LACK OF SPARES";
p1.Color = Color.Yellow;
series1.Points.Add(counter_tools);
p1 = series1.Points[2];
if(counter_tools!=0)
p1.AxisLabel = (cc.ToString() + "%");
p1.LegendText = "LACK OF TOOLS";
p1.Color = Color.Orange;
series1.Points.Add(counter_other);
p1 = series1.Points[3];
p1.AxisLabel = (dd.ToString() + "%");
p1.LegendText = "OTHER";
p1.Color = Color.Maroon;
//pieChart.Invalidate();
splitContainer1.Panel1.Controls.Add(pieChart);
}
I can't seem to find out why, any suggestions?
I use the following function to initalize the graph:
private void InitializeChart()
{
this.components = new System.ComponentModel.Container();
ChartArea chartArea1 = new ChartArea();
Legend legend1 = new Legend() { BackColor = Color.White, ForeColor = Color.Black, Title = "CAUSE" };
pieChart = new Chart();
((ISupportInitialize)(pieChart)).BeginInit();
SuspendLayout();
//===Pie chart
chartArea1.Name = "PieChartArea";
pieChart.ChartAreas.Add(chartArea1);
pieChart.Dock = System.Windows.Forms.DockStyle.Fill;
legend1.Name = "Legend1";
pieChart.Legends.Add(legend1);
pieChart.Location = new System.Drawing.Point(0, 50);
//====Bar Chart
AutoScaleDimensions = new System.Drawing.Size(284,262);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
//this.ClientSize = new System.Drawing.Size(284, 262);
this.Load += new EventHandler(Form1_Load);
((ISupportInitialize)(this.pieChart)).EndInit();
this.ResumeLayout(false);
}
I'm not sure where pieChart is defined... a private field?? If so, try creating a new PieChart after you have removed the previous one from the container:
splitContainer1.Panel1.Controls.Remove(pieChart);
pieChart = new PieChart();
pieChart.Series.Clear();
I'm guessing somewhere in your code you are accumulating a value instead of assigning it, so it is getting progressively smaller as you call the function on the same PieChart. Clearing out the pieChart should fix this problem.