27

I am using MPAndroidChart library. Anybody has this problem? When I put the labels in BOTTOM position, these are cut.

Thank you

enter image description here

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56

9 Answers9

52

This seems to be a new feature since June (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;
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
33

They are cut because your text is too long and the library does not support "wrapping" of the labels to a new line.

You will either have to shorten your legend labels or implement the desired functionality yourself.

UPDATE:

Word wrapping for the Legend is now supported.

chart.getLegend().setWordWrapEnabled(true);
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
8

Here I will show you an easy way by "Traditional Android Way", it's quite simple, my code is below:

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

Just need to add some padding to the container layout, or some margin to the chart view, and finally set clipChildren&clipToPadding to false.

Result below:

the blue area is padding or margin area.

enter image description here

codezjx
  • 9,012
  • 5
  • 47
  • 57
7

You have to implement customize legends with their legends colours and lables by following steps Step 1

Legend legend = mChart.getLegend();

Step 2

int colorcodes[] = legend.Colors();

Steps 3

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

Steps 4

Then you have to take one layout horizontal or vertical. You have to get legends color codes and legends lable and according to legends length create layout and label. Code sample is given below

        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));

Hope this will help you

JFreeman
  • 974
  • 1
  • 10
  • 26
Amandeep Rohila
  • 3,558
  • 2
  • 28
  • 34
4

Set wrap content support only Legends postion when legend position is BelowChartLeft, BelowChartRight, BelowChartCenter

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

After this set dataset

 pieChart.setData(pieData)

it will work fine

Vinod Ranga
  • 511
  • 8
  • 15
3
 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);
Linh
  • 57,942
  • 23
  • 262
  • 279
1

To avoid clipping of legend values use the following block of code

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

Doc (for legend):

/**
     * 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;
    }

To avoid clipping of x-axis labels use

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

Doc (for x-axis labels):

/**
     * 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;
    }
1

Try this:

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);
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58
0

After a long research, I found the solution. The below code solved it.

chart.getLegend().setWordWrapEnabled(true);

SFDCCoder
  • 23
  • 4