Click here to Skip to main content
15,917,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I'm trying to draw a chart on my form by using MsChart.
I have used the following codes:
C#
Legend Legend1;
          // Create a Chart
         Chart chart1 = new Chart();

          // Create Chart Area
          ChartArea chartArea1 = new ChartArea();

          // Add Chart Area to the Chart
          chart1.ChartAreas.Add(chartArea1);

          // Create a data series
          Series series1 = new Series("series1");
          Series series2 = new Series("series2");
          //********************************زوم کردن
          // Enable range selection and zooming end user interface
          chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
          chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
          chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
          chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
          chartArea1.AxisX.ScaleView.Size = 200;
          //*******************************************************
          //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++           // show the chart in 3D
          chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

          // set the Rotation value
          chart1.ChartAreas[0].Area3DStyle.Rotation = 10;

          // set the Inclination value
          chart1.ChartAreas[0].Area3DStyle.Inclination = 40;

          // set the wall width for the axis
          chart1.ChartAreas[0].Area3DStyle.WallWidth = 7;




          // Add data points to the first series

          //  series1.BackGradientStyle=GradientStyle.TopBottom ;
          //هاشور زدن
          //  series1.BackHatchStyle = ChartHatchStyle.HorizontalBrick;
          //عکس پس زمینه
          //    series1.BackImage = @"F:\Picture\us337misdildf391klt.jpg";
          //عکس پس زمینه کجا باشد
          //series1.BackImageAlignment = ChartImageAlignmentStyle.BottomLeft;
          series1.BackSecondaryColor = Color.PowderBlue;
          series1.BackSecondaryColor = Color.Red;
          series1.BackImageTransparentColor = Color.Red;
          // بعضی از انواع باید یک جور باشند مثل رادار
          series1.ChartType = SeriesChartType.Pyramid;
          series2.ChartType = SeriesChartType.Pyramid;





          //    series1.ChartArea = "hasan";
          series1.Points.Add(34);
          series1.Points.Add(24);
          series1.Points.Add(32);
          series1.Points.Add(28);
          series1.Points.Add(44);

          // Add data points to the second series
          series2.Points.Add(14);
          series2.Points.Add(44);
          series2.Points.Add(24);
          series2.Points.Add(32);
          series2.Points.Add(28);

          // Add series to the chart
          chart1.Series.Add(series1);
          chart1.Series.Add(series2);

          // Set chart control location
          chart1.Location = new System.Drawing.Point(16, 48);

          // Set Chart control size
          chart1.Size = new System.Drawing.Size(360, 260);

          // Add chart control to the form
          this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });
          //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          //&&&&&&&&&&&&&&&&&&&&&&&&&&&اضافه نمودن جزیات
          Legend1 = new Legend("legend1");
          this.chart1.Legends.Add(Legend1);
          chart1.Series["series1"].Palette = ChartColorPalette.BrightPastel;
          chart1.Series["series1"].IsVisibleInLegend = false;
          chart1.ApplyPaletteColors();
          foreach (DataPoint dp in chart1.Series["series1"].Points)
          {
              chart1.Legends["legend1"].CustomItems.Add(dp.Color, dp.AxisLabel);
          }


but it gives me error at the line"
C#
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });

" and
"this.chart1.Legends.Add(Legend1);"

what should I do to solve it?
Posted
Updated 23-Feb-12 21:19pm
Comments
OriginalGriff 24-Feb-12 3:28am    
What is the error message?

1 solution

I found the answer myself .I changed the line:
C#
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });

to the line:
C#
this.Controls.AddRange(new System.Windows.Forms.Control[] { chart1});

and the line
C#
this.chart1.Legends.Add(Legend1);

to the line
C#
chart1.Legends.Add(Legend1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900