Java Code Examples for com.github.mikephil.charting.data.LineData#getDataSetByIndex()
The following examples show how to use
com.github.mikephil.charting.data.LineData#getDataSetByIndex() .
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 |
/** * 动态增加一个点数据 * @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 |
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 |
/** * 动态增加一个点数据 * @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: DynamicalAddingActivity.java From StockChart-MPAndroidChart with MIT License | 6 votes |
private void removeLastEntry() { LineData data = chart.getData(); if (data != null) { ILineDataSet set = data.getDataSetByIndex(0); if (set != null) { Entry e = set.getEntryForXValue(set.getEntryCount() - 1, Float.NaN); data.removeEntry(e, 0); // or remove by index // mData.removeEntryByXValue(xIndex, dataSetIndex); data.notifyDataChanged(); chart.notifyDataSetChanged(); chart.invalidate(); } } }
Example 5
Source File: TimeLineView.java From android-kline with Apache License 2.0 | 6 votes |
/** * 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 6
Source File: DynamicalAddingActivity.java From Stayfit with Apache License 2.0 | 5 votes |
private void addEntry() { LineData data = mChart.getData(); if(data != null) { ILineDataSet set = data.getDataSetByIndex(0); // set.addEntry(...); // can be called as well if (set == null) { set = createSet(); data.addDataSet(set); } // add a new x-value first data.addXValue(set.getEntryCount() + ""); // choose a random dataSet int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount()); data.addEntry(new Entry((float) (Math.random() * 10) + 50f, set.getEntryCount()), randomDataSetIndex); // let the chart know it's data has changed mChart.notifyDataSetChanged(); mChart.setVisibleXRangeMaximum(6); mChart.setVisibleYRangeMaximum(15, AxisDependency.LEFT); // // // this automatically refreshes the chart (calls invalidate()) mChart.moveViewTo(data.getXValCount()-7, 50f, AxisDependency.LEFT); } }
Example 7
Source File: RealtimeLineChartActivity.java From Stayfit with Apache License 2.0 | 5 votes |
private void addEntry() { LineData data = mChart.getData(); if (data != null) { ILineDataSet set = data.getDataSetByIndex(0); // set.addEntry(...); // can be called as well if (set == null) { set = createSet(); data.addDataSet(set); } // add a new x-value first data.addXValue(mMonths[data.getXValCount() % 12] + " " + (year + data.getXValCount() / 12)); data.addEntry(new Entry((float) (Math.random() * 40) + 30f, set.getEntryCount()), 0); // let the chart know it's data has changed mChart.notifyDataSetChanged(); // limit the number of visible entries mChart.setVisibleXRangeMaximum(120); // mChart.setVisibleYRange(30, AxisDependency.LEFT); // move to the latest entry mChart.moveViewToX(data.getXValCount() - 121); // this automatically refreshes the chart (calls invalidate()) // mChart.moveViewTo(data.getXValCount()-7, 55f, // AxisDependency.LEFT); } }
Example 8
Source File: FiveDayChart.java From StockChart-MPAndroidChart with MIT License | 5 votes |
/** * 动态更新最后一点数据 * @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 9
Source File: RealtimeLineChartActivity.java From StockChart-MPAndroidChart with MIT License | 5 votes |
private void addEntry() { LineData data = chart.getData(); if (data != null) { ILineDataSet set = data.getDataSetByIndex(0); // set.addEntry(...); // can be called as well if (set == null) { set = createSet(); data.addDataSet(set); } data.addEntry(new Entry(set.getEntryCount(), (float) (Math.random() * 40) + 30f), 0); data.notifyDataChanged(); // let the chart know it's data has changed chart.notifyDataSetChanged(); // limit the number of visible entries chart.setVisibleXRangeMaximum(120); // chart.setVisibleYRange(30, AxisDependency.LEFT); // move to the latest entry chart.moveViewToX(data.getEntryCount()); // this automatically refreshes the chart (calls invalidate()) // chart.moveViewTo(data.getXValCount()-7, 55f, // AxisDependency.LEFT); } }
Example 10
Source File: LineChartRenderer.java From Stayfit with Apache License 2.0 | 5 votes |
@Override public void initBuffers() { LineData lineData = mChart.getLineData(); mLineBuffers = new LineBuffer[lineData.getDataSetCount()]; mCircleBuffers = new CircleBuffer[lineData.getDataSetCount()]; for (int i = 0; i < mLineBuffers.length; i++) { ILineDataSet set = lineData.getDataSetByIndex(i); mLineBuffers[i] = new LineBuffer(set.getEntryCount() * 4 - 4); mCircleBuffers[i] = new CircleBuffer(set.getEntryCount() * 2); } }
Example 11
Source File: MyLineChartRenderer.java From shinny-futures-android with GNU General Public License v3.0 | 5 votes |
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { LineData lineData = mChart.getLineData(); for (Highlight high : indices) { ILineDataSet set = lineData.getDataSetByIndex(high.getDataSetIndex()); if (set == null || !set.isHighlightEnabled()) continue; Entry e = set.getEntryForXValue(high.getX(), high.getY()); if (!isInBoundsX(e, set)) continue; MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), e.getY() * mAnimator.getPhaseY()); float xp = (float) pix.x; mHighlightPaint.setColor(set.getHighLightColor()); mHighlightPaint.setStrokeWidth(set.getHighlightLineWidth()); float xMax = mViewPortHandler.contentRight(); //绘制竖线 c.drawLine(xp, 1, xp, mChart.getHeight(), mHighlightPaint); //判断是否画横线 float y = high.getDrawY(); if (y >= 0 && y <= mViewPortHandler.contentBottom()) {//在区域内即绘制横线 //绘制横线 c.drawLine(0, y, xMax, y, mHighlightPaint); } } }
Example 12
Source File: TimeLineView.java From android-kline with Apache License 2.0 | 5 votes |
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 13
Source File: LineChartRenderer.java From iMoney with Apache License 2.0 | 5 votes |
@Override public void initBuffers() { LineData lineData = mChart.getLineData(); mLineBuffers = new LineBuffer[lineData.getDataSetCount()]; mCircleBuffers = new CircleBuffer[lineData.getDataSetCount()]; for (int i = 0; i < mLineBuffers.length; i++) { LineDataSet set = lineData.getDataSetByIndex(i); mLineBuffers[i] = new LineBuffer(set.getEntryCount() * 4 - 4); mCircleBuffers[i] = new CircleBuffer(set.getEntryCount() * 2); } }
Example 14
Source File: TickChart.java From android-kline with Apache License 2.0 | 5 votes |
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 15
Source File: LineChartRenderer.java From android-kline with Apache License 2.0 | 4 votes |
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { LineData lineData = mChart.getLineData(); for (Highlight high : indices) { ILineDataSet set = lineData.getDataSetByIndex(high.getDataSetIndex()); if (set == null || !set.isHighlightEnabled()) continue; Entry e = set.getEntryForXValue(high.getX(), high.getY()); if (!isInBoundsX(e, set)) continue; MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), e.getY() * mAnimator .getPhaseY()); high.setDraw((float) pix.x, (float) pix.y); // draw the lines drawHighlightLines(c, (float) pix.x, (float) pix.y, set); } }
Example 16
Source File: DynamicalAddingActivity.java From Stayfit with Apache License 2.0 | 4 votes |
private void removeLastEntry() { LineData data = mChart.getData(); if(data != null) { ILineDataSet set = data.getDataSetByIndex(0); if (set != null) { Entry e = set.getEntryForXIndex(set.getEntryCount() - 1); data.removeEntry(e, 0); // or remove by index // mData.removeEntry(xIndex, dataSetIndex); mChart.notifyDataSetChanged(); mChart.invalidate(); } } }
Example 17
Source File: KLineView.java From android-kline with Apache License 2.0 | 4 votes |
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 18
Source File: LineChartRenderer.java From NetKnight with Apache License 2.0 | 4 votes |
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { LineData lineData = mChart.getLineData(); for (Highlight high : indices) { final int minDataSetIndex = high.getDataSetIndex() == -1 ? 0 : high.getDataSetIndex(); final int maxDataSetIndex = high.getDataSetIndex() == -1 ? lineData.getDataSetCount() : (high.getDataSetIndex() + 1); if (maxDataSetIndex - minDataSetIndex < 1) continue; for (int dataSetIndex = minDataSetIndex; dataSetIndex < maxDataSetIndex; dataSetIndex++) { ILineDataSet set = lineData.getDataSetByIndex(dataSetIndex); if (set == null || !set.isHighlightEnabled()) continue; int xIndex = high.getXIndex(); // get the // x-position if (xIndex > mChart.getXChartMax() * mAnimator.getPhaseX()) continue; final float yVal = set.getYValForXIndex(xIndex); if (Float.isNaN(yVal)) continue; float y = yVal * mAnimator.getPhaseY(); // get // the // y-position float[] pts = new float[]{ xIndex, y }; mChart.getTransformer(set.getAxisDependency()).pointValuesToPixel(pts); // draw the lines drawHighlightLines(c, pts, set); } } }
Example 19
Source File: LineChartRenderer.java From android-kline with Apache License 2.0 | 3 votes |
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { LineData lineData = mChart.getLineData(); for (Highlight high : indices) { ILineDataSet set = lineData.getDataSetByIndex(high.getDataSetIndex()); if (set == null || !set.isHighlightEnabled()) continue; Entry e = set.getEntryForXValue(high.getX(), high.getY()); if (!isInBoundsX(e, set)) continue; MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), e.getY() * mAnimator .getPhaseY()); high.setDraw((float) pix.x, (float) pix.y); // draw the lines drawHighlightLines(c, (float) pix.x, (float) pix.y, set); // draw circle // drawCircle(c, high, (float) pix.x); } }
Example 20
Source File: LineChartRenderer.java From StockChart-MPAndroidChart with MIT License | 3 votes |
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { LineData lineData = mChart.getLineData(); for (Highlight high : indices) { ILineDataSet set = lineData.getDataSetByIndex(high.getDataSetIndex()); if (set == null || !set.isHighlightEnabled()) { continue; } Entry e = set.getEntryForXValue(high.getX(), high.getY()); if (!isInBoundsX(e, set)) continue; MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), e.getY() * mAnimator .getPhaseY()); high.setDraw((float) pix.x, (float) pix.y); // draw the lines drawHighlightLines(c, (float) pix.x, (float) pix.y, set); } }