SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
SciChart Android ships with ~90Â Android Chart Examples which you can browse, play with, view the source-code and even export each SciChart Android Chart Example to a stand-alone Android Studio project. All of this is possible with the new and improved SciChart Android Examples Suite, which ships as part of our Android Charts SDK.
SciChart charts are very popular with scientists and engineers, but also find their application in sales dashboards. Drawing a scatter diagram is the first step in looking for correlation between variables. This example generates a Scatter Chart in code.
The XyScatterRenderableSeries can be used to render an XyDataSeries (which contains one X-point and one Y-point), XyyDataSeries (renders Y values), XyzDataSeries, HlDataSeries and OhlcDataSeries(renders Close values).
The Scatter Chart uses the PointMarker API to define the marker shape and size. This can be customized in code or you can create your own Point-Markers using our SpritePointMarker or DrawablePointMarker type.
Tip!
Perhaps you wanted a scatter point with a line? If so you can do this using the FastLineRenderableSeries and calling the setPointMarker() method.
The full source code for the Android Scatter Chart example is included below (Scroll down!).
Did you know you can also view the source code from one of the following sources as well?
<?xml version="1.0" encoding="utf-8"?><!--*************************************************************************-->
<!-- SCICHART® Copyright SciChart Ltd. 2011-2016. All rights reserved. -->
<!-- -->
<!-- Web: http://www.scichart.com -->
<!-- Support: support@scichart.com -->
<!-- Sales: sales@scichart.com -->
<!-- -->
<!-- example_single_chart_fragment.xml is part of the SCICHART® Examples. Permission is hereby granted -->
<!-- to modify, create derivative works, distribute and publish any part of this source -->
<!-- code whether for commercial, private or personal use. -->
<!-- -->
<!-- The SCICHART® examples are distributed in the hope that they will be useful, but -->
<!-- without any warranty. It is provided "AS IS" without warranty of any kind, either -->
<!-- expressed or implied. -->
<!--*************************************************************************-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.scichart.charting.visuals.SciChartSurface
android:id="@+id/chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2017. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// ScatterChartFragment.java is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
//******************************************************************************
package com.scichart.examples.fragments;
import androidx.annotation.ColorInt;
import android.view.animation.DecelerateInterpolator;
import com.scichart.charting.model.dataSeries.IXyDataSeries;
import com.scichart.charting.modifiers.AxisDragModifierBase.AxisDragMode;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.axes.IAxis;
import com.scichart.charting.visuals.pointmarkers.EllipsePointMarker;
import com.scichart.charting.visuals.pointmarkers.IPointMarker;
import com.scichart.charting.visuals.pointmarkers.TrianglePointMarker;
import com.scichart.charting.visuals.renderableSeries.XyScatterRenderableSeries;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.examples.R;
import com.scichart.examples.fragments.base.ExampleBaseFragment;
import java.util.Collections;
import java.util.Random;
import butterknife.BindView;
public class ScatterChartFragment extends ExampleBaseFragment {
@BindView(R.id.chart)
SciChartSurface surface;
private final Random random = new Random();
@Override
protected int getLayoutId() {
return R.layout.example_single_chart_fragment;
}
@Override
protected void initExample() {
final IAxis xAxis = sciChartBuilder.newNumericAxis().withGrowBy(0.1d, 0.1d).build();
final IAxis yAxis = sciChartBuilder.newNumericAxis().withGrowBy(0.1d, 0.1d).build();
final XyScatterRenderableSeries rSeries1 = getScatterRenderableSeries(new TrianglePointMarker(), 0xFFFFEB01, false);
final XyScatterRenderableSeries rSeries2 = getScatterRenderableSeries(new EllipsePointMarker(), 0xFFffA300, false);
final XyScatterRenderableSeries rSeries3 = getScatterRenderableSeries(new TrianglePointMarker(), 0xFFff6501, true);
final XyScatterRenderableSeries rSeries4 = getScatterRenderableSeries(new EllipsePointMarker(), 0xFFffa300, true);
UpdateSuspender.using(surface, new Runnable() {
@Override
public void run() {
Collections.addAll(surface.getXAxes(), xAxis);
Collections.addAll(surface.getYAxes(), yAxis);
Collections.addAll(surface.getRenderableSeries(), rSeries1, rSeries2, rSeries3, rSeries4);
Collections.addAll(surface.getChartModifiers(), sciChartBuilder.newModifierGroup()
.withZoomExtentsModifier().build()
.withPinchZoomModifier().build()
.withCursorModifier().withReceiveHandledEvents(true).build()
.withXAxisDragModifier().withReceiveHandledEvents(true).build()
.withYAxisDragModifier().withDragMode(AxisDragMode.Pan).build()
.build());
sciChartBuilder.newAnimator(rSeries1).withWaveTransformation().withInterpolator(new DecelerateInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(rSeries2).withWaveTransformation().withInterpolator(new DecelerateInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(rSeries3).withWaveTransformation().withInterpolator(new DecelerateInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(rSeries4).withWaveTransformation().withInterpolator(new DecelerateInterpolator()).withDuration(3000).withStartDelay(350).start();
}
});
}
private XyScatterRenderableSeries getScatterRenderableSeries(IPointMarker pointMarker, @ColorInt int color, boolean negative) {
final String seriesName = pointMarker instanceof EllipsePointMarker ?
negative ? "Negative Ellipse" : "Positive Ellipse" :
negative ? "Negative" : "Positive";
final IXyDataSeries<Integer, Double> dataSeries = sciChartBuilder.newXyDataSeries(Integer.class, Double.class).withSeriesName(seriesName).build();
for (int i = 0; i < 200; i++) {
final double time = (i < 100) ? getRandom(random, 0, i + 10) / 100 : getRandom(random, 0, 200 - i + 10) / 100;
final double y = negative ? -time * time * time : time * time * time;
dataSeries.append(i, y);
}
return sciChartBuilder.newScatterSeries()
.withDataSeries(dataSeries)
.withStrokeStyle(color)
.withPointMarker(sciChartBuilder.newPointMarker(pointMarker)
.withSize(6, 6)
.withStroke(0xFFFFFFFF, 0.1f)
.withFill(color)
.build())
.build();
}
private double getRandom(Random random, double min, double max) {
return min + (max - min) * random.nextDouble();
}
}