Standard bars are created with an instance of the BarSeries class. It is derived from the Series base class and inherits all its functionality. The following figure represents a typical bar chart.
figure 1.
Creating the Bar Series
An instance of the BarSeries class can be obtained from the SeriesCollection. Add method. The method will add the newly created series to the series collection and return a reference to it. If the user wants to save the reference for further use, he must explicitly cast it to the BarSeries type. The following code will create a BarSeries object in the series collection and save the returned reference:
VB.NET |
|
' there is one chart created by default Dim chart As Chart = CType(chartControl1.Charts(0), Chart) ' add bar series to it Dim bar As BarSeries = CType(chart.Series.Add(SeriesType.Bar), BarSeries)
|
C# |
|
// there is one chart created by default Chart chart = (Chart)chartControl1.Charts[0]; // add bar series to it BarSeries bar = (BarSeries)chart.Series.Add(SeriesType.Bar); |
Passing Data
Once the bar series is created, you can add data to it. A bar series uses the Values data series of its base class for the bar values. Helper methods provided by the Series class can be used to insert values into the data series that is to be used. For example, the values displayed on figure 1 were added with the following code:
VB.NET |
|
bar.Add(21) bar.Add(11) bar.Add(37) bar.Add(48) bar.Add(69)
|
C# |
|
bar.Add(21); bar.Add(11); bar.Add(37); bar.Add(48); bar.Add(69); |
You can also easily create a labeled chart:
VB.NET |
|
bar.Add(21, "Apples") bar.Add(11, "Oranges") bar.Add(37, "Bananas") ' show labels in data point labels bar.DataLabels.Format = "<label>"
|
C# |
|
bar.Add(21, "Apples"); bar.Add(11, "Oranges"); bar.Add(37, "Bananas"); // show labels in data point labels bar.DataLabels.Format = "<label>"; |
Creating a chart with different fill effects and labels:
VB.NET |
|
bar.Add(21,"Apples",New FillEffect(Color.Red)) bar.Add(11,"Oranges",New FillEffect(Color.Gold)) bar.Add(37,"Bananas",New FillEffect(Color.LimeGreen)) ' show labels in data point labels polar.DataLabels.Mode = DataLabelsMode.Every bar.DataLabels.Format = "<label>" ' instruct the bar to use the data point fill effects bar.Appearance.FillMode = AppearanceFillMode.DataPoints
|
C# |
|
bar.Add(21, "Apples", new FillEffect(Color.Red)); bar.Add(11, "Oranges", new FillEffect(Color.Gold)); bar.Add(37, "Bananas", new FillEffect(Color.LimeGreen)); // show labels in data point labels polar.DataLabels.Mode = DataLabelsMode.Every; bar.DataLabels.Format = "<label>"; // instruct the bar to use the data point fill effects bar.Appearance.FillMode = AppearanceFillMode.DataPoints; |
Controlling the Bar's Dimensions
The width and depth dimensions of the bars are specified as a percentage of the grid cell they occupy. The WidthPercent and DepthPercent properties control these percent values. By default the WidthPercent property is set to 70 and the DepthPercent property is set to 50. The following code will make the bars slightly bolder:
VB.NET |
|
bar.WidthPercent = 80 bar.DepthPercent = 70
|
C# |
|
bar.WidthPercent = 80; bar.DepthPercent = 70; |
Controlling the Bar Style
The shape of the bars is controlled with the BarStyle property. It is of type BarStyle and accepts the following values (for details, see BarStyle Enumeration):
Bar
Cylinder
Cone
InvertedCone
Pyramid
InvertedPyramid
Ellipsoid
SmoothEdgeBar
CutEdgeBar
The following code will display the bars as Cones:
VB.NET |
|
bar.BarStyle = BarStyle.Cone |
C# |
|
bar.BarStyle = BarStyle.Cone; |
When bars are displayed as Smooth or Cut edge, the user can control whether the top and bottom edges of the bar are displayed in the respective manner with the HasTopEdge and HasBottomEdge properties. The following code will display the bar as a smooth-edge bar with top and bottom edges:
VB.NET |
|
bar.BarStyle = BarStyle.SmoothEdgeBar bar.HasTopEdge = True bar.HasBottomEdge = True
|
C# |
|
bar.BarStyle = BarStyle.SmoothEdgeBar; bar.HasTopEdge = true; bar.HasBottomEdge = true; |
The size of the edge is represented as a percentage of the smaller width or depth of the bar's dimensions. By default it is set to 15. The following code will make the smooth bar edges two times larger:
VB.NET |
|
bar.EdgePercent = 30 |
The following table shows bar charts with different styles:
Demonstration of Different Bar Styles |
|
|
|
|
|
|
Controlling the Bar's Appearance
By default all bars are displayed with the filling specified by the FillEffect object accessible through the BarFillEffect property and the border specified by the LineProperties object accessible through the BarBorder property. The following example will display all bars in blue with yellow border.
VB.NET |
|
bar.BarFillEffect.SetSolidColor(Color.Blue) bar.BarBorder.Color = Color.Yellow
|
C# |
|
bar.BarFillEffect.SetSolidColor(Color.Blue); bar.BarBorder.Color = Color.Yellow; |
Please refer to the Series Appearance topic which describes how to apply individual fillings and lines to the series data points.
Controlling the Bar's Origin
By default all bars use zero as their origin. The user can turn off this behavior by setting the UseOrigin property to false.
VB.NET |
|
bar.UseOrigin = False |
C# |
|
bar.UseOrigin = false; |
If the bars do not use an origin value, they will all begin at the minimum bar value (the minimal bar will be displayed with zero height).
In some cases, the user may want the bars to begin at a certain value, for example 15. The following code will achieve this result:
VB.NET |
|
bar.UseOrigin = True bar.Origin = 15
|
C# |
|
bar.UseOrigin = true; bar.Origin = 15; |
Related Examples
Windows Forms: Series\Bar\Standard Bar
See Also
BarSeries