Search in sources :

Example 1 with TDoubleArrayList

use of gnu.trove.TDoubleArrayList in project android by JetBrains.

the class SpeedData method convertTrafficsToSpeeds.

private void convertTrafficsToSpeeds(long timestampUs, long sentBytes, long receivedBytes) {
    long lastTimeUs = myTrafficTimeData.get(myTrafficTimeData.size() - 1);
    long lastReceivedSpeed = myReceivedSpeeds.get(myReceivedSpeeds.size() - 1);
    long lastSentSpeed = mySentSpeeds.get(mySentSpeeds.size() - 1);
    double timestampDeltaSec = (timestampUs - lastTimeUs) / SEC_TO_US;
    double receivedTimeDeltaSec = calcTrapezoidMaxWidth(timestampDeltaSec, lastReceivedSpeed, receivedBytes);
    double sentTimeDeltaSec = calcTrapezoidMaxWidth(timestampDeltaSec, lastSentSpeed, sentBytes);
    // TODO: This may be unecessary if we keep a separate timestamp list for sent and received bytes
    TDoubleArrayList timeDeltasSec = new TDoubleArrayList(3);
    timeDeltasSec.add(receivedTimeDeltaSec);
    timeDeltasSec.add(sentTimeDeltaSec);
    timeDeltasSec.add(timestampDeltaSec);
    timeDeltasSec.sort();
    long[] receivedSpeeds = new long[timeDeltasSec.size()];
    long[] sentSpeeds = new long[timeDeltasSec.size()];
    for (int i = 0; i < timeDeltasSec.size(); ++i) {
        double timeDeltaSec = timeDeltasSec.get(i);
        if (timeDeltaSec <= receivedTimeDeltaSec) {
            // Derived from trapezoid's area equation: (a + b) * width / 2 = area, i.e
            // (lastReceivedSpeed + receivedSpeed) * receivedTimeDeltaSec / 2 = receivedBytes
            double receivedSpeed = 2.0 * receivedBytes / receivedTimeDeltaSec - lastReceivedSpeed;
            receivedSpeeds[i] = (long) interpolate(lastReceivedSpeed, receivedSpeed, timeDeltaSec / receivedTimeDeltaSec);
        } else {
            receivedSpeeds[i] = 0;
        }
    }
    for (int i = 0; i < timeDeltasSec.size(); ++i) {
        double timeDeltaSec = timeDeltasSec.get(i);
        if (timeDeltaSec <= sentTimeDeltaSec) {
            // Derived from trapezoid's area equation: (a + b) * width / 2 = area, i.e
            // (lastSentSpeed + sentSpeed) * sentTimeDeltaSec / 2 = sentBytes
            double sentSpeed = 2.0 * sentBytes / sentTimeDeltaSec - lastSentSpeed;
            sentSpeeds[i] = (long) interpolate(lastSentSpeed, sentSpeed, timeDeltaSec / sentTimeDeltaSec);
        } else {
            sentSpeeds[i] = 0;
        }
    }
    for (int i = 0; i < timeDeltasSec.size(); ++i) {
        double timeIntervalSec = timeDeltasSec.get(i);
        if (i > 0 && timeIntervalSec == timeDeltasSec.get(i - 1)) {
            // Eliminate degenerate points on the path
            continue;
        }
        myTrafficTimeData.add((long) (timeIntervalSec * SEC_TO_US + lastTimeUs));
        mySentSpeeds.add(sentSpeeds[i]);
        myReceivedSpeeds.add(receivedSpeeds[i]);
    }
}
Also used : TDoubleArrayList(gnu.trove.TDoubleArrayList)

Example 2 with TDoubleArrayList

use of gnu.trove.TDoubleArrayList in project android by JetBrains.

the class LineChart method postAnimate.

@Override
public void postAnimate() {
    long duration = System.nanoTime();
    int p = 0;
    // Store the Y coordinates of the last stacked series to use them to increment the Y values
    // of the current stacked series.
    TDoubleArrayList lastStackedSeriesY = null;
    Deque<Path2D> orderedPaths = new ArrayDeque<>(myLinesConfig.size());
    Deque<LineConfig> orderedConfigs = new ArrayDeque<>(myLinesConfig.size());
    for (Map.Entry<RangedContinuousSeries, LineConfig> lineConfig : myLinesConfig.entrySet()) {
        final RangedContinuousSeries ranged = lineConfig.getKey();
        final LineConfig config = lineConfig.getValue();
        // Stores the y coordinates of the current series in case it's used as a stacked series
        final TDoubleArrayList currentSeriesY = new TDoubleArrayList();
        Path2D path = new Path2D.Float();
        double xMin = ranged.getXRange().getMin();
        double xMax = ranged.getXRange().getMax();
        double yMin = ranged.getYRange().getMin();
        double yMax = ranged.getYRange().getMax();
        // X coordinate of the first point
        double firstXd = 0f;
        List<SeriesData<Long>> seriesList = ranged.getSeries();
        for (int i = 0; i < seriesList.size(); i++) {
            // TODO: refactor to allow different types (e.g. double)
            SeriesData<Long> seriesData = seriesList.get(i);
            long currX = seriesData.x;
            long currY = seriesData.value;
            double xd = (currX - xMin) / (xMax - xMin);
            double yd = (currY - yMin) / (yMax - yMin);
            // prior iteration). In this case, yd of the current series shouldn't change.
            if (config.isStacked() && lastStackedSeriesY != null && i < lastStackedSeriesY.size()) {
                yd += lastStackedSeriesY.get(i);
            }
            currentSeriesY.add(yd);
            // Swing's (0, 0) coordinate is in top-left. As we use bottom-left (0, 0), we need to adjust the y coordinate.
            float adjustedYd = 1 - (float) yd;
            if (i == 0) {
                path.moveTo(xd, adjustedYd);
                firstXd = xd;
            } else {
                // drawing a line to the destination point itself (e.g. (x1, y1)).
                if (config.isStepped()) {
                    float y = (float) path.getCurrentPoint().getY();
                    path.lineTo(xd, y);
                }
                path.lineTo(xd, adjustedYd);
            }
        }
        if (config.isFilled() && path.getCurrentPoint() != null) {
            // If the chart is filled, but not stacked, draw a line from the last point to X
            // axis and another one from this new point to the first destination point.
            path.lineTo(path.getCurrentPoint().getX(), 1f);
            path.lineTo(firstXd, 1f);
        }
        if (config.isStacked()) {
            lastStackedSeriesY = currentSeriesY;
        }
        if (config.isFilled()) {
            // Draw the filled lines first, otherwise other lines won't be visible.
            // Also, to draw stacked and filled lines correctly, they need to be drawn in reverse order to their adding order.
            orderedPaths.addFirst(path);
            orderedConfigs.addFirst(config);
        } else {
            orderedPaths.addLast(path);
            orderedConfigs.addLast(config);
        }
        addDebugInfo("Range[%d] Max: %.2f", p, xMax);
        p++;
    }
    myLinePaths.clear();
    myLinePaths.addAll(orderedPaths);
    myLinePathConfigs.clear();
    myLinePathConfigs.addAll(orderedConfigs);
    addDebugInfo("postAnimate time: %d ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - duration));
}
Also used : Path2D(java.awt.geom.Path2D) RangedContinuousSeries(com.android.tools.adtui.model.RangedContinuousSeries) SeriesData(com.android.tools.adtui.model.SeriesData) TDoubleArrayList(gnu.trove.TDoubleArrayList)

Example 3 with TDoubleArrayList

use of gnu.trove.TDoubleArrayList in project intellij-community by JetBrains.

the class JsonReaders method readDoubleArray.

public static double[] readDoubleArray(JsonReaderEx reader) {
    checkIsNull(reader, null);
    reader.beginArray();
    if (!reader.hasNext()) {
        reader.endArray();
        return new double[] { 0 };
    }
    TDoubleArrayList result = new TDoubleArrayList();
    do {
        result.add(reader.nextDouble());
    } while (reader.hasNext());
    reader.endArray();
    return result.toNativeArray();
}
Also used : TDoubleArrayList(gnu.trove.TDoubleArrayList)

Aggregations

TDoubleArrayList (gnu.trove.TDoubleArrayList)3 RangedContinuousSeries (com.android.tools.adtui.model.RangedContinuousSeries)1 SeriesData (com.android.tools.adtui.model.SeriesData)1 Path2D (java.awt.geom.Path2D)1