[graph] Stop copying the trace name (#21211)

This commit is contained in:
J. Nick Koston
2026-10-20 15:33:38 -10:00
committed by GitHub
parent e70b0ace13
commit 7fe4352786
2 changed files with 12 additions and 12 deletions
+8 -8
View File
@@ -47,7 +47,7 @@ void HistoryData::take_sample(float data) {
}
void GraphTrace::init(Graph *g) {
ESP_LOGI(TAG, "Init trace for sensor %s", this->get_name().c_str());
ESP_LOGI(TAG, "Init trace for sensor %s", this->name_);
this->data_.init(g->get_width());
sensor_->add_on_state_callback([this](float state) { this->data_.take_sample(state); });
this->data_.set_update_time_ms(g->get_duration() * 1000 / g->get_width());
@@ -220,16 +220,16 @@ void GraphLegend::init(Graph *g) {
int valw = 0, valh = 0;
int lt = 0;
for (auto *trace : g->traces_) {
std::string txtstr = trace->get_name();
const char *txtstr = trace->name_;
int fw, fos, fbl, fh;
this->font_label_->measure(txtstr.c_str(), &fw, &fos, &fbl, &fh);
this->font_label_->measure(txtstr, &fw, &fos, &fbl, &fh);
if (fw > txtw)
txtw = fw;
if (fh > txth)
txth = fh;
if (trace->get_line_thickness() > lt)
lt = trace->get_line_thickness();
ESP_LOGI(TAGL, " %s %d %d", txtstr.c_str(), fw, fh);
ESP_LOGI(TAGL, " %s %d %d", txtstr, fw, fh);
if (this->values_ != VALUE_POSITION_TYPE_NONE) {
char valstr[VALUE_ACCURACY_MAX_LEN];
@@ -351,10 +351,10 @@ void Graph::draw_legend(display::Display *buff, uint16_t x_offset, uint16_t y_of
int x = x_offset + legend_->x0_;
int y = y_offset;
for (auto *trace : traces_) {
std::string txtstr = trace->get_name();
ESP_LOGV(TAG, " %s", txtstr.c_str());
const char *txtstr = trace->name_;
ESP_LOGV(TAG, " %s", txtstr);
buff->printf(x, y, legend_->font_label_, trace->get_line_color(), TextAlign::TOP_CENTER, "%s", txtstr.c_str());
buff->printf(x, y, legend_->font_label_, trace->get_line_color(), TextAlign::TOP_CENTER, "%s", txtstr);
if (legend_->lines_) {
uint16_t thick = trace->get_line_thickness();
@@ -393,7 +393,7 @@ void Graph::setup() {
void Graph::dump_config() {
for (auto *trace : traces_) {
ESP_LOGCONFIG(TAG, "Graph for sensor %s", trace->get_name().c_str());
ESP_LOGCONFIG(TAG, "Graph for sensor %s", trace->name_);
}
}
+4 -4
View File
@@ -1,9 +1,9 @@
#pragma once
#include <cstdint>
#include <utility>
#include <vector>
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/color.h"
#include "esphome/core/string_ref.h"
#include "esphome/core/component.h"
namespace esphome {
@@ -108,7 +108,7 @@ class HistoryData {
class GraphTrace final {
public:
void init(Graph *g);
void set_name(std::string name) { name_ = std::move(name); }
void set_name(const char *name) { this->name_ = name; }
void set_sensor(sensor::Sensor *sensor) { sensor_ = sensor; }
uint8_t get_line_thickness() { return this->line_thickness_; }
void set_line_thickness(uint8_t val) { this->line_thickness_ = val; }
@@ -118,12 +118,12 @@ class GraphTrace final {
void set_line_color(Color val) { this->line_color_ = val; }
bool get_continuous() { return this->continuous_; }
void set_continuous(bool continuous) { this->continuous_ = continuous; }
std::string get_name() { return name_; }
StringRef get_name() const { return StringRef(this->name_); }
const HistoryData *get_tracedata() { return &data_; }
protected:
sensor::Sensor *sensor_{nullptr};
std::string name_;
const char *name_{""};
uint8_t line_thickness_{3};
enum LineType line_type_ { LINE_TYPE_SOLID };
Color line_color_{COLOR_ON};