仅使用Mpchart在android中显示的第一个和最后一个标签

时间:2020-04-13 08:08:25

标签: android bar-chart

enter image description here have a look bars are not displaying properly我有一个包含大约10个x轴标签的数据集。我的条形图未在我的x轴上显示所有标签。

仅显示第一个和最后一个标签。我必须在条形图中显示所有10个x轴标签。

下面是图片的参考when I do pinchzoom with fingers that time I can able to see the remaing Labels.But bars not displaying properly

     private void setData_chart(int count, float range) {

        float barWidth = 10f;
        float spaceForBar = 12f;
        float groupSpace = 0.06f;
        float barSpace = 0.02f;


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

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range);
            values.add(new BarEntry(i * spaceForBar, val,
                    getResources().getDrawable(R.drawable.ic_launcher)));
        }

        BarDataSet set1;
        if (chart1.getData() != null &&
                chart1.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) chart1.getData().getDataSetByIndex(0);
            set1.setValues(values);
            chart1.getData().notifyDataChanged();
            chart1.notifyDataSetChanged();
        }
        else {
            set1 = new BarDataSet(values, "taskTypes");


            set1.setDrawIcons(false);

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

            BarData data = new BarData(dataSets);
            data.setValueTextSize(2f);
            data.setValueTypeface(tfLight);
            data.setBarWidth(barWidth);
            chart1.setData(data);
           // chart1.groupBars(0f, groupSpace, barSpace);
            chart1.setFitBars(true);
            chart.setData(data);
            chart.setFitBars(true);
            chart_modality.setData(data);
            chart_modality.setFitBars(true);
            chart_support.setData(data);
            chart_support.setFitBars(true);
        }
    }

    public class CustomXAxisRenderer extends XAxisRenderer {
        public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis1, Transformer trans) {
            super(viewPortHandler, xAxis1, trans);
        }

        @Override
        protected void drawLabel(Canvas c, String formattedLabel, float x, float y,
                                 MPPointF anchor, float angleDegrees) {
            String line[] = formattedLabel.split("\n");
            Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
            for (int i = 1; i < line.length; i++) { // we've already processed 1st line
                Utils.drawXAxisValue(c, line[i], x, y + mAxisLabelPaint.getTextSize() * i,
                        mAxisLabelPaint, anchor, angleDegrees);
            }
        }
    }
 xAxis1.setValueFormatter(new ValueFormatter() {
                                            @Override
                                            public String getFormattedValue(float value) {

                                                    if (value < 0 || value > taskTypes.size() - 1) {
                                                        return "";
                                                    }
                                                    String valueStr = String.valueOf(taskTypes);
                                                    String[] taskList= valueStr.split(",");
                                                    return taskList[(int)value];
                                            }
                                        });
                                        xAxis1.setGranularity(1);
                                        xAxis1.setCenterAxisLabels(true);
                                        xAxis1.setTextSize(8f);
                                        xAxis1.setLabelRotationAngle(-30f);
                                        xAxis1.setLabelCount(taskTypes.size());
                                        xAxis1.setGranularityEnabled(true);

build.gradle:

implementation project(':MPChartLib')

1 个答案:

答案 0 :(得分:0)

您可以尝试这些

1-

@Override
public String getFormattedValue(float value) {
   if (value < 0 || value > taskTypes.size() - 1) {
        return ""
    }
    String valueStr = String.valueOf(taskTypes);
    String[] taskList= valueStr.split(",");
    return taskList[(int)value];
}

2-

xAxis1.setLabelRotationAngle(-50f);

3-

xAxis1.setTextSize(8f);

4-

float barWidth = 0.9f;

5-

values.add(new BarEntry(i, val, getResources().getDrawable(R.drawable.ic_launcher)));

6-

public class CustomXAxisRenderer extends XAxisRenderer {
public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
super(viewPortHandler, xAxis, trans);
}

@Override
protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
String line[] = formattedLabel.split(" ");
Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
if (line.length >1)
Utils.drawXAxisValue(c, line[1], x + mAxisLabelPaint.getTextSize(), y + mAxisLabelPaint.getTextSize(), mAxisLabelPaint, anchor, angleDegrees);
}
}