Search in sources :

Example 6 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project MPAndroidChart by PhilJay.

the class StackedBarActivityNegative method onCreate.

@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("Age Distribution Austria");
    mChart = (HorizontalBarChart) findViewById(R.id.chart1);
    mChart.setOnChartValueSelectedListener(this);
    mChart.setDrawGridBackground(false);
    mChart.getDescription().setEnabled(false);
    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);
    mChart.setDrawBarShadow(false);
    mChart.setDrawValueAboveBar(true);
    mChart.setHighlightFullBarEnabled(false);
    mChart.getAxisLeft().setEnabled(false);
    mChart.getAxisRight().setAxisMaximum(25f);
    mChart.getAxisRight().setAxisMinimum(-25f);
    mChart.getAxisRight().setDrawGridLines(false);
    mChart.getAxisRight().setDrawZeroLine(true);
    mChart.getAxisRight().setLabelCount(7, false);
    mChart.getAxisRight().setValueFormatter(new CustomFormatter());
    mChart.getAxisRight().setTextSize(9f);
    XAxis xAxis = mChart.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 IAxisValueFormatter() {

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

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return format.format(value) + "-" + format.format(value + 10);
        }
    });
    Legend l = mChart.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> yValues = new ArrayList<BarEntry>();
    yValues.add(new BarEntry(5, new float[] { -10, 10 }));
    yValues.add(new BarEntry(15, new float[] { -12, 13 }));
    yValues.add(new BarEntry(25, new float[] { -15, 15 }));
    yValues.add(new BarEntry(35, new float[] { -17, 17 }));
    yValues.add(new BarEntry(45, new float[] { -19, 20 }));
    yValues.add(new BarEntry(45, new float[] { -19, 20 }, getResources().getDrawable(R.drawable.star)));
    yValues.add(new BarEntry(55, new float[] { -19, 19 }));
    yValues.add(new BarEntry(65, new float[] { -16, 16 }));
    yValues.add(new BarEntry(75, new float[] { -13, 14 }));
    yValues.add(new BarEntry(85, new float[] { -10, 11 }));
    yValues.add(new BarEntry(95, new float[] { -5, 6 }));
    yValues.add(new BarEntry(105, new float[] { -1, 2 }));
    BarDataSet set = new BarDataSet(yValues, "Age Distribution");
    set.setDrawIcons(false);
    set.setValueFormatter(new CustomFormatter());
    set.setValueTextSize(7f);
    set.setAxisDependency(YAxis.AxisDependency.RIGHT);
    set.setColors(new int[] { Color.rgb(67, 67, 72), Color.rgb(124, 181, 236) });
    set.setStackLabels(new String[] { "Men", "Women" });
    String[] xLabels = new String[] { "0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+" };
    BarData data = new BarData(set);
    data.setBarWidth(8.5f);
    mChart.setData(data);
    mChart.invalidate();
}
Also used : Legend(com.github.mikephil.charting.components.Legend) BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) BarEntry(com.github.mikephil.charting.data.BarEntry) XAxis(com.github.mikephil.charting.components.XAxis) BarData(com.github.mikephil.charting.data.BarData)

Example 7 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project MPAndroidChart by PhilJay.

the class RealmWikiExample method setData.

private void setData() {
    // LINE-CHART
    final RealmResults<Score> results = mRealm.where(Score.class).findAll();
    IAxisValueFormatter formatter = new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return results.get((int) value).getPlayerName();
        }
    };
    lineChart.getXAxis().setValueFormatter(formatter);
    barChart.getXAxis().setValueFormatter(formatter);
    RealmLineDataSet<Score> lineDataSet = new RealmLineDataSet<Score>(results, "scoreNr", "totalScore");
    lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    lineDataSet.setLabel("Result Scores");
    lineDataSet.setDrawCircleHole(false);
    lineDataSet.setColor(ColorTemplate.rgb("#FF5722"));
    lineDataSet.setCircleColor(ColorTemplate.rgb("#FF5722"));
    lineDataSet.setLineWidth(1.8f);
    lineDataSet.setCircleRadius(3.6f);
    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    dataSets.add(lineDataSet);
    LineData lineData = new LineData(dataSets);
    styleData(lineData);
    // set data
    lineChart.setData(lineData);
    lineChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
    // BAR-CHART
    RealmBarDataSet<Score> barDataSet = new RealmBarDataSet<Score>(results, "scoreNr", "totalScore");
    barDataSet.setColors(new int[] { ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4") });
    barDataSet.setLabel("Realm BarDataSet");
    ArrayList<IBarDataSet> barDataSets = new ArrayList<IBarDataSet>();
    barDataSets.add(barDataSet);
    BarData barData = new BarData(barDataSets);
    styleData(barData);
    barChart.setData(barData);
    barChart.setFitBars(true);
    barChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
Also used : ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) RealmLineDataSet(com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) RealmBarDataSet(com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 8 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project Gadgetbridge by Freeyourgadget.

the class AbstractChartFragment method refresh.

protected DefaultChartsData<CombinedData> refresh(GBDevice gbDevice, List<? extends ActivitySample> samples) {
    //        Calendar cal = GregorianCalendar.getInstance();
    //        cal.clear();
    TimestampTranslation tsTranslation = new TimestampTranslation();
    //        Date date;
    //        String dateStringFrom = "";
    //        String dateStringTo = "";
    //        ArrayList<String> xLabels = null;
    LOG.info("" + getTitle() + ": number of samples:" + samples.size());
    CombinedData combinedData;
    if (samples.size() > 1) {
        boolean annotate = true;
        boolean use_steps_as_movement;
        int last_type = ActivityKind.TYPE_UNKNOWN;
        int numEntries = samples.size();
        List<BarEntry> activityEntries = new ArrayList<>(numEntries);
        boolean hr = supportsHeartrate(gbDevice);
        List<Entry> heartrateEntries = hr ? new ArrayList<Entry>(numEntries) : null;
        // this is kinda inefficient...
        List<Integer> colors = new ArrayList<>(numEntries);
        int lastHrSampleIndex = -1;
        for (int i = 0; i < numEntries; i++) {
            ActivitySample sample = samples.get(i);
            int type = sample.getKind();
            int ts = tsTranslation.shorten(sample.getTimestamp());
            //                System.out.println(ts);
            //                ts = i;
            // determine start and end dates
            //                if (i == 0) {
            //                    cal.setTimeInMillis(ts * 1000L); // make sure it's converted to long
            //                    date = cal.getTime();
            //                    dateStringFrom = dateFormat.format(date);
            //                } else if (i == samples.size() - 1) {
            //                    cal.setTimeInMillis(ts * 1000L); // same here
            //                    date = cal.getTime();
            //                    dateStringTo = dateFormat.format(date);
            //                }
            float movement = sample.getIntensity();
            float value = movement;
            switch(type) {
                case ActivityKind.TYPE_DEEP_SLEEP:
                    value += SleepUtils.Y_VALUE_DEEP_SLEEP;
                    colors.add(akDeepSleep.color);
                    break;
                case ActivityKind.TYPE_LIGHT_SLEEP:
                    colors.add(akLightSleep.color);
                    break;
                case ActivityKind.TYPE_NOT_WORN:
                    //a small value, just to show something on the graphs
                    value = SleepUtils.Y_VALUE_DEEP_SLEEP;
                    colors.add(akNotWorn.color);
                    break;
                default:
                    //                        short steps = sample.getSteps();
                    //                        if (use_steps_as_movement && steps != 0) {
                    //                            // I'm not sure using steps for this is actually a good idea
                    //                            movement = steps;
                    //                        }
                    //                        value = ((float) movement) / movement_divisor;
                    colors.add(akActivity.color);
            }
            activityEntries.add(createBarEntry(value, ts));
            if (hr && isValidHeartRateValue(sample.getHeartRate())) {
                if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 60 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
                    heartrateEntries.add(createLineEntry(0, lastHrSampleIndex + 1));
                    heartrateEntries.add(createLineEntry(0, ts - 1));
                }
                heartrateEntries.add(createLineEntry(sample.getHeartRate(), ts));
                lastHrSampleIndex = ts;
            }
            String xLabel = "";
            if (annotate) {
            //                    cal.setTimeInMillis((ts + tsOffset) * 1000L);
            //                    date = cal.getTime();
            //                    String dateString = annotationDateFormat.format(date);
            //                    xLabel = dateString;
            //                    if (last_type != type) {
            //                        if (isSleep(last_type) && !isSleep(type)) {
            //                            // woken up
            //                            LimitLine line = new LimitLine(i, dateString);
            //                            line.enableDashedLine(8, 8, 0);
            //                            line.setTextColor(Color.WHITE);
            //                            line.setTextSize(15);
            //                            chart.getXAxis().addLimitLine(line);
            //                        } else if (!isSleep(last_type) && isSleep(type)) {
            //                            // fallen asleep
            //                            LimitLine line = new LimitLine(i, dateString);
            //                            line.enableDashedLine(8, 8, 0);
            //                            line.setTextSize(15);
            //                            line.setTextColor(Color.WHITE);
            //                            chart.getXAxis().addLimitLine(line);
            //                        }
            //                    }
            //                    last_type = type;
            }
        }
        BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
        // create a data object with the datasets
        //            combinedData = new CombinedData(xLabels);
        combinedData = new CombinedData();
        List<IBarDataSet> list = new ArrayList<>();
        list.add(activitySet);
        BarData barData = new BarData(list);
        barData.setBarWidth(100f);
        //            barData.setGroupSpace(0);
        combinedData.setData(barData);
        if (hr && heartrateEntries.size() > 0) {
            LineDataSet heartrateSet = createHeartrateSet(heartrateEntries, "Heart Rate");
            LineData lineData = new LineData(heartrateSet);
            combinedData.setData(lineData);
        }
    //            chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
    //            chart.setDescriptionPosition(?, ?);
    } else {
        combinedData = new CombinedData();
    }
    IAxisValueFormatter xValueFormatter = new SampleXLabelFormatter(tsTranslation);
    return new DefaultChartsData(combinedData, xValueFormatter);
}
Also used : AbstractActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample) ActivitySample(nodomain.freeyourgadget.gadgetbridge.model.ActivitySample) BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) CombinedData(com.github.mikephil.charting.data.CombinedData) BarEntry(com.github.mikephil.charting.data.BarEntry) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) LineData(com.github.mikephil.charting.data.LineData) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 9 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project MPAndroidChart by PhilJay.

the class BarChartPositiveNegative method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart_noseekbar);
    mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
    mChart = (BarChart) findViewById(R.id.chart1);
    mChart.setBackgroundColor(Color.WHITE);
    mChart.setExtraTopOffset(-30f);
    mChart.setExtraBottomOffset(10f);
    mChart.setExtraLeftOffset(70f);
    mChart.setExtraRightOffset(70f);
    mChart.setDrawBarShadow(false);
    mChart.setDrawValueAboveBar(true);
    mChart.getDescription().setEnabled(false);
    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);
    mChart.setDrawGridBackground(false);
    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(mTf);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextColor(Color.LTGRAY);
    xAxis.setTextSize(13f);
    xAxis.setLabelCount(5);
    xAxis.setCenterAxisLabels(true);
    xAxis.setGranularity(1f);
    YAxis left = mChart.getAxisLeft();
    left.setDrawLabels(false);
    left.setSpaceTop(25f);
    left.setSpaceBottom(25f);
    left.setDrawAxisLine(false);
    left.setDrawGridLines(false);
    // draw a zero line
    left.setDrawZeroLine(true);
    left.setZeroLineColor(Color.GRAY);
    left.setZeroLineWidth(0.7f);
    mChart.getAxisRight().setEnabled(false);
    mChart.getLegend().setEnabled(false);
    // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
    final List<Data> data = new ArrayList<>();
    data.add(new Data(0f, -224.1f, "12-29"));
    data.add(new Data(1f, 238.5f, "12-30"));
    data.add(new Data(2f, 1280.1f, "12-31"));
    data.add(new Data(3f, -442.3f, "01-01"));
    data.add(new Data(4f, -2280.1f, "01-02"));
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return data.get(Math.min(Math.max((int) value, 0), data.size() - 1)).xAxisValue;
        }
    });
    setData(data);
}
Also used : ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) BarData(com.github.mikephil.charting.data.BarData) AxisBase(com.github.mikephil.charting.components.AxisBase) XAxis(com.github.mikephil.charting.components.XAxis) YAxis(com.github.mikephil.charting.components.YAxis)

Aggregations

IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)9 AxisBase (com.github.mikephil.charting.components.AxisBase)7 XAxis (com.github.mikephil.charting.components.XAxis)7 Legend (com.github.mikephil.charting.components.Legend)6 YAxis (com.github.mikephil.charting.components.YAxis)6 BarData (com.github.mikephil.charting.data.BarData)4 ArrayList (java.util.ArrayList)4 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)3 BarDataSet (com.github.mikephil.charting.data.BarDataSet)2 BarEntry (com.github.mikephil.charting.data.BarEntry)2 CombinedData (com.github.mikephil.charting.data.CombinedData)2 LineData (com.github.mikephil.charting.data.LineData)2 TextView (android.widget.TextView)1 MarkerView (com.github.mikephil.charting.components.MarkerView)1 Entry (com.github.mikephil.charting.data.Entry)1 LineDataSet (com.github.mikephil.charting.data.LineDataSet)1 RealmBarDataSet (com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet)1 RealmLineDataSet (com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet)1 LargeValueFormatter (com.github.mikephil.charting.formatter.LargeValueFormatter)1 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)1