private void AnnotateSlices(C1.Win.C1Chart.C1Chart chart) { Style s = chart.ChartLabels.DefaultLabelStyle; s.BackColor = SystemColors.Info; s.Opaque = true; s.Border.BorderStyle = BorderStyleEnum.Solid; ChartDataSeriesCollection series = chart.ChartGroups[0].ChartData.SeriesList; chart.ChartLabels.LabelsCollection.Clear(); // Attach labels to each slice for (int i = 0; i < series.Count; i++) { C1.Win.C1Chart.Label label = chart.ChartLabels.LabelsCollection.AddNewLabel(); label.Text = String.Format("{0:C0}", series[i].Y[0]); label.Compass = LabelCompassEnum.Radial; label.Offset = 20; label.Connected = true; label.Visible = true; label.AttachMethod = AttachMethodEnum.DataIndex; AttachMethodData am = label.AttachMethodData; am.GroupIndex = 0; am.SeriesIndex = i; am.PointIndex = 0; series[i].Offset = 0; } }
public void AnnotateSlicewithImage(C1.Win.C1Chart.C1Chart chart, int seriesIndex) { ChartDataSeriesCollection series = chart.ChartGroups[0].ChartData.SeriesList; LabelsCollection labels = c1Chart1.ChartLabels.LabelsCollection; C1.Win.C1Chart.Label label; label = labels.AddNewLabel(); label.Image = imageList1.Images[seriesIndex]; label.Style.BackColor = Color.Transparent; label.Style.Border.BorderStyle = BorderStyleEnum.None; label.Style.GradientStyle = GradientStyleEnum.None; label.Style.ImageAlignment = AlignImageEnum.Top; label.Size = label.Image.Size + new Size(0, 20); label.Compass = LabelCompassEnum.Radial; label.Offset = 30; label.Connected = false; label.Visible = true; label.AttachMethod = AttachMethodEnum.DataIndex; label.Text = String.Format("{0:C0}", series[seriesIndex].Y[0]); AttachMethodData am = label.AttachMethodData; am.GroupIndex = 0; am.SeriesIndex = seriesIndex; am.PointIndex = 0; }
/// <summary> /// Update the position of the coordinate specified labels. /// Must use the AttachMethodData object of each label to specify these. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void c1Chart1_Resize(object sender, EventArgs e) { // update the label positions to match the new chart boundaries. AttachMethodData adm = c1Chart1.ChartLabels[1].AttachMethodData; adm.X = -5 + this.ClientSize.Width; adm.Y = 5; adm = c1Chart1.ChartLabels[2].AttachMethodData; adm.X = -5 + this.ClientSize.Width; adm.Y = -5 + this.ClientSize.Height; adm = c1Chart1.ChartLabels[3].AttachMethodData; adm.X = 5; adm.Y = -5 + this.ClientSize.Height; }
/// <summary> /// Sets the labels for each slice. /// </summary> private void SetSizeLabels(bool create) { if (create) { // Set labels for Used Space for (int i = 0; i < diskCount + 1; i++) { for (int j = 0; j < 2; j++) { ChartDataSeries series = c1Chart1.ChartGroups[0].ChartData.SeriesList[j]; // Add a chart label for each slice C1.Win.C1Chart.Label lbl = c1Chart1.ChartLabels.LabelsCollection.AddNewLabel(); lbl.AttachMethod = AttachMethodEnum.DataIndex; lbl.Text = GetSizeString((Int64)series.Y[i], this.Format); if (j == 0) { lbl.Compass = LabelCompassEnum.North; } else { lbl.Compass = LabelCompassEnum.South; } lbl.Offset = 0; lbl.Visible = true; AttachMethodData amd = lbl.AttachMethodData; amd.GroupIndex = 0; amd.SeriesIndex = j; amd.PointIndex = i; } } } else { for (int i = 0; i < diskCount + 1; i++) { for (int j = 0; j < 2; j++) { ChartDataSeries series = c1Chart1.ChartGroups[0].ChartData.SeriesList[j]; // Add a chart label for each slice C1.Win.C1Chart.Label lbl = c1Chart1.ChartLabels.LabelsCollection[diskCount + 1 + i * 2 + j]; lbl.Text = GetSizeString((Int64)series.Y[i], this.Format); } } } }
private void GetLineChart(DataTable dt, string XDataName, string[] YCols, int Groupindex) { try { DataView dv = dt.DefaultView; int countColumns = YCols.Length; //多少列 ;第一列未x坐标值 ;其他为数据 int countRows = dt.Rows.Count; PointF[][] data = new PointF[countColumns][]; //视图的数据 for (int i = 0; i < countColumns; i++) { data[i] = new PointF[countRows]; } //各列对应的数据 for (int j = 0; j < countColumns; j++) { for (int i = 0; i < countRows; i++) { string data1 = dv[i][YCols[j]].ToString().Replace("天", "").Trim(); float y = 0; if (!string.IsNullOrEmpty(data1)) { y = float.Parse(data1); } data[j][i] = new PointF(i, y); } } //绑定数据 ChartDataSeriesCollection collSeries = c1Chart1.ChartGroups[Groupindex].ChartData.SeriesList; collSeries.Clear(); for (int i = 0; i < countColumns; i++) //如果是双条,则显示 { ChartDataSeries series = collSeries.AddNewSeries(); series.PointData.CopyDataIn(data[i]); series.FitType = C1.Win.C1Chart.FitTypeEnum.Line; series.LineStyle.Pattern = LinePatternEnum.Solid; //series.Label = dt.Columns[i].ColumnName; } for (int i = 0; i < countColumns; i++) //如果是双条,则显示 { //lend,对三种颜色Bar块的描述 c1Chart1.ChartGroups[Groupindex].ChartData.SeriesList[i].Label = YCols[i]; } ChartDataSeriesCollection dscoll = c1Chart1.ChartGroups[Groupindex].ChartData.SeriesList; //dscoll.Remove(c1Chart1.ChartGroups[0].ChartData.SeriesList[3]); if (Groupindex == 0) { c1Chart1.ChartLabels.LabelsCollection.Clear(); } for (int i = 0; i < dscoll.Count; i++) { ChartDataSeries series = dscoll[i]; for (int j = 0; j < dv.Count; j++) { //加标签,在Bar块上面显示数据 C1.Win.C1Chart.Label lbl = c1Chart1.ChartLabels.LabelsCollection.AddNewLabel(); string data1 = dv[j][YCols[i]].ToString().Replace("天", "").Trim(); if (!string.IsNullOrEmpty(data1)) { lbl.Text = string.Format("{0}", float.Parse(data1)); } lbl.Compass = LabelCompassEnum.North; //lbl.Style.BackColor = Color.Brown; lbl.Style.ForeColor = Color.Blue; lbl.Offset = 10; lbl.Connected = false; lbl.Visible = true; lbl.AttachMethod = AttachMethodEnum.DataIndex; AttachMethodData am = lbl.AttachMethodData; am.GroupIndex = Groupindex; //0 am.SeriesIndex = i; //i am.PointIndex = j; //0 } } if (Groupindex == 0) { //显示X轴标签 Axis ax = c1Chart1.ChartArea.AxisX; //ax.Min = 10; ax.TickMinor = TickMarksEnum.None; ax.ValueLabels.Clear(); ax.AnnoMethod = AnnotationMethodEnum.ValueLabels; for (int i = 0; i < dv.Count; i++) { ax.ValueLabels.Add(i, dv[i][XDataName].ToString()); } //ax.TickLabels = TickLabelsEnum.High; Axis ay = c1Chart1.ChartArea.AxisY; //ay.TickMinor = TickMarksEnum.None; ay.GridMajor.Visible = true; ay.GridMajor.Color = Color.White; ay.Min = 0; } } catch (Exception ex) { throw ex; } }
private void GetPieChart(DataTable dt, string XDataName, string[] YCols) { try { DataView dv = dt.DefaultView; int countColumns = YCols.Length; //多少列 int allNum = 0; PointF[][] data = new PointF[countColumns][]; //视图的数据 for (int i = 0; i < countColumns; i++) { data[i] = new PointF[dv.Count]; } for (int j = 0; j < countColumns; j++) { //各列对应的数据 for (int i = 0; i < dv.Count; i++) { float y = float.Parse((dv[i][j]).ToString()); data[j][i] = new PointF(i, y); } } //绑定数据 ChartDataSeriesCollection collSeries = c1Chart1.ChartGroups[0].ChartData.SeriesList; collSeries.Clear(); for (int i = 0; i < countColumns; i++) //如果是双条,则显示 { ChartDataSeries series = collSeries.AddNewSeries(); series.PointData.CopyDataIn(data[i]); //collSeries[i].LineStyle.Pattern = LinePatternEnum.DashDot; series.FitType = C1.Win.C1Chart.FitTypeEnum.Beziers; //series.PointData.CopyDataIn(data[i]);// 这里的data是PointF类型 //series.FitType = C1.Win.C1Chart.FitTypeEnum.Spline; series.Label = YCols[i]; } for (int i = 0; i < countColumns; i++) //如果是双条,则显示 { //lend,对三种颜色Bar块的描述 c1Chart1.ChartGroups[0].ChartData.SeriesList[i].Label = YCols[i]; } // c1Chart1.ChartGroups[0].ChartData.SeriesList[2].Label = "英语成绩"; //c1Chart1.ChartGroups[0].ChartData[3].LineStyle.Color = Color.LightSteelBlue; //c1Chart1.ChartGroups[0].ChartData[3].LineStyle.Thickness = 0; //c1Chart1.ChartGroups[0].ChartData.SeriesList[3].Group.Visible = true; //坐标上的柱被隐藏 //c1Chart1.ChartGroups[0].ChartData.SeriesList[3].Display = SeriesDisplayEnum.Hide; // c1Chart1.ChartGroups[0].ChartData.SeriesList[3].Display = SeriesDisplayEnum.Exclude; //c1Chart1.ChartGroups[0].ChartData[3].Display = SeriesDisplayEnum.Exclude; //c1Chart1.Legend //在Bar中显示值 ChartDataSeriesCollection dscoll = c1Chart1.ChartGroups[0].ChartData.SeriesList; //dscoll.Remove(c1Chart1.ChartGroups[0].ChartData.SeriesList[3]); c1Chart1.ChartLabels.LabelsCollection.Clear(); for (int i = 0; i < dscoll.Count; i++) { ChartDataSeries series = dscoll[i]; for (int j = 0; j < dv.Count; j++) { allNum = 0; for (int k = 0; k < YCols.Length; k++) { allNum += int.Parse(dv[j][YCols[k]].ToString()); } //加标签,在Bar块上面显示数据 C1.Win.C1Chart.Label lbl = c1Chart1.ChartLabels.LabelsCollection.AddNewLabel(); string c1Label = string.Format("{0}", float.Parse(dv[j][series.Label].ToString())); float labelNum = float.Parse(dv[j][series.Label].ToString());//显示的实际数值 lbl.Text = series.Label; lbl.Text += ":" + labelNum.ToString(); if (allNum != 0) { float percent = labelNum * 100 / allNum; lbl.Text += ", " + String.Format("{0:F2} ", percent) + "%"; } //lbl.Text = string.Format("{0}", float.Parse(dv[j][series.Label].ToString())); lbl.Compass = LabelCompassEnum.Radial; //lbl.Style.BackColor = Color.Brown; lbl.Style.ForeColor = Color.Blue; lbl.Offset = 10; lbl.Connected = false; lbl.Visible = true; lbl.AttachMethod = AttachMethodEnum.DataIndex; AttachMethodData am = lbl.AttachMethodData; am.GroupIndex = 0; //0 am.SeriesIndex = i; //i am.PointIndex = j; //0 } } //显示X轴标签 //Axis ax = c1Chart1.ChartArea.AxisX; //ax.ValueLabels.Clear(); //ax.AnnoMethod = AnnotationMethodEnum.ValueLabels; //for (int i = 0; i < dv.Count; i++) //{ // //ax.ValueLabels.Add(i, dv[i]["path_code"].ToString()); //} ////显示X轴标签 //Axis ay = c1Chart1.ChartArea.AxisY; //ay.Text="" } catch (Exception ex) { throw ex; } }
void SetChart(DataTable table1) { try { c1Chart1.Reset(); c1Chart1.ChartLabels.LabelsCollection.Clear(); c1Chart1.ChartArea.AxisX.ValueLabels.Clear(); c1Chart1.ChartArea.AxisX.Text = "日期"; c1Chart1.ChartArea.AxisY.Text = "数值"; c1Chart1.Legend.Visible = false; c1Chart1.Legend.Orientation = LegendOrientationEnum.Auto; c1Chart1.Legend.Text = "平均时间(分)"; c1Chart1.Legend.Style.Border.BorderStyle = BorderStyleEnum.Solid; //c1Chart1.ChartGroups.Group0.ChartType = Chart2DTypeEnum.Area; c1Chart1.ChartGroups[0].ChartData.SeriesList.Clear(); if (table1 != null) { DataTable objTable = table1; if (objTable == null) { return; } //非饼图的数据加载 for (int j = 1; j < objTable.Columns.Count; j++) { //if (j > 1) // continue; ChartDataSeries tval = new ChartDataSeries(); c1Chart1.ChartArea.AxisX.AnnoMethod = C1.Win.C1Chart.AnnotationMethodEnum.ValueLabels; tval.DataLabel.Visible = true; tval.Label = objTable.Columns[j].ColumnName; for (int i = 0; i < objTable.Rows.Count; i++) { float fvalue = 0; float.TryParse(objTable.Rows[i][j].ToString(), out fvalue); tval.PointData.Add(new PointF(i, fvalue)); #region 具体每一项的值显示 C1.Win.C1Chart.Label lab = c1Chart1.ChartLabels.LabelsCollection.AddNewLabel(); lab.AttachMethod = AttachMethodEnum.DataIndex; AttachMethodData amd = lab.AttachMethodData; amd.GroupIndex = 0; amd.PointIndex = i; amd.SeriesIndex = j - 1; //数据集的索引 lab.Text = objTable.Rows[i][j].ToString(); //标签值 //lab.Visible = chklabVisable.Checked; lab.Visible = true; lab.Compass = LabelCompassEnum.NorthEast; //值显示的位置 lab.Connected = false; lab.Offset = 20; //值显示的位置的偏移量 lab.TooltipText = objTable.Columns[j].ColumnName + ":" + lab.Text; #endregion if (j == 1) { //X轴中的内容标签 ValueLabel vlbl = new ValueLabel(); vlbl.NumericValue = i; vlbl.Text = objTable.Rows[i]["月份"].ToString() + "月"; c1Chart1.ChartArea.AxisX.ValueLabels.Add(vlbl); } } c1Chart1.ChartGroups[0].ChartData.SeriesList.Add(tval); } } } catch (Exception ex) { ex.Message.ToString(); } }
private void Form1_Load(object sender, System.EventArgs e) { // Center the form this.CenterToParent(); // All properties can be set at Design time, however // for instructional purposes, it is easier to see if // the properties are set at runtime. // Set up the Chart Appearance c1Chart1.Width = this.ClientSize.Width - c1Chart1.Left; c1Chart1.Height = this.ClientSize.Height; c1Chart1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;//!!VBPassThru c1Chart1.Style.BackColor = Color.CadetBlue; c1Chart1.Style.Border.BorderStyle = BorderStyleEnum.InsetBevel; // Add in the header Title hdr = c1Chart1.Header; hdr.Text = "Pie Chart Stuff"; hdr.Style.Font = new Font("Arial Black", 16); hdr.Style.BackColor = Color.Tan; hdr.Style.Border.BorderStyle = BorderStyleEnum.RaisedBevel; hdr.Style.Border.Thickness = 4; // Add in the footer Title ftr = c1Chart1.Footer; ftr.Text = "Nowhere"; ftr.Style.Font = new Font("Arial Narrow", 12, FontStyle.Bold); ftr.LocationDefault = new Point(10, -1); // Setup the legend. Legend lgd = c1Chart1.Legend; lgd.Compass = CompassEnum.East; lgd.Style.Border.BorderStyle = BorderStyleEnum.RaisedBevel; lgd.Style.Border.Color = Color.CadetBlue; lgd.Style.Border.Thickness = 4; lgd.Style.Font = new Font("Arial Narrow", 10); lgd.Style.HorizontalAlignment = AlignHorzEnum.Center; lgd.Text = "Series"; lgd.Visible = true; // Set the Chart area style. Area area = c1Chart1.ChartArea; area.Style.Border.BorderStyle = BorderStyleEnum.RaisedBevel; area.Style.Border.Thickness = 4; // Set the default label style. By using the default style, // all the labels styles can be handled uniformly c1Chart1.ChartLabels.DefaultLabelStyle.BackColor = SystemColors.Info; c1Chart1.ChartLabels.DefaultLabelStyle.Border.BorderStyle = BorderStyleEnum.Solid; // Set up a Pie chart with 8 slices ChartGroup grp = c1Chart1.ChartGroups[0]; grp.ChartType = Chart2DTypeEnum.Pie; grp.Pie.OtherOffset = 0; grp.Pie.Start = int.Parse(txtStartAngle.Text); // Clear existing, and add new data. ChartData dat = grp.ChartData; dat.SeriesList.Clear(); // SliceValue is the relative value of the slice in the whole. // These are the Y values. int [] SliceValue = new int [] { 30, 18, 26, 10, 40, 26, 12, 35 }; // Pick a nice color for each Series. Color [] ColorValue = new Color [] { Color.Red, Color.Tan, Color.LightGreen, Color.MediumTurquoise, Color.Blue, Color.Magenta, Color.GreenYellow, Color.MediumBlue }; for (int slice = 0; slice < SliceValue.Length; slice++) { ChartDataSeries series = dat.SeriesList.AddNewSeries(); series.PointData.Length = 1; series.PointData[0] = new PointF(1f, SliceValue[slice]); series.LineStyle.Color = ColorValue[slice]; series.Label = slice.ToString(); // set the DataLabel to show each value and percent of the whole DataLabel dlab = series.DataLabel; dlab.Compass = LabelCompassEnum.RadialText; dlab.Offset = -2; dlab.Text = "{#YVAL} ({%YVAL:0.00%})"; dlab.Style.BackColor = Color.Transparent; dlab.Style.Border.BorderStyle = BorderStyleEnum.None; dlab.Visible = radioDataLabOn.Checked; // Add a chart label for each slice C1.Win.C1Chart.Label lab = c1Chart1.ChartLabels.LabelsCollection.AddNewLabel(); lab.AttachMethod = AttachMethodEnum.DataIndex; AttachMethodData amd = lab.AttachMethodData; amd.GroupIndex = 0; amd.PointIndex = 0; amd.SeriesIndex = slice; lab.Text = "Chart Label " + slice.ToString(); lab.Compass = LabelCompassEnum.Radial; lab.Connected = true; lab.Offset = 10; lab.Visible = radioLabelsOn.Checked; } udDepth.Minimum = 5; udDepth.Maximum = 100; udDepth.Value = 30; udDepth.Increment = 5; udElev.Minimum = 5; udElev.Maximum = 85; udElev.Value = 45; udElev.Increment = 5; }