internal Image BuildChart(DataView data) { // select chart type ChartGroup cg = _chart.ChartGroups.Group0; Area ca = _chart.ChartArea; switch (_chartType.Text) { case "Area": cg.ChartType = Chart2DTypeEnum.Area; ca.Inverted = false; ca.AxisX.AnnotationRotation = 45; break; case "Bar": cg.ChartType = Chart2DTypeEnum.Bar; ca.Inverted = true; ca.AxisX.AnnotationRotation = 0; break; default: cg.ChartType = Chart2DTypeEnum.XYPlot; ca.Inverted = false; ca.AxisX.AnnotationRotation = 45; break; } // add data to the chart PointF[] points = new PointF[data.Count]; for (int i = 0; i < data.Count; i++) { points[i].X = i; points[i].Y = float.Parse(data[i]["ProductSales"].ToString()); } cg.ChartData.SeriesList[0].PointData.CopyDataIn(points); // add X labels to the chart Axis ax = _chart.ChartArea.AxisX; ax.ValueLabels.Clear(); for (int i = 0; i < data.Count; i++) { ax.ValueLabels.Add(i, data[i]["ProductName"].ToString()); } ax.AnnoMethod = AnnotationMethodEnum.ValueLabels; // get chart image return(_chart.GetImage(ImageFormat.Emf)); }
private void ChartSetup() { // Reset the chart, position it, and appropriately anchor c1Chart1.Reset(); c1Chart1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left; //!!VBPassThru // Setup the Chart appearance Style sty = c1Chart1.Style; sty.BackColor = Color.FromKnownColor(KnownColor.Window); // The Chart border Border b = sty.Border; b.BorderStyle = BorderStyleEnum.Solid; b.Rounding.All = 10; b.Thickness = 3; b.Color = Color.Black; // Turn on tooltips c1Chart1.ToolTip.Enabled = true; // Generate scatter data and populate the chart. PointF [] scatter = GenerateScatterData(100, 100f, 75f); // Plot the data in the first ChartGroup. For scatter data, turn off the lines. ChartDataSeries cds = c1Chart1.ChartGroups[0].ChartData.SeriesList.AddNewSeries(); cds.LineStyle.Pattern = LinePatternEnum.None; cds.PointData.CopyDataIn(scatter); cds.TooltipText = "({#XVAL:0.00},{#YVAL:0.00})"; // Force the chart to scale the axes right now. c1Chart1.GetImage(); // Automatically label each of the points by its PointIndex. cds.DataLabel.Compass = LabelCompassEnum.Auto; cds.DataLabel.Offset = 4; cds.DataLabel.Connected = true; cds.DataLabel.Text = "{#IPOINT}"; cds.DataLabel.Visible = true; // Provide Header for description Title t = c1Chart1.Header; t.Style.Font = new Font("Tahoma", 14); t.Text = "Histogram/Frequency Data Measuring Distance from Crosshairs to Scatter data."; // Provide Footer for instructions t = c1Chart1.Footer; Font f = new Font("Tahoma", 12); t.AppendRtfText("Drag the ", f); t.AppendRtfText("Red", Color.Red, f); t.AppendRtfText(" arrows to move the crosshairs", Color.Black, f, HorizontalAlignment.Center); // Switch over the axes to mixed label mode and add some centered // markers to form a target. Area carea = c1Chart1.ChartArea; double xMarker = AddChartAxisMarker(carea.AxisX); double yMarker = AddChartAxisMarker(carea.AxisY); // Set the Axis Titles. carea.AxisX.Text = "Scatter data X values / Distances from crosshair"; carea.AxisY.Text = "Scatter data Y values"; carea.AxisY2.Text = "Count of data at distance from crosshair"; }
private void button1_Click(object sender, System.EventArgs e) { Image img = _c1c.GetImage(ImageFormat.Emf); CreatePdf(img); }
private void Form1_Load(object sender, System.EventArgs e) { // Size the form to fit any screen this.ClientSize = new Size(600, 400); // Center the form this.CenterToParent(); // Fill the form client area with the chart c1Chart1.Dock = DockStyle.Fill; // Get the data arrays and data. int[] StudentScores = this.GetStudentPointTotals(); int[] StudentNumbers = (int[])Array.CreateInstance(typeof(int), StudentScores.Length); int nStudents = StudentScores.Length; int i; for (i = 0; i < nStudents; i++) { StudentNumbers[i] = i; } // Get the statistics double mean = FindMean(StudentScores); double stddev = FindStdDev(StudentScores, mean); // Set up the header c1Chart1.Header.Text = "Student Scores and Grades"; c1Chart1.Header.Style.Font = new Font("Tahoma", 18, FontStyle.Bold); // Set the background color c1Chart1.Style.BackColor = Color.FromArgb(128, 192, 150); // Clear the existing data c1Chart1.ChartGroups[0].ChartData.SeriesList.Clear(); // Add the data ChartData data = c1Chart1.ChartGroups[0].ChartData; ChartDataSeriesCollection series = data.SeriesList; //plot the student scores ChartDataSeries StuSeries = series.AddNewSeries(); StuSeries.Label = "raw scores"; StuSeries.LineStyle.Pattern = LinePatternEnum.None; StuSeries.LineStyle.Color = c1Chart1.Style.BackColor; StuSeries.SymbolStyle.Shape = SymbolShapeEnum.Star; StuSeries.SymbolStyle.Color = Color.DarkRed; StuSeries.X.CopyDataIn(StudentNumbers); StuSeries.Y.CopyDataIn(StudentScores); StuSeries = null; // mean + 2 s LinePatternEnum [] LinePatterns = new LinePatternEnum[] { LinePatternEnum.Dash, LinePatternEnum.DashDot, LinePatternEnum.Solid, LinePatternEnum.DashDotDot, LinePatternEnum.Dot }; for (i = 2; i >= -2; i--) { ChartDataSeries StatSeries = series.AddNewSeries(); double [] xd = new double [] { 0, nStudents }; double [] yd = new double [] { mean + i * stddev, mean + i * stddev }; StatSeries.X.CopyDataIn(xd); StatSeries.Y.CopyDataIn(yd); StatSeries.SymbolStyle.Shape = SymbolShapeEnum.None; StatSeries.LineStyle.Pattern = LinePatterns[i + 2]; StatSeries.LineStyle.Color = Color.Black; StatSeries.LineStyle.Thickness = 1; if (i > 0) { StatSeries.Label = "m+" + i.ToString() + "s"; } else if (i < 0) { StatSeries.Label = "m" + i.ToString() + "s"; } else { StatSeries.Label = "mean"; StatSeries.LineStyle.Thickness = 2; StatSeries.LineStyle.Pattern = LinePatternEnum.Solid; } } // box the plot area c1Chart1.ChartArea.PlotArea.Boxed = true; // Show the legend c1Chart1.Legend.Visible = true; // Set the Axis titles c1Chart1.ChartArea.AxisX.Text = "Student Number"; c1Chart1.ChartArea.AxisY.Text = "Student Accumulated Points"; // sort the student scores so they can be analyzed Array.Sort(StudentScores); // Define each of the letter grades string [] GradeLetter = new String[] { "A", "B", "C", "D", "F" }; // Get the bounds of each letter grade // At most 95% of the students will not get an A // At most 75% of the students will not get a B or higher // At most 25% of the students will not get a C or higher // At most 5% of the students will not get a D or higher int [] GradeBounds = new int[] { StudentScores[GetBoundingIndex(StudentScores, 0.95)], StudentScores[GetBoundingIndex(StudentScores, 0.75)], StudentScores[GetBoundingIndex(StudentScores, 0.25)], StudentScores[GetBoundingIndex(StudentScores, 0.05)] }; // get the color codes for each grade Color [] GradeColor = new Color[] { Color.FromArgb(128, 128, 225), Color.FromArgb(128, 255, 128), Color.FromArgb(255, 228, 128), Color.FromArgb(55, 228, 228), Color.FromArgb(255, 192, 192) }; // Add the chart labels. They will be positioned later ChartLabels labels = c1Chart1.ChartLabels; labels.DefaultLabelStyle.BackColor = c1Chart1.Style.BackColor; labels.DefaultLabelStyle.Font = new Font("Courier New", 16, FontStyle.Bold); C1.Win.C1Chart.Label lab = null; for (i = 0; i < 5; i++) { lab = labels.LabelsCollection.AddNewLabel(); lab.Text = GradeLetter[i]; lab.Style.BackColor = GradeColor[i]; lab.AttachMethod = AttachMethodEnum.Coordinate; lab.Visible = true; } // Below are calculations and settings that depend upon auto // positioning of the chart. The auto positions are only // calculated during rendering of the chart. Force the // chart to be rendered so the chart element positions are // calculated. // Force calculation of chart element positions c1Chart1.GetImage(); // Add and show the alarm zones AlarmZonesCollection zones = c1Chart1.ChartArea.PlotArea.AlarmZones; for (i = 0; i < 5; i++) { AlarmZone zone = zones.AddNewZone(); zone.Name = GradeLetter[i]; zone.BackColor = GradeColor[i]; if (i == 0) { zone.UpperExtent = c1Chart1.ChartArea.AxisY.Max; } else { zone.UpperExtent = zones[i - 1].LowerExtent; } if (i == 4) { zone.LowerExtent = c1Chart1.ChartArea.AxisY.Min; } else { zone.LowerExtent = GradeBounds[i]; } zone.Visible = true; } PositionLegends(); }