Java Code Examples for com.github.mikephil.charting.data.BarEntry

The following examples show how to use com.github.mikephil.charting.data.BarEntry. These examples are extracted from open source projects. 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 Project: iMoney   Source File: BarChartActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 生成柱状图的数据
 */
private BarData generateDataBar() {

    ArrayList<BarEntry> entries = new ArrayList<BarEntry>();

    for (int i = 0; i < 12; i++) {
        entries.add(new BarEntry((int) (Math.random() * 70) + 30, i));
    }

    BarDataSet d = new BarDataSet(entries, "New DataSet ");
    // 设置柱状图之间的间距
    d.setBarSpacePercent(20f);
    // 设置显示的柱状图的颜色
    d.setColors(ColorTemplate.VORDIPLOM_COLORS);
    // 设置高亮的透明度:当点击柱状图时显示的透明度
    d.setHighLightAlpha(255);

    BarData cd = new BarData(getMonths(), d);
    return cd;
}
 
Example 2
/**
 * date: 6/1/18
 * author: chenli
 * description: K线图刷新时生成单个数据
 */
private void generateCandleAndLineDataEntry(int left_index, int index) {
    Map<String, KlineEntity.DataEntity> dataEntities = mKlineEntity.getData();
    KlineEntity.DataEntity dataEntity = dataEntities.get(String.valueOf(index));
    if (dataEntity == null) return;
    mCalendar.setTimeInMillis(Long.valueOf(dataEntity.getDatetime()) / 1000000);
    xVals.put(index - mBaseIndex, mSimpleDateFormat.format(mCalendar.getTime()));

    List<Entry> entries = generateMultiDataEntry(index, dataEntity, left_index);
    mTopChartViewBase.getCandleData().getDataSetByIndex(0).addEntryOrdered((CandleEntry) entries.get(0));
    mMiddleChartViewBase.getLineData().getDataSetByIndex(0).addEntryOrdered(entries.get(1));
    mMiddleChartViewBase.getBarData().getDataSetByIndex(0).addEntryOrdered((BarEntry) entries.get(2));
    mBottomChartViewBase.getLineData().getDataSetByIndex(0).addEntryOrdered(entries.get(3));
    mBottomChartViewBase.getLineData().getDataSetByIndex(1).addEntryOrdered(entries.get(4));
    mBottomChartViewBase.getBarData().getDataSetByIndex(0).addEntryOrdered((BarEntry) entries.get(5));

    for (int i = 0; i < mas.size(); i++) {
        int para = mas.get(i);
        if (index >= left_index + para - 1) {
            Entry entry = generateMALineDataEntry(index, para - 1);
            mLineData.getDataSetByIndex(i).addEntryOrdered(entry);
        }
    }
}
 
Example 3
Source Project: memorize   Source File: StatisticActivity.java    License: MIT License 6 votes vote down vote up
private BarData generateDataBar(int cnt) {

        ArrayList<BarEntry> entries = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            entries.add(new BarEntry(i, (int) (Math.random() * 70) + 30));
        }

        BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);
        d.setColors(ColorTemplate.VORDIPLOM_COLORS);
        d.setHighLightAlpha(255);

        BarData cd = new BarData(d);
        cd.setBarWidth(0.9f);
        return cd;
    }
 
Example 4
Source Project: MPAndroidChart-Realm   Source File: RealmBarDataSet.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void calcMinMax(BarEntry e) {

    if (e != null && !Float.isNaN(e.getY())) {

        if (e.getYVals() == null) {

            if (e.getY() < mYMin)
                mYMin = e.getY();

            if (e.getY() > mYMax)
                mYMax = e.getY();
        } else {

            if (-e.getNegativeSum() < mYMin)
                mYMin = -e.getNegativeSum();

            if (e.getPositiveSum() > mYMax)
                mYMax = e.getPositiveSum();
        }

        calcMinMaxX(e);
    }
}
 
Example 5
Source Project: StockChart-MPAndroidChart   Source File: StackedValueFormatter.java    License: MIT License 6 votes vote down vote up
@Override
public String getBarStackedLabel(float value, BarEntry entry) {
    if (!mDrawWholeStack) {

        float[] vals = entry.getYVals();

        if (vals != null) {

            // find out if we are on top of the stack
            if (vals[vals.length - 1] == value) {

                // return the "sum" across all stack values
                return mFormat.format(entry.getY()) + mSuffix;
            } else {
                return ""; // return empty
            }
        }
    }

    // return the "proposed" value
    return mFormat.format(value) + mSuffix;
}
 
Example 6
Source Project: Notification-Analyser   Source File: BarChart.java    License: MIT License 6 votes vote down vote up
/**
 * Returns the bounding box of the specified Entry in the specified DataSet.
 * Returns null if the Entry could not be found in the charts data.
 * 
 * @param e
 * @param dataSetIndex
 * @return
 */
public RectF getBarBounds(BarEntry e) {

    BarDataSet set = mOriginalData.getDataSetForEntry(e);

    if (set == null)
        return null;

    float barspace = set.getBarSpace();
    float y = e.getVal();
    float x = e.getXIndex();

    float spaceHalf = barspace / 2f;
    float left = x + spaceHalf;
    float right = x + 1f - spaceHalf;
    float top = y >= 0 ? y : 0;
    float bottom = y <= 0 ? y : 0;

    RectF bounds = new RectF(left, top, right, bottom);

    transformRect(bounds);

    return bounds;
}
 
Example 7
Source Project: StockChart-MPAndroidChart   Source File: OneDayChart.java    License: 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 8
Source Project: StockChart-MPAndroidChart   Source File: KLineDataManage.java    License: MIT License 6 votes vote down vote up
/**
 * 初始化自己计算MACD
 */
public void initMACD() {
    MACDEntity macdEntity = new MACDEntity(getKLineDatas(), SHORT, LONG, M);

    macdData = new ArrayList<>();
    deaData = new ArrayList<>();
    difData = new ArrayList<>();
    for (int i = 0; i < macdEntity.getMACD().size(); i++) {
        macdData.add(new BarEntry(i + offSet, macdEntity.getMACD().get(i), macdEntity.getMACD().get(i)));
        deaData.add(new Entry(i + offSet, macdEntity.getDEA().get(i)));
        difData.add(new Entry(i + offSet, macdEntity.getDIF().get(i)));
    }
    barDataMACD = setABar(macdData);
    lineDataMACD.add(setALine(ColorType.blue, deaData));
    lineDataMACD.add(setALine(ColorType.yellow, difData));
}
 
Example 9
Source Project: Stayfit   Source File: BarChartActivitySinus.java    License: Apache License 2.0 6 votes vote down vote up
private void setData(int count) {

        ArrayList<String> xVals = new ArrayList<String>();
        
        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
        
        for (int i = 0; i < count; i++) {
            xVals.add(i+"");
            entries.add(mSinusData.get(i));
        }
        
        BarDataSet set = new BarDataSet(entries, "Sinus Function");
        set.setBarSpacePercent(40f);
        set.setColor(Color.rgb(240, 120, 124));

        BarData data = new BarData(xVals, set);
        data.setValueTextSize(10f);
        data.setValueTypeface(mTf);
        data.setDrawValues(false);

        mChart.setData(data);
    }
 
Example 10
Source Project: Stayfit   Source File: ListViewBarChartActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * generates a random ChartData object with just one DataSet
 * 
 * @return
 */
private BarData generateData(int cnt) {

    ArrayList<BarEntry> entries = new ArrayList<BarEntry>();

    for (int i = 0; i < 12; i++) {
        entries.add(new BarEntry((int) (Math.random() * 70) + 30, i));
    }

    BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);    
    d.setBarSpacePercent(20f);
    d.setColors(ColorTemplate.VORDIPLOM_COLORS);
    d.setBarShadowColor(Color.rgb(203, 203, 203));
    
    ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>();
    sets.add(d);
    
    BarData cd = new BarData(getMonths(), sets);
    return cd;
}
 
Example 11
Source Project: StockChart-MPAndroidChart   Source File: SimpleFragment.java    License: MIT License 6 votes vote down vote up
protected BarData generateBarData(int dataSets, float range, int count) {

        ArrayList<IBarDataSet> sets = new ArrayList<>();

        for(int i = 0; i < dataSets; i++) {

            ArrayList<BarEntry> entries = new ArrayList<>();

            for(int j = 0; j < count; j++) {
                entries.add(new BarEntry(j, (float) (Math.random() * range) + range / 4));
            }

            BarDataSet ds = new BarDataSet(entries, getLabel(i));
            ds.setColors(ColorTemplate.VORDIPLOM_COLORS);
            sets.add(ds);
        }

        BarData d = new BarData(sets);
        d.setValueTypeface(tf);
        return d;
    }
 
Example 12
/**
 * generates a random ChartData object with just one DataSet
 *
 * @return Bar data
 */
private BarData generateData(int cnt) {

    ArrayList<BarEntry> entries = new ArrayList<>();

    for (int i = 0; i < 12; i++) {
        entries.add(new BarEntry(i, (float) (Math.random() * 70) + 30));
    }

    BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);
    d.setColors(ColorTemplate.VORDIPLOM_COLORS);
    d.setBarShadowColor(Color.rgb(203, 203, 203));

    ArrayList<IBarDataSet> sets = new ArrayList<>();
    sets.add(d);

    BarData cd = new BarData(sets);
    cd.setBarWidth(0.9f);
    return cd;
}
 
Example 13
Source Project: StockChart-MPAndroidChart   Source File: StackedBarsMarkerView.java    License: MIT License 6 votes vote down vote up
@Override
public void refreshContent(Entry e, Highlight highlight) {

    if (e instanceof BarEntry) {

        BarEntry be = (BarEntry) e;

        if(be.getYVals() != null) {

            // draw the stack value
            tvContent.setText(Utils.formatNumber(be.getYVals()[highlight.getStackIndex()], 0, true));
        } else {
            tvContent.setText(Utils.formatNumber(be.getY(), 0, true));
        }
    } else {

        tvContent.setText(Utils.formatNumber(e.getY(), 0, true));
    }

    super.refreshContent(e, highlight);
}
 
Example 14
Source Project: MPAndroidChart-Realm   Source File: RealmBarDataSet.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public BarEntry buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    if (dynamicObject.getFieldType(mYValuesField) == RealmFieldType.LIST) {

        RealmList<DynamicRealmObject> list = dynamicObject.getList(mYValuesField);
        float[] values = new float[list.size()];

        int i = 0;
        for (DynamicRealmObject o : list) {
            values[i] = o.getFloat(mStackValueFieldName);
            i++;
        }

        return new BarEntry(
                mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField), values);
    } else {
        float value = dynamicObject.getFloat(mYValuesField);
        return new BarEntry(mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField), value);
    }
}
 
Example 15
Source Project: StockChart-MPAndroidChart   Source File: ScrollViewActivity.java    License: MIT License 6 votes vote down vote up
private void setData(int count) {

        ArrayList<BarEntry> values = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * count) + 15;
            values.add(new BarEntry(i, (int) val));
        }

        BarDataSet set = new BarDataSet(values, "Data Set");
        set.setColors(ColorTemplate.VORDIPLOM_COLORS);
        set.setDrawValues(false);

        BarData data = new BarData(set);

        chart.setData(data);
        chart.invalidate();
        chart.animateY(800);
    }
 
Example 16
@Override
public void onValueSelected(Entry e, Highlight h) {

    if (e == null)
        return;

    RectF bounds = mOnValueSelectedRectF;
    chart.getBarBounds((BarEntry) e, bounds);

    MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex())
            .getAxisDependency());

    Log.i("bounds", bounds.toString());
    Log.i("position", position.toString());

    MPPointF.recycleInstance(position);
}
 
Example 17
Source Project: Ticket-Analysis   Source File: BarChart.java    License: MIT License 6 votes vote down vote up
/**
 * The passed outputRect will be assigned the values of the bounding box of the specified Entry in the specified DataSet.
 * The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data.
 *
 * @param e
 * @return
 */
public void getBarBounds(BarEntry e, RectF outputRect) {

    RectF bounds = outputRect;

    IBarDataSet set = mData.getDataSetForEntry(e);

    if (set == null) {
        bounds.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
        return;
    }

    float y = e.getY();
    float x = e.getX();

    float barWidth = mData.getBarWidth();

    float left = x - barWidth / 2f;
    float right = x + barWidth / 2f;
    float top = y >= 0 ? y : 0;
    float bottom = y <= 0 ? y : 0;

    bounds.set(left, top, right, bottom);

    getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect);
}
 
Example 18
Source Project: Ticket-Analysis   Source File: HorizontalBarChart.java    License: MIT License 6 votes vote down vote up
@Override
public void getBarBounds(BarEntry e, RectF outputRect) {

    RectF bounds = outputRect;
    IBarDataSet set = mData.getDataSetForEntry(e);

    if (set == null) {
        outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
        return;
    }

    float y = e.getY();
    float x = e.getX();

    float barWidth = mData.getBarWidth();

    float top = x - barWidth / 2f;
    float bottom = x + barWidth / 2f;
    float left = y >= 0 ? y : 0;
    float right = y <= 0 ? y : 0;

    bounds.set(left, top, right, bottom);

    getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);

}
 
Example 19
Source Project: FaceT   Source File: ProductDetailActivity.java    License: Mozilla Public License 2.0 6 votes vote down vote up
public BarData getBarData() {

        ArrayList<BarEntry> entries = new ArrayList<>();
        float overall_people = 100f;

        Log.d(TAG + "barData", barRatingCount[3] + "");
        entries.add(new BarEntry(4, barRatingCount[4]));
        entries.add(new BarEntry(3, barRatingCount[3]));
        entries.add(new BarEntry(2, barRatingCount[2]));
        entries.add(new BarEntry(1, barRatingCount[1]));
        entries.add(new BarEntry(0, barRatingCount[0]));

        BarDataSet dataset = new BarDataSet(entries, "");
        dataset.setColors(CUSTOM_COLOR);
        dataset.setDrawValues(false);

        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(dataset);

        BarData data = new BarData(dataSets);
//        data.setValueTextSize(10f);
//        data.setValueTypeface(fontType);
        data.setBarWidth(1f);

        return data;
    }
 
Example 20
Source Project: NetKnight   Source File: BarChart.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be
 * found in the charts data.
 * 
 * @param e
 * @return
 */
public RectF getBarBounds(BarEntry e) {

	IBarDataSet set = mData.getDataSetForEntry(e);

	if (set == null)
		return null;

	float barspace = set.getBarSpace();
	float y = e.getVal();
	float x = e.getXIndex();

	float barWidth = 0.5f;

	float spaceHalf = barspace / 2f;
	float left = x - barWidth + spaceHalf;
	float right = x + barWidth - spaceHalf;
	float top = y >= 0 ? y : 0;
	float bottom = y <= 0 ? y : 0;

	RectF bounds = new RectF(left, top, right, bottom);

	getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);

	return bounds;
}
 
Example 21
Source Project: Ticket-Analysis   Source File: AvgAnalysisActivity.java    License: MIT License 6 votes vote down vote up
private List<BarEntry> generateEntry(List<TicketOpenData> list, int codeLength) {
    float[] valuesCount = new float[codeLength];
    for (int j = 0; j < list.size(); j++) {
        Pair<String[], String[]> sPair = translateCodeToList(list.get(j).openCode);
        String[] values = ArrayUtil.concat(sPair.first, sPair.second);
        for (int k = 0; k < codeLength; k++) {
            try {
                valuesCount[k] += Float.valueOf(values[k]);//非数字直接抛异常
            } catch (Exception e) {
                e.printStackTrace();
                valuesCount[k] += 0;
            }
        }
    }
    List<BarEntry> barEntries = new ArrayList<>();
    float count = 0;
    for (int i = 0; i < codeLength - 1; i++) {
        count += valuesCount[i] / list.size();
        barEntries.add(new BarEntry(i, valuesCount[i] / list.size()));
    }
    barEntries.add(new BarEntry(codeLength - 1, count));
    return barEntries;
}
 
Example 22
Source Project: android-kline   Source File: HorizontalBarChart.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void getBarBounds(BarEntry e, RectF outputRect) {

    RectF bounds = outputRect;
    IBarDataSet set = mData.getDataSetForEntry(e);

    if (set == null) {
        outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
        return;
    }

    float y = e.getY();
    float x = e.getX();

    float barWidth = mData.getBarWidth();

    float top = x - barWidth / 2f;
    float bottom = x + barWidth / 2f;
    float left = y >= 0 ? y : 0;
    float right = y <= 0 ? y : 0;

    bounds.set(left, top, right, bottom);

    getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);

}
 
Example 23
Source Project: android-kline   Source File: StackedValueFormatter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

    if (!mDrawWholeStack && entry instanceof BarEntry) {

        BarEntry barEntry = (BarEntry) entry;
        float[] vals = barEntry.getYVals();

        if (vals != null) {

            // find out if we are on top of the stack
            if (vals[vals.length - 1] == value) {

                // return the "sum" across all stack values
                return mFormat.format(barEntry.getY()) + mAppendix;
            } else {
                return ""; // return empty
            }
        }
    }

    // return the "proposed" value
    return mFormat.format(value) + mAppendix;
}
 
Example 24
Source Project: Stayfit   Source File: StackedValueFormatter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

    if (!mDrawWholeStack && entry instanceof BarEntry) {

        BarEntry barEntry = (BarEntry) entry;
        float[] vals = barEntry.getVals();

        if (vals != null) {

            // find out if we are on top of the stack
            if (vals[vals.length - 1] == value) {

                // return the "sum" across all stack values
                return mFormat.format(barEntry.getVal()) + mAppendix;
            } else {
                return ""; // return empty
            }
        }
    }

    // return the "proposed" value
    return mFormat.format(value) + mAppendix;
}
 
Example 25
Source Project: android-kline   Source File: TimeLineView.java    License: Apache License 2.0 6 votes vote down vote up
private void initChartVolumeData() {
    ArrayList<BarEntry> barEntries = new ArrayList<>();
    ArrayList<BarEntry> paddingEntries = new ArrayList<>();
    for (int i = 0; i < mData.size(); i++) {
        HisData t = mData.get(i);
        barEntries.add(new BarEntry(i, (float) t.getVol(), t));
    }
    int maxCount = MAX_COUNT;
    if (!mData.isEmpty() && mData.size() < maxCount) {
        for (int i = mData.size(); i < maxCount; i++) {
            paddingEntries.add(new BarEntry(i, 0));
        }
    }

    BarData barData = new BarData(setBar(barEntries, NORMAL_LINE), setBar(paddingEntries, INVISIABLE_LINE));
    barData.setBarWidth(0.75f);
    CombinedData combinedData = new CombinedData();
    combinedData.setData(barData);
    mChartVolume.setData(combinedData);

    mChartVolume.setVisibleXRange(MAX_COUNT, MIN_COUNT);

    mChartVolume.notifyDataSetChanged();
    mChartVolume.moveViewToX(combinedData.getEntryCount());

}
 
Example 26
Source Project: AndroidDatabaseLibraryComparison   Source File: MainActivity.java    License: MIT License 6 votes vote down vote up
private void initChart() {
    ArrayList<BarDataSet> dataSets = new ArrayList<>();
    // note that we show save first because that's how we initialize the DB
    for (String frameworkName : chartEntrySets.keySet()) {
        ArrayList<BarEntry> entrySet = chartEntrySets.get(frameworkName);
        BarDataSet dataSet = new BarDataSet(entrySet, frameworkName);
        dataSet.setColor(getFrameworkColor(frameworkName));
        dataSets.add(dataSet);
    }
    // load data and animate it
    ArrayList<String> xAxisLabels = new ArrayList<>();
    xAxisLabels.add("Save (msec)");
    xAxisLabels.add("Load (msec)");
    BarData data = new BarData(xAxisLabels, dataSets);
    chartView.setData(data);
    chartView.setDescription(null); // this takes up too much space, so clear it
    chartView.animateXY(2000, 2000);
    chartView.invalidate();
}
 
Example 27
Source Project: Stayfit   Source File: HorizontalBarChartActivity.java    License: Apache License 2.0 6 votes vote down vote up
private void setData(int count, float range) {

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        ArrayList<String> xVals = new ArrayList<String>();

        for (int i = 0; i < count; i++) {
            xVals.add(mMonths[i % 12]);
            yVals1.add(new BarEntry((float) (Math.random() * range), i));
        }

        BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");

        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);

        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(tf);

        mChart.setData(data);
    }
 
Example 28
/**
 * date: 6/1/18
 * author: chenli
 * description: 分时图初始化时添加数据
 */
private List<Entry> generateMultiDataEntry(int index, KlineEntity.DataEntity dataEntity) {
    List<Entry> entries = new ArrayList<>();
    String close = dataEntity.getClose();
    Float closeF = Float.valueOf(close);
    String volume = dataEntity.getVolume();
    Float volumeF = Float.valueOf(volume);
    String oi = dataEntity.getClose_oi();
    Float oiF = Float.valueOf(oi);
    Float cv = volumeF * closeF;
    mSumVolume += volumeF;
    mSumVolumeMap.put(index, volumeF);
    mSumCV += cv;
    mSumCVMap.put(index, cv);
    float average = mSumVolume != 0 ? mSumCV / mSumVolume : 0;
    float emas = closeF;
    float emal = closeF;
    float dif = 0;
    float dea = 0;
    float macd = 0;
    if (index != mTradingDayStartIndex){
        emas = mEMAs.get(index - 1) * (mS - 1) / ( mS + 1) + closeF * 2 / (mS + 1);
        emal = mEMAl.get(index - 1) * (mL - 1) / ( mL + 1) + closeF * 2 / (mL + 1);
        dif = emas - emal;
        dea = mDEA.get(index - 1) * (mN - 1) / (mN + 1) + dif * 2 / (mN + 1);
        macd = (dif - dea) * 2;
    }
    mEMAs.put(index, emas);
    mEMAl.put(index, emal);
    mDEA.put(index, dea);
    entries.add(new Entry(index, closeF));
    entries.add(new Entry(index, average));
    entries.add(new Entry(index, oiF));
    entries.add(new BarEntry(index, volumeF));
    entries.add(new Entry(index, dif));
    entries.add(new Entry(index, dea));
    entries.add(new BarEntry(index, macd, macd));
    return entries;
}
 
Example 29
/**
 * date: 2019/7/30
 * author: chenli
 * description: 重新计算MACD
 */
private void reGenerateMACDEntries(int left_index, int last_index) {
    mBottomChartViewBase.getLineData().getDataSetByIndex(0).clear();
    mBottomChartViewBase.getLineData().getDataSetByIndex(1).clear();
    mBottomChartViewBase.getBarData().getDataSetByIndex(0).clear();

    for (int i = left_index; i <= last_index; i++) {
        Map<String, KlineEntity.DataEntity> dataEntities = mKlineEntity.getData();
        KlineEntity.DataEntity dataEntity = dataEntities.get(String.valueOf(i));
        if (dataEntity == null) continue;
        float close = Float.valueOf(dataEntity.getClose());
        float emas = close;
        float emal = close;
        float dif = 0;
        float dea = 0;
        float macd = 0;
        if (i != left_index && mEMAs.get(i - mBaseIndex - 1) != null) {
            emas = mEMAs.get(i - mBaseIndex - 1) * (mS - 1) / (mS + 1) + close * 2 / (mS + 1);
            emal = mEMAl.get(i - mBaseIndex - 1) * (mL - 1) / (mL + 1) + close * 2 / (mL + 1);
            dif = emas - emal;
            dea = mDEA.get(i - mBaseIndex - 1) * (mN - 1) / (mN + 1) + dif * 2 / (mN + 1);
            macd = (dif - dea) * 2;
        }
        mEMAs.put(i - mBaseIndex, emas);
        mEMAl.put(i - mBaseIndex, emal);
        mDEA.put(i - mBaseIndex, dea);
        mBottomChartViewBase.getLineData().getDataSetByIndex(0).addEntry(new Entry(i - mBaseIndex, dif));
        mBottomChartViewBase.getLineData().getDataSetByIndex(1).addEntry(new Entry(i - mBaseIndex, dea));
        mBottomChartViewBase.getBarData().getDataSetByIndex(0).addEntry(new BarEntry(i - mBaseIndex, macd, macd));
    }

}
 
Example 30
/**
 * date: 6/1/18
 * author: chenli
 * description: K线图初始化时生成单个数据
 */
private List<Entry> generateMultiDataEntry(int i, KlineEntity.DataEntity dataEntity, int left_index) {
    List<Entry> entries = new ArrayList<>();
    mCalendar.setTimeInMillis(Long.valueOf(dataEntity.getDatetime()) / 1000000);
    xVals.put(i - mBaseIndex, mSimpleDateFormat.format(mCalendar.getTime()));
    float high = Float.valueOf(dataEntity.getHigh());
    float low = Float.valueOf(dataEntity.getLow());
    float open = Float.valueOf(dataEntity.getOpen());
    float close = Float.valueOf(dataEntity.getClose());
    float volume = Float.valueOf(dataEntity.getVolume());
    float oi = Float.valueOf(dataEntity.getClose_oi());
    float sub = open - close;
    float emas = close;
    float emal = close;
    float dif = 0;
    float dea = 0;
    float macd = 0;
    if (i != left_index && mEMAs.get(i - mBaseIndex - 1) != null) {
        emas = mEMAs.get(i - mBaseIndex - 1) * (mS - 1) / (mS + 1) + close * 2 / (mS + 1);
        emal = mEMAl.get(i - mBaseIndex - 1) * (mL - 1) / (mL + 1) + close * 2 / (mL + 1);
        dif = emas - emal;
        dea = mDEA.get(i - mBaseIndex - 1) * (mN - 1) / (mN + 1) + dif * 2 / (mN + 1);
        macd = (dif - dea) * 2;
    }
    mEMAs.put(i - mBaseIndex, emas);
    mEMAl.put(i - mBaseIndex, emal);
    mDEA.put(i - mBaseIndex, dea);
    entries.add(new CandleEntry(i - mBaseIndex, high, low, open, close));
    entries.add(new Entry(i - mBaseIndex, oi));
    entries.add(new BarEntry(i - mBaseIndex, volume, sub));
    entries.add(new Entry(i - mBaseIndex, dif));
    entries.add(new Entry(i - mBaseIndex, dea));
    entries.add(new BarEntry(i - mBaseIndex, macd, macd));
    return entries;
}