You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MPAndroidChart:如何在LineChart的LimitLines间填充颜色并设置图例

解决MPAndroidChart LineChart LimitLines间填充颜色及自定义图例问题

一、LimitLines之间填充颜色

通过自定义LineChartRenderer,在绘制折线数据前先绘制填充区域,避免遮挡折线:

  1. 自定义Renderer类
public class CustomLineChartRenderer extends LineChartRenderer {
    private final Paint fillPaint;
    private LimitLine upperLimitLine;
    private LimitLine lowerLimitLine;

    public CustomLineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
        fillPaint = new Paint();
        fillPaint.setStyle(Paint.Style.FILL);
        // 设置半透明填充色,避免遮挡折线
        fillPaint.setColor(Color.argb(120, 255, 0, 0));
    }

    // 设置需要填充的上下限LimitLine
    public void setTargetLimitLines(LimitLine upper, LimitLine lower) {
        this.upperLimitLine = upper;
        this.lowerLimitLine = lower;
    }

    @Override
    public void drawData(Canvas c) {
        // 先绘制填充区域
        drawLimitAreaFill(c);
        // 再绘制折线数据
        super.drawData(c);
    }

    private void drawLimitAreaFill(Canvas c) {
        if (upperLimitLine == null || lowerLimitLine == null) return;

        LineChart chart = (LineChart) mChart;
        YAxis leftAxis = chart.getAxisLeft();

        // 将LimitLine数值转换为屏幕像素坐标
        float upperY = leftAxis.getPixelForValue(upperLimitLine.getLimit());
        float lowerY = leftAxis.getPixelForValue(lowerLimitLine.getLimit());

        // 处理上下限的屏幕坐标顺序(屏幕Y轴向下递增,数值大的坐标更小)
        float fillTop = Math.min(upperY, lowerY);
        float fillBottom = Math.max(upperY, lowerY);

        // 获取图表内容区域的左右边界
        float contentLeft = mViewPortHandler.contentLeft();
        float contentRight = mViewPortHandler.contentRight();

        // 绘制填充矩形
        c.drawRect(contentLeft, fillTop, contentRight, fillBottom, fillPaint);
    }
}
  1. 在页面中配置Renderer
LineChart lineChart = findViewById(R.id.line_chart);

// 1. 创建并添加LimitLines到Y轴
LimitLine upperLimit = new LimitLine(80f, "上限");
upperLimit.setLineColor(Color.RED);
LimitLine lowerLimit = new LimitLine(20f, "下限");
lowerLimit.setLineColor(Color.GREEN);

YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.addLimitLine(upperLimit);
leftAxis.addLimitLine(lowerLimit);

// 2. 设置自定义Renderer
CustomLineChartRenderer customRenderer = new CustomLineChartRenderer(
        lineChart, lineChart.getAnimator(), lineChart.getViewPortHandler()
);
customRenderer.setTargetLimitLines(upperLimit, lowerLimit);
lineChart.setRenderer(customRenderer);

// 3. 加载折线数据等其他配置
// ...

二、自定义图例(匹配需求样式)

利用MPAndroidChart自带的Legend组件自定义条目,无需额外TextView:

Legend legend = lineChart.getLegend();
// 配置图例位置与基础样式
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(false);
legend.setTextSize(12f);
legend.setFormSize(14f); // 设置颜色块大小

// 创建自定义图例条目
List<LegendEntry> legendEntries = new ArrayList<>();

// 填充区域对应的图例条目
LegendEntry fillEntry = new LegendEntry();
fillEntry.formColor = Color.argb(120, 255, 0, 0); // 和填充色保持一致
fillEntry.label = "正常区间";
legendEntries.add(fillEntry);

// 折线数据对应的图例条目
LegendEntry lineEntry = new LegendEntry();
lineEntry.formColor = Color.BLUE; // 与折线颜色一致
lineEntry.label = "实时数据";
legendEntries.add(lineEntry);

// 设置自定义图例
legend.setCustom(legendEntries);

注意事项

  • 若动态修改LimitLines,需调用customRenderer.setTargetLimitLines()更新后,再调用lineChart.invalidate()刷新图表
  • 多区间填充(超过两个LimitLine)可扩展drawLimitAreaFill方法,遍历相邻LimitLine对绘制多个填充矩形
  • 填充色建议使用半透明值,避免遮挡折线、坐标轴文字等内容

内容的提问来源于stack exchange,提问作者karanatwal.github.io

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:26:00