Implement axis labels for plots - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2011年12月20日 14:09:24 +0100
committerFrancesco Abbate <francesco.bbt@gmail.com>2011年12月21日 08:52:44 +0100
commitc16aebdd99c9f0ec0639ec207d4405a983cf2149 (patch)
tree8c854f695fff4817c8bba0530aefd35f62057d4a
parent6908a17576d1321eaf34899970ab136f1b18b8ef (diff)
downloadgsl-shell-c16aebdd99c9f0ec0639ec207d4405a983cf2149.tar.gz
Implement axis labels for plots
Diffstat
-rw-r--r--agg-plot/lua-plot.cpp 64
-rw-r--r--agg-plot/plot.h 38
-rw-r--r--demos/plot.lua 1
3 files changed, 82 insertions, 21 deletions
diff --git a/agg-plot/lua-plot.cpp b/agg-plot/lua-plot.cpp
index 8ea2ab81..0a80e79a 100644
--- a/agg-plot/lua-plot.cpp
+++ b/agg-plot/lua-plot.cpp
@@ -51,6 +51,10 @@ static int plot_free (lua_State *L);
static int plot_show (lua_State *L);
static int plot_title_set (lua_State *L);
static int plot_title_get (lua_State *L);
+static int plot_xlab_set (lua_State *L);
+static int plot_xlab_get (lua_State *L);
+static int plot_ylab_set (lua_State *L);
+static int plot_ylab_get (lua_State *L);
static int plot_units_set (lua_State *L);
static int plot_units_get (lua_State *L);
static int plot_set_limits (lua_State *L);
@@ -101,6 +105,8 @@ static const struct luaL_Reg plot_methods[] = {
static const struct luaL_Reg plot_properties_get[] = {
{"title", plot_title_get },
+ {"xlab", plot_xlab_get },
+ {"ylab", plot_ylab_get },
{"units", plot_units_get },
{"sync", plot_sync_mode_get },
{"pad", plot_pad_mode_get },
@@ -110,6 +116,8 @@ static const struct luaL_Reg plot_properties_get[] = {
static const struct luaL_Reg plot_properties_set[] = {
{"title", plot_title_set },
+ {"xlab", plot_xlab_set },
+ {"ylab", plot_ylab_set },
{"units", plot_units_set },
{"sync", plot_sync_mode_set },
{"pad", plot_pad_mode_set },
@@ -234,17 +242,28 @@ plot_add_line (lua_State *L)
return plot_add_gener (L, true);
}
-int
-plot_title_set (lua_State *L)
+static int plot_string_property_get (lua_State* L, const char* (lua_plot::*getter)() const)
+{
+ lua_plot *p = object_check<lua_plot>(L, 1, GS_PLOT);
+
+ AGG_LOCK();
+ const char *title = (p->*getter)();
+ lua_pushstring (L, title);
+ AGG_UNLOCK();
+
+ return 1;
+}
+
+static int plot_string_property_set (lua_State* L, void (lua_plot::*setter)(const char*))
{
sg_plot *p = object_check<sg_plot>(L, 1, GS_PLOT);
- const char *title = lua_tostring (L, 2);
+ const char *s = lua_tostring (L, 2);
- if (title == NULL)
+ if (s == NULL)
return gs_type_error (L, 2, "string");
AGG_LOCK();
- p->set_title(title);
+ (p->*setter)(s);
AGG_UNLOCK();
plot_update_raw (L, p, 1);
@@ -253,18 +272,39 @@ plot_title_set (lua_State *L)
}
int
+plot_title_set (lua_State *L)
+{
+ return plot_string_property_set(L, &lua_plot::set_title);
+}
+
+int
plot_title_get (lua_State *L)
{
- sg_plot *p = object_check<sg_plot>(L, 1, GS_PLOT);
+ return plot_string_property_get(L, &gs_plot::title);
+}
- AGG_LOCK();
+int
+plot_xlab_set (lua_State *L)
+{
+ return plot_string_property_set(L, &lua_plot::set_xlabel);
+}
- const char *title = p->title();
- lua_pushstring (L, title);
+int
+plot_xlab_get (lua_State *L)
+{
+ return plot_string_property_get(L, &lua_plot::xlabel);
+}
- AGG_UNLOCK();
-
- return 1;
+int
+plot_ylab_set (lua_State *L)
+{
+ return plot_string_property_set(L, &lua_plot::set_ylabel);
+}
+
+int
+plot_ylab_get (lua_State *L)
+{
+ return plot_string_property_get(L, &lua_plot::ylabel);
}
int
diff --git a/agg-plot/plot.h b/agg-plot/plot.h
index bb0398cf..50f8f4cc 100644
--- a/agg-plot/plot.h
+++ b/agg-plot/plot.h
@@ -103,7 +103,7 @@ public:
m_drawing_queue(0), m_clip_flag(true),
m_need_redraw(true), m_rect(),
m_use_units(use_units), m_pad_units(false), m_title(),
- m_sync_mode(true)
+ m_xlabel(), m_ylabel(), m_sync_mode(true)
{
compute_user_trans();
};
@@ -122,6 +122,12 @@ public:
void set_title(const char *text);
const char *title() const { return m_title.cstr(); };
+ void set_xlabel(const char *text) { str_copy_c(&m_xlabel, text); }
+ const char *xlabel() const { return m_xlabel.cstr(); };
+
+ void set_ylabel(const char *text) { str_copy_c(&m_ylabel, text); }
+ const char *ylabel() const { return m_ylabel.cstr(); };
+
void set_units(bool use_units);
bool use_units() const { return m_use_units; };
@@ -204,7 +210,7 @@ protected:
private:
bool m_pad_units;
- str m_title;
+ str m_title, m_xlabel, m_ylabel;
bool m_sync_mode;
};
@@ -285,8 +291,6 @@ template <class Canvas> void plot<VS,RM>::draw(Canvas& _canvas, agg::trans_affin
template <class VS, class RM>
void plot<VS,RM>::draw_title(canvas_type& canvas, agg::trans_affine& canvas_mtx)
{
- double xt = 0.5, yt = 1.05;
-
agg::trans_affine m;
this->viewport_scale(m);
trans_affine_compose (m, canvas_mtx);
@@ -296,10 +300,25 @@ void plot<VS,RM>::draw_title(canvas_type& canvas, agg::trans_affine& canvas_mtx)
draw::text title(12.0 * scale, std_line_width(scale));
title.set_text(m_title.cstr());
title.hjustif(0.5);
- title.set_point(xt, yt);
+ title.set_point(0.5, 1.05);
title.apply_transform(m, 1.0);
+ draw::text xlabel(11.0 * scale, std_line_width(scale));
+ xlabel.set_text(m_xlabel.cstr());
+ xlabel.hjustif(0.5);
+ xlabel.set_point(0.5, -0.1);
+ xlabel.apply_transform(m, 1.0);
+
+ draw::text ylabel(11.0 * scale, std_line_width(scale));
+ ylabel.set_text(m_ylabel.cstr());
+ ylabel.hjustif(0.5);
+ ylabel.set_point(-0.1, 0.5);
+ ylabel.angle(M_PI/2.0);
+ ylabel.apply_transform(m, 1.0);
+
canvas.draw(title, agg::rgba(0, 0, 0));
+ canvas.draw(xlabel, agg::rgba(0, 0, 0));
+ canvas.draw(ylabel, agg::rgba(0, 0, 0));
}
template <class VS, class RM>
@@ -449,7 +468,7 @@ void plot<VS,RM>::draw_axis(canvas_type& canvas, agg::trans_affine& canvas_mtx)
this->m_trans.transform(&x, &y);
if (y >= - yeps && y <= 1.0 + yeps)
{
- draw::text label(12.0 * scale, line_width);
+ draw::text label(10.0 * scale, line_width);
char lab_text[32];
m_uy.mark_label(lab_text, 32, j);
@@ -481,7 +500,7 @@ void plot<VS,RM>::draw_axis(canvas_type& canvas, agg::trans_affine& canvas_mtx)
this->m_trans.transform(&x, &y);
if (x >= - xeps && x <= 1.0 + xeps)
{
- draw::text label(12.0 * scale, line_width);
+ draw::text label(10.0 * scale, line_width);
char lab_text[32];
m_ux.mark_label(lab_text, 32, j);
@@ -532,8 +551,9 @@ void plot<VS,RM>::draw_axis(canvas_type& canvas, agg::trans_affine& canvas_mtx)
template<class VS, class RM>
void plot<VS,RM>::viewport_scale(agg::trans_affine& m)
{
- const double xoffs = 0.09375, yoffs = 0.09375;
- static agg::trans_affine rsz(1-2*xoffs, 0.0, 0.0, 1-2*yoffs, xoffs, yoffs);
+ const double L = 0.12, R = 0.095;
+ const double B = 0.1, T = 0.095;
+ static agg::trans_affine rsz(1-L-R, 0.0, 0.0, 1-B-T, L, B);
trans_affine_compose (m, rsz);
}
diff --git a/demos/plot.lua b/demos/plot.lua
index 7bdc1c15..1b3f172b 100644
--- a/demos/plot.lua
+++ b/demos/plot.lua
@@ -15,6 +15,7 @@ local function demo2()
p:add(b, 'darkgreen')
p:addline(b, rgba(0, 0, 0, 0.6))
p:addline(fxline(f, -4, 4), 'red')
+ p.xlab, p.ylab = 'x', 'Frequency'
p:show()
return p
end
generated by cgit v1.2.3 (git 2.39.1) at 2025年10月04日 02:44:28 +0000

AltStyle によって変換されたページ (->オリジナル) /