Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class Downsample
- Object
- Downsample
Largest-Triangle-Three-Buckets downsampling (Steinarsson, 2013): picks the points of a long series that keep its visual shape, so a million samples can be handed to a chart as a few thousand.
A LineSeries already draws only the highest and lowest point of every pixel column, so it
stays fast without this. Use it to cut memory and the cost of animating, hit testing and
tooltips on very large data:
double[] x = null;
double[] y = new double[100000];
for (int i = 0; i < y.length; i++) {
y[i] = Math.sin(i / 500.0);
}
LineSeries series = new LineSeries("Signal");
int[] keep = Downsample.lttb(x, y, 2000);
// pick(x, keep) is the kept indices when x is null, so the points keep their positions
series.setData(Downsample.pick(x, keep), Downsample.pick(y, keep));Methods
public static int[] lttb(double[] x, double[] y, int threshold) | The indices of the points LTTB keeps, ascending; the first and last are always kept. |
public static double[] pick(double[] values, int[] indices) | The values at the given indices. |
Inherited methods
Method details
lttb
public static int[] lttb(double[] x, double[] y, int threshold)The indices of the points LTTB keeps, ascending; the first and last are always kept.
A NaN value is a gap. Gaps never take part in the triangle areas or bucket averages. A
bucket that contains one keeps its first NaN index as a gap marker, so the gap survives
downsampling rather than being bridged, and also keeps its best real point, so a sparse
NaN does not cost the bucket its peak. The result therefore has threshold indices when
the data has no gaps, and never more than 2 * threshold - 2.
Parameters
xdouble[]- the x values, ascending; null means the index
ydouble[]- the y values
thresholdint- how many points to keep, at least 3
Returns
pick
public static double[] pick(double[] values, int[] indices)Parameters
valuesdouble[]- the full array; null stands for an index based x, so the result is the kept indices themselves as doubles and the points keep their original x positions
indicesint[]- what
lttbreturned