MPAndroidChart:如何为BarChart添加图例(解释使用的颜色的文字)?

时间:2017-01-19 10:55:57

标签: android bar-chart mpandroidchart

我正在使用MPAndroidChart库来绘制BarChart,我使用3种颜色来绘制条形图,基于条形值。

我想显示解释每种颜色(即图例)的文字,如下图所示。

a bar chart with 3 colors and a legend on the side labelling the green color "company A", the blue color "company B" etc.

在示例图片中,我想要的文字是“公司A”,“公司B”,“公司C”。

1 个答案:

答案 0 :(得分:1)

您可以使用setCustom()

Legend l = mChart.getLegend();
l.setCustum(colorArray, labelArray);


 /**
 * 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 ColorTemplate.COLOR_SKIP
 * color will avoid drawing a form This will disable the feature that
 * automatically calculates the legend labels and colors from the datasets.
 * Call resetCustom() to re-enable automatic calculation (and then
 * notifyDataSetChanged() is needed to auto-calculate the legend again)
 */