MPAndroidChart:如何为条形图添加图例(解释所用颜色的文本)?

MPAndroidChart: How to add legend (text explaining colors used) for BarChart?

我正在使用 MPAndroidChart 库绘制条形图,我使用 3 种三种颜色根据条形值绘制条形图。

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

在示例图片中,我想要的文字是 "Company A"、"Company B"、"Company C"。

您可以使用 setCustom() 来实现。

Legend l = mChart.getLegend();
l.setCustom(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)
 */