[python/matplotlib] Add grid control

This commit is contained in:
Romain Goyet
2020-03-11 17:53:19 -04:00
committed by Émilie Feral
parent a3b1b51f75
commit a062c570d5
7 changed files with 34 additions and 13 deletions

View File

@@ -75,6 +75,7 @@ Q(set_pixel)
// Matplotlib QSTRs
Q(axis)
Q(grid)
Q(plot)
Q(pyplot)
Q(scatter)

View File

@@ -8,7 +8,7 @@ extern "C" {
Matplotlib::PlotStore * sPlotStore = nullptr;
Matplotlib::PlotController * sPlotController = nullptr;
static int paletteIndex = 0; // FIXME: Needs to be reset at some point
static int paletteIndex = 0;
// Internal functions
@@ -18,6 +18,7 @@ mp_obj_t modpyplot___init__() {
sPlotStore = &plotStore;
sPlotController = &plotController;
sPlotStore->flush();
paletteIndex = 0;
return mp_const_none;
}
@@ -41,7 +42,7 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) {
#warning Use mp_obj_is_bool when upgrading uPy
if (mp_obj_is_type(arg, &mp_type_bool)) {
sPlotStore->setGrid(mp_obj_is_true(arg));
sPlotStore->setAxesRequested(mp_obj_is_true(arg));
} else {
mp_obj_t * items;
mp_obj_get_array_fixed_n(arg, 4, &items);
@@ -60,6 +61,15 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) {
return mp_obj_new_tuple(4, coords);
}
mp_obj_t modpyplot_grid(mp_obj_t b) {
if (mp_obj_is_type(b, &mp_type_bool)) {
sPlotStore->setGridRequested(mp_obj_is_true(b));
} else {
sPlotStore->setGridRequested(!sPlotStore->gridRequested());
}
return mp_const_none;
}
mp_obj_t modpyplot_scatter(mp_obj_t x, mp_obj_t y) {
assert(sPlotStore != nullptr);

View File

@@ -4,10 +4,11 @@ mp_obj_t modpyplot___init__();
void modpyplot_gc_collect();
mp_obj_t modpyplot_axis(mp_obj_t arg);
mp_obj_t modpyplot_grid(mp_obj_t b);
mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y);
mp_obj_t modpyplot_scatter(mp_obj_t x, mp_obj_t y);
mp_obj_t modpyplot_text(mp_obj_t x, mp_obj_t y, mp_obj_t s);
//mp_obj_t grid();
//mp_obj_t arrow();
//mp_obj_t bar();
//mp_obj_t hist();
mp_obj_t modpyplot_show();

View File

@@ -2,6 +2,7 @@
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modpyplot___init___obj, modpyplot___init__);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modpyplot_axis_obj, modpyplot_axis);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modpyplot_grid_obj, modpyplot_grid);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(modpyplot_plot_obj, modpyplot_plot);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(modpyplot_scatter_obj, modpyplot_scatter);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modpyplot_show_obj, modpyplot_show);
@@ -11,6 +12,7 @@ STATIC const mp_rom_map_elem_t modpyplot_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyplot) },
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&modpyplot___init___obj) },
{ MP_ROM_QSTR(MP_QSTR_axis), MP_ROM_PTR(&modpyplot_axis_obj) },
{ MP_ROM_QSTR(MP_QSTR_grid), MP_ROM_PTR(&modpyplot_grid_obj) },
{ MP_ROM_QSTR(MP_QSTR_plot), MP_ROM_PTR(&modpyplot_plot_obj) },
{ MP_ROM_QSTR(MP_QSTR_scatter), MP_ROM_PTR(&modpyplot_scatter_obj) },
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&modpyplot_show_obj) },

View File

@@ -3,7 +3,8 @@
namespace Matplotlib {
PlotStore::PlotStore() : Shared::InteractiveCurveViewRange(),
m_grid(false)
m_axesRequested(true),
m_gridRequested(false)
{
flush();
}

View File

@@ -96,15 +96,18 @@ public:
void addLabel(mp_obj_t x, mp_obj_t y, mp_obj_t string);
Iterable<ListIterator<Label>> labels() { return Iterable<ListIterator<Label>>(m_labels); }
void setGrid(bool grid) { m_grid = grid; }
bool grid() { return m_grid; }
void setAxesRequested(bool b) { m_axesRequested = b; }
bool axesRequested() const { return m_axesRequested; }
void setGridRequested(bool b) { m_gridRequested = b; }
bool gridRequested() const { return m_gridRequested; }
private:
mp_obj_t m_dots; // List of (x, y, color)
mp_obj_t m_labels; // List of (x, y, string)
mp_obj_t m_segments; // List of (x, y, dx, dy, style, color)
mp_obj_t m_rects; // List of (x, y, w, h, color)
bool m_grid;
bool m_axesRequested;
bool m_gridRequested;
};
}

View File

@@ -4,12 +4,16 @@ namespace Matplotlib {
void PlotView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(rect, KDColorWhite);
if (m_store->grid()) {
if (m_store->gridRequested()) {
drawGrid(ctx, rect);
}
drawAxes(ctx, rect);
drawLabelsAndGraduations(ctx, rect, Axis::Vertical, true);
drawLabelsAndGraduations(ctx, rect, Axis::Horizontal, true);
if (m_store->axesRequested()) {
drawAxes(ctx, rect);
drawLabelsAndGraduations(ctx, rect, Axis::Vertical, true);
drawLabelsAndGraduations(ctx, rect, Axis::Horizontal, true);
}
for (PlotStore::Dot dot : m_store->dots()) {
drawDot(ctx, rect, dot.x(), dot.y(), dot.color());
@@ -32,7 +36,6 @@ void PlotView::drawRect(KDContext * ctx, KDRect rect) const {
segment.color()
);
}
}
}