水平BarChart MpAndroidChart的定位问题

时间:2019-06-11 02:17:20

标签: android mpandroidchart

我正在使用mpAndroidChart来进行简单的水平条形图。只有一个BarDataSet,问题是barChart没有居中,并且第一个条被剪切。

enter image description here

这是我的代码:

  barWidth = 0.8f;
        barSpace = 0f;
        groupSpace = 0.2f;
        chart = (HorizontalBarChart) rootView.findViewById(R.id.barChart6);
        chart.setDescription(null);
        chart.setPinchZoom(false);
        chart.setScaleEnabled(false);
        chart.setDrawBarShadow(false);
        chart.setDrawGridBackground(false);
                int groupCount = 3;

                ArrayList xVals = new ArrayList();


                ArrayList yVals1 = new ArrayList();



                    xVals.add(getResources().getString(R.string.current_year));
                    yVals1.add(new BarEntry(1, (float) fromJan));
  xVals.add(getResources().getString(R.string.last_month));
                   yVals1.add(new BarEntry(2, (float) lastMonth));
                    xVals.add(getResources().getString(R.string.current_month));
                    yVals1.add(new BarEntry(3, (float) currentMonth));

                BarDataSet set1;
                set1 = new BarDataSet(yVals1, "Total sick days");
                set1.setColor(mContext.getResources().getColor( R.color.blue_t ));

                BarData data = new BarData(set1);
                data.setValueFormatter(new MyValueFormatter());
                data.setValueTextSize(10f);
                chart.setData(data);
                chart.getBarData().setBarWidth(barWidth);
                chart.getXAxis().setAxisMinimum(0);
                chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
                chart.getData().setHighlightEnabled(false);

                chart.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                chart.getRenderer().getPaintRender().setShadowLayer(3, 6, 6, Color.GRAY);
                chart.animateY(1000);
                chart.invalidate();   

                //X-axis
                XAxis xAxis = chart.getXAxis();
                xAxis.setGranularity(1f);
                xAxis.setGranularityEnabled(true);
                xAxis.setCenterAxisLabels(true);
                xAxis.setDrawGridLines(true);
                xAxis.setTextSize(12f);
                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                xAxis.setLabelRotationAngle(-90);
                xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
                xAxis.setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
                //Y-axis
                chart.getAxisRight().setEnabled(false);
                YAxis leftAxis = chart.getAxisLeft();
                leftAxis.setValueFormatter(new MyValueFormatter());
                leftAxis.setDrawGridLines(true);
                leftAxis.setSpaceTop(35f);
                leftAxis.setAxisMinimum(0f);
                leftAxis.setCenterAxisLabels(true);

所以我的问题如下:如何解决此问题并使标签和条形位置匹配?

0 个答案:

没有答案