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

The following examples show how to use com.github.mikephil.charting.interfaces.datasets.ICandleDataSet#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: KLineChart.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * 动态更新最后一点数据 最新数据集
 *
 * @param kLineData
 */
public void dynamicsUpdateOne(KLineDataManage kLineData) {
    int size = kLineData.getKLineDatas().size();
    int i = size - 1;
    CombinedData candleChartData = candleChart.getData();
    CandleData candleData = candleChartData.getCandleData();
    ICandleDataSet candleDataSet = candleData.getDataSetByIndex(0);
    candleDataSet.removeEntry(i);

    candleDataSet.addEntry(new CandleEntry(i + kLineData.getOffSet(), (float) kLineData.getKLineDatas().get(i).getHigh(), (float) kLineData.getKLineDatas().get(i).getLow(), (float) kLineData.getKLineDatas().get(i).getOpen(), (float) kLineData.getKLineDatas().get(i).getClose()));
    if (chartType1 == 1) {//副图是成交量
        CombinedData barChartData = barChart.getData();
        IBarDataSet barDataSet = barChartData.getBarData().getDataSetByIndex(0);
        barDataSet.removeEntry(i);
        float color = kLineData.getKLineDatas().get(i).getOpen() == kLineData.getKLineDatas().get(i).getClose()?0f:kLineData.getKLineDatas().get(i).getOpen() > kLineData.getKLineDatas().get(i).getClose() ? -1f : 1f;
        BarEntry barEntry = new BarEntry(i + kLineData.getOffSet(), (float) kLineData.getKLineDatas().get(i).getVolume(), color);
        barDataSet.addEntry(barEntry);
    } else {//副图是其他技术指标
        doBarChartSwitch(chartType1);
    }

    candleChart.notifyDataSetChanged();
    barChart.notifyDataSetChanged();
    candleChart.invalidate();
    barChart.invalidate();
}
 
Example 2
Source File: KLineChart.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
/**
 * 动态增加一个点数据
 *
 * @param kLineData 最新数据集
 */
public void dynamicsAddOne(KLineDataManage kLineData) {
    int size = kLineData.getKLineDatas().size();
    CombinedData candleChartData = candleChart.getData();
    CandleData candleData = candleChartData.getCandleData();
    ICandleDataSet candleDataSet = candleData.getDataSetByIndex(0);
    int i = size - 1;
    candleDataSet.addEntry(new CandleEntry(i + kLineData.getOffSet(), (float) kLineData.getKLineDatas().get(i).getHigh(), (float) kLineData.getKLineDatas().get(i).getLow(), (float) kLineData.getKLineDatas().get(i).getOpen(), (float) kLineData.getKLineDatas().get(i).getClose()));
    kLineData.getxVals().add(DataTimeUtil.secToDate(kLineData.getKLineDatas().get(i).getDateMills()));
    candleChart.getXAxis().setAxisMaximum(kLineData.getKLineDatas().size() < 70 ? 70 : candleChartData.getXMax() + kLineData.getOffSet());

    if (chartType1 == 1) {//副图是成交量
        CombinedData barChartData = barChart.getData();
        IBarDataSet barDataSet = barChartData.getBarData().getDataSetByIndex(0);
        if (barDataSet == null) {//当没有数据时
            return;
        }
        float color = kLineData.getKLineDatas().get(i).getOpen() == kLineData.getKLineDatas().get(i).getClose()?0f:kLineData.getKLineDatas().get(i).getOpen() > kLineData.getKLineDatas().get(i).getClose() ? -1f : 1f;
        BarEntry barEntry = new BarEntry(i + kLineData.getOffSet(), (float) kLineData.getKLineDatas().get(i).getVolume(), color);

        barDataSet.addEntry(barEntry);
        barChart.getXAxis().setAxisMaximum(kLineData.getKLineDatas().size() < 70 ? 70 : barChartData.getXMax() + kLineData.getOffSet());
    } else {//副图是其他技术指标
        doBarChartSwitch(chartType1);
    }

    candleChart.notifyDataSetChanged();
    barChart.notifyDataSetChanged();
    if (kLineData.getKLineDatas().size() > 70) {
        //moveViewTo(...) 方法会自动调用 invalidate()
        candleChart.moveViewToX(kLineData.getKLineDatas().size() - 1);
        barChart.moveViewToX(kLineData.getKLineDatas().size() - 1);
    } else {
        candleChart.invalidate();
        barChart.invalidate();
    }
}
 
Example 3
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()));

}