From 8b6a35845250deee6021fcd993467023f35d7f49 Mon Sep 17 00:00:00 2001 From: chiahsing Date: Mon, 29 Apr 2024 03:49:27 +0800 Subject: [PATCH] Fixed the issue that graph draws out of the boundary. (#6651) --- esphome/components/graph/graph.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/esphome/components/graph/graph.cpp b/esphome/components/graph/graph.cpp index 1178af911d..09f7557714 100644 --- a/esphome/components/graph/graph.cpp +++ b/esphome/components/graph/graph.cpp @@ -177,22 +177,26 @@ void Graph::draw(Display *buff, uint16_t x_offset, uint16_t y_offset, Color colo bool b = (trace->get_line_type() & bit) == bit; if (b) { int16_t y = (int16_t) roundf((this->height_ - 1) * (1.0 - v)) - thick / 2 + y_offset; + auto draw_pixel_at = [&buff, c, y_offset, this](int16_t x, int16_t y) { + if (y >= y_offset && y < y_offset + this->height_) + buff->draw_pixel_at(x, y, c); + }; if (!continuous || !has_prev || !prev_b || (abs(y - prev_y) <= thick)) { for (int16_t t = 0; t < thick; t++) { - buff->draw_pixel_at(x, y + t, c); + draw_pixel_at(x, y + t); } } else { int16_t mid_y = (y + prev_y + thick) / 2; if (y > prev_y) { for (int16_t t = prev_y + thick; t <= mid_y; t++) - buff->draw_pixel_at(x + 1, t, c); + draw_pixel_at(x + 1, t); for (int16_t t = mid_y + 1; t < y + thick; t++) - buff->draw_pixel_at(x, t, c); + draw_pixel_at(x, t); } else { for (int16_t t = prev_y - 1; t >= mid_y; t--) - buff->draw_pixel_at(x + 1, t, c); + draw_pixel_at(x + 1, t); for (int16_t t = mid_y - 1; t >= y; t--) - buff->draw_pixel_at(x, t, c); + draw_pixel_at(x, t); } } prev_y = y;