[pyton/matplotlib] Support plot

This commit is contained in:
Romain Goyet
2020-03-11 17:25:23 -04:00
committed by Émilie Feral
parent a6773ad73b
commit a3b1b51f75
9 changed files with 119 additions and 22 deletions

View File

@@ -417,6 +417,14 @@ void CurveView::drawSegment(KDContext * ctx, KDRect rect, Axis axis, float coord
}
}
void CurveView::drawSegment2(KDContext * ctx, KDRect rect, float x, float y, float u, float v, KDColor color, bool thick) const {
float pxf = floatToPixel(Axis::Horizontal, x);
float pyf = floatToPixel(Axis::Vertical, y);
float puf = floatToPixel(Axis::Horizontal, u);
float pvf = floatToPixel(Axis::Vertical, v);
straightJoinDots(ctx, rect, pxf, pyf, puf, pvf, color, thick);
}
void CurveView::drawDot(KDContext * ctx, KDRect rect, float x, float y, KDColor color, Size size) const {
KDCoordinate diameter = 0;
const uint8_t * mask = nullptr;

View File

@@ -65,6 +65,10 @@ protected:
void drawSegment(KDContext * ctx, KDRect rect, Axis axis,
float coordinate, float lowerBound, float upperBound,
KDColor color, KDCoordinate thickness = 1, KDCoordinate dashSize = -1) const;
void drawSegment2(KDContext * ctx, KDRect rect,
float x, float y, float u, float v,
KDColor color, bool thick = true
) const; // FIXME: Name conflict? This one seems better though...
enum class Size : uint8_t {
Small,
Medium,

View File

@@ -77,6 +77,7 @@ Q(set_pixel)
Q(axis)
Q(plot)
Q(pyplot)
Q(scatter)
Q(show)
Q(text)

View File

@@ -60,7 +60,7 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) {
return mp_obj_new_tuple(4, coords);
}
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) {
assert(sPlotStore != nullptr);
// Input parameter validation
@@ -80,13 +80,36 @@ mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) {
return mp_const_none;
}
mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) {
assert(sPlotStore != nullptr);
assert(sPlotStore != nullptr);
// Input parameter validation
size_t xLength, yLength;
mp_obj_t * xItems, * yItems;
mp_obj_get_array(x, &xLength, &xItems);
mp_obj_get_array(y, &yLength, &yItems);
if (xLength != yLength) {
mp_raise_ValueError("x and y must have same dimension");
}
KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine
for (size_t i=0; i<xLength-1; i++) {
sPlotStore->addSegment(xItems[i], yItems[i], xItems[i+1], yItems[i+1], color);
}
return mp_const_none;
}
mp_obj_t modpyplot_text(mp_obj_t x, mp_obj_t y, mp_obj_t s) {
assert(sPlotStore != nullptr);
// Input parameter validation
mp_obj_get_float(x);
mp_obj_get_float(y);
mp_obj_str_get_str(s);
sPlotStore->addText(x, y, s);
sPlotStore->addLabel(x, y, s);
return mp_const_none;
}

View File

@@ -5,13 +5,9 @@ void modpyplot_gc_collect();
mp_obj_t modpyplot_axis(mp_obj_t arg);
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);
// axis(*args, emit=True, **kwargs)
//mp_obj_t grid();
//mp_obj_t scatter(mp_obj_t x, mp_obj_t y);
//mp_obj_t bar();
//mp_obj_t hist();
//mp_obj_t axis(mp_obj_t v);
//mp_obj_t text(mp_obj_t x, mp_obj_t y, mp_obj_t text);
mp_obj_t modpyplot_show();

View File

@@ -3,6 +3,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_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);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modpyplot_text_obj, modpyplot_text);
@@ -11,6 +12,7 @@ STATIC const mp_rom_map_elem_t modpyplot_module_globals_table[] = {
{ 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_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) },
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&modpyplot_text_obj) },
};

View File

@@ -1,5 +1,4 @@
#include "plot_store.h"
#include <escher/palette.h>
namespace Matplotlib {
@@ -11,7 +10,8 @@ PlotStore::PlotStore() : Shared::InteractiveCurveViewRange(),
void PlotStore::flush() {
m_dots = mp_obj_new_list(0, nullptr);
m_texts = mp_obj_new_list(0, nullptr);
m_segments = mp_obj_new_list(0, nullptr);
m_labels = mp_obj_new_list(0, nullptr);
}
// Iterators
@@ -51,7 +51,7 @@ T PlotStore::ListIterator<T>::operator*() {
return T(m_tuples[m_tupleIndex]);
};
// Dots
// Dot
template class PlotStore::ListIterator<PlotStore::Dot>;
@@ -70,9 +70,32 @@ void PlotStore::addDot(mp_obj_t x, mp_obj_t y, KDColor c) {
mp_obj_list_append(m_dots, tuple);
}
// Text
// Segment
PlotStore::Text::Text(mp_obj_t tuple) {
template class PlotStore::ListIterator<PlotStore::Segment>;
PlotStore::Segment::Segment(mp_obj_t tuple) {
mp_obj_t * elements;
mp_obj_get_array_fixed_n(tuple, 5, &elements);
m_xStart = mp_obj_get_float(elements[0]);
m_yStart = mp_obj_get_float(elements[1]);
m_xEnd = mp_obj_get_float(elements[2]);
m_yEnd = mp_obj_get_float(elements[3]);
m_color = KDColor::RGB16(mp_obj_get_int(elements[4]));
}
void PlotStore::addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c) {
mp_obj_t color = mp_obj_new_int(c);
mp_obj_t items[5] = {xStart, yStart, xEnd, yEnd, color};
mp_obj_t tuple = mp_obj_new_tuple(5, items);
mp_obj_list_append(m_segments, tuple);
}
// Label
template class PlotStore::ListIterator<PlotStore::Label>;
PlotStore::Label::Label(mp_obj_t tuple) {
mp_obj_t * elements;
mp_obj_get_array_fixed_n(tuple, 3, &elements);
m_x = mp_obj_get_float(elements[0]);
@@ -80,10 +103,10 @@ PlotStore::Text::Text(mp_obj_t tuple) {
m_string = mp_obj_str_get_str(elements[2]);
}
void PlotStore::addText(mp_obj_t x, mp_obj_t y, mp_obj_t string) {
void PlotStore::addLabel(mp_obj_t x, mp_obj_t y, mp_obj_t string) {
mp_obj_t items[3] = {x, y, string};
mp_obj_t tuple = mp_obj_new_tuple(3, items);
mp_obj_list_append(m_texts, tuple);
mp_obj_list_append(m_labels, tuple);
}
}

View File

@@ -41,7 +41,7 @@ public:
mp_obj_t m_list;
};
// Dots
// Dot
class Dot {
public:
@@ -58,11 +58,32 @@ public:
void addDot(mp_obj_t x, mp_obj_t y, KDColor c);
Iterable<ListIterator<Dot>> dots() { return Iterable<ListIterator<Dot>>(m_dots); }
// Texts
// Segment
class Text {
class Segment {
public:
Text(mp_obj_t tuple);
Segment(mp_obj_t tuple);
float xStart() const { return m_xStart; }
float yStart() const { return m_yStart; }
float xEnd() const { return m_xEnd; }
float yEnd() const { return m_yEnd; }
KDColor color() const { return m_color; }
private:
float m_xStart;
float m_yStart;
float m_xEnd;
float m_yEnd;
KDColor m_color;
};
void addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c);
Iterable<ListIterator<Segment>> segments() { return Iterable<ListIterator<Segment>>(m_segments); }
// Label
class Label {
public:
Label(mp_obj_t tuple);
float x() const { return m_x; }
float y() const { return m_y; }
const char * string() const { return m_string; }
@@ -72,16 +93,16 @@ public:
const char * m_string;
};
void addText(mp_obj_t x, mp_obj_t y, mp_obj_t string);
Iterable<ListIterator<Text>> texts() { return Iterable<ListIterator<Text>>(m_texts); }
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; }
private:
mp_obj_t m_dots; // List of (x, y, color)
mp_obj_t m_texts; // List of (x, y, string)
mp_obj_t m_rects; // List of (x, y, w, h, 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;
};

View File

@@ -14,6 +14,25 @@ void PlotView::drawRect(KDContext * ctx, KDRect rect) const {
for (PlotStore::Dot dot : m_store->dots()) {
drawDot(ctx, rect, dot.x(), dot.y(), dot.color());
}
for (PlotStore::Label label : m_store->labels()) {
drawLabel(ctx, rect,
label.x(), label.y(), label.string(),
KDColorBlack,
CurveView::RelativePosition::Before,
CurveView::RelativePosition::None
);
}
for (PlotStore::Segment segment : m_store->segments()) {
drawSegment2(
ctx, rect,
segment.xStart(), segment.yStart(),
segment.xEnd(), segment.yEnd(),
segment.color()
);
}
}
}