Search in sources :

Example 31 with BarDataSet

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

the class ScrollViewActivity method setData.

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);
}
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 32 with BarDataSet

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

the class StackedBarActivityNegative method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.viewGithub:
            {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java"));
                startActivity(i);
                break;
            }
        case R.id.actionToggleValues:
            {
                List<IBarDataSet> sets = chart.getData().getDataSets();
                for (IBarDataSet iSet : sets) {
                    BarDataSet set = (BarDataSet) iSet;
                    set.setDrawValues(!set.isDrawValuesEnabled());
                }
                chart.invalidate();
                break;
            }
        case R.id.actionToggleIcons:
            {
                List<IBarDataSet> sets = chart.getData().getDataSets();
                for (IBarDataSet iSet : sets) {
                    BarDataSet set = (BarDataSet) iSet;
                    set.setDrawIcons(!set.isDrawIconsEnabled());
                }
                chart.invalidate();
                break;
            }
        case R.id.actionToggleHighlight:
            {
                if (chart.getData() != null) {
                    chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
                    chart.invalidate();
                }
                break;
            }
        case R.id.actionTogglePinch:
            {
                if (chart.isPinchZoomEnabled())
                    chart.setPinchZoom(false);
                else
                    chart.setPinchZoom(true);
                chart.invalidate();
                break;
            }
        case R.id.actionToggleAutoScaleMinMax:
            {
                chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
                chart.notifyDataSetChanged();
                break;
            }
        case R.id.actionToggleBarBorders:
            {
                for (IBarDataSet set : chart.getData().getDataSets()) ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
                chart.invalidate();
                break;
            }
        case R.id.animateX:
            {
                chart.animateX(3000);
                break;
            }
        case R.id.animateY:
            {
                chart.animateY(3000);
                break;
            }
        case R.id.animateXY:
            {
                chart.animateXY(3000, 3000);
                break;
            }
        case R.id.actionSave:
            {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    saveToGallery();
                } else {
                    requestStoragePermission(chart);
                }
                break;
            }
    }
    return true;
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) Intent(android.content.Intent) ArrayList(java.util.ArrayList) List(java.util.List)

Example 33 with BarDataSet

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

the class StackedBarActivity 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 mul = (seekBarY.getProgress() + 1);
        float val1 = (float) (Math.random() * mul) + mul / 3;
        float val2 = (float) (Math.random() * mul) + mul / 3;
        float val3 = (float) (Math.random() * mul) + mul / 3;
        values.add(new BarEntry(i, new float[] { val1, val2, val3 }, getResources().getDrawable(R.drawable.star)));
    }
    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, "Statistics Vienna 2014");
        set1.setDrawIcons(false);
        set1.setColors(getColors());
        set1.setStackLabels(new String[] { "Births", "Divorces", "Marriages" });
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueFormatter(new MyValueFormatter());
        data.setValueTextColor(Color.WHITE);
        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) MyValueFormatter(com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter)

Example 34 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project TumCampusApp by TCA-Team.

the class GradesActivity method showBarChart.

private void showBarChart(List<Exam> exams) {
    pieChart.setVisibility(View.GONE);
    barChart.setVisibility(View.VISIBLE);
    Map<String, Integer> gradeCount = calculateGradeDistribution(exams);
    List<BarEntry> entries = new ArrayList<>();
    for (int i = 0; i < GRADES.length; i++) {
        entries.add(new BarEntry(i, gradeCount.get(GRADES[i])));
    }
    BarDataSet set = new BarDataSet(entries, getString(R.string.grades_without_weight));
    set.setColors(GRADE_COLORS, this);
    BarData data = new BarData(set);
    barChart.setData(data);
    barChart.setFitBars(true);
    XAxis xAxis = barChart.getXAxis();
    xAxis.setGranularity(1);
    xAxis.setValueFormatter((value, axis) -> GRADES[(int) value]);
    barChart.setDescription(null);
    barChart.invalidate();
}
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) XAxis(com.github.mikephil.charting.components.XAxis)

Example 35 with BarDataSet

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

the class BarChart method drawData.

@Override
protected void drawData() {
    ArrayList<BarDataSet> dataSets = mData.getDataSets();
    int setCount = mData.getDataSetCount();
    // the space between bar-groups
    float space = mData.getGroupSpace();
    // 2D drawing
    for (int i = 0; i < setCount; i++) {
        BarDataSet dataSet = dataSets.get(i);
        boolean noStacks = dataSet.getStackSize() == 1 ? true : false;
        ArrayList<BarEntry> entries = dataSet.getYVals();
        // do the drawing
        for (int j = 0; j < dataSet.getEntryCount() * mPhaseX; j++) {
            BarEntry e = entries.get(j);
            // calculate the x-position, depending on datasetcount
            float x = e.getXIndex() + j * (setCount - 1) + i + space * j + space / 2f;
            float y = e.getVal();
            // no stacks
            if (noStacks) {
                prepareBar(x, y, dataSet.getBarSpace());
                // avoid drawing outofbounds values
                if (isOffContentRight(mBarRect.left))
                    break;
                if (isOffContentLeft(mBarRect.right)) {
                    continue;
                }
                // if drawing the bar shadow is enabled
                if (mDrawBarShadow) {
                    mRenderPaint.setColor(dataSet.getBarShadowColor());
                    mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
                }
                // Set the color for the currently drawn value. If the index
                // is
                // out of bounds, reuse colors.
                mRenderPaint.setColor(dataSet.getColor(j));
                mDrawCanvas.drawRect(mBarRect, mRenderPaint);
            } else {
                // stacked bars
                float[] vals = e.getVals();
                // in between
                if (vals == null) {
                    prepareBar(x, y, dataSet.getBarSpace());
                    // if drawing the bar shadow is enabled
                    if (mDrawBarShadow) {
                        mRenderPaint.setColor(dataSet.getBarShadowColor());
                        mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
                    }
                    mRenderPaint.setColor(dataSet.getColor(0));
                    mDrawCanvas.drawRect(mBarRect, mRenderPaint);
                } else {
                    float all = e.getVal();
                    // if drawing the bar shadow is enabled
                    if (mDrawBarShadow) {
                        prepareBar(x, y, dataSet.getBarSpace());
                        mRenderPaint.setColor(dataSet.getBarShadowColor());
                        mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
                    }
                    // draw the stack
                    for (int k = 0; k < vals.length; k++) {
                        all -= vals[k];
                        prepareBar(x, vals[k] + all, dataSet.getBarSpace());
                        mRenderPaint.setColor(dataSet.getColor(k));
                        mDrawCanvas.drawRect(mBarRect, mRenderPaint);
                    }
                }
                // avoid drawing outofbounds values
                if (isOffContentRight(mBarRect.left))
                    break;
            }
        }
    }
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) 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