work on axis (adding log10 ticks).

This commit is contained in:
Martin Preuss
2024-06-17 22:56:48 +02:00
parent 7ba618f832
commit fe56360a36
9 changed files with 180 additions and 26 deletions

View File

@@ -65,6 +65,7 @@
$(gwenhywfar_libs)
$(cairo_libs)
$(zlib_libs)
-lm
</libraries>

View File

@@ -97,7 +97,6 @@
<sources>
$(local/typefiles)
graph.c
g_setup.c
w_graph.c
w_xaxis.c
w_yaxis.c

View File

@@ -19,6 +19,8 @@
<headers>
<header type="sys" loc="pre">aqdiagram/aqdg_api.h</header>
<header type="sys" loc="pre">aqdiagram/graph/tick.h</header>
<header type="sys" loc="pre">gwenhywfar/buffer.h</header>
<header type="sys" loc="pre">math.h</header>
</headers>
<inlines>
@@ -45,6 +47,89 @@
} \n
</content>
</inline>
<inline loc="end" access="public">
<content>
$(api) void $(struct_prefix)_GenLog10Ticks($(struct_type) *st);
</content>
</inline>
<inline loc="code">
<content>
void $(struct_prefix)_GenLog10Ticks($(struct_type) *st) \n
{ \n
double absMaxValue; \n
double v; \n
double vRun; \n
double startValue; \n
GWEN_BUFFER *dbuf; \n
\n
absMaxValue=abs(st-&gt;minValue); \n
v=abs(st-&gt;maxValue); \n
absMaxValue=(v&gt;absMaxValue)?v:absMaxValue; \n
\n
dbuf=GWEN_Buffer_new(0, 64, 0, 1); \n
v=pow(10, floor(log10(absMaxValue))); \n
\n
startValue=v*10.0; \n
if ((st-&gt;minValue)&lt;startValue) \n
startValue=-startValue; \n
\n
/* add ticks for next higher log10 (e.g. "1000.0" for maxValue="365") */ \n
vRun=startValue; \n
while(vRun&lt;=(st-&gt;maxValue)) { \n
if (vRun&gt;=(st-&gt;minValue)) { \n
if (!$(struct_prefix)_HasTickValue(st, vRun)) { \n
GWEN_Buffer_AppendArgs(dbuf, "%.*f", st-&gt;precision, vRun); \n
$(struct_prefix)_AddNewTick(st, GWEN_Buffer_GetStart(dbuf), vRun, 0, 0); \n
GWEN_Buffer_Reset(dbuf); \n
} \n
} \n
vRun+=v; \n
} \n
\n
vRun=startValue; \n
v/=10.0; \n
while(vRun&lt;=(st-&gt;maxValue)) { \n
if (vRun&gt;=(st-&gt;minValue)) { \n
if (!$(struct_prefix)_HasTickValue(st, vRun)) { \n
GWEN_Buffer_AppendArgs(dbuf, "%.*f", st-&gt;precision, vRun); \n
$(struct_prefix)_AddNewTick(st, GWEN_Buffer_GetStart(dbuf), vRun, 1, 0); \n
GWEN_Buffer_Reset(dbuf); \n
} \n
} \n
vRun+=v; \n
} \n
GWEN_Buffer_free(dbuf); \n
} \n
</content>
</inline>
<inline loc="end" access="public">
<content>
$(api) int $(struct_prefix)_HasTickValue(const $(struct_type) *st, double v);
</content>
</inline>
<inline loc="code">
<content>
int $(struct_prefix)_HasTickValue(const $(struct_type) *st, double v) \n
{ \n
const AQDG_GRAPH_TICK *tick; \n
\n
tick=AQDG_Graph_Tick_List_First(st-&gt;tickList); \n
while(tick) { \n
if (AQDG_Graph_Tick_GetValue(tick)==v) \n
return 1; \n
tick=AQDG_Graph_Tick_List_Next(tick); \n
} \n
return 0; \n
} \n
</content>
</inline>
</inlines>
</lang>

View File

@@ -47,25 +47,25 @@ typedef struct AQDG_GRAPH AQDG_GRAPH;
AQDG_GRAPH *AQDG_Graph_new(void);
void AQDG_Graph_free(AQDG_GRAPH *g);
AQDG_API AQDG_GRAPH *AQDG_Graph_new(void);
AQDG_API void AQDG_Graph_free(AQDG_GRAPH *g);
const char *AQDG_Graph_GetTitle(const AQDG_GRAPH *g);
void AQDG_Graph_SetTitle(AQDG_GRAPH *g, const char *s);
AQDG_API const char *AQDG_Graph_GetTitle(const AQDG_GRAPH *g);
AQDG_API void AQDG_Graph_SetTitle(AQDG_GRAPH *g, const char *s);
const char *AQDG_Graph_GetSubTitle(const AQDG_GRAPH *g);
void AQDG_Graph_SetSubTitle(AQDG_GRAPH *g, const char *s);
AQDG_API const char *AQDG_Graph_GetSubTitle(const AQDG_GRAPH *g);
AQDG_API void AQDG_Graph_SetSubTitle(AQDG_GRAPH *g, const char *s);
AQDG_GRAPH_AXIS *AQDG_Graph_GetAxisByIndex(const AQDG_GRAPH *g, int idx);
int AQDG_Graph_SetAxis(AQDG_GRAPH *g, int idx, AQDG_GRAPH_AXIS *ax);
AQDG_API AQDG_GRAPH_AXIS *AQDG_Graph_GetAxisByIndex(const AQDG_GRAPH *g, int idx);
AQDG_API int AQDG_Graph_SetAxis(AQDG_GRAPH *g, int idx, AQDG_GRAPH_AXIS *ax);
AQDG_GRAPH_SUBGRAPH_LIST *AQDG_Graph_GetSubGraphList(const AQDG_GRAPH *g);
void AQDG_Graph_AddSubGraph(AQDG_GRAPH *g, AQDG_GRAPH_SUBGRAPH *sg);
AQDG_API AQDG_GRAPH_SUBGRAPH_LIST *AQDG_Graph_GetSubGraphList(const AQDG_GRAPH *g);
AQDG_API void AQDG_Graph_AddSubGraph(AQDG_GRAPH *g, AQDG_GRAPH_SUBGRAPH *sg);
AQDG_DRAW_CONTEXT *AQDG_Graph_GetDrawContext(const AQDG_GRAPH *g);
void AQDG_Graph_SetDrawContext(AQDG_GRAPH *g, AQDG_DRAW_CONTEXT *dc);
AQDG_API AQDG_DRAW_CONTEXT *AQDG_Graph_GetDrawContext(const AQDG_GRAPH *g);
AQDG_API void AQDG_Graph_SetDrawContext(AQDG_GRAPH *g, AQDG_DRAW_CONTEXT *dc);
#endif

View File

@@ -81,21 +81,21 @@ enum {
AQDG_OBJECT *AQDG_GraphWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_DRAW_CONTEXT *drawContext, AQDG_GRAPH *graph);
AQDG_API AQDG_OBJECT *AQDG_GraphWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_DRAW_CONTEXT *drawContext, AQDG_GRAPH *graph);
AQDG_GRAPH *AQDG_GraphWidget_GetGraph(const AQDG_OBJECT *o);
AQDG_API AQDG_GRAPH *AQDG_GraphWidget_GetGraph(const AQDG_OBJECT *o);
int AQDG_GraphWidget_GetPen(const AQDG_OBJECT *o, int idx);
void AQDG_GraphWidget_SetPen(AQDG_OBJECT *o, int idx, int penId);
AQDG_API int AQDG_GraphWidget_GetPen(const AQDG_OBJECT *o, int idx);
AQDG_API void AQDG_GraphWidget_SetPen(AQDG_OBJECT *o, int idx, int penId);
int AQDG_GraphWidget_GetFont(const AQDG_OBJECT *o, int idx);
void AQDG_GraphWidget_SetFont(AQDG_OBJECT *o, int idx, int fontId);
AQDG_API int AQDG_GraphWidget_GetFont(const AQDG_OBJECT *o, int idx);
AQDG_API void AQDG_GraphWidget_SetFont(AQDG_OBJECT *o, int idx, int fontId);
int AQDG_GraphWidget_CreatePenFromDb(AQDG_OBJECT *o, GWEN_DB_NODE *db, const char *name, uint32_t defColor, int defWidth, int defDash);
int AQDG_GraphWidget_CreateFontFromDb(AQDG_OBJECT *o, GWEN_DB_NODE *db, const char *name, int defaultFontSize);
uint32_t AQDG_GraphWidget_GetStandardCurveColor(int idx, uint32_t defCol);
AQDG_API int AQDG_GraphWidget_CreatePenFromDb(AQDG_OBJECT *o, GWEN_DB_NODE *db, const char *name, uint32_t defColor, int defWidth, int defDash);
AQDG_API int AQDG_GraphWidget_CreateFontFromDb(AQDG_OBJECT *o, GWEN_DB_NODE *db, const char *name, int defaultFontSize);
AQDG_API uint32_t AQDG_GraphWidget_GetStandardCurveColor(int idx, uint32_t defCol);
#endif

View File

@@ -12,7 +12,7 @@
#include <aqdiagram/graph/w_graph.h>
AQDG_OBJECT *AQDG_ViewportWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_OBJECT *graphObject);
AQDG_API AQDG_OBJECT *AQDG_ViewportWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_OBJECT *graphObject);

View File

@@ -12,7 +12,7 @@
#include <aqdiagram/graph/w_graph.h>
AQDG_OBJECT *AQDG_XAxisWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_OBJECT *graphObject, int axisIndex);
AQDG_API AQDG_OBJECT *AQDG_XAxisWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_OBJECT *graphObject, int axisIndex);

View File

@@ -12,7 +12,7 @@
#include <aqdiagram/graph/w_graph.h>
AQDG_OBJECT *AQDG_YAxisWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_OBJECT *graphObject, int axisIndex);
AQDG_API AQDG_OBJECT *AQDG_YAxisWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_OBJECT *graphObject, int axisIndex);

View File

@@ -17,6 +17,9 @@
#include <aqdiagram/draw/w_hlayout.h>
#include <aqdiagram/draw/w_label.h>
#include <aqdiagram/draw/context_cairo.h>
#include <aqdiagram/graph/graph.h>
#include <aqdiagram/graph/axis.h>
#include <aqdiagram/graph/subgraph.h>
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/cgui.h>
@@ -33,6 +36,10 @@
static void _dumpTicks(const AQDG_GRAPH_TICK_LIST *tickList);
int test1(int argc, char **argv)
{
AQDG_DRAW_CONTEXT *dc;
@@ -115,6 +122,67 @@ int test1(int argc, char **argv)
int test2(int argc, char **argv)
{
AQDG_GRAPH *g;
AQDG_GRAPH_AXIS *xAxis;
AQDG_GRAPH_AXIS *yAxis;
g=AQDG_Graph_new();
AQDG_Graph_SetTitle(g, "Test Graph");
AQDG_Graph_SetSubTitle(g, "Test Data");
xAxis=AQDG_Graph_Axis_new();
AQDG_Graph_Axis_SetLabel(xAxis, "Label X");
AQDG_Graph_Axis_SetMinValue(xAxis, -5.0);
AQDG_Graph_Axis_SetMaxValue(xAxis, 117.0);
AQDG_Graph_Axis_SetPrecision(xAxis, 0);
AQDG_Graph_SetAxis(g, AQDG_GRAPH_AXISPOS_BOTTOM, xAxis);
fprintf(stderr, "Generating ticks for X-axis\n");
AQDG_Graph_Axis_GenLog10Ticks(xAxis);
fprintf(stderr, "Ticks for X-Axis (%f - %f)\n",
AQDG_Graph_Axis_GetMinValue(xAxis),
AQDG_Graph_Axis_GetMaxValue(xAxis));
_dumpTicks(AQDG_Graph_Axis_GetTickList(xAxis));
yAxis=AQDG_Graph_Axis_new();
AQDG_Graph_Axis_SetLabel(yAxis, "Label Y");
AQDG_Graph_Axis_SetMinValue(yAxis, -27.0);
AQDG_Graph_Axis_SetMaxValue(yAxis, 1235.0);
AQDG_Graph_Axis_SetPrecision(yAxis, 2);
fprintf(stderr, "Generating ticks for Y-axis\n");
AQDG_Graph_Axis_GenLog10Ticks(yAxis);
fprintf(stderr, "Ticks for Y-Axis (%f - %f)\n",
AQDG_Graph_Axis_GetMinValue(yAxis),
AQDG_Graph_Axis_GetMaxValue(yAxis));
_dumpTicks(AQDG_Graph_Axis_GetTickList(yAxis));
AQDG_Graph_SetAxis(g, AQDG_GRAPH_AXISPOS_LEFT, yAxis);
return 0;
}
void _dumpTicks(const AQDG_GRAPH_TICK_LIST *tickList)
{
const AQDG_GRAPH_TICK *tick;
fprintf(stderr, "Ticks:\n");
tick=AQDG_Graph_Tick_List_First(tickList);
while(tick) {
fprintf(stderr,
"- %f (\"%s\") [%d]\n",
AQDG_Graph_Tick_GetValue(tick),
AQDG_Graph_Tick_GetLabel(tick),
AQDG_Graph_Tick_GetLevel(tick));
tick=AQDG_Graph_Tick_List_Next(tick);
}
}
int modTest(int argc, char **argv)
{
int rv;
@@ -158,7 +226,8 @@ int main(int argc, char **argv)
GWEN_Logger_SetLevel(AQDG_LOGDOMAIN, GWEN_LoggerLevel_Info);
GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Info);
rv=test1(argc, argv);
/*rv=test1(argc, argv);*/
rv=test2(argc, argv);
if (rv!=0){
DBG_ERROR(NULL, "here (%d)", rv);