当前位置:   article > 正文

Dev中ChartControl——属性熟悉与简单应用

chartcontrol
图表元素之间的关系:

根据DevExpress帮助文档中描述:

创建点图:

1、创建图表

  1. /// <summary>
  2. /// 创建图表
  3. /// </summary>
  4. private void CreatChart()
  5. {
  6.     // Create a new chart.图表控件
  7.     ChartControl pointChart = new ChartControl();
  8.     // Add the chart to the form.
  9.     pointChart.Dock = DockStyle.Fill;//停靠方式
  10.     this.Controls.Add(pointChart);
  11.     //pointChart.RightToLeft = System.Windows.Forms.RightToLeft.Inherit;  //指示文本是否从右至左显示
  12.     //添加 图表标题
  13.     pointChart.Titles.Add(new ChartTitle());
  14.     pointChart.Titles[0].Text = "A Point Chart";
  15.     //Legend  调整图例
  16.     pointChart.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside; //获取或设置图表控件中的图例的水平对齐方式。
  17.     pointChart.Legend.AlignmentVertical = LegendAlignmentVertical.Top;//获取或设置图表控件中的图例的竖直对齐方式。
  18.     pointChart.Legend.Direction = LegendDirection.BottomToTop;//获取或设置在该图例中显示系列名称的方向。
  19.     pointChart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True;//是否在图表上显示图例
  20.     Series series1 = CreateSeries("Series 1");//创建系列
  21.     Series series2 = CreateSeries("Series 2");
  22.     pointChart.Series.Add(series1);// Add the series to the chart.将点系列加入图表控件
  23.     pointChart.Series.AddRange(new Series[] { series2 });
  24.     //Diagram 对象等于 null (在 Visual Basic 中为 Nothing),直到图表至少拥有一个系列。
  25.     SetDiagarm(pointChart);//设置图像
  26.     CreateAxisY(series2, pointChart);
  27.     // Add points to it.
  28.     series1.Points.Add(new SeriesPoint(1, 10));
  29.     series1.Points.Add(new SeriesPoint(2, 22));
  30.     series1.Points.Add(new SeriesPoint(3, 14));
  31.     series1.Points.Add(new SeriesPoint(4, 27));
  32.     series1.Points.Add(new SeriesPoint(5, 15));
  33.     series1.Points.Add(new SeriesPoint(6, 28));
  34.     series1.Points.Add(new SeriesPoint(7, 15));
  35.     SeriesPoint spoint = new SeriesPoint(8, 33);
  36.     spoint.Color = Color.Red;//定制点的颜色
  37.     series1.Points.Add(spoint);
  38.     //系列2采用DataTable绑定
  39.     // Create an empty table.
  40.     DataTable table = new DataTable("Table1");
  41.     // Add two columns to the table.
  42.     table.Columns.Add("Argument", typeof(Int32));
  43.     table.Columns.Add("Value", typeof(Int32));
  44.     // Add data rows to the table.
  45.     Random rnd = new Random();
  46.     DataRow row = null;
  47.     for (int i = 0; i < 10; i++)
  48.     {
  49.         row = table.NewRow();
  50.         row["Argument"] = i;
  51.         row["Value"] = rnd.Next(30);
  52.         table.Rows.Add(row);
  53.     }
  54.     // Specify data members to bind the series.
  55.     series2.ArgumentScaleType = ScaleType.Numerical;
  56.     series2.ArgumentDataMember = "Argument";//设置参数数据字段的名称
  57.     series2.ValueScaleType = ScaleType.Numerical;
  58.     series2.ValueDataMembers.AddRange(new string[] { "Value" });
  59.     series2.DataSource = table;
  60. }

2、创建系列

  1. /// <summary>
  2. /// 创建系列
  3. /// </summary>
  4. /// <param name="Caption">图形标题</param>
  5. /// <returns></returns>
  6. private Series CreateSeries(string Caption)
  7. {
  8.     // Create a point series.创建一个点系列
  9.     Series series1 = new Series(Caption, ViewType.Point);
  10.     series1.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.Default; //获取或设置一个值,该值指定此系列是否启用十字准线游标。
  11.     series1.CrosshairLabelVisibility = DevExpress.Utils.DefaultBoolean.True;//指定是否在特定的2D DEVExt.xCARTARTSs系列的图表上显示十字准线标签。
  12.     series1.CrosshairLabelPattern = "坐标:({A},{V})";//获取或设置一个字符串表示指定要在当前系列的瞄准线标签中显示的文本的模式
  13.     series1.CrosshairHighlightPoints = DevExpress.Utils.DefaultBoolean.Default;//获取或设置一个值,该值指定当十字准线光标悬停时,系列点是否突出显示。
  14.     // Set the numerical argument scale type for the series,as it is qualitative, by default.
  15.     series1.ArgumentScaleType = ScaleType.Numerical;   //指定系列的参数刻度类型。默认为(ScaleType.Auto)
  16.     series1.ValueScaleType = ScaleType.Numerical;  //指定系列的取值刻度类型
  17.     series1.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;  //是否显示系列点标签
  18.     series1.LegendText = Caption;//设置标志系列的图例文本
  19.             
  20.     //调整 系列点标签
  21.     PointSeriesLabel myLable1 = (PointSeriesLabel)series1.Label;
  22.     myLable1.Angle = 0;//获取或设置控制数据点标签位置的角度
  23.     myLable1.TextPattern = "坐标:({A},{V})";//获取或设置一个字符串,该字符串表示指定要在系列标注标签中显示的文本的模式。
  24.     myLable1.Position = PointLabelPosition.Outside;//获取或设置点标记所在的位置。
  25.     myLable1.ResolveOverlappingMode=ResolveOverlappingMode.Default;//启用系列标签的自动冲突检测和解决
  26.     // 点系列视图属性设置
  27.     PointSeriesView myView1 = (PointSeriesView)series1.View;//转换系列的视图类型为点类型
  28.     myView1.PointMarkerOptions.Kind = MarkerKind.Star;//标记的形状
  29.     myView1.PointMarkerOptions.StarPointCount = 5;//设置星形标记具有的点数
  30.     myView1.PointMarkerOptions.Size = 8;//标记大小
  31.     //在不同的窗格中显示系列
  32.     //myView1.Pane—— 在 属性 窗口中展开 XYDiagramSeriesViewBase.Pane 属性的下拉菜单,并单击 New pane(新建窗格) 菜单项。
  33.  
  34.     return series1;
  35. }

3、图像设置

  1. /// <summary>
  2. /// 图像设置
  3. /// </summary>
  4. /// <param name="pointChart">图表控件</param>
  5. private void SetDiagarm(ChartControl pointChart)
  6. {
  7.     // Access the diagram's properties.把 Diagram 对象转换为所需的图象类型
  8.     XYDiagram diagram = (XYDiagram)pointChart.Diagram;
  9.     diagram.Rotated = false;//图像是否旋转
  10.     diagram.EnableAxisXScrolling = true;//X轴是否允许滚动
  11.     diagram.EnableAxisXZooming = true;//X轴是否允许缩放
  12.     diagram.PaneLayoutDirection = PaneLayoutDirection.Horizontal;//窗格的对齐方式
  13.     // Customize the appearance of the X-axis title.调整 X-轴
  14.     AxisX xAxis = diagram.AxisX;//获取X轴
  15.     xAxis.Alignment = AxisAlignment.Near;//指定轴相对于另一主轴的位置。属性 AxisAlignment.Zero 设置仅对主轴可用
  16.     xAxis.Title.Text = "X轴";//设置轴标题
  17.     xAxis.Title.Visibility = DevExpress.Utils.DefaultBoolean.True; //是否显示轴标题
  18.     xAxis.Label.TextPattern = "";
  19.     //防止过长的轴标签产生重叠
  20.     xAxis.Label.Angle = -45;//设置轴标签文本旋转的角度
  21.     xAxis.Label.EnableAntialiasing = DevExpress.Utils.DefaultBoolean.Default;//获取或设置是否对轴标签的文本应用反走样(平滑)
  22.     xAxis.Label.Staggered = true;//轴标签是否是交错排列的
  23.     //轴取值的范围是自动确定的(当启用了  xAxis.WholeRange.Auto 属性时),也可以人工指定两个轴取值来限定轴的范围
  24.     //xAxis.WholeRange.Auto = false;
  25.     //xAxis.WholeRange.MaxValue = 70;
  26.     //xAxis.WholeRange.MinValue = -70;
  27.     //通过 AxisBase.Logarithmic 和 AxisBase.LogarithmicBase 属性,以对数来呈现轴的取值。
  28.     //xAxis.Logarithmic = true;
  29.     //xAxis.LogarithmicBase = 10;
  30.     // Create a constant line. 设置常数线
  31.     ConstantLine constantLine1 = new ConstantLine("Constant Line 1");
  32.     xAxis.ConstantLines.Add(constantLine1);
  33.     // Create a strip with its maximum and minimum axis value defined.数值带
  34.     xAxis.Strips.Add(new Strip("Strip 1", 5, 15));
  35.     // Enable automatic scale breaks creation, and define their maximum number.启用自动刻度分隔线
  36.     diagram.AxisY.AutoScaleBreaks.Enabled = true;
  37.     diagram.AxisY.AutoScaleBreaks.MaxCount = 5;
  38.     // Add scale breaks to the Y-axis collection, with their Edge1 and Edge2 properties defined in the constructor.人工把刻度分隔线插入到轴中
  39.     diagram.AxisY.ScaleBreaks.Add(new ScaleBreak("Scale Break 1", 10, 100));
  40.     diagram.AxisY.ScaleBreaks.Add(new ScaleBreak("Scale Break 2", 110, 2000));
  41.     // Customize the appearance of the axes' grid lines.网格显示和设置
  42.     diagram.AxisY.GridLines.Visible = true;
  43.     diagram.AxisY.GridLines.MinorVisible = true;
  44.     // Customize the appearance of the axes' tickmarks.刻度线显示和设置
  45.     diagram.AxisY.Tickmarks.Visible = false;
  46.     diagram.AxisY.Tickmarks.MinorVisible = false;
  47.     //定制窗格的滚动条的外观
  48.     ScrollBarOptions scrollBarOptions = diagram.DefaultPane.ScrollBarOptions;
  49.     scrollBarOptions.BackColor = Color.White;
  50.     scrollBarOptions.BarColor = Color.Blue;
  51.     scrollBarOptions.BorderColor = Color.Navy;
  52.     scrollBarOptions.BarThickness = 15;
  53.     scrollBarOptions.XAxisScrollBarAlignment = ScrollBarAlignment.Far;
  54.     scrollBarOptions.XAxisScrollBarVisible = true;
  55.     // Obtain a diagram and clear its collection of panes.
  56.     diagram.Panes.Clear();
  57.     // Create a pane for each series.
  58.     for (int i = 1; i < pointChart.Series.Count; i++)
  59.     {
  60.         XYDiagramPane pane = new XYDiagramPane("The Pane's Name");
  61.         diagram.Panes.Add(pane);
  62.         XYDiagramSeriesViewBase view = (XYDiagramSeriesViewBase)pointChart.Series[i].View;
  63.         view.Pane = pane;
  64.     }
  65.     
  66.     AxisY myAxis = ((XYDiagram)pointChart.Diagram).AxisY;
  67. }

4、创建辅助轴

  1. /// <summary>
  2. /// 创建图表的第二坐标系
  3. /// </summary>
  4. /// <param name="series">Series对象</param>
  5. /// <returns></returns>
  6. private SecondaryAxisY CreateAxisY(Series series, ChartControl chartControl)
  7. {
  8.     // Create secondary axes, and add them to the chart's Diagram.
  9.     SecondaryAxisY myAxis = new SecondaryAxisY(series.Name);
  10.     ((XYDiagram)chartControl.Diagram).SecondaryAxesY.Add(myAxis);
  11.     // Assign the series2 to the created axes.
  12.     ((PointSeriesView)series.View).AxisY = myAxis;//为当前系列指定Y轴
  13.     myAxis.Title.Text = "A Secondary Y-Axis";
  14.     myAxis.Title.Alignment = StringAlignment.Far; //顶部对齐
  15.     myAxis.Title.Visibility = DevExpress.Utils.DefaultBoolean.True;
  16.     myAxis.Title.Font = new Font("宋体", 9.0f);
  17.     series.View.Color = Color.Green;
  18.     Color color = series.View.Color;//设置坐标的颜色和图表线条颜色一致
  19.     myAxis.Title.TextColor = color;
  20.     myAxis.Label.TextColor = color;
  21.     myAxis.Color = color;
  22.     return myAxis;
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/230470
推荐阅读
相关标签
  

闽ICP备14008679号