Java Code Examples for com.github.mikephil.charting.interfaces.datasets.ILineDataSet#addEntry()

The following examples show how to use com.github.mikephil.charting.interfaces.datasets.ILineDataSet#addEntry() . 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: KLineDataManage.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
public void setOneMaValue(LineData lineData, int i) {
    for (int k = 0; k < lineData.getDataSets().size(); k++) {
        ILineDataSet lineDataSet = lineData.getDataSetByIndex(k);
        lineDataSet.removeEntryByXValue(i);
        if (k == 0) {
            if (i >= N1) {
                sum = 0;
                float all5 = getSum(i - (N1 - 1), i) / N1;
                lineDataSet.addEntry(new Entry(i + offSet, all5));
            }
        } else if (k == 1) {
            if (i >= N2) {
                sum = 0;
                float all10 = getSum(i - (N2 - 1), i) / N2;
                lineDataSet.addEntry(new Entry(i + offSet, all10));
            }
        } else if (k == 2) {
            if (i >= N3) {
                sum = 0;
                float all20 = getSum(i - (N3 - 1), i) / N3;
                lineDataSet.addEntry(new Entry(i + offSet, all20));
            }
        }
    }
}
 
Example 3
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 4
Source File: TimeLineView.java    From android-kline with Apache License 2.0 6 votes vote down vote up
/**
 * according to the price to refresh the last data of the chart
 */
public void refreshData(float price) {
    if (price <= 0 || price == mLastPrice) {
        return;
    }
    mLastPrice = price;
    CombinedData data = mChartPrice.getData();
    if (data == null) return;
    LineData lineData = data.getLineData();
    if (lineData != null) {
        ILineDataSet set = lineData.getDataSetByIndex(0);
        if (set.removeLast()) {
            set.addEntry(new Entry(set.getEntryCount(), price));
        }
    }

    mChartPrice.notifyDataSetChanged();
    mChartPrice.invalidate();
}
 
Example 5
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 6
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 7
Source File: TimeLineView.java    From android-kline with Apache License 2.0 5 votes vote down vote up
public void addData(HisData hisData) {
    hisData = DataUtils.calculateHisData(hisData, mData);
    CombinedData combinedData = mChartPrice.getData();
    LineData priceData = combinedData.getLineData();
    ILineDataSet priceSet = priceData.getDataSetByIndex(0);
    ILineDataSet aveSet = priceData.getDataSetByIndex(1);
    IBarDataSet volSet = mChartVolume.getData().getBarData().getDataSetByIndex(0);
    if (mData.contains(hisData)) {
        int index = mData.indexOf(hisData);
        priceSet.removeEntry(index);
        aveSet.removeEntry(index);
        volSet.removeEntry(index);
        mData.remove(index);
    }
    mData.add(hisData);
    priceSet.addEntry(new Entry(priceSet.getEntryCount(), (float) hisData.getClose()));
    aveSet.addEntry(new Entry(aveSet.getEntryCount(), (float) hisData.getAvePrice()));
    volSet.addEntry(new BarEntry(volSet.getEntryCount(), hisData.getVol(), hisData));

    mChartPrice.setVisibleXRange(MAX_COUNT, MIN_COUNT);
    mChartVolume.setVisibleXRange(MAX_COUNT, MIN_COUNT);

    mChartPrice.getXAxis().setAxisMaximum(combinedData.getXMax() + 1.5f);
    mChartVolume.getXAxis().setAxisMaximum(mChartVolume.getData().getXMax() + 1.5f);

    mChartPrice.notifyDataSetChanged();
    mChartPrice.invalidate();
    mChartVolume.notifyDataSetChanged();
    mChartVolume.invalidate();

    setDescription(mChartVolume, "成交量 " + hisData.getVol());
}
 
Example 8
Source File: TickChart.java    From android-kline with Apache License 2.0 5 votes vote down vote up
public void refreshData(float price) {
    if (price <= 0 || price == mLastPrice) {
        return;
    }
    mLastPrice = price;
    LineData data = mChart.getData();

    if (data != null) {
        ILineDataSet setSell = data.getDataSetByIndex(DATA_SET_PRICE);
        if (setSell == null) {
            setSell = createSet(TYPE_FULL);
            data.addDataSet(setSell);
        }

        data.removeEntry(setSell.getEntryCount(), DATA_SET_PRICE);
        Entry entry = new Entry(setSell.getEntryCount(), price);
        data.addEntry(entry, DATA_SET_PRICE);

        ILineDataSet paddingSet = data.getDataSetByIndex(DATA_SET_PADDING);
        if (paddingSet == null) {
            paddingSet = createSet(TYPE_DASHED);
            data.addDataSet(paddingSet);
        }

        int count = paddingSet.getEntryCount();
        paddingSet.clear();
        for (int i = 0; i < count; i++) {
            paddingSet.addEntry(new Entry(setSell.getEntryCount() + i, price));
        }

        Highlight chartHighlighter = new Highlight(setSell.getEntryCount() + paddingSet.getEntryCount(), price, DATA_SET_PADDING);
        mChart.highlightValue(chartHighlighter);

        data.notifyDataChanged();
        mChart.notifyDataSetChanged();
        mChart.invalidate();


    }
}
 
Example 9
Source File: KLineView.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public void addData(HisData hisData) {
    hisData = DataUtils.calculateHisData(hisData, mData);
    CombinedData combinedData = mChartPrice.getData();
    LineData priceData = combinedData.getLineData();
    ILineDataSet ma5Set = priceData.getDataSetByIndex(1);
    ILineDataSet ma10Set = priceData.getDataSetByIndex(2);
    ILineDataSet ma20Set = priceData.getDataSetByIndex(3);
    ILineDataSet ma30Set = priceData.getDataSetByIndex(4);
    CandleData kData = combinedData.getCandleData();
    ICandleDataSet klineSet = kData.getDataSetByIndex(0);
    IBarDataSet volSet = mChartVolume.getData().getBarData().getDataSetByIndex(0);
    IBarDataSet macdSet = mChartMacd.getData().getBarData().getDataSetByIndex(0);
    ILineDataSet difSet = mChartMacd.getData().getLineData().getDataSetByIndex(0);
    ILineDataSet deaSet = mChartMacd.getData().getLineData().getDataSetByIndex(1);
    LineData kdjData = mChartKdj.getData().getLineData();
    ILineDataSet kSet = kdjData.getDataSetByIndex(0);
    ILineDataSet dSet = kdjData.getDataSetByIndex(1);
    ILineDataSet jSet = kdjData.getDataSetByIndex(2);

    if (mData.contains(hisData)) {
        int index = mData.indexOf(hisData);
        klineSet.removeEntry(index);
        // ma比较特殊,entry数量和k线的不一致,移除最后一个
        ma5Set.removeLast();
        ma10Set.removeLast();
        ma20Set.removeLast();
        ma30Set.removeLast();
        volSet.removeEntry(index);
        macdSet.removeEntry(index);
        difSet.removeEntry(index);
        deaSet.removeEntry(index);
        kSet.removeEntry(index);
        dSet.removeEntry(index);
        jSet.removeEntry(index);
        mData.remove(index);
    }
    mData.add(hisData);
    int klineCount = klineSet.getEntryCount();
    klineSet.addEntry(new CandleEntry(klineCount, (float) hisData.getHigh(), (float) hisData.getLow(), (float) hisData.getOpen(), (float) hisData.getClose()));
    volSet.addEntry(new BarEntry(volSet.getEntryCount(), hisData.getVol(), hisData));

    macdSet.addEntry(new BarEntry(macdSet.getEntryCount(), (float) hisData.getMacd()));
    difSet.addEntry(new Entry(difSet.getEntryCount(), (float) hisData.getDif()));
    deaSet.addEntry(new Entry(deaSet.getEntryCount(), (float) hisData.getDea()));

    kSet.addEntry(new Entry(kSet.getEntryCount(), (float) hisData.getK()));
    dSet.addEntry(new Entry(dSet.getEntryCount(), (float) hisData.getD()));
    jSet.addEntry(new Entry(jSet.getEntryCount(), (float) hisData.getJ()));

    // 因为ma的数量会少,所以这里用kline的set数量作为x
    if (!Double.isNaN(hisData.getMa5())) {
        ma5Set.addEntry(new Entry(klineCount, (float) hisData.getMa5()));
    }
    if (!Double.isNaN(hisData.getMa10())) {
        ma10Set.addEntry(new Entry(klineCount, (float) hisData.getMa10()));
    }
    if (!Double.isNaN(hisData.getMa20())) {
        ma20Set.addEntry(new Entry(klineCount, (float) hisData.getMa20()));
    }
    if (!Double.isNaN(hisData.getMa30())) {
        ma30Set.addEntry(new Entry(klineCount, (float) hisData.getMa30()));
    }


    mChartPrice.getXAxis().setAxisMaximum(combinedData.getXMax() + 1.5f);
    mChartVolume.getXAxis().setAxisMaximum(mChartVolume.getData().getXMax() + 1.5f);
    mChartMacd.getXAxis().setAxisMaximum(mChartMacd.getData().getXMax() + 1.5f);
    mChartKdj.getXAxis().setAxisMaximum(mChartKdj.getData().getXMax() + 1.5f);


    mChartPrice.setVisibleXRange(MAX_COUNT, MIN_COUNT);
    mChartVolume.setVisibleXRange(MAX_COUNT, MIN_COUNT);
    mChartMacd.setVisibleXRange(MAX_COUNT, MIN_COUNT);
    mChartKdj.setVisibleXRange(MAX_COUNT, MIN_COUNT);

    mChartPrice.notifyDataSetChanged();
    mChartPrice.invalidate();
    mChartVolume.notifyDataSetChanged();
    mChartVolume.invalidate();
    mChartMacd.notifyDataSetChanged();
    mChartMacd.invalidate();
    mChartKdj.notifyDataSetChanged();
    mChartKdj.invalidate();


    setDescription(mChartPrice, String.format(Locale.getDefault(), "MA5:%.2f  MA10:%.2f  MA20:%.2f  MA30:%.2f",
            hisData.getMa5(), hisData.getMa10(), hisData.getMa20(), hisData.getMa30()));
    setDescription(mChartVolume, "成交量 " + hisData.getVol());
    setDescription(mChartMacd, String.format(Locale.getDefault(), "MACD:%.2f  DEA:%.2f  DIF:%.2f",
            hisData.getMacd(), hisData.getDea(), hisData.getDif()));
    setDescription(mChartKdj, String.format(Locale.getDefault(), "K:%.2f  D:%.2f  J:%.2f",
            hisData.getK(), hisData.getD(), hisData.getJ()));

}
 
Example 10
Source File: TickChart.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public void addEntry(HisData hisData) {
    hisData = DataUtils.calculateHisData(hisData, mList);
    LineData data = mChart.getData();

    if (data != null) {
        ILineDataSet setSell = data.getDataSetByIndex(DATA_SET_PRICE);
        if (setSell == null) {
            setSell = createSet(TYPE_FULL);
            data.addDataSet(setSell);
        }
        ILineDataSet aveSet = data.getDataSetByIndex(DATA_SET_AVE);
        if (aveSet == null) {
            aveSet = createSet(DATA_SET_AVE);
            data.addDataSet(aveSet);
        }

        int index = mList.indexOf(hisData);
        if (index >= 0) {
            mList.remove(hisData);
            data.removeEntry(index, DATA_SET_PRICE);
            data.removeEntry(index, DATA_SET_AVE);
        }
        mList.add(hisData);
        float price = (float) hisData.getClose();
        data.addEntry(new Entry(setSell.getEntryCount(), price), DATA_SET_PRICE);
        data.addEntry(new Entry(setSell.getEntryCount(), (float) hisData.getAvePrice()), DATA_SET_AVE);

        ILineDataSet paddingSet = data.getDataSetByIndex(DATA_SET_PADDING);
        if (paddingSet == null) {
            paddingSet = createSet(TYPE_DASHED);
            data.addDataSet(paddingSet);
        }

        int count = paddingSet.getEntryCount();

        if (count > PADDING_COUNT && index < 0) {
            count--;
        }
        paddingSet.clear();
        for (int i = 0; i < count; i++) {
            paddingSet.addEntry(new Entry(setSell.getEntryCount() + i, price));
        }

        Highlight chartHighlighter = new Highlight(setSell.getEntryCount() + paddingSet.getEntryCount(), price, DATA_SET_PADDING);
        mChart.highlightValue(chartHighlighter);

        data.notifyDataChanged();
        mChart.notifyDataSetChanged();
        mChart.invalidate();
    }
}