SciChart Android 2D Charts API > SciChart Android Basics > Chart Builders Helper Classes
Chart Builders Helper Classes

To simplify creation and configuring of a SciChartSurface, SciChart Android provides a bunch of helper classes. They follow the XXXBuilder naming pattern and allow to initialize virtually anything in an easier and more convenient way. Different propert of classes can be preset via the withXXX(…) methods of the helpers, a.k.a. "chart builders".

 

Referencing SciChart Builders

SciChart builder classes provided as seperate library which is called 'extensions'. If you want to use then you'll need to add it into your project as dependency. This is available from our Maven feed, or in the Android Charts SDK at www.scichart.com/downloads.

Adding extension library
Copy Code
dependencies {
     // Default module dependencies
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:23.4.0'
     // Declare SciChart libraries as module dependencies
     compile (name:'charting-release', ext:'aar')
     compile (name:'drawing-release', ext:'aar')
     compile (name:'data-release', ext:'aar')
     compile (name:'core-release', ext:'aar'
     // Declare SciChart extension library as module dependencies
     compile (name:'extensions-release', ext:'aar')
 }

Using Chart Builders in Code

The main chart builder class is called SciChartBuilder. It acts as a factory of builders of other types, representing different chart aspects.

Using chart builders, the code snippet for chart initialization from the section above can be rewritten as follows:

Example
Copy Code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout from “base_scichart_layout.xml”
    View view = inflater.inflate(R.layout.base_scichart_layout, container, false);

    // find the surface by id
    SciChartSurface surface = (SciChartSurface) view.findViewById(R.id.chart);
        
    // need to init SciChartBuilder class with application context before using
    SciChartBuilder.init(this);
    // obtain the SciChartBuilder instance
    final SciChartBuilder sciChartBuilder = SciChartBuilder.instance();

    // create numeric X axis
    final IAxis xAxis = sciChartBuilder.newNumericAxis()
            .withAxisTitle("X Axis Title")
            .withVisibleRange(-5, 15)
            .build();

    // create a numeric Y axis
    final IAxis yAxis = sciChartBuilder.newNumericAxis()
            .withAxisTitle("Y Axis Title").withVisibleRange(0, 100).build();

    // create a TextAnnotation and specify the inscription and position for it
    TextAnnotation textAnnotation = sciChartBuilder.newTextAnnotation()
            .withX1(5.0)
            .withY1(2.5)
            .withText("Hello World!")
            .withFontStyle(10, ColorUtil.White)
            .build();

    // create interactivity modifiers
    ModifierGroup chartModifiers = sciChartBuilder.newModifierGroupWithDefaultModifiers().build();

    // add the Y axis to the YAxes collection of the surface
    Collections.addAll(surface.getYAxes(), yAxis);

    // add the X axis to the XAxes collection of the surface
    Collections.addAll(surface.getXAxes(), xAxis);

    // add the annotation to the Annotations collection of the surface
    Collections.addAll(surface.getAnnotations(), textAnnotation);

    // add the interactions to the ChartModifiers collection of the surface
    Collections.addAll(surface.getChartModifiers(), chartModifiers);

    return view;
}

Usually this approach to chart initialization results in more concise and elegent code. It is preferred for the vast majority of examples from the Android Examples Suite. We would recommend using it in applications too. However, it doesn't give any other benefits except those stated above, so the standard way of objects initialization is equally fine.

See Also