From 0ae81374d901ee8b8e4eb1fa4674f2c129d2156a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 17 Mar 2020 13:46:08 +0100 Subject: [PATCH] [python] matplotlib port: implement bar method --- python/port/genhdr/qstrdefs.in.h | 2 ++ python/port/mod/matplotlib/modpyplot.cpp | 12 +++++++++++ python/port/mod/matplotlib/modpyplot.h | 1 - python/port/mod/matplotlib/modpyplot_table.c | 2 ++ python/port/mod/matplotlib/plot_store.cpp | 22 ++++++++++++++++++++ python/port/mod/matplotlib/plot_store.h | 2 +- python/port/mod/matplotlib/plot_view.cpp | 4 ++++ 7 files changed, 43 insertions(+), 2 deletions(-) diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 8168d8e69..b67444ed1 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -75,6 +75,8 @@ Q(set_pixel) // Matplotlib QSTRs Q(axis) +Q(bar) +Q(grid) Q(grid) Q(plot) Q(pyplot) diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp index 31ab79470..f17fa3926 100644 --- a/python/port/mod/matplotlib/modpyplot.cpp +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -78,7 +78,19 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) { mp_obj_t modpyplot_bar(mp_obj_t x, mp_obj_t height) { assert(sPlotStore != nullptr); + // Input parameter validation + mp_obj_t * xItems, * hItems; + size_t length = extract_and_validate_plot_input(x, height, &xItems, &hItems); + mp_float_t w = 0.8; // TODO: w should be an optional parameter + KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine + for (size_t i=0; iaddRect(mp_obj_new_float(rectX), mp_obj_new_float(rectY), mp_obj_new_float(w), mp_obj_new_float(std::fabs(h)), color); + } + return mp_const_none; } mp_obj_t modpyplot_grid(mp_obj_t b) { diff --git a/python/port/mod/matplotlib/modpyplot.h b/python/port/mod/matplotlib/modpyplot.h index 777ba7684..00fa667f6 100644 --- a/python/port/mod/matplotlib/modpyplot.h +++ b/python/port/mod/matplotlib/modpyplot.h @@ -10,6 +10,5 @@ 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 arrow(); -//mp_obj_t bar(); //mp_obj_t hist(); mp_obj_t modpyplot_show(); diff --git a/python/port/mod/matplotlib/modpyplot_table.c b/python/port/mod/matplotlib/modpyplot_table.c index 8300fb92c..5516dcde9 100644 --- a/python/port/mod/matplotlib/modpyplot_table.c +++ b/python/port/mod/matplotlib/modpyplot_table.c @@ -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_2(modpyplot_bar_obj, modpyplot_bar); 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); @@ -12,6 +13,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_bar), MP_ROM_PTR(&modpyplot_bar_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) }, diff --git a/python/port/mod/matplotlib/plot_store.cpp b/python/port/mod/matplotlib/plot_store.cpp index 0f2cf3ccb..393087e4d 100644 --- a/python/port/mod/matplotlib/plot_store.cpp +++ b/python/port/mod/matplotlib/plot_store.cpp @@ -12,6 +12,7 @@ PlotStore::PlotStore() : Shared::InteractiveCurveViewRange(), void PlotStore::flush() { m_dots = mp_obj_new_list(0, nullptr); m_segments = mp_obj_new_list(0, nullptr); + m_rects = mp_obj_new_list(0, nullptr); m_labels = mp_obj_new_list(0, nullptr); } @@ -92,6 +93,27 @@ void PlotStore::addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_o mp_obj_list_append(m_segments, tuple); } +// Rect + +template class PlotStore::ListIterator; + +PlotStore::Rect::Rect(mp_obj_t tuple) { + mp_obj_t * elements; + mp_obj_get_array_fixed_n(tuple, 5, &elements); + m_x = mp_obj_get_float(elements[0]); + m_y = mp_obj_get_float(elements[1]); + m_width = mp_obj_get_float(elements[2]); + m_height = mp_obj_get_float(elements[3]); + m_color = KDColor::RGB16(mp_obj_get_int(elements[4])); +} + +void PlotStore::addRect(mp_obj_t x, mp_obj_t y, mp_obj_t width, mp_obj_t height, KDColor c) { + mp_obj_t color = mp_obj_new_int(c); + mp_obj_t items[5] = {x, y, width, height, color}; + mp_obj_t tuple = mp_obj_new_tuple(5, items); + mp_obj_list_append(m_rects, tuple); +} + // Label template class PlotStore::ListIterator; diff --git a/python/port/mod/matplotlib/plot_store.h b/python/port/mod/matplotlib/plot_store.h index 96e6511f9..e5f027028 100644 --- a/python/port/mod/matplotlib/plot_store.h +++ b/python/port/mod/matplotlib/plot_store.h @@ -98,7 +98,7 @@ public: }; void addRect(mp_obj_t x, mp_obj_t y, mp_obj_t width, mp_obj_t height, KDColor c); - Iterable> rects() { return Iterable>(m_segments); } + Iterable> rects() { return Iterable>(m_rects); } // Label diff --git a/python/port/mod/matplotlib/plot_view.cpp b/python/port/mod/matplotlib/plot_view.cpp index 3e7ae4f0c..f2e5ba1d3 100644 --- a/python/port/mod/matplotlib/plot_view.cpp +++ b/python/port/mod/matplotlib/plot_view.cpp @@ -26,6 +26,10 @@ void PlotView::drawRect(KDContext * ctx, KDRect rect) const { for (PlotStore::Segment segment : m_store->segments()) { traceSegment(ctx, rect, segment); } + + for (PlotStore::Rect rectangle : m_store->rects()) { + traceRect(ctx, rect, rectangle); + } } void PlotView::traceDot(KDContext * ctx, KDRect r, PlotStore::Dot dot) const {