Java Code Examples for com.github.mikephil.charting.data.BarData#notifyDataChanged()

The following examples show how to use com.github.mikephil.charting.data.BarData#notifyDataChanged() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OneDayChart.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * 动态增加一个点数据
 * @param timeDatamodel
 * @param length
 */
public void dynamicsAddOne(TimeDataModel timeDatamodel, int length) {
    int index = length - 1;
    LineData lineData = lineChart.getData();
    ILineDataSet d1 = lineData.getDataSetByIndex(0);
    d1.addEntry(new Entry(index, (float) timeDatamodel.getNowPrice()));
    ILineDataSet d2 = lineData.getDataSetByIndex(1);
    d2.addEntry(new Entry(index, (float) timeDatamodel.getAveragePrice()));

    BarData barData = barChart.getData();
    IBarDataSet barDataSet = barData.getDataSetByIndex(0);
    float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
    barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume(),color));
    lineData.notifyDataChanged();
    lineChart.notifyDataSetChanged();
    barData.notifyDataChanged();
    barChart.notifyDataSetChanged();
    lineChart.setVisibleXRange(maxCount, maxCount);
    barChart.setVisibleXRange(maxCount, maxCount);
    //动态添加或移除数据后, 调用invalidate()刷新图表之前 必须调用 notifyDataSetChanged() .
    lineChart.moveViewToX(index);
    barChart.moveViewToX(index);
}
 
Example 2
Source File: FiveDayChart.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * 动态增加一个点数据
 * @param timeDatamodel
 * @param length
 */
public void dynamicsAddOne(TimeDataModel timeDatamodel, int length) {
    int index = length - 1;
    LineData lineData = lineChart.getData();
    ILineDataSet d1 = lineData.getDataSetByIndex(0);
    d1.addEntry(new Entry( index, (float) timeDatamodel.getNowPrice()));
    ILineDataSet d2 = lineData.getDataSetByIndex(1);
    d2.addEntry(new Entry( index, (float) timeDatamodel.getAveragePrice()));

    BarData barData = barChart.getData();
    IBarDataSet barDataSet = barData.getDataSetByIndex(0);
    float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
    barDataSet.addEntry(new BarEntry( index, timeDatamodel.getVolume(),color));
    lineData.notifyDataChanged();
    lineChart.notifyDataSetChanged();
    barData.notifyDataChanged();
    barChart.notifyDataSetChanged();
    lineChart.setVisibleXRange(maxCount, maxCount);
    barChart.setVisibleXRange(maxCount, maxCount);
    //动态添加或移除数据后, 调用invalidate()刷新图表之前 必须调用 notifyDataSetChanged() .
    lineChart.moveViewToX(index);
    barChart.moveViewToX(index);
}
 
Example 3
Source File: HistogramChart.java    From walt with Apache License 2.0 6 votes vote down vote up
void recalculateDataSet(final BarData barData) {
    minBin = Math.floor(min / binWidth) * binWidth;
    maxBin = Math.floor(max / binWidth) * binWidth;

    int[][] bins = new int[rawData.size()][getNumBins()];

    for (int setNum = 0; setNum < rawData.size(); setNum++) {
        for (Double d : rawData.get(setNum)) {
            ++bins[setNum][(int) (Math.floor((d - minBin) / binWidth))];
        }
    }

    for (int setNum = 0; setNum < barData.getDataSetCount(); setNum++) {
        final IBarDataSet dataSet = barData.getDataSetByIndex(setNum);
        dataSet.clear();
        for (int i = 0; i < bins[setNum].length; i++) {
            dataSet.addEntry(new BarEntry(i, bins[setNum][i]));
        }
    }
    groupBars(barData);
    barData.notifyDataChanged();
}
 
Example 4
Source File: OneDayChart.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
/**
 * 动态更新最后一点数据
 * @param timeDatamodel
 * @param length
 */
public void dynamicsUpdateOne(TimeDataModel timeDatamodel, int length) {
    int index = length - 1;
    LineData lineData = lineChart.getData();
    ILineDataSet d1 = lineData.getDataSetByIndex(0);
    Entry e = d1.getEntryForIndex(index);
    d1.removeEntry(e);
    d1.addEntry(new Entry(index, (float) timeDatamodel.getNowPrice()));

    ILineDataSet d2 = lineData.getDataSetByIndex(1);
    Entry e2 = d2.getEntryForIndex(index);
    d2.removeEntry(e2);
    d2.addEntry(new Entry(index, (float) timeDatamodel.getAveragePrice()));

    BarData barData = barChart.getData();
    IBarDataSet barDataSet = barData.getDataSetByIndex(0);
    barDataSet.removeEntry(index);
    float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
    barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume(),color));

    lineData.notifyDataChanged();
    lineChart.notifyDataSetChanged();
    lineChart.moveViewToX(index);

    barData.notifyDataChanged();
    barChart.notifyDataSetChanged();
    barChart.moveViewToX(index);
}
 
Example 5
Source File: FiveDayChart.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
/**
 * 动态更新最后一点数据
 * @param timeDatamodel
 * @param length
 */
public void dynamicsUpdateOne(TimeDataModel timeDatamodel, int length) {
    int index = length - 1;
    LineData lineData = lineChart.getData();
    ILineDataSet d1 = lineData.getDataSetByIndex(0);
    Entry e = d1.getEntryForIndex(index);
    d1.removeEntry(e);
    d1.addEntry(new Entry( index, (float) timeDatamodel.getNowPrice()));

    ILineDataSet d2 = lineData.getDataSetByIndex(1);
    Entry e2 = d2.getEntryForIndex(index);
    d2.removeEntry(e2);
    d2.addEntry(new Entry( index, (float) timeDatamodel.getAveragePrice()));

    BarData barData = barChart.getData();
    IBarDataSet barDataSet = barData.getDataSetByIndex(0);
    barDataSet.removeEntry(index);
    float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
    barDataSet.addEntry(new BarEntry( index, timeDatamodel.getVolume(),color));

    lineData.notifyDataChanged();
    lineChart.notifyDataSetChanged();
    lineChart.moveViewToX(index);

    barData.notifyDataChanged();
    barChart.notifyDataSetChanged();
    barChart.moveViewToX(index);
}
 
Example 6
Source File: HistogramChart.java    From walt with Apache License 2.0 5 votes vote down vote up
/**
 * Re-implementation of BarData.groupBars(), but allows grouping with only 1 BarDataSet
 * This adjusts the x-coordinates of entries, which centers the bars between axis labels
 */
static void groupBars(final BarData barData) {
    IBarDataSet max = barData.getMaxEntryCountSet();
    int maxEntryCount = max.getEntryCount();
    float groupSpaceWidthHalf = GROUP_SPACE / 2f;
    float barWidthHalf = barData.getBarWidth() / 2f;
    float interval = barData.getGroupWidth(GROUP_SPACE, 0);
    float fromX = 0;

    for (int i = 0; i < maxEntryCount; i++) {
        float start = fromX;
        fromX += groupSpaceWidthHalf;

        for (IBarDataSet set : barData.getDataSets()) {
            fromX += barWidthHalf;
            if (i < set.getEntryCount()) {
                BarEntry entry = set.getEntryForIndex(i);
                if (entry != null) {
                    entry.setX(fromX);
                }
            }
            fromX += barWidthHalf;
        }

        fromX += groupSpaceWidthHalf;
        float end = fromX;
        float innerInterval = end - start;
        float diff = interval - innerInterval;

        // correct rounding errors
        if (diff > 0 || diff < 0) {
            fromX += diff;
        }
    }
    barData.notifyDataChanged();
}
 
Example 7
Source File: FreeHandView.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    BarData exampleData;

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN :
            touchStart(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE :
            touchMove(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP :
            touchUp();
            //toGrayscale(mBitmap);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(mBitmap, mClassifier.getImageSizeX(), mClassifier.getImageSizeY(), true);
            Random rng = new Random();

            try {
                File mFile;
                mFile = this.getContext().getExternalFilesDir(String.valueOf(rng.nextLong() + ".png"));
                FileOutputStream pngFile = new FileOutputStream(mFile);
            }
            catch (Exception e){
            }
            //scaledBitmap.compress(Bitmap.CompressFormat.PNG, 90, pngFile);
            Float prediction = mClassifier.classifyFrame(scaledBitmap);
            exampleData = updateBarEntry();
            barChart.animateY(1000, Easing.EasingOption.EaseOutQuad);
            XAxis xAxis = barChart.getXAxis();
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return xAxisLabel.get((int) value);
                }
            });
            barChart.setData(exampleData);
            exampleData.notifyDataChanged(); // let the data know a dataSet changed
            barChart.notifyDataSetChanged(); // let the chart know it's data changed
            break;
    }

    return true;
}
 
Example 8
Source File: TestCaseRunner.java    From SQLite-Performance with The Unlicense 4 votes vote down vote up
@Override
protected void onProgressUpdate(TestCase.Metrics... values) {
    super.onProgressUpdate(values);
    mDataSetIndex = mChart.getData().getIndexOfDataSet(mDataSet);
    if (mDataSetIndex < 0) {
        mChart.getData().addDataSet(mDataSet);
        mDataSetIndex = mChart.getData().getIndexOfDataSet(mDataSet);
    }

    BarData data = mChart.getData();
    for (TestCase.Metrics m : values) {
        data.addEntry(new BarEntry(m.getVariable(), (float) m.getElapsedTime()), mDataSetIndex);
    }
    if (data.getDataSetCount() > 1) {
        int count = data.getDataSetCount();
        float groupSpace = 0.06f;
        float barSpace = 0.02f;
        float numBars = count;
        float barWidth = (1.0f - ((count - 1) * barSpace) - groupSpace) / numBars;

        Collections.sort(
                data.getDataSets(),
                new Comparator<IBarDataSet>() {
                    @Override
                    public int compare(IBarDataSet o1, IBarDataSet o2) {
                        float[] hsl1 = new float[3];
                        float[] hsl2 = new float[3];
                        ColorUtils.colorToHSL(o1.getColor(), hsl1);
                        ColorUtils.colorToHSL(o2.getColor(), hsl2);

                        if (Math.abs(hsl1[0] - hsl2[0]) > 10) {
                            return Float.compare(hsl1[0], hsl2[0]);
                        }

                        if (Math.abs(hsl1[2] - hsl2[2]) > 0.1) {
                            return Float.compare(hsl1[2], hsl2[2]);
                        }

                        return Float.compare(hsl1[1], hsl2[1]);
                    }
                }
        );

        data.setBarWidth(barWidth);
        data.groupBars(2, groupSpace, barSpace);
    }
    mChart.setFitBars(true);
    data.notifyDataChanged();
    mChart.notifyDataSetChanged();
    mChart.invalidate();
}