Java Code Examples for com.github.mikephil.charting.data.BarData#setBarWidth()

The following examples show how to use com.github.mikephil.charting.data.BarData#setBarWidth() . 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 File: StatisticActivity.java    From memorize with 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 2
Source File: ListViewBarChartActivity.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * 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 3
Source File: ProductDetailActivity.java    From FaceT with 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 4
Source File: ListViewMultiChartActivity.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * generates a random ChartData object with just one DataSet
 *
 * @return Bar data
 */
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 5
Source File: TimeLineView.java    From android-kline with 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 6
Source File: AnalysisFragment.java    From outlay with Apache License 2.0 5 votes vote down vote up
@Override
public void showAnalysis(Report report) {
    List<BarEntry> barEntries = new ArrayList<>();
    List<Expense> expenses = report.getExpenses();

    for (int i = 0; i < expenses.size(); i++) {
        Expense expense = expenses.get(i);
        barEntries.add(new BarEntry(
                i,
                expense.getAmount().floatValue())
        );
    }
    BarDataSet barSet = new BarDataSet(barEntries, "");
    barSet.setColor(selectedCategory.getColor());

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

    BarData data = new BarData(dataSets);
    data.setValueTextSize(10f);
    data.setValueTextColor(getOutlayTheme().secondaryTextColor);
    data.setBarWidth(0.9f);

    dayAxisValueFormatter.setExpenses(expenses);
    barChart.setData(data);
    barChart.invalidate();
    barChart.animateY(500);
}
 
Example 7
Source File: HistogramChartTest.java    From walt with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    mockStatic(android.graphics.Color.class);
    when(android.graphics.Color.rgb(anyInt(), anyInt(), anyInt())).thenReturn(0);
    barData = new BarData();
    barData.setBarWidth((1f - HistogramChart.GROUP_SPACE)/1);
    barData.addDataSet(new BarDataSet(new ArrayList<BarEntry>(), "SomeLabel"));
    data = new HistogramChart.HistogramData(1, 5f);
    data.addEntry(barData, 0, 12);
    data.addEntry(barData, 0, 14);
    data.addEntry(barData, 0, 16);
    data.addEntry(barData, 0, 21);
}
 
Example 8
Source File: KLineView.java    From android-kline with Apache License 2.0 5 votes vote down vote up
private void initChartMacdData() {
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        ArrayList<BarEntry> paddingEntries = new ArrayList<>();
        ArrayList<Entry> difEntries = new ArrayList<>();
        ArrayList<Entry> deaEntries = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            HisData t = mData.get(i);
            barEntries.add(new BarEntry(i, (float) t.getMacd()));
            difEntries.add(new Entry(i, (float) t.getDif()));
            deaEntries.add(new Entry(i, (float) t.getDea()));
        }
        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);
        LineData lineData = new LineData(setLine(DIF, difEntries), setLine(DEA, deaEntries));
        combinedData.setData(lineData);
        mChartMacd.setData(combinedData);

        mChartMacd.setVisibleXRange(MAX_COUNT, MIN_COUNT);

        mChartMacd.notifyDataSetChanged();
//        mChartMacd.moveViewToX(combinedData.getEntryCount());
        moveToLast(mChartMacd);
    }
 
Example 9
Source File: KLineView.java    From android-kline with Apache License 2.0 5 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());
        moveToLast(mChartVolume);

    }
 
Example 10
Source File: CombinedChartActivity.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
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;
        float barSpace = 0.02f; // x2 dataset
        float barWidth = 0.45f; // x2 dataset
        // (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
        d.groupBars(0, groupSpace, barSpace); // start at x = 0

        return d;
    }
 
Example 11
Source File: TimeLineView.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public void initDatas(List<HisData>... hisDatas) {
        // 设置标签数量,并让标签居中显示
        XAxis xAxis = mChartVolume.getXAxis();
        xAxis.setLabelCount(hisDatas.length + 1, true);
        xAxis.setAvoidFirstLastClipping(false);
        xAxis.setCenterAxisLabels(true);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                value += 1; // 这里不设置+1会有bug
                if (mData.isEmpty()) {
                    return "";
                }
                if (value < 0) {
                    value = 0;
                }
                if (value < mData.size()) {
                    return DateUtils.formatDate(mData.get((int) value).getDate(), mDateFormat);
                }
                return "";
            }
        });
        mData.clear();
        ArrayList<ILineDataSet> sets = new ArrayList<>();
        ArrayList<IBarDataSet> barSets = new ArrayList<>();

        for (List<HisData> hisData : hisDatas) {
            hisData = DataUtils.calculateHisData(hisData);
            ArrayList<Entry> priceEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<Entry> aveEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<Entry> paddingEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<BarEntry> barPaddingEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<BarEntry> barEntries = new ArrayList<>(INIT_COUNT);

            for (int i = 0; i < hisData.size(); i++) {
                HisData t = hisData.get(i);
                priceEntries.add(new Entry(i + mData.size(), (float) t.getClose()));
                aveEntries.add(new Entry(i + mData.size(), (float) t.getAvePrice()));
                barEntries.add(new BarEntry(i + mData.size(), (float) t.getVol(), t));
            }
            if (!hisData.isEmpty() && hisData.size() < INIT_COUNT / hisDatas.length) {
                for (int i = hisData.size(); i < INIT_COUNT / hisDatas.length; i++) {
                    paddingEntries.add(new Entry(i, (float) hisData.get(hisData.size() - 1).getClose()));
                    barPaddingEntries.add(new BarEntry(i, (float) hisData.get(hisData.size() - 1).getClose()));
                }
            }
            sets.add(setLine(NORMAL_LINE_5DAY, priceEntries));
            sets.add(setLine(AVE_LINE, aveEntries));
            sets.add(setLine(INVISIABLE_LINE, paddingEntries));
            barSets.add(setBar(barEntries, NORMAL_LINE));
            barSets.add(setBar(barPaddingEntries, INVISIABLE_LINE));
            barSets.add(setBar(barPaddingEntries, INVISIABLE_LINE));
            mData.addAll(hisData);
        }

        LineData lineData = new LineData(sets);

        CombinedData combinedData = new CombinedData();
        combinedData.setData(lineData);
        mChartPrice.setData(combinedData);
        mChartPrice.setVisibleXRange(MAX_COUNT, MIN_COUNT);
        mChartPrice.notifyDataSetChanged();
//        mChartPrice.moveViewToX(combinedData.getEntryCount());
        moveToLast(mChartVolume);


        BarData barData = new BarData(barSets);
        barData.setBarWidth(0.75f);
        CombinedData combinedData2 = new CombinedData();
        combinedData2.setData(barData);
        mChartVolume.setData(combinedData2);
        mChartVolume.setVisibleXRange(MAX_COUNT, MIN_COUNT);
        mChartVolume.notifyDataSetChanged();
        mChartVolume.moveViewToX(combinedData2.getEntryCount());

        mChartPrice.getXAxis().setAxisMaximum(combinedData.getXMax() + 0.5f);
        mChartVolume.getXAxis().setAxisMaximum(mChartVolume.getData().getXMax() + 0.5f);

        mChartPrice.zoom(MAX_COUNT * 1f / INIT_COUNT, 0, 0, 0);
        mChartVolume.zoom(MAX_COUNT * 1f / INIT_COUNT, 0, 0, 0);

        setDescription(mChartVolume, "成交量 " + getLastData().getVol());
    }
 
Example 12
Source File: BarDataTest.java    From android-kline with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 13
Source File: BarDataTest.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
@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);
}
 
Example 14
Source File: TestCaseRunner.java    From SQLite-Performance with The Unlicense 4 votes vote down vote up
@Override
protected void onProgressUpdate(TestCase.Metrics... values) {
    super.onProgressUpdate(values);
    mDataSetIndex = mChart.getData().getIndexOfDataSet(mDataSet);
    if (mDataSetIndex < 0) {
        mChart.getData().addDataSet(mDataSet);
        mDataSetIndex = mChart.getData().getIndexOfDataSet(mDataSet);
    }

    BarData data = mChart.getData();
    for (TestCase.Metrics m : values) {
        data.addEntry(new BarEntry(m.getVariable(), (float) m.getElapsedTime()), mDataSetIndex);
    }
    if (data.getDataSetCount() > 1) {
        int count = data.getDataSetCount();
        float groupSpace = 0.06f;
        float barSpace = 0.02f;
        float numBars = count;
        float barWidth = (1.0f - ((count - 1) * barSpace) - groupSpace) / numBars;

        Collections.sort(
                data.getDataSets(),
                new Comparator<IBarDataSet>() {
                    @Override
                    public int compare(IBarDataSet o1, IBarDataSet o2) {
                        float[] hsl1 = new float[3];
                        float[] hsl2 = new float[3];
                        ColorUtils.colorToHSL(o1.getColor(), hsl1);
                        ColorUtils.colorToHSL(o2.getColor(), hsl2);

                        if (Math.abs(hsl1[0] - hsl2[0]) > 10) {
                            return Float.compare(hsl1[0], hsl2[0]);
                        }

                        if (Math.abs(hsl1[2] - hsl2[2]) > 0.1) {
                            return Float.compare(hsl1[2], hsl2[2]);
                        }

                        return Float.compare(hsl1[1], hsl2[1]);
                    }
                }
        );

        data.setBarWidth(barWidth);
        data.groupBars(2, groupSpace, barSpace);
    }
    mChart.setFitBars(true);
    data.notifyDataChanged();
    mChart.notifyDataSetChanged();
    mChart.invalidate();
}
 
Example 15
Source File: HistogramChart.java    From walt with Apache License 2.0 4 votes vote down vote up
public HistogramChart(Context context, AttributeSet attrs) {
    super(context, attrs);
    inflate(getContext(), R.layout.histogram, this);

    barChart = (BarChart) findViewById(R.id.bar_chart);
    findViewById(R.id.button_close_bar_chart).setOnClickListener(this);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HistogramChart);
    final String descString;
    final int numDataSets;
    final float binWidth;
    try {
        descString = a.getString(R.styleable.HistogramChart_description);
        numDataSets = a.getInteger(R.styleable.HistogramChart_numDataSets, 1);
        binWidth = a.getFloat(R.styleable.HistogramChart_binWidth, 5f);
    } finally {
        a.recycle();
    }

    ArrayList<IBarDataSet> dataSets = new ArrayList<>(numDataSets);
    for (int i = 0; i < numDataSets; i++) {
        final BarDataSet dataSet = new BarDataSet(new ArrayList<BarEntry>(), "");
        dataSet.setColor(ColorTemplate.MATERIAL_COLORS[i]);
        dataSets.add(dataSet);
    }

    BarData barData = new BarData(dataSets);
    barData.setBarWidth((1f - GROUP_SPACE)/numDataSets);
    barChart.setData(barData);
    histogramData = new HistogramData(numDataSets, binWidth);
    groupBars(barData);
    final Description desc = new Description();
    desc.setText(descString);
    desc.setTextSize(12f);
    barChart.setDescription(desc);

    XAxis xAxis = barChart.getXAxis();
    xAxis.setGranularityEnabled(true);
    xAxis.setGranularity(1);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        DecimalFormat df = new DecimalFormat("#.##");

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return df.format(histogramData.getDisplayValue(value));
        }
    });

    barChart.setFitBars(true);
    barChart.invalidate();
}
 
Example 16
Source File: StackedBarActivityNegative.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_age_distribution);

    setTitle("StackedBarActivityNegative");

    chart = findViewById(R.id.chart1);
    chart.setOnChartValueSelectedListener(this);
    chart.setDrawGridBackground(false);
    chart.getDescription().setEnabled(false);

    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);
    chart.setHighlightFullBarEnabled(false);

    chart.getAxisLeft().setEnabled(false);
    chart.getAxisRight().setAxisMaximum(25f);
    chart.getAxisRight().setAxisMinimum(-25f);
    chart.getAxisRight().setDrawGridLines(false);
    chart.getAxisRight().setDrawZeroLine(true);
    chart.getAxisRight().setLabelCount(7, false);
    chart.getAxisRight().setValueFormatter(new CustomFormatter());
    chart.getAxisRight().setTextSize(9f);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTH_SIDED);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextSize(9f);
    xAxis.setAxisMinimum(0f);
    xAxis.setAxisMaximum(110f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setLabelCount(12);
    xAxis.setGranularity(10f);
    xAxis.setValueFormatter(new ValueFormatter() {

        private final DecimalFormat format = new DecimalFormat("###");

        @Override
        public String getFormattedValue(float value) {
            return format.format(value) + "-" + format.format(value + 10);
        }
    });

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setFormSize(8f);
    l.setFormToTextSpace(4f);
    l.setXEntrySpace(6f);

    // IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first
    ArrayList<BarEntry> values = new ArrayList<>();
    values.add(new BarEntry(5, new float[]{ -10, 10 }));
    values.add(new BarEntry(15, new float[]{ -12, 13 }));
    values.add(new BarEntry(25, new float[]{ -15, 15 }));
    values.add(new BarEntry(35, new float[]{ -17, 17 }));
    values.add(new BarEntry(45, new float[]{ -19, 20 }));
    values.add(new BarEntry(45, new float[]{ -19, 20 }, getResources().getDrawable(R.drawable.star)));
    values.add(new BarEntry(55, new float[]{ -19, 19 }));
    values.add(new BarEntry(65, new float[]{ -16, 16 }));
    values.add(new BarEntry(75, new float[]{ -13, 14 }));
    values.add(new BarEntry(85, new float[]{ -10, 11 }));
    values.add(new BarEntry(95, new float[]{ -5, 6 }));
    values.add(new BarEntry(105, new float[]{ -1, 2 }));

    BarDataSet set = new BarDataSet(values, "Age Distribution");
    set.setDrawIcons(false);
    set.setValueFormatter(new CustomFormatter());
    set.setValueTextSize(7f);
    set.setAxisDependency(YAxis.AxisDependency.RIGHT);
    set.setColors(Color.rgb(67,67,72), Color.rgb(124,181,236));
    set.setStackLabels(new String[]{
            "Men", "Women"
    });

    BarData data = new BarData(set);
    data.setBarWidth(8.5f);
    chart.setData(data);
    chart.invalidate();
}
 
Example 17
Source File: ReportAdapter.java    From privacy-friendly-interval-timer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    switch (getItemViewType(position)) {
        case TYPE_CHART:
            ActivityChart barChartData = (ActivityChart) mItems.get(position);
            CombinedChartViewHolder barChartViewHolder = (CombinedChartViewHolder) holder;
            barChartViewHolder.mTitleTextView.setText(barChartData.getTitle());
            int barChartI = 0;
            ArrayList<String> barChartXValues = new ArrayList<>();
            Map<String, Double> barChartDataMap;
            String barChartLabel;
            if (barChartData.getDisplayedDataType() == null) {
                barChartDataMap = barChartData.getTime();
                barChartLabel = barChartViewHolder.context.getString(R.string.report_workout_time);
            } else {
                switch (barChartData.getDisplayedDataType()) {
                    case CALORIES:
                        barChartDataMap = barChartData.getCalories();
                        barChartLabel = barChartViewHolder.context.getString(R.string.report_calories);
                        break;
                    case TIME:
                    default:
                        barChartDataMap = barChartData.getTime();
                        barChartLabel = barChartViewHolder.context.getString(R.string.report_workout_time);
                        break;
                }
            }
            List<BarEntry> dataEntries = new ArrayList<>();
            for (Map.Entry<String, Double> dataEntry : barChartDataMap.entrySet()) {
                barChartXValues.add(barChartI, dataEntry.getKey());
                if (dataEntry.getValue() != null) {
                    float val = dataEntry.getValue().floatValue();
                    dataEntries.add(new BarEntry(barChartI, val));
                }
                barChartI++;
            }
            BarDataSet barDataSet = new BarDataSet(dataEntries, barChartLabel);
            String formatPattern = "###,###,##0.0";
            barDataSet.setValueFormatter(new DoubleValueFormatter(formatPattern));

            ArrayList<ILineDataSet> lineDataSets = new ArrayList<>();

            // make sure, that the first and last entry are fully displayed
            Entry start = new Entry(0, 0);
            Entry end = new Entry(barChartI - 1, 0);
            LineDataSet chartLineDataSet = new LineDataSet(Arrays.asList(start, end), "");
            chartLineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
            chartLineDataSet.setDrawCircles(false);
            chartLineDataSet.setColor(ContextCompat.getColor(barChartViewHolder.context, R.color.transparent), 0);
            chartLineDataSet.setDrawValues(false);
            lineDataSets.add(chartLineDataSet);

            CombinedData combinedData = new CombinedData();
            BarData barData = new BarData(barDataSet);
            barData.setBarWidth(0.5f);
            combinedData.setData(barData);
            combinedData.setData(new LineData(lineDataSets));
            barDataSet.setColor(ContextCompat.getColor(barChartViewHolder.context, R.color.colorPrimary));
            barChartViewHolder.mChart.setData(combinedData);
            barChartViewHolder.mChart.getXAxis().setValueFormatter(new ArrayListAxisValueFormatter(barChartXValues));
            barChartViewHolder.mChart.invalidate();
            break;
        case TYPE_SUMMARY:
            ActivitySummary summaryData = (ActivitySummary) mItems.get(position);
            SummaryViewHolder summaryViewHolder = (SummaryViewHolder) holder;
            summaryViewHolder.mTitleTextView.setText(summaryData.getTitle());
            summaryViewHolder.mTimeTextView.setText(formatTime(summaryData.getTime()));
            summaryViewHolder.mCaloriesTextView.setText(String.valueOf(summaryData.getCalories()));
            break;
    }
}
 
Example 18
Source File: BarDataTest.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@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);
}