2828import com .github .mikephil .charting .data .ChartData ;
2929import com .github .mikephil .charting .data .DataSet ;
3030import com .github .mikephil .charting .data .Entry ;
31+ import com .github .mikephil .charting .data .PieDataSet ;
3132import com .github .mikephil .charting .interfaces .OnChartValueSelectedListener ;
3233import com .github .mikephil .charting .utils .Highlight ;
3334import com .github .mikephil .charting .utils .Legend ;
@@ -433,6 +434,7 @@ public void prepareLegend() {
433434 ArrayList <String > labels = new ArrayList <String >();
434435 ArrayList <Integer > colors = new ArrayList <Integer >();
435436
437+ // loop for building up the colors and labels used in the legend
436438 for (int i = 0 ; i < mOriginalData .getDataSetCount (); i ++) {
437439
438440 DataSet dataSet = mOriginalData .getDataSetByIndex (i );
@@ -452,9 +454,25 @@ public void prepareLegend() {
452454 colors .add (clrs .get (j ));
453455 }
454456
457+ // add the legend description label
455458 colors .add (-1 );
456459 labels .add (bds .getLabel ());
457460
461+ } else if (dataSet instanceof PieDataSet ) {
462+
463+ ArrayList <String > xVals = mOriginalData .getXVals ();
464+ PieDataSet pds = (PieDataSet ) dataSet ;
465+
466+ for (int j = 0 ; j < clrs .size () && j < entryCount && j < xVals .size () ; j ++) {
467+
468+ labels .add (xVals .get (j ));
469+ colors .add (clrs .get (j ));
470+ }
471+
472+ // add the legend description label
473+ colors .add (-1 );
474+ labels .add (pds .getLabel ());
475+
458476 } else { // all others
459477
460478 for (int j = 0 ; j < clrs .size () && j < entryCount ; j ++) {
@@ -474,9 +492,6 @@ public void prepareLegend() {
474492 }
475493 }
476494
477- // Log.i(LOG_TAG, "Preparing legend, colors size: " + colors.size() +
478- // ", labels size: " + labels.size());
479-
480495 Legend l = new Legend (colors , labels );
481496
482497 if (mLegend != null ) {
@@ -650,7 +665,8 @@ protected void drawLegend() {
650665 float formToTextSpace = mLegend .getFormToTextSpace () + formSize ;
651666
652667 // space between the entries
653- float entrySpace = mLegend .getEntrySpace () + formSize ;
668+ float xEntrySpace = mLegend .getXEntrySpace () + formSize ;
669+ float yEntrySpace = mLegend .getYEntrySpace () + formSize ;
654670
655671 float textSize = mLegendLabelPaint .getTextSize ();
656672
@@ -681,9 +697,9 @@ protected void drawLegend() {
681697 if (mLegend .getColors ()[i ] != -1 ) posX += formToTextSpace ;
682698
683699 mLegend .drawLabel (mDrawCanvas , posX , posY + textDrop , mLegendLabelPaint , i );
684- posX += Utils .calcTextWidth (mLegendLabelPaint , labels [i ]) + entrySpace ;
700+ posX += Utils .calcTextWidth (mLegendLabelPaint , labels [i ]) + xEntrySpace ;
685701 } else {
686- posX += entrySpace ;
702+ posX += xEntrySpace ;
687703 }
688704 }
689705
@@ -705,14 +721,14 @@ protected void drawLegend() {
705721 mLegend .drawForm (mDrawCanvas , posX , posY , mLegendFormPaint , i );
706722
707723 // make a step to the left
708- posX -= entrySpace ;
724+ posX -= xEntrySpace ;
709725 }
710726
711727 break ;
712728 case RIGHT_OF_CHART :
713729
714730 if (this instanceof BarLineChartBase ) {
715- posX = getWidth () - mLegend .getOffsetRight () + Utils .convertDpToPixel (10f );
731+ posX = getWidth () - mLegend .getOffsetRight () + Utils .convertDpToPixel (9f );
716732 posY = mLegend .getOffsetTop ();
717733 } else {
718734 posX = getWidth () - mLegend .getMaximumEntryLength (mLegendLabelPaint );
@@ -733,20 +749,20 @@ protected void drawLegend() {
733749 float x = posX ;
734750
735751 if (mLegend .getColors ()[i ] != -1 ) x += formToTextSpace ;
736-
737752
738753 mLegend .drawLabel (mDrawCanvas , x , posY + textDrop ,
739754 mLegendLabelPaint , i );
740755 } else {
741756
742757 mLegend .drawLabel (mDrawCanvas , posX , posY + textSize + formSize
743- + mLegend .getEntrySpace (),
744- mLegendLabelPaint , i );
745- posY += entrySpace ;
746- }
758+ + mLegend .getYEntrySpace (),
759+ mLegendLabelPaint , i );
760+
761+ posY += yEntrySpace ;
762+ }
747763
748764 // make a step down
749- posY += entrySpace + textSize ;
765+ posY += mLegend . getYEntrySpace () + textSize ;
750766 stack = 0f ;
751767 } else {
752768 stack += formSize + 4f ;
0 commit comments