Подтвердить что ты не робот

MPAndroidChart - метки легенд обрезаются

Я использую библиотеку MPAndroidChart. У кого-нибудь есть эта проблема? Когда я помещаю метки в положение BOTTOM, они вырезаются.

Спасибо

enter image description here

4b9b3361

Ответ 1

Они обрезаются из-за того, что ваш текст слишком длинный и библиотека не поддерживает "перенос" меток на новую строку.

Вам придется либо сократить ярлыки легенды, либо самостоятельно реализовать нужные функции.

ОБНОВИТЬ:

Перенос слов для Legend теперь поддерживается.

chart.getLegend().setWordWrapEnabled(true);

Ответ 2

Кажется, это новая функция с июня (2015):

chart.getLegend().setWordWrapEnabled(true);

Javadoc:

/**
 * Should the legend word wrap? / this is currently supported only for:
 * BelowChartLeft, BelowChartRight, BelowChartCenter. / note that word
 * wrapping a legend takes a toll on performance. / you may want to set
 * maxSizePercent when word wrapping, to set the point where the text wraps.
 * / default: false
 * 
 * @param enabled
 */
public void setWordWrapEnabled(boolean enabled) {
    mWordWrapEnabled = enabled;
}

Ответ 3

Вы должны выполнить настройку легенд с их цветами и ярлыками, выполнив шаги Шаг 1

Legend legend = mChart.getLegend();

Шаг 2

int colorcodes[] = legend.Colors();

Шаги 3

for (int i = 0; i <  legend.Colors().length-1; i++) {
 .....
 .....
 }

Шаги 4

Затем вы должны взять один макет по горизонтали или вертикали. Вы должны получить цветовые коды легенд и ярлык легенды и в соответствии с длиной легенды создать макет и метку. Пример кода приведен ниже

        LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        parms_left_layout.weight = 1F;
        LinearLayout left_layout = new LinearLayout(context);
        left_layout.setOrientation(LinearLayout.HORIZONTAL);
        left_layout.setGravity(Gravity.CENTER);
        left_layout.setLayoutParams(parms_left_layout);

        LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
                20, 20);
        parms_legen_layout.setMargins(0, 0, 20, 0);
        LinearLayout legend_layout = new LinearLayout(context);
        legend_layout.setLayoutParams(parms_legen_layout);
        legend_layout.setOrientation(LinearLayout.HORIZONTAL);
        legend_layout.setBackgroundColor(colorcodes[i]);
        left_layout.addView(legend_layout);

        TextView txt_unit = new TextView(context);
        txt_unit.setText(legend.getLabel(i));

Надеюсь, что это поможет вам

Ответ 4

Здесь я покажу вам простой способ "Традиционный способ Android", это довольно просто, мой код ниже:

<LinearLayout
    android:id="@+id/i_am_chart_view_container"
    ...
    android:paddingRight="20dp"
    android:clipChildren="false"
    android:clipToPadding="false"
    .../>

Просто добавьте немного padding в макет контейнера или несколько margin в представление диаграммы и, наконец, установите clipChildren & clipToPadding в значение false.

Результат ниже:

синяя область - это отступы или область поля.

enter image description here

Ответ 5

 Legend l = pieChart.getLegend();
 l.setPosition(LegendPosition.BELOW_CHART_LEFT);
 l.setXEntrySpace(7f);
 l.setYEntrySpace(0f);
 l.setYOffset(0f);
 l.setDirection(LegendDirection.LEFT_TO_RIGHT);
 l.setWordWrapEnabled(true);

Ответ 6

Только для поддержки содержимого обертки. Показ легенд, когда позиция легенды BelowChartLeft, BelowChartRight, BelowChartCenter

     Legend legend = pieChart.getLegend();
        legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
        legend.setWordWrapEnabled(true);

После этого набора данных

 pieChart.setData(pieData)

он отлично работает

Ответ 7

Чтобы избежать отсечения значений легенды, используйте следующий блок кода

Legend legend=lineChart.getLegend();
legend.setWordWrapEnabled(true);

Doc (для легенды):

/**
     * Should the legend word wrap? / this is currently supported only for:
     * BelowChartLeft, BelowChartRight, BelowChartCenter. / note that word
     * wrapping a legend takes a toll on performance. / you may want to set
     * maxSizePercent when word wrapping, to set the point where the text wraps.
     * / default: false
     * 
     * @param enabled
     */
    public void setWordWrapEnabled(boolean enabled) {
        mWordWrapEnabled = enabled;
    }

Чтобы избежать обрезания меток оси x, используйте

XAxis xAxis = lineChart.getXAxis();
xAxis.setAvoidFirstLastClipping(true);

Doc (для меток по оси x):

/**
     * if set to true, the chart will avoid that the first and last label entry
     * in the chart "clip" off the edge of the chart or the screen
     * 
     * @param enabled
     */
    public void setAvoidFirstLastClipping(boolean enabled) {
        mAvoidFirstLastClipping = enabled;
    }

Ответ 8

После долгого исследования я нашел решение. Следующий код решил это.

chart.getLegend() setWordWrapEnabled (истина);.

Ответ 9

Попробуйте это:

Legend l = pieChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setXEntrySpace(4f);
l.setYEntrySpace(0f);
l.setWordWrapEnabled(true);