Search in sources :

Example 26 with BarDataSet

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

the class BarChartPositiveNegative method setData.

private void setData(List<Data> dataList) {
    ArrayList<BarEntry> values = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();
    int green = Color.rgb(110, 190, 102);
    int red = Color.rgb(211, 74, 88);
    for (int i = 0; i < dataList.size(); i++) {
        Data d = dataList.get(i);
        BarEntry entry = new BarEntry(d.xValue, d.yValue);
        values.add(entry);
        // specific colors
        if (d.yValue >= 0)
            colors.add(red);
        else
            colors.add(green);
    }
    BarDataSet set;
    if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
        set = (BarDataSet) chart.getData().getDataSetByIndex(0);
        set.setValues(values);
        chart.getData().notifyDataChanged();
        chart.notifyDataSetChanged();
    } else {
        set = new BarDataSet(values, "Values");
        set.setColors(colors);
        set.setValueTextColors(colors);
        BarData data = new BarData(set);
        data.setValueTextSize(13f);
        data.setValueTypeface(tfRegular);
        data.setValueFormatter(new ValueFormatter());
        data.setBarWidth(0.8f);
        chart.setData(data);
        chart.invalidate();
    }
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData) ArrayList(java.util.ArrayList) BarData(com.github.mikephil.charting.data.BarData) IValueFormatter(com.github.mikephil.charting.formatter.IValueFormatter) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 27 with BarDataSet

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

the class HorizontalBarChartActivity method setData.

private void setData(int count, float range) {
    float barWidth = 9f;
    float spaceForBar = 10f;
    ArrayList<BarEntry> values = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        float val = (float) (Math.random() * range);
        values.add(new BarEntry(i * spaceForBar, val, 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, "DataSet 1");
        set1.setDrawIcons(false);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(tfLight);
        data.setBarWidth(barWidth);
        chart.setData(data);
    }
}
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 28 with BarDataSet

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

the class HorizontalBarNegativeChartActivity 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/HorizontalBarChartActivity.java"));
                startActivity(i);
                break;
            }
        case R.id.actionToggleValues:
            {
                List<IBarDataSet> sets = chart.getData().getDataSets();
                for (IBarDataSet iSet : sets) {
                    iSet.setDrawValues(!iSet.isDrawValuesEnabled());
                }
                chart.invalidate();
                break;
            }
        case R.id.actionToggleIcons:
            {
                List<IBarDataSet> sets = chart.getData().getDataSets();
                for (IBarDataSet iSet : sets) {
                    iSet.setDrawIcons(!iSet.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(2000);
                break;
            }
        case R.id.animateY:
            {
                chart.animateY(2000);
                break;
            }
        case R.id.animateXY:
            {
                chart.animateXY(2000, 2000);
                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 29 with BarDataSet

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

the class BarChartActivity method setData.

private void setData(int count, float range) {
    float start = 1f;
    ArrayList<BarEntry> values = new ArrayList<>();
    for (int i = (int) start; i < start + count; i++) {
        float val = (float) (Math.random() * (range + 1));
        if (Math.random() * 100 < 25) {
            values.add(new BarEntry(i, val, getResources().getDrawable(R.drawable.star)));
        } else {
            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, "The year 2017");
        set1.setDrawIcons(false);
        int startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
        int startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light);
        int startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
        int startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light);
        int startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light);
        int endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark);
        int endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple);
        int endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark);
        int endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark);
        int endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark);
        List<Fill> gradientFills = new ArrayList<>();
        gradientFills.add(new Fill(startColor1, endColor1));
        gradientFills.add(new Fill(startColor2, endColor2));
        gradientFills.add(new Fill(startColor3, endColor3));
        gradientFills.add(new Fill(startColor4, endColor4));
        gradientFills.add(new Fill(startColor5, endColor5));
        set1.setFills(gradientFills);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(tfLight);
        data.setBarWidth(0.9f);
        chart.setData(data);
    }
}
Also used : Fill(com.github.mikephil.charting.utils.Fill) 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 30 with BarDataSet

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

the class CombinedChartActivity method generateBarData.

private BarData generateBarData() {
    ArrayList<BarEntry> entries1 = new ArrayList<>();
    ArrayList<BarEntry> entries2 = new ArrayList<>();
    for (int index = 0; index < count; index++) {
        entries1.add(new BarEntry(0, getRandom(25, 25)));
        // stacked
        entries2.add(new BarEntry(0, new float[] { getRandom(13, 12), getRandom(13, 12) }));
    }
    BarDataSet set1 = new BarDataSet(entries1, "Bar 1");
    set1.setColor(Color.rgb(60, 220, 78));
    set1.setValueTextColor(Color.rgb(60, 220, 78));
    set1.setValueTextSize(10f);
    set1.setAxisDependency(YAxis.AxisDependency.LEFT);
    BarDataSet set2 = new BarDataSet(entries2, "");
    set2.setStackLabels(new String[] { "Stack 1", "Stack 2" });
    set2.setColors(Color.rgb(61, 165, 255), Color.rgb(23, 197, 255));
    set2.setValueTextColor(Color.rgb(61, 165, 255));
    set2.setValueTextSize(10f);
    set2.setAxisDependency(YAxis.AxisDependency.LEFT);
    float groupSpace = 0.06f;
    // x2 dataset
    float barSpace = 0.02f;
    // x2 dataset
    float barWidth = 0.45f;
    // (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
    BarData d = new BarData(set1, set2);
    d.setBarWidth(barWidth);
    // make this BarData object grouped
    // start at x = 0
    d.groupBars(0, groupSpace, barSpace);
    return d;
}
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)

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