Search in sources :

Example 16 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.

the class AnotherBarActivity method onProgressChanged.

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    tvX.setText(String.valueOf(seekBarX.getProgress()));
    tvY.setText(String.valueOf(seekBarY.getProgress()));
    ArrayList<BarEntry> values = new ArrayList<>();
    for (int i = 0; i < seekBarX.getProgress(); i++) {
        float multi = (seekBarY.getProgress() + 1);
        float val = (float) (Math.random() * multi) + multi / 3;
        values.add(new BarEntry(i, val));
    }
    BarDataSet set1;
    if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
        set1.setValues(values);
        chart.getData().notifyDataChanged();
        chart.notifyDataSetChanged();
    } else {
        set1 = new BarDataSet(values, "Data Set");
        set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
        set1.setDrawValues(false);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        chart.setData(data);
        chart.setFitBars(true);
    }
    chart.invalidate();
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 17 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.

the class BarDataTest method testGroupBars.

@Test
public void testGroupBars() {
    float groupSpace = 5f;
    float barSpace = 1f;
    List<BarEntry> values1 = new ArrayList<>();
    List<BarEntry> values2 = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        values1.add(new BarEntry(i, 50));
        values2.add(new BarEntry(i, 60));
    }
    BarDataSet barDataSet1 = new BarDataSet(values1, "Set1");
    BarDataSet barDataSet2 = new BarDataSet(values2, "Set2");
    BarData data = new BarData(barDataSet1, barDataSet2);
    data.setBarWidth(10f);
    float groupWidth = data.getGroupWidth(groupSpace, barSpace);
    assertEquals(27f, groupWidth, 0.01f);
    assertEquals(0f, values1.get(0).getX(), 0.01f);
    assertEquals(1f, values1.get(1).getX(), 0.01f);
    data.groupBars(1000, groupSpace, barSpace);
    // 1000 + 2.5 + 0.5 + 5
    assertEquals(1008f, values1.get(0).getX(), 0.01f);
    assertEquals(1019f, values2.get(0).getX(), 0.01f);
    assertEquals(1035f, values1.get(1).getX(), 0.01f);
    assertEquals(1046f, values2.get(1).getX(), 0.01f);
    data.groupBars(-1000, groupSpace, barSpace);
    assertEquals(-992f, values1.get(0).getX(), 0.01f);
    assertEquals(-981f, values2.get(0).getX(), 0.01f);
    assertEquals(-965f, values1.get(1).getX(), 0.01f);
    assertEquals(-954f, values2.get(1).getX(), 0.01f);
    data.setBarWidth(20f);
    groupWidth = data.getGroupWidth(groupSpace, barSpace);
    assertEquals(47f, groupWidth, 0.01f);
    data.setBarWidth(10f);
    data.groupBars(-20, groupSpace, barSpace);
    assertEquals(-12f, values1.get(0).getX(), 0.01f);
    assertEquals(-1f, values2.get(0).getX(), 0.01f);
    assertEquals(15f, values1.get(1).getX(), 0.01f);
    assertEquals(26f, values2.get(1).getX(), 0.01f);
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry) Test(org.junit.Test)

Example 18 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project Gadgetbridge by Freeyourgadget.

the class LiveActivityFragment method setupCommonChart.

private BarDataSet setupCommonChart(CustomBarChart chart, BarEntry entry, String title) {
    chart.setSinglAnimationEntry(entry);
    // chart.getXAxis().setPosition(XAxis.XAxisPosition.TOP);
    chart.getXAxis().setDrawLabels(false);
    chart.getXAxis().setEnabled(false);
    chart.getXAxis().setTextColor(CHART_TEXT_COLOR);
    chart.getAxisLeft().setTextColor(CHART_TEXT_COLOR);
    chart.setBackgroundColor(BACKGROUND_COLOR);
    chart.getDescription().setTextColor(DESCRIPTION_COLOR);
    chart.getDescription().setText(title);
    // chart.setNoDataTextDescription("");
    chart.setNoDataText("");
    chart.getAxisRight().setEnabled(false);
    List<BarEntry> entries = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();
    entries.add(entry);
    colors.add(akActivity.color);
    colors.add(akActivity.color);
    colors.add(akActivity.color);
    // //we don't want labels
    // xLabels.add("");
    // xLabels.add("");
    // xLabels.add("");
    BarDataSet set = new BarDataSet(entries, "");
    set.setDrawValues(false);
    set.setColors(colors);
    BarData data = new BarData(set);
    // data.setGroupSpace(0);
    chart.setData(data);
    chart.getLegend().setEnabled(false);
    return set;
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 19 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project Gadgetbridge by Freeyourgadget.

the class SpeedZonesFragment method refreshStats.

private MySpeedZonesData refreshStats(List<? extends ActivitySample> samples) {
    ActivityAnalysis analysis = new ActivityAnalysis();
    analysis.calculateActivityAmounts(samples);
    BarData data = new BarData();
    data.setValueTextColor(CHART_TEXT_COLOR);
    List<BarEntry> entries = new ArrayList<>();
    ActivityUser user = new ActivityUser();
    for (Map.Entry<Integer, Long> entry : analysis.stats.entrySet()) {
        entries.add(new BarEntry(entry.getKey(), entry.getValue() / 60));
    }
    BarDataSet set = new BarDataSet(entries, "");
    set.setValueTextColor(CHART_TEXT_COLOR);
    set.setColors(getColorFor(ActivityKind.TYPE_ACTIVITY));
    // set.setDrawValues(false);
    // data.setBarWidth(0.1f);
    data.addDataSet(set);
    return new MySpeedZonesData(data);
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData) ArrayList(java.util.ArrayList) ActivityUser(nodomain.freeyourgadget.gadgetbridge.model.ActivityUser) BarEntry(com.github.mikephil.charting.data.BarEntry) Map(java.util.Map)

Example 20 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project carat by amplab.

the class BarChart method drawValues.

@Override
protected void drawValues() {
    // if values are drawn
    if (mDrawYValues && mData.getYValCount() < mMaxVisibleCount * mTrans.getScaleX()) {
        ArrayList<BarDataSet> dataSets = ((BarData) mData).getDataSets();
        float posOffset = 0f;
        float negOffset = 0f;
        // calculate the correct offset depending on the draw position of
        // the value
        posOffset = getPositiveYOffset(mDrawValueAboveBar);
        negOffset = getNegativeYOffset(mDrawValueAboveBar);
        for (int i = 0; i < mData.getDataSetCount(); i++) {
            BarDataSet dataSet = dataSets.get(i);
            ArrayList<BarEntry> entries = dataSet.getYVals();
            float[] valuePoints = mTrans.generateTransformedValuesBarChart(entries, i, mData, mPhaseY);
            // if only single values are drawn (sum)
            if (!mDrawValuesForWholeStack) {
                for (int j = 0; j < valuePoints.length * mPhaseX; j += 2) {
                    if (isOffContentRight(valuePoints[j]))
                        break;
                    if (isOffContentLeft(valuePoints[j]) || isOffContentTop(valuePoints[j + 1]) || isOffContentBottom(valuePoints[j + 1]))
                        continue;
                    float val = entries.get(j / 2).getVal();
                    drawValue(val, valuePoints[j], valuePoints[j + 1] + (val >= 0 ? posOffset : negOffset));
                }
            // if each value of a potential stack should be drawn
            } else {
                for (int j = 0; j < (valuePoints.length - 1) * mPhaseX; j += 2) {
                    if (isOffContentRight(valuePoints[j]))
                        break;
                    if (isOffContentLeft(valuePoints[j]) || isOffContentTop(valuePoints[j + 1]) || isOffContentBottom(valuePoints[j + 1]))
                        continue;
                    BarEntry e = entries.get(j / 2);
                    float[] vals = e.getVals();
                    // in between
                    if (vals == null) {
                        drawValue(e.getVal(), valuePoints[j], valuePoints[j + 1] + (e.getVal() >= 0 ? posOffset : negOffset));
                    } else {
                        float[] transformed = new float[vals.length * 2];
                        int cnt = 0;
                        float add = e.getVal();
                        for (int k = 0; k < transformed.length; k += 2) {
                            add -= vals[cnt];
                            transformed[k + 1] = (vals[cnt] + add) * mPhaseY;
                            cnt++;
                        }
                        mTrans.pointValuesToPixel(transformed);
                        for (int k = 0; k < transformed.length; k += 2) {
                            drawValue(vals[k / 2], valuePoints[j], transformed[k + 1] + (vals[k / 2] >= 0 ? posOffset : negOffset));
                        }
                    }
                }
            }
        }
    }
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint)

Aggregations

BarDataSet (com.github.mikephil.charting.data.BarDataSet)41 ArrayList (java.util.ArrayList)27 BarEntry (com.github.mikephil.charting.data.BarEntry)25 BarData (com.github.mikephil.charting.data.BarData)24 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)22 Intent (android.content.Intent)8 Paint (android.graphics.Paint)5 List (java.util.List)4 SuppressLint (android.annotation.SuppressLint)2 XAxis (com.github.mikephil.charting.components.XAxis)2 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)2 IDataSet (com.github.mikephil.charting.interfaces.datasets.IDataSet)2 Context (android.content.Context)1 Path (android.graphics.Path)1 PointF (android.graphics.PointF)1 RectF (android.graphics.RectF)1 BarChart (com.github.mikephil.charting.charts.BarChart)1 AxisBase (com.github.mikephil.charting.components.AxisBase)1 Legend (com.github.mikephil.charting.components.Legend)1 LimitLine (com.github.mikephil.charting.components.LimitLine)1