pearmak
pearmak

Reputation: 5027

Android: MPAndroidChart first Bar not showing properly

I am using MPAndroidChart for plotting scatteredBarChart. The code is as follows:

Code:

public void setupScatteredBarChart(ArrayList<String> x, ArrayList<String> y1, ArrayList<String> y2 )
    {
        int maxYvalue = 0;
        final String[] x_name = new String[x.size()];
        for (int j = 0;  j <x.size(); j++)
        {
            String[] temp = x.get(j).split("-");
            x_name[j] = temp[0] + "/" + temp[1];
        }

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

        for (int i = 0; i < x_name.length; i++)
        {
            float val1 = Float.parseFloat(y1.get(i));
            float val2 = Float.parseFloat(y2.get(i));

            yVals1.add(new BarEntry(i, new float[]{val1, val2},getResources().getDrawable(R.drawable.ic_launcher)));

            float val_total = val1 + val2;
            int I = (int) Math.ceil(val_total);
            if (maxYvalue < I)
            {
                maxYvalue = I;
            }
        }

        chart_scattered = (BarChart) findViewById(R.id.chart_scattered);
        chart_scattered.setOnChartValueSelectedListener(Stat.this);
        chart_scattered.getDescription().setEnabled(false);

        chart_scattered.setMaxVisibleValueCount(40); // if more than 40 entries are displayed in the chart, no values will be drawn
        chart_scattered.setPinchZoom(false);      // scaling can now only be done on x- and y-axis separately
        chart_scattered.setDrawGridBackground(false);
        chart_scattered.setDrawBarShadow(false);
        chart_scattered.setDrawValueAboveBar(false);
        chart_scattered.setHighlightFullBarEnabled(false);
        chart_scattered.setDoubleTapToZoomEnabled(false);
        chart_scattered.setTouchEnabled(false);
        chart_scattered.setFitBars(true);
        chart_scattered.animateXY(800, 800);
        chart_scattered.getAxisRight().setEnabled(false);
        chart_scattered.getXAxis().setAxisMinimum(-0.5f); // adjust for showing first bar properly

        // Left Y axis
        YAxis leftAxis = chart_scattered.getAxisLeft();       // change the position of the y-labels
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        chart_scattered.getAxisLeft().setLabelCount(maxYvalue + 1, true);
        chart_scattered.getAxisLeft().setAxisMinValue(0f);
        chart_scattered.getAxisLeft().setValueFormatter(new MyYAxisValueFormatter());

        // Bottom X axis
        XAxis xLabels = chart_scattered.getXAxis();

        xLabels.setPosition(XAxis.XAxisPosition.BOTTOM);
        xLabels.setAxisMinimum(0f);
        xLabels.setGranularity(1f);
        xLabels.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return x_name[(int) value % x_name.length];
            }
        });

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

        BarDataSet set1;

        if (chart_scattered.getData() != null && chart_scattered.getData().getDataSetCount() > 0)
        {
            set1 = (BarDataSet) chart_scattered.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            chart_scattered.getData().notifyDataChanged();
            chart_scattered.notifyDataSetChanged();
        }
        else
        {
            set1 = new BarDataSet(yVals1, "");
            set1.setDrawIcons(false);
            set1.setColors(getColors());
            set1.setStackLabels(new String[]{"A", "B"});

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

            BarData data = new BarData(dataSets);
            data.setValueTextColor(Color.WHITE);

            chart_scattered.setData(data);
        }
        chart_scattered.invalidate();
    }

Screen Capture:

enter image description here

Question:

  1. How could the first Bar to be showing properly? I have researched a lot and added the following to the code but is still not successful as showing in the screen capture above.

    chart_scattered.getXAxis().setAxisMinimum(-0.5f);

    chart_scattered.setFitBars(true);

  2. As the date will go on increasing, could it set max 7 Bars and more than that, it has to be scrolled horizontally?

Thanks!

Upvotes: 0

Views: 1252

Answers (1)

sauvik
sauvik

Reputation: 2254

Question 1

remove this line from your code

xLabels.setAxisMinimum(0f);

will solve your problem.

Question 2

To only show the first 7 bars, use this..

chart_scattered.setVisibleXRangeMaximum(7f);

but this will not allow the user to scroll horizontally yet, if you want to enable scrolling, you'll have to remove this line from your code

chart_scattered.setTouchEnabled(false);

I am assuming you used the above line so the user can't highlight the bars. To keep that behavior, use this function..

chart_scattered.setHighlightPerTapEnabled(false); 

Upvotes: 1

Related Questions