Skip to content

Commit

Permalink
Allow custom legend data
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed May 19, 2015
1 parent d8af192 commit c3cae65
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ public void notifyDataSetChanged() {

mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());

mLegendRenderer.computeLegend(mData);
if (mLegend != null && !mLegend.isLegendCustom())
mLegendRenderer.computeLegend(mData);

calculateOffsets();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public void notifyDataSetChanged() {

calcMinMax();

mLegendRenderer.computeLegend(mData);
if (mLegend != null && !mLegend.isLegendCustom())
mLegendRenderer.computeLegend(mData);

calculateOffsets();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public void notifyDataSetChanged() {
mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum);
mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());

mLegendRenderer.computeLegend(mData);
if (mLegend != null && !mLegend.isLegendCustom())
mLegendRenderer.computeLegend(mData);

calculateOffsets();
}
Expand Down
55 changes: 55 additions & 0 deletions MPChartLib/src/com/github/mikephil/charting/components/Legend.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public enum LegendDirection {
/** labels that will be appended to the end of the labels array after calculating the legend. a null label will start a group. */
private String[] mExtraLabels;

/**
* Are the legend labels/colors a custom value or auto calculated? If false, then it's auto, if true, then custom.
* default false (automatic legend)
*/
private boolean mIsLegendCustom = false;

/** the position relative to the chart the legend is drawn on */
private LegendPosition mPosition = LegendPosition.BELOW_CHART_LEFT;

Expand Down Expand Up @@ -257,6 +263,55 @@ public void setExtraLabels(String[] labels) {
this.mExtraLabels = labels;
}

/**
* Sets a custom legend's labels and colors arrays.
* The colors count should match the labels count.
* * Each color is for the form drawn at the same index.
* * A null label will start a group.
* * A (-2) color will avoid drawing a form
* This will disable the feature that automatically calculates the legend labels and colors from the datasets.
* Call resetLegendToAuto(...) to re-enable automatic calculation.
*/
public void setLegend(int[] colors, String[] labels)
{
mLabels = labels;
mColors = colors;
mIsLegendCustom = true;
}

/**
* Sets a custom legend's labels and colors arrays.
* The colors count should match the labels count.
* * Each color is for the form drawn at the same index.
* * A null label will start a group.
* * A (-2) color will avoid drawing a form
* This will disable the feature that automatically calculates the legend labels and colors from the datasets.
* Call resetLegendToAuto(...) to re-enable automatic calculation (and then notifyDataSetChanged() is needed)
*/
public void setLegend(List<Integer> colors, List<String> labels)
{
mColors = Utils.convertIntegers(colors);
mLabels = Utils.convertStrings(labels);
mIsLegendCustom = true;
}

/**
* Calling this will disable the custom legend labels (set by setLegend(...)). Instead, the labels will again be calculated automatically (after notifyDataSetChanged() is called).
*/
public void resetLegendToAuto()
{
mIsLegendCustom = false;
}

/**
* @return true if a custom legend labels and colors has been set
* default false (automatic legend)
*/
public boolean isLegendCustom()
{
return mIsLegendCustom;
}

/**
* returns the position of the legend relative to the chart
*
Expand Down

0 comments on commit c3cae65

Please sign in to comment.