.netCHARTING v10.2 Documentation Send comments on this topic.
Shortcuts and Efficient Code
Getting Started > General Tutorials > Shortcuts and Efficient Code

Glossary Item Box

Introduction

The .netCHARTING API has been designed to allow users to dramatically cut down on the amount of code necessary to perform common tasks. This topic will demonstrate how many multi-task operations can be done very easily, usually with a single line of code. If you spend much of your time working with charts please read on.

 

Labels

With the addition of the label markup language, it is possible to apply styling to labels without having to set those style explicitly through label properties. It can be done inline while text is specified for those labels. For example, to make a label reading 'My Chart' bold, you can use '<block fStyle='bold'>MyChart'.

 

Defaults

One of the most time saving features are the property defaults. These allow you to set properties of all similar objects with a single line of code. For example, a chart can have a large number of series and elements. To quickly set any of those series or element properties a single line of code can be used as shown here:

[C#]

Chart.DefaultSeries.Type = SeriesType.Line;
Chart.DefaultElement.ShowValue = true;
[Visual Basic]

Chart.DefaultSeries.Type = SeriesType.Line
Chart.DefaultElement.ShowValue = True

Not every single objects of the same type has to bet set using defaults. They can also be set selectively for specific groups. For example, all elements of only a particular series can be set without changing element in other series using the following code:

[C#]

mySeries.DefaultElement.ShowValue = true;
[Visual Basic]

mySeries.DefaultElement.ShowValue = True

This is a list of default properties that can be used to quickly specify settings:

Adding Styled Annotations

Because annotations are often used as buttons and may require specific styling to be set for each of those buttons, a new constructor has beed added. It can take a default annotatation object and will automatically take on the property settings of that annotation. This way all the style specific settings can be specified once and used for any other new annotations. Sample AnnotationConstruction.aspx demonstrates this. 

Selective Element Defaults 

Using default series or elements can drastically improve programming efficiency, however, they dont help when only certain elements of a particular series that fulfill some criteria need to be modified. For this task a new method 'SelectiveElementDefaults' available at the series and seriescollection level is provided. This method is related to the Series.Trim method as far as specifying which elements are affected. For more information on the trim method, see the DataManipulation tutorial. This method works like the trim method except, instead of cutting those elements out, it takes the properties of a specified element and applies them to the specified range of elements. The specified element is just a new element instance with the settings you want to propagate to the range of elements.

[C#]

Element myDefaultElement = new Element();
myDefaultElement.ShowValue = true;
myDefaultElement.Outline.Color = Color.White;
myDefaultElement.SmartLabel.GlowColor = Color.White; mySC.SelectiveElementDefaults(new ScaleRange(30, 50), ElementValue.YValue, myDefaultElement);

[Visual Basic]

Dim myDefaultElement As New Element() myDefaultElement.ShowValue = True
myDefaultElement.Outline.Color = Color.White
myDefaultElement.SmartLabel.GlowColor = Color.White
mySC.SelectiveElementDefaults(New ScaleRange(30, 50), ElementValue.YValue, myDefaultElement)

 

Sample:

  • SelectiveDefaults.aspx
  • SelectiveDefaultsInclusive.aspx
  • SelectiveDefaultsSC.aspx

Common Collection Construction

Many collection objects in .netCHARTING have methods that make it much faster and easier to create them. Let's take a look at some of these features using the ChartAreaCollection object.

Constructor
public ChartAreaCollection(params ChartArea[] chartAreaList)

This constructor allows you to create a collection with a number of chart areas using a single line of code:

[C#]

ChartAreaCollection cac = new ChartAreaCollection(ca1, ca2, ca3, ca4);
[Visual Basic]

Dim cac As New ChartAreaCollection(ca1, ca2, ca3, ca4)

Add Methods

public void Add(params ChartArea[] chartAreaList)

Objects can be added to existing collections in the same manner as the constructor

[C#]

Chart.ExtraChartAreas.Add(ca1, ca2, ca3, ca4);
[Visual Basic]

Chart.ExtraChartAreas.Add(ca1, ca2, ca3, ca4)

public void Add(ChartAreaCollection chartAreaCollection)

Adding a collection of chart areas to another chart area collection can be done like so:

[C#]

Chart.ExtraChartAreas.Add(myChartAreaCollection);
[Visual Basic]

Chart.ExtraChartAreas.Add(myChartAreaCollection)

Almost all collections contain these shortcut members.

 

Collection Type Specific Construction & Features

Some collections also have shortcut constructors that are specific to the type of data they represent.

AxisTickCollection

public static AxisTickCollection FromValues(params DateTime[] list)
public static AxisTickCollection FromValues(params double[] list)

This static constructor allows you to add axis ticks with specific values using a single line of code.

[C#]

Chart.YAxis.ClearValues = true;
Chart.YAxis.ExtraTicks.Add(AxisTickCollection.FromValues(1,2,3,4,5,6);
[Visual Basic]

Chart.YAxis.ClearValues = True
Chart.YAxis.ExtraTicks.Add(AxisTickCollection.FromValues(1,2,3,4,5,6)
Notice: The ExtraTicks.Add method is taking an AxisTickCollection.


Series

The Series object is not a collection. The elements are contained in the Series.Elements ElementCollection property. There are, however, features available that allow managing a series' elements simpler. Series can be populated with elements quickly using the FromYValues constructor.

public static Series FromYValues(params double[] yValues)

[C#]

Series s = Series.FromYValues(1,5,6,34,2);
[Visual Basic]

Dim s As Series = Series.FromYValues(1, 5, 6, 34, 2)


The following method allows constructing a series from a list of elements.

public Series(string name, params Element[] elementList)

[C#]

Series s = new Series("mySeries", element1, element2,element3, element4);
[Visual Basic]

Dim s As New Series("mySeries", element1, element2,element3, element4)

There is one more series feature worth noting. As mentioned above, the series is not a collection. Nevertheless, its elements can be accessed easily using the series' indexer:

[C#]

Element myElement = mySeries[1];
// Equivalent to:
Element myElement = mySeries.Elements[1];
[Visual Basic]

Dim myElement As Element = mySeries(1)
' Equivalent to:
Dim myElement As Element = mySeries.Elements(1)

Using these collection object shortcuts will allow you to write code much faster and spend more time focusing on your application logic.

 

©2021. All Rights Reserved.