DecoratedBarChart - labels and axis with ticks

This commit is contained in:
Sassa NF
2024-09-29 08:29:40 +01:00
parent c78fa6de7a
commit 02df8efa1c
4 changed files with 84 additions and 40 deletions
+59
View File
@@ -90,3 +90,62 @@ int BarChart::y2pos(float y)
return height - height * (y - min_y) / (max_y - min_y);
}
void DecoratedBarChart::draw()
{
bool draw_axis = redraw_all;
BarChart::draw();
display.setColor(BLACK);
display.fillRect(pos_x, text_y, width, pos_y - text_y);
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_LEFT);
for (uint16_t x = 0; x < width; x++)
{
float y = ys[x];
if (y >= level_y)
{
String s = String(ys[x], 0);
uint16_t w = display.getStringWidth(s);
uint16_t x1 = x;
for (; x < x1 + w; x++)
{
if (ys[x] > y)
{
y = ys[x];
s = String(y, 0);
w = max(w, display.getStringWidth(s));
}
}
display.drawString(x1, text_y, s);
}
}
if (draw_axis)
{
display.setColor(WHITE);
uint16_t y = pos_y + height + 1;
display.fillRect(pos_x, y, width, X_AXIS_WEIGHT);
// Start and end ticks
display.fillRect(pos_x, y + 1, 2, AXIS_HEIGHT);
display.fillRect(pos_x + width - 2, y + 1, 2, AXIS_HEIGHT);
for (float step = 0; min_x + step * MAJOR_TICKS < max_x; step += 1)
{
int tick_pos = x2pos(min_x + step * MAJOR_TICKS);
display.drawVerticalLine(pos_x + tick_pos, y + 1, MAJOR_TICK_LENGTH);
}
for (float step = 0; min_x + step * MINOR_TICKS < max_x; step += 1)
{
int tick_pos = x2pos(min_x + step * MINOR_TICKS);
display.drawVerticalLine(pos_x + tick_pos, y + 1, MINOR_TICK_LENGTH);
}
}
}
+21
View File
@@ -64,4 +64,25 @@ struct BarChart : Chart
int x2pos(float x);
int y2pos(float y);
};
#define LABEL_HEIGHT 6
#define X_AXIS_WEIGHT 1
#define MAJOR_TICK_LENGTH 2
#define MAJOR_TICKS 10
#define MINOR_TICK_LENGTH 1
#define MINOR_TICKS 5
#define AXIS_HEIGHT (X_AXIS_WEIGHT + MAJOR_TICK_LENGTH + 1)
struct DecoratedBarChart : BarChart
{
int text_y;
DecoratedBarChart(OLEDDisplay &d, uint16_t x, uint16_t y, uint16_t w, uint16_t h,
float min_x, float max_x, float min_y, float max_y, float level_y)
: BarChart(d, x, y + LABEL_HEIGHT, w, h - LABEL_HEIGHT - AXIS_HEIGHT, min_x,
max_x, min_y, max_y, level_y),
text_y(y) {};
void draw() override;
};
#endif