Previous blog C#char The use of the control in a brief introduction an example of using the chart control this article details the use of the chart control.
To use a good chart control, we first have to be clear about its structure. As shown in the following figure: (only the approximate structure is drawn, more details will make the picture less visible)
1. A chart can contain multiple ChartArea. The ChartArea is a specific coordinate region.
2. Each chartarea includes the x axis, the y axis, the vice X axis (above), the Vice Y axis (right), the bound line, and the legend of the binding.
3. Lines can have many, as long as the line is bound to ChartArea can be displayed in the corresponding ChartArea.
Its tree-shaped entity structure is as follows: For many of the style attributes within each entity, you can try to study them yourself.
A basic understanding of the structure of chart can be fully realized. followed by the code to explain
1. The creation of lines is used here to create 4 line segments.
Public Series maxtemp;
Public Series avgtemp;
Public Series mintemp;
public Series Vibration;
Maxtemp = new Series ("Maxtemp");
Avgtemp = new Series ("Avgtemp");
Mintemp = new Series ("Mintemp");
Vibration = new Series ("vibration");
2. Set the style of the line
The color of the curve Maxtemp.bordercolor = Color.Black;
Maxtemp.color = color.red;
Avgtemp.bordercolor = Color.Black;
Avgtemp.color = Color.green;
Mintemp.bordercolor = Color.Black;
Mintemp.color = Color.Blue; Vibration.
BorderColor = Color.Black; Vibration.
Color = Color.Blue;
Width of the curve maxtemp.borderwidth = 2;
Avgtemp.borderwidth = 2;
Avgtemp.borderwidth = 2; Vibration.
BorderWidth = 2;
The style of the curve has a circular curve, a ladder curve, a polyline, and so on.
Maxtemp.charttype = Seriescharttype.spline;
Avgtemp.charttype = Seriescharttype.spline;
Mintemp.charttype = Seriescharttype.spline; Vibration.
ChartType =seriescharttype.spline;
The curve's shadow style can make the curve more prominent with three-dimensional.
Maxtemp.shadowoffset = 1;
Avgtemp.shadowoffset = 1;
Mintemp.shadowoffset = 1; Vibration.
Shadowoffset = 1; The style of the tag sets the style for each data point in the curve. You can display the value of a data point in a tag, but too many
Data points will not have visibility.
maxtemp.markercolor = Color.White;
Maxtemp.markerstyle = Markerstyle.square;
Avgtemp.markercolor = Color.White;
Avgtemp.markerstyle = Markerstyle.square;
Mintemp.markercolor = Color.White;
Mintemp.markerstyle = Markerstyle.square;
Vibration. Markercolor = Color.White;
Vibration. MarkerStyle = Markerstyle.square;
Set the axis type of the line main set the following side do X axis or above do X axis, left do Y axis or right do y axis
maxtemp.xaxistype = axistype.primary;
Maxtemp.yaxistype = axistype.primary;
Avgtemp.xaxistype = axistype.primary;
Avgtemp.yaxistype = axistype.primary;
Mintemp.xaxistype = axistype.primary;
Mintemp.yaxistype = axistype.primary;
Vibration. Xaxistype = axistype.primary;
Vibration. Yaxistype = axistype.primary;
Vibration. Isvalueshownaslabel = true; This property takes the value of the data point as a label.
2. ChartArea creation and property settings
Public ChartArea mainArea1 = new ChartArea ("Areadts");
Public ChartArea mainArea2 = new ChartArea ("Areadovs");
Set chart area users can drag the cursor here to set up the user can zoom in to view the area by dragging the cursor
Set chart area users can drag the cursor
mainArea1.CursorX.IsUserEnabled = true;
MainArea1.CursorY.IsUserEnabled = true;
MainArea2.CursorX.IsUserEnabled = true;
MainArea2.CursorY.IsUserEnabled = true;
Sets the caption for x Y axis coordinates.
mainArea1.AxisX.Title = "Fiber partition number/(scalar)";
MainArea1.AxisY.Title = "Temperature value/(degree Celsius)";
MainArea2.AxisX.Title = "Fiber vibration position/(M)";
MainArea2.AxisY.Title = "Vibration value/(a.u.)";
Sets the grid. The main grid corresponds to the primary scale corresponding to the sub scale, drawing a line from the scale to the other end. If there is too much data in the line,
Producing more grid lines will make the entire area too dense, even for all black. So in the case of a lot of data points to close the secondary grid, or even the main grid.
mainArea1.AxisX.MajorGrid.Enabled = false;
mainArea1.AxisY.MajorGrid.Enabled = false;
mainArea1.AxisX.MinorTickMark.Enabled = false;
mainArea2.AxisX.MajorGrid.Enabled = false;
mainArea2.AxisY.MajorGrid.Enabled = false;
mainArea2.AxisX.MinorTickMark.Enabled = false;
Set curve horizontal axis value type as time type
Vibration. Xvaluetype = Chartvaluetype.datetime;
The label of the AXISX to which this line is bound is set to the time format.
MainArea1.AxisX.LabelStyle.Format = "Mm-dd hh:mm";
Setting major tick marks and secondary tick marks generally only the main tick marks have corresponding label values.
mainArea1.AxisX.MinorTickMark.Enabled = true;
MainArea1.AxisX.MajorTickMark.Enabled = true;
3. Create a legend title
Legend Legend = new Legend ();
Chart. Legends[0]. Position = p; Each created object is stored in a Chart collection property and can be accessed randomly in the form of an array.
4. Add the previously created object to the parent entity to which you belong.
Title title = new Title ("Chart use Method");
Chart. Titles.add (title);
Chart2. Chartareas.add (MAINAREA1); Adds a line to the chart Chartareas collection property
Chart2. Chartareas.add (MAINAREA2);
Series4. ChartArea = "Areadovs"; Bind the line to the corresponding ChartArea the SERIES3 can be bound by the name of the ChartArea
. ChartArea = "Areadts";
Chart2. Series.add (SERIES3); Adds a line to the Series collection property of chart.
Chart2. Series.add (SERIES4);
5. Sample effects. Note The example effect is independent of the preceding code, which is for illustration purposes only.
The preceding code does not tell you how to add data points to a line, and the previous blog C # uses tips to add data points to lines.