[python] matplotlib port: implement bar method

This commit is contained in:
Émilie Feral
2020-03-17 13:46:08 +01:00
parent 373a85f2b7
commit 0ae81374d9
7 changed files with 43 additions and 2 deletions

View File

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

View File

@@ -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; i<length; i++) {
mp_float_t rectX = mp_obj_get_float(xItems[i])-w/2.0;
mp_float_t h = mp_obj_get_float(hItems[i]);
mp_float_t rectY = h < 0.0 ? 0.0 : h;
sPlotStore->addRect(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) {

View File

@@ -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();

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_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) },

View File

@@ -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>;
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<PlotStore::Label>;

View File

@@ -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<ListIterator<Rect>> rects() { return Iterable<ListIterator<Rect>>(m_segments); }
Iterable<ListIterator<Rect>> rects() { return Iterable<ListIterator<Rect>>(m_rects); }
// Label

View File

@@ -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 {