Search in sources :

Example 1 with LargeValueFormatter

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

the class BarChartActivityMultiDataset 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);
    tvX = (TextView) findViewById(R.id.tvXMax);
    tvX.setTextSize(10);
    tvY = (TextView) findViewById(R.id.tvYMax);
    mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
    mSeekBarX.setOnSeekBarChangeListener(this);
    mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
    mSeekBarY.setOnSeekBarChangeListener(this);
    mChart = (BarChart) findViewById(R.id.chart1);
    mChart.setOnChartValueSelectedListener(this);
    mChart.getDescription().setEnabled(false);
    //        mChart.setDrawBorders(true);
    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);
    mChart.setDrawBarShadow(false);
    mChart.setDrawGridBackground(false);
    // create a custom MarkerView (extend MarkerView) and specify the layout
    // to use for it
    MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
    // For bounds control
    mv.setChartView(mChart);
    // Set the marker to the chart
    mChart.setMarker(mv);
    mSeekBarX.setProgress(10);
    mSeekBarY.setProgress(100);
    Legend l = mChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(true);
    l.setTypeface(mTfLight);
    l.setYOffset(0f);
    l.setXOffset(10f);
    l.setYEntrySpace(0f);
    l.setTextSize(8f);
    XAxis xAxis = mChart.getXAxis();
    xAxis.setTypeface(mTfLight);
    xAxis.setGranularity(1f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return String.valueOf((int) value);
        }
    });
    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(mTfLight);
    leftAxis.setValueFormatter(new LargeValueFormatter());
    leftAxis.setDrawGridLines(false);
    leftAxis.setSpaceTop(35f);
    // this replaces setStartAtZero(true)
    leftAxis.setAxisMinimum(0f);
    mChart.getAxisRight().setEnabled(false);
}
Also used : Legend(com.github.mikephil.charting.components.Legend) LargeValueFormatter(com.github.mikephil.charting.formatter.LargeValueFormatter) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) MyMarkerView(com.xxmassdeveloper.mpchartexample.custom.MyMarkerView) XAxis(com.github.mikephil.charting.components.XAxis) YAxis(com.github.mikephil.charting.components.YAxis)

Example 2 with LargeValueFormatter

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

the class BarChartActivityMultiDataset method onProgressChanged.

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    float groupSpace = 0.08f;
    // x4 DataSet
    float barSpace = 0.03f;
    // x4 DataSet
    float barWidth = 0.2f;
    // (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
    int groupCount = mSeekBarX.getProgress() + 1;
    int startYear = 1980;
    int endYear = startYear + groupCount;
    tvX.setText(startYear + "-" + endYear);
    tvY.setText("" + (mSeekBarY.getProgress()));
    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
    ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
    ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
    ArrayList<BarEntry> yVals4 = new ArrayList<BarEntry>();
    float randomMultiplier = mSeekBarY.getProgress() * 100000f;
    for (int i = startYear; i < endYear; i++) {
        yVals1.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
        yVals2.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
        yVals3.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
        yVals4.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
    }
    BarDataSet set1, set2, set3, set4;
    if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
        set2 = (BarDataSet) mChart.getData().getDataSetByIndex(1);
        set3 = (BarDataSet) mChart.getData().getDataSetByIndex(2);
        set4 = (BarDataSet) mChart.getData().getDataSetByIndex(3);
        set1.setValues(yVals1);
        set2.setValues(yVals2);
        set3.setValues(yVals3);
        set4.setValues(yVals4);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    } else {
        // create 4 DataSets
        set1 = new BarDataSet(yVals1, "Company A");
        set1.setColor(Color.rgb(104, 241, 175));
        set2 = new BarDataSet(yVals2, "Company B");
        set2.setColor(Color.rgb(164, 228, 251));
        set3 = new BarDataSet(yVals3, "Company C");
        set3.setColor(Color.rgb(242, 247, 158));
        set4 = new BarDataSet(yVals4, "Company D");
        set4.setColor(Color.rgb(255, 102, 0));
        BarData data = new BarData(set1, set2, set3, set4);
        data.setValueFormatter(new LargeValueFormatter());
        data.setValueTypeface(mTfLight);
        mChart.setData(data);
    }
    // specify the width each bar should have
    mChart.getBarData().setBarWidth(barWidth);
    // restrict the x-axis range
    mChart.getXAxis().setAxisMinimum(startYear);
    // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
    mChart.getXAxis().setAxisMaximum(startYear + mChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
    mChart.groupBars(startYear, groupSpace, barSpace);
    mChart.invalidate();
}
Also used : LargeValueFormatter(com.github.mikephil.charting.formatter.LargeValueFormatter) BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) BarData(com.github.mikephil.charting.data.BarData) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 3 with LargeValueFormatter

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

the class LargeValueFormatterTest method test.

@Test
public void test() {
    LargeValueFormatter formatter = new LargeValueFormatter();
    String result = formatter.getFormattedValue(5f, null);
    assertEquals("5", result);
    result = formatter.getFormattedValue(5.5f, null);
    assertEquals("5.5", result);
    result = formatter.getFormattedValue(50f, null);
    assertEquals("50", result);
    result = formatter.getFormattedValue(50.5f, null);
    assertEquals("50.5", result);
    result = formatter.getFormattedValue(500f, null);
    assertEquals("500", result);
    result = formatter.getFormattedValue(1100f, null);
    assertEquals("1.1k", result);
    result = formatter.getFormattedValue(10000f, null);
    assertEquals("10k", result);
    result = formatter.getFormattedValue(10500f, null);
    assertEquals("10.5k", result);
    result = formatter.getFormattedValue(100000f, null);
    assertEquals("100k", result);
    result = formatter.getFormattedValue(1000000f, null);
    assertEquals("1m", result);
    result = formatter.getFormattedValue(1500000f, null);
    assertEquals("1.5m", result);
    result = formatter.getFormattedValue(9500000f, null);
    assertEquals("9.5m", result);
    result = formatter.getFormattedValue(22200000f, null);
    assertEquals("22.2m", result);
    result = formatter.getFormattedValue(222000000f, null);
    assertEquals("222m", result);
    result = formatter.getFormattedValue(1000000000f, null);
    assertEquals("1b", result);
    result = formatter.getFormattedValue(9900000000f, null);
    assertEquals("9.9b", result);
    result = formatter.getFormattedValue(99000000000f, null);
    assertEquals("99b", result);
    result = formatter.getFormattedValue(99500000000f, null);
    assertEquals("99.5b", result);
    result = formatter.getFormattedValue(999000000000f, null);
    assertEquals("999b", result);
    result = formatter.getFormattedValue(1000000000000f, null);
    assertEquals("1t", result);
    // quadrillion support
    formatter.setSuffix(new String[] { "", "k", "m", "b", "t", "q" });
    result = formatter.getFormattedValue(1000000000000000f, null);
    assertEquals("1q", result);
    result = formatter.getFormattedValue(1100000000000000f, null);
    assertEquals("1.1q", result);
    result = formatter.getFormattedValue(10000000000000000f, null);
    assertEquals("10q", result);
    result = formatter.getFormattedValue(13300000000000000f, null);
    assertEquals("13.3q", result);
    result = formatter.getFormattedValue(100000000000000000f, null);
    assertEquals("100q", result);
}
Also used : LargeValueFormatter(com.github.mikephil.charting.formatter.LargeValueFormatter) Test(org.junit.Test)

Aggregations

LargeValueFormatter (com.github.mikephil.charting.formatter.LargeValueFormatter)3 AxisBase (com.github.mikephil.charting.components.AxisBase)1 Legend (com.github.mikephil.charting.components.Legend)1 XAxis (com.github.mikephil.charting.components.XAxis)1 YAxis (com.github.mikephil.charting.components.YAxis)1 BarData (com.github.mikephil.charting.data.BarData)1 BarDataSet (com.github.mikephil.charting.data.BarDataSet)1 BarEntry (com.github.mikephil.charting.data.BarEntry)1 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)1 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)1 MyMarkerView (com.xxmassdeveloper.mpchartexample.custom.MyMarkerView)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1