Search in sources :

Example 1 with ICandleDataSet

use of com.github.mikephil.charting.interfaces.datasets.ICandleDataSet in project MPAndroidChart by PhilJay.

the class LegendRenderer method computeLegend.

/**
     * Prepares the legend and calculates all needed forms, labels and colors.
     *
     * @param data
     */
public void computeLegend(ChartData<?> data) {
    if (!mLegend.isLegendCustom()) {
        computedEntries.clear();
        // loop for building up the colors and labels used in the legend
        for (int i = 0; i < data.getDataSetCount(); i++) {
            IDataSet dataSet = data.getDataSetByIndex(i);
            List<Integer> clrs = dataSet.getColors();
            int entryCount = dataSet.getEntryCount();
            // if we have a barchart with stacked bars
            if (dataSet instanceof IBarDataSet && ((IBarDataSet) dataSet).isStacked()) {
                IBarDataSet bds = (IBarDataSet) dataSet;
                String[] sLabels = bds.getStackLabels();
                for (int j = 0; j < clrs.size() && j < bds.getStackSize(); j++) {
                    computedEntries.add(new LegendEntry(sLabels[j % sLabels.length], dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), clrs.get(j)));
                }
                if (bds.getLabel() != null) {
                    // add the legend description label
                    computedEntries.add(new LegendEntry(dataSet.getLabel(), Legend.LegendForm.NONE, Float.NaN, Float.NaN, null, ColorTemplate.COLOR_NONE));
                }
            } else if (dataSet instanceof IPieDataSet) {
                IPieDataSet pds = (IPieDataSet) dataSet;
                for (int j = 0; j < clrs.size() && j < entryCount; j++) {
                    computedEntries.add(new LegendEntry(pds.getEntryForIndex(j).getLabel(), dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), clrs.get(j)));
                }
                if (pds.getLabel() != null) {
                    // add the legend description label
                    computedEntries.add(new LegendEntry(dataSet.getLabel(), Legend.LegendForm.NONE, Float.NaN, Float.NaN, null, ColorTemplate.COLOR_NONE));
                }
            } else if (dataSet instanceof ICandleDataSet && ((ICandleDataSet) dataSet).getDecreasingColor() != ColorTemplate.COLOR_NONE) {
                int decreasingColor = ((ICandleDataSet) dataSet).getDecreasingColor();
                int increasingColor = ((ICandleDataSet) dataSet).getIncreasingColor();
                computedEntries.add(new LegendEntry(null, dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), decreasingColor));
                computedEntries.add(new LegendEntry(dataSet.getLabel(), dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), increasingColor));
            } else {
                for (int j = 0; j < clrs.size() && j < entryCount; j++) {
                    String label;
                    // if multiple colors are set for a DataSet, group them
                    if (j < clrs.size() - 1 && j < entryCount - 1) {
                        label = null;
                    } else {
                        // add label to the last entry
                        label = data.getDataSetByIndex(i).getLabel();
                    }
                    computedEntries.add(new LegendEntry(label, dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), clrs.get(j)));
                }
            }
        }
        if (mLegend.getExtraEntries() != null) {
            Collections.addAll(computedEntries, mLegend.getExtraEntries());
        }
        mLegend.setEntries(computedEntries);
    }
    Typeface tf = mLegend.getTypeface();
    if (tf != null)
        mLegendLabelPaint.setTypeface(tf);
    mLegendLabelPaint.setTextSize(mLegend.getTextSize());
    mLegendLabelPaint.setColor(mLegend.getTextColor());
    // calculate all dimensions of the mLegend
    mLegend.calculateDimensions(mLegendLabelPaint, mViewPortHandler);
}
Also used : ICandleDataSet(com.github.mikephil.charting.interfaces.datasets.ICandleDataSet) LegendEntry(com.github.mikephil.charting.components.LegendEntry) IPieDataSet(com.github.mikephil.charting.interfaces.datasets.IPieDataSet) Typeface(android.graphics.Typeface) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) IDataSet(com.github.mikephil.charting.interfaces.datasets.IDataSet) Paint(android.graphics.Paint)

Example 2 with ICandleDataSet

use of com.github.mikephil.charting.interfaces.datasets.ICandleDataSet in project MPAndroidChart by PhilJay.

the class CandleStickChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    // if values are drawn
    if (isDrawingValuesAllowed(mChart)) {
        List<ICandleDataSet> dataSets = mChart.getCandleData().getDataSets();
        for (int i = 0; i < dataSets.size(); i++) {
            ICandleDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
            mXBounds.set(mChart, dataSet);
            float[] positions = trans.generateTransformedValuesCandle(dataSet, mAnimator.getPhaseX(), mAnimator.getPhaseY(), mXBounds.min, mXBounds.max);
            float yOffset = Utils.convertDpToPixel(5f);
            MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
            iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
            iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
            for (int j = 0; j < positions.length; j += 2) {
                float x = positions[j];
                float y = positions[j + 1];
                if (!mViewPortHandler.isInBoundsRight(x))
                    break;
                if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))
                    continue;
                CandleEntry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
                if (dataSet.isDrawValuesEnabled()) {
                    drawValue(c, dataSet.getValueFormatter(), entry.getHigh(), entry, i, x, y - yOffset, dataSet.getValueTextColor(j / 2));
                }
                if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                    Drawable icon = entry.getIcon();
                    Utils.drawImage(c, icon, (int) (x + iconsOffset.x), (int) (y + iconsOffset.y), icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                }
            }
            MPPointF.recycleInstance(iconsOffset);
        }
    }
}
Also used : ICandleDataSet(com.github.mikephil.charting.interfaces.datasets.ICandleDataSet) Transformer(com.github.mikephil.charting.utils.Transformer) CandleEntry(com.github.mikephil.charting.data.CandleEntry) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) Paint(android.graphics.Paint)

Example 3 with ICandleDataSet

use of com.github.mikephil.charting.interfaces.datasets.ICandleDataSet in project MPAndroidChart by PhilJay.

the class RealmDatabaseActivityCandle method setData.

private void setData() {
    RealmResults<RealmDemoData> result = mRealm.where(RealmDemoData.class).findAll();
    RealmCandleDataSet<RealmDemoData> set = new RealmCandleDataSet<RealmDemoData>(result, "xValue", "high", "low", "open", "close");
    set.setLabel("Realm CandleDataSet");
    set.setShadowColor(Color.DKGRAY);
    set.setShadowWidth(0.7f);
    set.setDecreasingColor(Color.RED);
    set.setDecreasingPaintStyle(Paint.Style.FILL);
    set.setIncreasingColor(Color.rgb(122, 242, 84));
    set.setIncreasingPaintStyle(Paint.Style.STROKE);
    set.setNeutralColor(Color.BLUE);
    ArrayList<ICandleDataSet> dataSets = new ArrayList<ICandleDataSet>();
    // add the dataset
    dataSets.add(set);
    // create a data object with the dataset list
    CandleData data = new CandleData(dataSets);
    styleData(data);
    // set data
    mChart.setData(data);
    mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
Also used : ICandleDataSet(com.github.mikephil.charting.interfaces.datasets.ICandleDataSet) CandleData(com.github.mikephil.charting.data.CandleData) ArrayList(java.util.ArrayList) RealmCandleDataSet(com.github.mikephil.charting.data.realm.implementation.RealmCandleDataSet) RealmDemoData(com.xxmassdeveloper.mpchartexample.custom.RealmDemoData)

Example 4 with ICandleDataSet

use of com.github.mikephil.charting.interfaces.datasets.ICandleDataSet in project MPAndroidChart by PhilJay.

the class CandleStickChartRenderer method drawHighlighted.

@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
    CandleData candleData = mChart.getCandleData();
    for (Highlight high : indices) {
        ICandleDataSet set = candleData.getDataSetByIndex(high.getDataSetIndex());
        if (set == null || !set.isHighlightEnabled())
            continue;
        CandleEntry e = set.getEntryForXValue(high.getX(), high.getY());
        if (!isInBoundsX(e, set))
            continue;
        float lowValue = e.getLow() * mAnimator.getPhaseY();
        float highValue = e.getHigh() * mAnimator.getPhaseY();
        float y = (lowValue + highValue) / 2f;
        MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), y);
        high.setDraw((float) pix.x, (float) pix.y);
        // draw the lines
        drawHighlightLines(c, (float) pix.x, (float) pix.y, set);
    }
}
Also used : Highlight(com.github.mikephil.charting.highlight.Highlight) ICandleDataSet(com.github.mikephil.charting.interfaces.datasets.ICandleDataSet) CandleEntry(com.github.mikephil.charting.data.CandleEntry) CandleData(com.github.mikephil.charting.data.CandleData) MPPointD(com.github.mikephil.charting.utils.MPPointD)

Aggregations

ICandleDataSet (com.github.mikephil.charting.interfaces.datasets.ICandleDataSet)4 Paint (android.graphics.Paint)2 CandleData (com.github.mikephil.charting.data.CandleData)2 CandleEntry (com.github.mikephil.charting.data.CandleEntry)2 Typeface (android.graphics.Typeface)1 Drawable (android.graphics.drawable.Drawable)1 LegendEntry (com.github.mikephil.charting.components.LegendEntry)1 RealmCandleDataSet (com.github.mikephil.charting.data.realm.implementation.RealmCandleDataSet)1 Highlight (com.github.mikephil.charting.highlight.Highlight)1 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)1 IDataSet (com.github.mikephil.charting.interfaces.datasets.IDataSet)1 IPieDataSet (com.github.mikephil.charting.interfaces.datasets.IPieDataSet)1 MPPointD (com.github.mikephil.charting.utils.MPPointD)1 MPPointF (com.github.mikephil.charting.utils.MPPointF)1 Transformer (com.github.mikephil.charting.utils.Transformer)1 RealmDemoData (com.xxmassdeveloper.mpchartexample.custom.RealmDemoData)1 ArrayList (java.util.ArrayList)1