TPCCLIB
Loading...
Searching...
No Matches
libtpcsvg.h File Reference

Header file for libtpcsvg. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

Go to the source code of this file.

Data Structures

struct  svg_viewport_pos
 
struct  svg_coord
 
struct  svg_viewports
 
struct  SVG_LEGEND
 
struct  SVG_LEGENDS
 

Macros

#define SVG_VIEWPORT_WIDTH   10000
 
#define SVG_VIEWPORT_HEIGHT   10000
 
#define MAX_TICK_NR   12
 
#define MAX_TICK_LABEL_LEN   20
 
#define MAX_SVG_LEGEND_LEN   30
 

Enumerations

enum  svgSymbolType {
  RECTANGLE , CIRCLE , UPTRIANGLE , DOWNTRIANGLE ,
  DIAMOND , LEFTTRIANGLE , RIGHTTRIANGLE
}
 
enum  svgSymbolFill { SYMBOLOPEN , SYMBOLFILLED }
 
enum  svgColor {
  BLACK , RED , BLUE , GREEN ,
  PURPLE , OLIVE , AQUA , FUCHSIA ,
  GRAY , LIME , MAROON , NAVY ,
  SILVER , TEAL , YELLOW
}
 

Functions

int axis_tick_positions (const double begin, const double end, double *ticks, int *tick_nr, double *scale_factor, int *tick_decimals, int verbose)
 
void axis_check_range (double *begin, double *end, int verbose)
 
void strRmExpZeroes (char *str)
 
FILE * svg_initiate (const char *filename, const double height, const double width, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_close (FILE *fp, char *errmsg, int verbose)
 
FILE * svg_xhtml_initiate (const char *filename, const char *XHTML_title, char *errmsg, int verbose)
 
int svg_xhtml_close (FILE *fp, char *errmsg, int verbose)
 
int svg_xhtml_svg_open (FILE *fp, const double height, const double width, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_xhtml_svg_close (FILE *fp, char *errmsg, int verbose)
 
int svg_write (FILE *fp, const char *svg_string, char *errmsg, int verbose)
 
void svg_init_viewport_pos (struct svg_viewport_pos *p)
 
void svg_init_coord (struct svg_coord *p)
 
void svg_init_viewports (struct svg_viewports *p)
 
int svg_define_viewports (const int main_viewport_width, const int main_viewport_height, const int is_main_title, const int is_yaxis_title, const int is_xaxis_title, const int is_label_area, struct svg_viewports *vp, int verbose)
 
int svg_start_plot_viewport (FILE *fp, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_end_plot_viewport (FILE *fp, char *errmsg, int verbose)
 
int svg_start_coordinate_viewport (FILE *fp, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_end_coordinate_viewport (FILE *fp, char *errmsg, int verbose)
 
int svg_calculate_axes (struct svg_viewports *vp, int verbose)
 
int svg_write_axes (FILE *fp, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_write_xticks (FILE *fp, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_write_yticks (FILE *fp, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_write_tac (FILE *fp, struct svg_viewports *vp, const int plot_type, const char *tac_id, const char *tac_title, double *x, double *y, const int data_nr, const char *color, const svgSymbolType symbol_type, const svgSymbolFill symbol_fill, char *errmsg, int verbose)
 
int get_line_intersection (const double a1x, const double a1y, const double a2x, const double a2y, const double b1x, const double b1y, const double b2x, const double b2y, double *ix, double *iy, int verbose)
 
int check_intersection_with_viewport (const double x1, const double y1, const double x2, const double y2, struct svg_viewport_pos *cavp, double *nx1, double *ny1, double *nx2, double *ny2, int verbose)
 
int svg_create_main_title (FILE *fp, const char *main_title_text, const char *sub_title_text, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_create_xaxis_title (FILE *fp, const char *title_text, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_create_yaxis_title (FILE *fp, const char *title_text, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_define_symbols (FILE *fp, char *errmsg, int verbose)
 
char * svgColorName (const svgColor index)
 
char * svgSymbolName (const svgSymbolType index)
 
void svg_init_legends (SVG_LEGENDS *legends)
 
void svg_legend_empty (SVG_LEGENDS *legends)
 
int svg_legend_add (SVG_LEGENDS *legends, const int plot_type, const int symbol_type, const svgSymbolFill symbol_fill, const int color, const char *text)
 
int svg_create_legends (FILE *fp, struct svg_viewports *vp, SVG_LEGENDS *legends, char *errmsg, int verbose)
 
char * svg_str_encode (const char *s)
 

Detailed Description

Header file for libtpcsvg.

Author
Vesa Oikonen

Definition in file libtpcsvg.h.

Macro Definition Documentation

◆ MAX_SVG_LEGEND_LEN

#define MAX_SVG_LEGEND_LEN   30

Max length of SVG legend texts

Definition at line 29 of file libtpcsvg.h.

Referenced by svg_legend_add().

◆ MAX_TICK_LABEL_LEN

#define MAX_TICK_LABEL_LEN   20

Max length of axis tick labels

Definition at line 27 of file libtpcsvg.h.

◆ MAX_TICK_NR

#define MAX_TICK_NR   12

Max nr of axis ticks

Definition at line 25 of file libtpcsvg.h.

Referenced by svg_calculate_axes(), and svg_init_coord().

◆ SVG_VIEWPORT_HEIGHT

#define SVG_VIEWPORT_HEIGHT   10000

Height of SVG viewport

Definition at line 23 of file libtpcsvg.h.

Referenced by svg_define_viewports().

◆ SVG_VIEWPORT_WIDTH

#define SVG_VIEWPORT_WIDTH   10000

Width of SVG viewport

Definition at line 21 of file libtpcsvg.h.

Referenced by svg_define_viewports().

Enumeration Type Documentation

◆ svgColor

enum svgColor

SVG plot colour

Definition at line 49 of file libtpcsvg.h.

49 {BLACK,RED,BLUE,GREEN,PURPLE,OLIVE,AQUA,FUCHSIA,GRAY,LIME,MAROON,
50 NAVY,SILVER,TEAL,YELLOW} svgColor;
svgColor
Definition libtpcsvg.h:49

◆ svgSymbolFill

SVG plot symbol open/filled

Definition at line 47 of file libtpcsvg.h.

47{SYMBOLOPEN,SYMBOLFILLED} svgSymbolFill;
svgSymbolFill
Definition libtpcsvg.h:47

◆ svgSymbolType

SVG plot symbol type

Definition at line 44 of file libtpcsvg.h.

44 {RECTANGLE,CIRCLE,UPTRIANGLE,DOWNTRIANGLE,DIAMOND,
45 LEFTTRIANGLE,RIGHTTRIANGLE} svgSymbolType;
svgSymbolType
Definition libtpcsvg.h:44

Function Documentation

◆ axis_check_range()

void axis_check_range ( double * begin,
double * end,
int verbose )

Check and if necessary correct axis range: min must be smaller than max. Also, if the range is close to zero, then make it larger. Also, if the range is relatively large, then change it to start from zero.

See also
axis_tick_positions, svg_calculate_axes
Parameters
beginaxis range minimum
endaxis range maximum
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 116 of file axis.c.

123 {
124 double temp;
125
126 if(verbose>0) printf("axis_check_range(%g, %g, %d)\n", *begin, *end, verbose);
127
128 if(*begin<*end) {
129 // fine now
130 } else if(*begin>*end) {
131 temp=*end; *end=*begin; *begin=temp;
132 } else { // this means that begin==end
133 if(*begin==0.0) {
134 *begin=-1.0; *end=1.0;
135 } else if(*begin<0.0) {
136 *begin*=2.0; *end=0.0;
137 } else {
138 *begin=0.0; *end*=2.0;
139 }
140 if(verbose>2) {
141 printf(" new begin := %g\n", *begin);
142 printf(" new end := %g\n", *end);
143 }
144 return;
145 }
146 /* If range is very small */
147 if((*end-*begin)<1.0e-98) {
148 if(*begin>=1.0e-98 || *begin<0.0) *begin-=1.0e-98;
149 else if(*begin>=0.0) *begin=0.0;
150 if(*end<=-1.0e-98 || *end>0.0) *end+=1.0e-98;
151 else if(*end<0.0) *end=0.0;
152 if(verbose>2) {
153 printf(" new begin := %g\n", *begin);
154 printf(" new end := %g\n", *end);
155 }
156 return;
157 }
158 /* If range is relatively large */
159 if(*begin>0.0 && *end>0.0) {
160 if((*end-*begin)>3.3*(*begin)) *begin=0.0;
161 } else if(*begin<0.0 && *end<0.0) {
162 if((*end-*begin)>3.3*(-*end)) *end=0.0;
163 }
164 /* If data range is relatively small (compared to level) */
165 temp=(*end-*begin)*2.0/(fabs(*end)+fabs(*begin));
166 if(temp<0.01) {
167 temp=0.5*(*end-*begin)*0.01/temp;
168 if(*begin<0.0 || *begin>temp) *begin-=temp; else *begin=0.0;
169 if(*end>0.0 || *end<-temp) *end+=temp; else *end=0.0;
170 }
171
172 return;
173}

Referenced by svg_calculate_axes().

◆ axis_tick_positions()

int axis_tick_positions ( const double begin,
const double end,
double * ticks,
int * tick_nr,
double * scale_factor,
int * tick_decimals,
int verbose )

Define suitable tick positions for XY plot.

Returns
Returns 0 if successful, <>0 in case of error.
See also
axis_check_range, svg_calculate_axes
Parameters
beginaxis range minimum.
endaxis range maximum.
ticksPointer to array where tick values will be written. Length must be at least tick_nr.
tick_nrInput: Max allowed nr of ticks; Output: actual nr of ticks.
scale_factorOutput: Suggested scale factor (x10^sf). NULL, if not needed.
tick_decimalsOutput: tick value precision (nr of decimals). NULL, if not needed.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 14 of file axis.c.

29 {
30 int ti;
31 double step, scale;
32
33 if(verbose>0)
34 printf("axis_tick_positions(%.10E, %.10E, ticks, %d, sf, td, %d)\n",
35 begin, end, *tick_nr, verbose);
36
37 /* Check input */
38 if(ticks==NULL || *tick_nr<1) return(1);
39 //if(verbose>1) printf(" begin:=%.10E\n end:=%.10E\n", begin, end);
40 if(end<=begin) {
41 *tick_nr=0;
42 if(scale_factor!=NULL) *scale_factor=1.0;
43 if(tick_decimals!=NULL) *tick_decimals=0;
44 return(0);
45 }
46 /* Calculate the initial tick step size */
47 step=(end-begin)/(double)(*tick_nr);
48 if(verbose>1) printf(" tick_nr:=%d\n step:=%20.10E\n", *tick_nr, step);
49
50 /* Calculate a feasible step size and scale */
51 scale=1.0;
52 while(step<=0.5) {step*=10.0; scale/=10.0;}
53 while(step>5.0) {step/=10.0; scale*=10.0;}
54 if(verbose>1) printf(" scaled_step:=%.10E\n scale:=%E\n", step, scale);
55 if (step<1.0) {step=1.0;}
56 else if(step<2.0) {step=2.0;}
57 else {step=5.0;}
58 if(verbose>1) {
59 printf(" feasible step:=%g\n", step);
60 }
61 ti=0; ticks[ti]=step*scale*ceil(begin/(step*scale));
62 while(ticks[ti]<=(end+(end-begin)*0.00001) && ti<*tick_nr) {
63 ti++; ticks[ti]=ticks[0]+step*scale*(double)ti;
64 }
65 *tick_nr=ti;
66
67 if(verbose>1) {
68 printf(" final tick_nr := %d\n", *tick_nr);
69 printf(" ticks: %.10E", ticks[0]);
70 for(ti=1; ti<*tick_nr; ti++) printf(", %.10E", ticks[ti]);
71 printf("\n");
72 }
73
74 /* Quit, if user did not like scales etc */
75 if(scale_factor==NULL && tick_decimals==NULL) return(0);
76
77 /* Calculate step scale */
78 double step_scale=log10(scale);
79 if(verbose>1) printf(" step_scale=%g\n", step_scale);
80
81 /* Find the highest tick label */
82 double tick_high;
83 int tick_scale, scale_dif;
84 if(fabs(ticks[0])>fabs(ticks[*tick_nr-1])) tick_high=ticks[0];
85 else tick_high=ticks[*tick_nr-1];
86 if(verbose>1) printf(" tick_high := %.10E\n", tick_high);
87 tick_scale=0;
88 while(fabs(tick_high)<1.0) {tick_high*=10.0; tick_scale--;}
89 while(fabs(tick_high)>=10.0) {tick_high/=10.0; tick_scale++;}
90 if(verbose>1) printf(" scaled_tick_high := %g\n tick_scale := %d\n", tick_high, tick_scale);
91
92 /* Calculate the difference between tick and step scales */
93 scale_dif=tick_scale-step_scale;
94 if(verbose>1) printf(" scale_dif := %d\n", scale_dif);
95 /* That determines the tick number precision */
96 if(tick_decimals!=NULL) {
97 *tick_decimals=1+scale_dif;
98 if(verbose>1) printf(" tick_decimals := %d\n", *tick_decimals);
99 }
100
101 /* Calculate the preferred tick scale factor, or quit, if it is not needed */
102 if(scale_factor==NULL) return(0);
103 *scale_factor=tick_scale;
104 if(verbose>1) printf(" scale_factor := %g\n", *scale_factor);
105
106 return(0);
107}

Referenced by svg_calculate_axes().

◆ check_intersection_with_viewport()

int check_intersection_with_viewport ( const double x1,
const double y1,
const double x2,
const double y2,
struct svg_viewport_pos * cavp,
double * nx1,
double * ny1,
double * nx2,
double * ny2,
int verbose )
extern

Check the intersections between specified line and viewport borders.

Returns
Returns the number (0-2) of crossings.
See also
get_line_intersection
Parameters
x1x,y coordinates of line at points 1 and 2; x coordinate of point 1.
y1y coordinate of point 1.
x2x coordinate of point 2.
y2y coordinate of point 2.
cavpPointer to coordinate area viewport.
nx1Pointers for (possibly) modified line coordinates; NULL if not needed.
ny1new y coordinate of point 1.
nx2new x coordinate of point 2.
ny2new y coordinate of point 2.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 73 of file svg_plot.c.

94 {
95 int i=0, n, crossings=0;
96 double ix, iy;
97 double mx[2], my[2];
98
99 if(verbose>0) {
100 printf("%s(%g, %g, %g, %g, cavp, nx1, ny1, nx2, ny2, %d)\n",
101 __func__, x1, y1, x2, y2, verbose);
102 fflush(stdout);
103 }
104
105 mx[0]=x1; my[0]=y1; mx[1]=x2; my[1]=y2;
106 /* Upper border */
107 n=get_line_intersection(x1, y1, x2, y2, 0.0, 0.0, (double)cavp->w, 0.0, &ix, &iy, verbose);
108 if(n>0) {
109 if(crossings==0) {
110 if(y1<iy) i=0; else i=1; // which point was out? change that one
111 } else { // both are out, change the other than first time
112 if(i==0) i=1; else i=0;
113 }
114 mx[i]=ix; my[i]=iy; crossings++;
115 if(verbose>3)
116 printf("line between (%g,%g) and (%g,%g) would cross upper border at (%g,%g)\n",
117 x1, y1, x2, y2, ix, iy);
118 }
119 /* Lower border */
120 n=get_line_intersection(x1, y1, x2, y2,
121 0.0, (double)cavp->h, (double)cavp->w, (double)cavp->h, &ix, &iy, verbose);
122 if(n>0) {
123 if(crossings==0) {
124 if(y1>iy) i=0; else i=1; // which point was out? change that one
125 } else { // both are out, change the other than first time
126 if(i==0) i=1; else i=0;
127 }
128 mx[i]=ix; my[i]=iy; crossings++;
129 if(verbose>3)
130 printf("line between (%g,%g) and (%g,%g) would cross lower border at (%g,%g)\n",
131 x1, y1, x2, y2, ix, iy);
132 }
133 /* Left border */
134 n=get_line_intersection(x1, y1, x2, y2,
135 0.0, 0.0, 0.0, cavp->h,
136 &ix, &iy, verbose);
137 if(n>0) {
138 if(crossings==0) {
139 if(x1<ix) i=0; else i=1; // which point was out? change that one
140 } else { // both are out, change the other than first time
141 if(i==0) i=1; else i=0;
142 }
143 mx[i]=ix; my[i]=iy; crossings++;
144 if(verbose>3)
145 printf("line between (%g,%g) and (%g,%g) would cross left border at (%g,%g)\n",
146 x1, y1, x2, y2, ix, iy);
147 }
148 /* Right border */
149 n=get_line_intersection(x1, y1, x2, y2,
150 (double)cavp->w, 0.0, (double)cavp->w, (double)cavp->h,
151 &ix, &iy, verbose);
152 if(n>0) {
153 if(crossings==0) {
154 if(x1>ix) i=0; else i=1; // which point was out? change that one
155 } else { // both are out, change the other than first time
156 if(i==0) i=1; else i=0;
157 }
158 mx[i]=ix; my[i]=iy; crossings++;
159 if(verbose>3)
160 printf("line between (%g,%g) and (%g,%g) would cross right border at (%g,%g)\n",
161 x1, y1, x2, y2, ix, iy);
162 }
163 if(verbose>3 && crossings>0) printf("crossings=%d\n", crossings);
164
165 if(nx1!=NULL) *nx1=mx[0];
166 if(ny1!=NULL) *ny1=my[0];
167 if(nx2!=NULL) *nx2=mx[1];
168 if(ny2!=NULL) *ny2=my[1];
169 if(verbose>2 && crossings>0)
170 printf("modified line (%g,%g) -> (%g,%g)\n", mx[0], my[0], mx[1], my[1]);
171
172 return(crossings);
173}
int get_line_intersection(const double a1x, const double a1y, const double a2x, const double a2y, const double b1x, const double b1y, const double b2x, const double b2y, double *ix, double *iy, int verbose)
Definition svg_plot.c:19

Referenced by svg_write_tac().

◆ get_line_intersection()

int get_line_intersection ( const double a1x,
const double a1y,
const double a2x,
const double a2y,
const double b1x,
const double b1y,
const double b2x,
const double b2y,
double * ix,
double * iy,
int verbose )
extern

Check whether two lines, each drawn between two points, intersect each other.

If either end of lines intersects with the other line, that is NOT counted as intersection.

Returns
Returns 1 in case of intersection, and 0 if they do not cross.
See also
check_intersection_with_viewport
Parameters
a1xx,y coordinates of line a at points 1 and 2; x coordinate of point 1.
a1yy coordinate of point 1.
a2xx coordinate of point 2.
a2yy coordinate of point 2.
b1xx,y coordinates of line b at points 1 and 2; x coordinate of point 1.
b1yy coordinate of point 1.
b2xx coordinate of point 2.
b2yy coordinate of point 2.
ixPointers for intersection point coordinates; NULL if not needed.
iyIntersection y coordinate
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 19 of file svg_plot.c.

42 {
43 double sax, say, sbx, sby, s, t, d;
44
45 if(verbose>0) {
46 printf("%s(%g, %g, %g, %g, %g, %g, %g, %g, ix, iy, %d)\n",
47 __func__, a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, verbose);
48 fflush(stdout);
49 }
50
51 if(a1x==a2x && a1y==a2y) return(0);
52 sax=a2x-a1x; say=a2y-a1y;
53 sbx=b2x-b1x; sby=b2y-b1y;
54 d=-sbx*say+sax*sby; if(d==0.0) return(0);
55 s=(-say*(a1x-b1x) + sax*(a1y-b1y)) / d;
56 t=(+sbx*(a1y-b1y) - sby*(a1x-b1x)) / d;
57//if(s>=0.0 && s<=1.0 && t>=0.0 && t<=1.0) {
58 if(s>0.0 && s<1.0 && t>0.0 && t<1.0) {
59 if(verbose>3) printf("s=%g t=%g\n", s, t);
60 if(ix!=NULL) *ix=a1x+(t*sax);
61 if(iy!=NULL) *iy=a1y+(t*say);
62 return(1);
63 }
64 return(0);
65}

Referenced by check_intersection_with_viewport().

◆ strRmExpZeroes()

void strRmExpZeroes ( char * str)

Remove initial exponential zeroes from the exponential part of string representation of values, for example, '1.01E-010' -> '1.01E-10'.

Definition at line 179 of file axis.c.

180{
181 char *p, *r;
182 int i, n;
183
184 if(str==NULL || strlen(str)<3) return;
185 // search the exp E
186 p=strrchr(str, 'E'); if(p==NULL) p=strrchr(str, 'e'); if(p==NULL) return;
187 // if only +,- and 0 after it, then remove also E
188 r=p+1; if(strspn(r, "0+-")==strlen(r)) {*p=(char)0; return;}
189 // remove next '+' or jump over '-'
190 p++;
191 if(*p=='+') {r=p; while(1) {*r=*(r+1); if(*r==(char)0) break; r++;}}
192 else if(*p=='-') p++;
193 // count initial zeroes
194 n=strspn(p, "0"); if(n==0) return;
195 // copy the rest of the string over zeroes
196 r=p+n; n=strlen(r);
197 for(i=0; i<=n; i++) {*p=*r; p++; r++;}
198 return;
199}

Referenced by svg_calculate_axes().

◆ svg_calculate_axes()

int svg_calculate_axes ( struct svg_viewports * vp,
int verbose )
extern

Calculate the axis tick positions. Before calling this, viewport must be filled with curve min and max values. This routine checks that max>min, changing the values if necessary.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_define_viewports, svg_write_axes, svg_write_xticks, svg_write_yticks, axis_tick_positions
Parameters
vpStruct containing the viewport sizes.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 364 of file svg_plot.c.

369 {
370 int ret, ti, prec;
371 double orig_min, orig_max;
372
373 if(verbose>0) {
374 printf("%s(vp, %d)\n", __func__, verbose);
375 fflush(stdout);
376 }
377
378 /* x axis */
379 orig_min=vp->x.min; orig_max=vp->x.max;
380 axis_check_range(&vp->x.min, &vp->x.max, verbose);
381 if(vp->x.fixed_min) vp->x.min=orig_min;
382 if(vp->x.fixed_max) vp->x.max=orig_max;
383 if(verbose>1) printf("x-range %g - %g -> %g - %g\n", orig_min, orig_max, vp->x.min, vp->x.max);
385 if((fabs(vp->x.max)<0.1 || fabs(vp->x.max)>10000) && vp->x.tick_nr>8) vp->x.tick_nr=8; // Only few long tick labels can fit on x axis
386 if((fabs(vp->x.max)<0.01 || fabs(vp->x.max)>100000) && vp->x.tick_nr>6) vp->x.tick_nr=5; // even fewer tick labels can fit on x axis
387 if(vp->label_area_viewport.is) vp->x.tick_nr=1+vp->x.tick_nr/2; // Legends reduce the width of x axis
389 vp->x.min, vp->x.max, vp->x.tick, &vp->x.tick_nr, &vp->x.tickscale,
390 &vp->x.tick_decimals, verbose);
391 if(ret!=0) return(ret+100);
392
393 /* create tick labels to be written later */
394 prec=vp->x.tick_decimals-1-(int)vp->x.tickscale; if(prec<0) prec=0;
395 for(ti=0; ti<vp->x.tick_nr; ti++) {
396 if(vp->x.tickscale<-2 || vp->x.tickscale>3) {
397 sprintf(vp->x.tick_label[ti], "%.*E", vp->x.tick_decimals-1, vp->x.tick[ti]);
398 strRmExpZeroes(vp->x.tick_label[ti]);
399 } else if(vp->x.tickscale<=0)
400 sprintf(vp->x.tick_label[ti], "%.*f", prec, vp->x.tick[ti]);
401 else
402 sprintf(vp->x.tick_label[ti], "%.*f", prec, vp->x.tick[ti]);
403 }
404
405 /* y axis */
406 orig_min=vp->y.min; orig_max=vp->y.max;
407 axis_check_range(&vp->y.min, &vp->y.max, verbose);
408 if(vp->y.fixed_min) vp->y.min=orig_min;
409 if(vp->y.fixed_max) vp->y.max=orig_max;
410 if(verbose>1) printf("y-range %g - %g -> %g - %g\n", orig_min, orig_max, vp->y.min, vp->y.max);
411 vp->y.tick_nr=MAX_TICK_NR; // 10
413 vp->y.min, vp->y.max, vp->y.tick, &vp->y.tick_nr, &vp->y.tickscale,
414 &vp->y.tick_decimals, verbose);
415 if(ret!=0) return(ret+200);
416 /* create tick labels to be written later */
417 prec=vp->y.tick_decimals-1-(int)vp->y.tickscale; if(prec<0) prec=0;
418 for(ti=0; ti<vp->y.tick_nr; ti++) {
419 if(vp->y.tickscale<-2 || vp->y.tickscale>3) {
420 sprintf(vp->y.tick_label[ti], "%.*E", vp->y.tick_decimals-1, vp->y.tick[ti]);
421 strRmExpZeroes(vp->y.tick_label[ti]);
422 } else if(vp->y.tickscale<=0)
423 sprintf(vp->y.tick_label[ti], "%.*f", prec, vp->y.tick[ti]);
424 else
425 sprintf(vp->y.tick_label[ti], "%.*f", prec, vp->y.tick[ti]);
426 }
427
428 return(0);
429}
void strRmExpZeroes(char *str)
Definition axis.c:179
void axis_check_range(double *begin, double *end, int verbose)
Definition axis.c:116
int axis_tick_positions(const double begin, const double end, double *ticks, int *tick_nr, double *scale_factor, int *tick_decimals, int verbose)
Definition axis.c:14
#define MAX_TICK_NR
Definition libtpcsvg.h:25
double tickscale
Definition libtpcsvg.h:87
int tick_nr
Definition libtpcsvg.h:83
int fixed_max
Definition libtpcsvg.h:97
int tick_decimals
Definition libtpcsvg.h:89
double tick[MAX_TICK_NR]
Definition libtpcsvg.h:85
double max
Definition libtpcsvg.h:77
int fixed_min
Definition libtpcsvg.h:95
char tick_label[MAX_TICK_NR][MAX_TICK_LABEL_LEN+1]
Definition libtpcsvg.h:91
double min
Definition libtpcsvg.h:75
struct svg_viewport_pos label_area_viewport
Definition libtpcsvg.h:112
struct svg_coord x
Definition libtpcsvg.h:118
struct svg_coord y
Definition libtpcsvg.h:120

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_close()

int svg_close ( FILE * fp,
char * errmsg,
int verbose )
extern

Close SVG graphics file.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_initiate
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 107 of file svg_file.c.

115 {
116 if(verbose>0) printf("%s(fp, errmsg, %d)\n", __func__, verbose);
117
118 if(svg_write(fp, "</svg>\n", errmsg, verbose-5)!=0) {fclose(fp); return(1);}
119 fflush(fp); fclose(fp);
120 return(0);
121}
int svg_write(FILE *fp, const char *svg_string, char *errmsg, int verbose)
Definition svg_file.c:304

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_create_legends()

int svg_create_legends ( FILE * fp,
struct svg_viewports * vp,
SVG_LEGENDS * legends,
char * errmsg,
int verbose )
extern

Create SVG plot legends.

Returns
Returns 0 if successful, <>0 in case of error.
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
legendsPointer to struct containing legends.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 76 of file svg_legend.c.

88 {
89 char tmp[1024], line[128], ilc[9];
90 double xpos, ypos, ygap, size=100.0, trgsize=140.0, circsize=120.0;
91 int ti, len, maxlen, text_space;
92 const int defNr=24;
93// const double hrratio=1.80; // average character height/width
94 const double hrratio=1.20; // average character height/width
95
96 if(verbose>0) printf("%s(fp, vp, legends, errmsg, %d)\n", __func__, verbose);
97
98 /* Check the input */
99 if(vp->label_area_viewport.is==0) return(0);
100 if(legends==NULL || legends->n<1) return(0);
101 if(fp==NULL) {
102 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
103 return(1);
104 }
105
106 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
107
108 /* Create a new viewport for plot legends */
109 strcpy(tmp, "\n <!-- Legends viewport -->\n");
110 sprintf(line, " <%ssvg x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%d\"",
112 vp->label_area_viewport.w, vp->label_area_viewport.h); strcat(tmp, line);
113 sprintf(line, "\n viewBox=\"0 0 %d %d\"",
114 vp->label_area_viewport.w, vp->label_area_viewport.h); strcat(tmp, line);
115 strcat(tmp, "\n preserveAspectRatio=\"xMidYMid meet\"");
116 strcat(tmp, ">\n");
117 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
118
119
120#if(0) // during testing show the vieport with background color
121 sprintf(tmp, " <%srect width=\"%dpx\" height=\"%dpx\" stroke=\"none\" fill=\"red\" fill-opacity=\"0.3\" />\n",
123 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
124#endif
125
126 /* Set the space for legend text */
127 text_space=(8*vp->label_area_viewport.w)/10;
128
129 /* Determine font size for legends */
130 /* first, based on legend nr */
131 ti=legends->n; if(ti<defNr) ti=defNr;
133 (double)vp->label_area_viewport.h/(double)(ti+1);
134 //printf("vp->label_area_viewport.chr_size := %d\n", vp->label_area_viewport.chr_size);
135 /* then, make smaller based on max length, if necessary */
136 ti=0; maxlen=len=strlen(legends->l[ti].text);
137 for(ti=1; ti<legends->n; ti++) {
138 len=strlen(legends->l[ti].text); if(len>maxlen) maxlen=len;}
139 //printf("legend max length := %d\n", maxlen);
140 if(vp->label_area_viewport.chr_size*maxlen>hrratio*(double)text_space)
141 vp->label_area_viewport.chr_size=hrratio*(double)text_space/(double)maxlen;
142 //printf("vp->label_area_viewport.chr_size := %d\n", vp->label_area_viewport.chr_size);
143 /* Set line gap, if there is space for that */
144 ygap=0; if(legends->n<=2*defNr/3) ygap=vp->label_area_viewport.chr_size/3;
145
146 /* Write legend texts as a group */
147 xpos=(double)(vp->label_area_viewport.w - text_space);
148 ypos=1.5*(double)vp->label_area_viewport.chr_size;
149 sprintf(tmp, " <%sg", ilc);
150 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
151 sprintf(line, " text-anchor=\"Start\""); strcat(tmp, line);
152 sprintf(line, " font-size=\"%d\"", vp->label_area_viewport.chr_size); strcat(tmp, line);
153 sprintf(line, " fill=\"black\""); strcat(tmp, line);
154 strcat(tmp, ">\n");
155 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(8);}
156 /* Write one legend text at a time */
157 for(ti=0; ti<legends->n; ti++) {
158 //printf("ti=%d text='%s'\n", ti, legends->l[ti].text);
159 sprintf(tmp, " <%s", ilc);
160 sprintf(line, "text x=\"%g\" y=\"%g\"", xpos, ypos); strcat(tmp, line);
161 sprintf(line, ">"); strcat(tmp, line);
162 char *senc=svg_str_encode(legends->l[ti].text);
163 if(senc==NULL) strcat(tmp, legends->l[ti].text); else {strcat(tmp, senc); free(senc);}
164 strcat(tmp, "</"); strcat(tmp, ilc); strcat(tmp, "text>\n");
165 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
166 ypos+=ygap+(double)vp->label_area_viewport.chr_size;
167 }
168 sprintf(tmp, " </%sg>\n", ilc);
169 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(10);}
170
171 /* Write legend symbols */
172 xpos=0.5*(double)(vp->label_area_viewport.w - text_space);
173 ypos=1.25*(double)vp->label_area_viewport.chr_size;
174 for(ti=0; ti<legends->n; ti++) {
175 sprintf(tmp, " <%sg", ilc);
176 sprintf(line, " stroke=\"%s\"", svgColorName(legends->l[ti].color)); strcat(tmp, line);
177 sprintf(line, " fill=\"%s\"", svgColorName(legends->l[ti].color)); strcat(tmp, line);
178 if(legends->l[ti].symbol_fill==SYMBOLOPEN) sprintf(line, " fill-opacity=\"0.02\"");
179 else sprintf(line, " fill-opacity=\"0.92\"");
180 strcat(tmp, line);
181 sprintf(line, " stroke-width=\"25\""); strcat(tmp, line);
182 strcat(tmp, ">\n");
183 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(8);}
184
185 /* Line */
186 if(legends->l[ti].plot_type==0 || legends->l[ti].plot_type==1) {
187 sprintf(tmp, " <%sline", ilc);
188 sprintf(line, " x1=\"%g\"", 0.25*xpos); strcat(tmp, line);
189 sprintf(line, " y1=\"%g\"", ypos); strcat(tmp, line);
190 sprintf(line, " x2=\"%g\"", 1.75*xpos); strcat(tmp, line);
191 sprintf(line, " y2=\"%g\"", ypos); strcat(tmp, line);
192 strcat(tmp, " />\n");
193 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(18);}
194 }
195
196 /* Symbol */
197 if(legends->l[ti].plot_type==0 || legends->l[ti].plot_type==2) {
198 sprintf(tmp, " <%suse ", ilc);
199 switch(legends->l[ti].symbol_type) {
200 case RECTANGLE:
201 sprintf(line, "xlink:href=\"#sym-rect\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
202 xpos-0.5*size, ypos-0.5*size, size, size);
203 break;
204 case UPTRIANGLE:
205 sprintf(line, "xlink:href=\"#sym-uptr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
206 xpos-0.5*trgsize, ypos-0.5*trgsize, trgsize, trgsize);
207 break;
208 case DOWNTRIANGLE:
209 sprintf(line, "xlink:href=\"#sym-dotr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
210 xpos-0.5*trgsize, ypos-0.5*trgsize, trgsize, trgsize);
211 break;
212 case DIAMOND:
213 sprintf(line, "xlink:href=\"#sym-diam\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
214 xpos-0.5*trgsize, ypos-0.5*trgsize, trgsize, trgsize);
215 break;
216 case LEFTTRIANGLE:
217 sprintf(line, "xlink:href=\"#sym-letr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
218 xpos-0.5*trgsize, ypos-0.5*trgsize, trgsize, trgsize);
219 break;
220 case RIGHTTRIANGLE:
221 sprintf(line, "xlink:href=\"#sym-ritr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
222 xpos-0.5*trgsize, ypos-0.5*trgsize, trgsize, trgsize);
223 break;
224 case CIRCLE:
225 default:
226 sprintf(line, "xlink:href=\"#sym-circ\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
227 xpos-0.5*circsize, ypos-0.5*circsize, circsize, circsize);
228 break;
229 }
230 strcat(tmp, line);
231 strcat(tmp, " />\n");
232 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(18);}
233 }
234
235
236 sprintf(tmp, " </%sg>\n", ilc);
237 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(10);}
238 ypos+=ygap+(double)vp->label_area_viewport.chr_size;
239 } // next legend
240
241 /* Close the view port */
242 sprintf(tmp, " </%ssvg>\n", ilc);
243 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
244
245 return(0);
246}
char * svgColorName(const svgColor index)
Definition svg_defs.c:38
int svg_write(FILE *fp, const char *svg_string, char *errmsg, int verbose)
Definition svg_file.c:304
int plot_type
Definition libtpcsvg.h:126
svgColor color
Definition libtpcsvg.h:132
svgSymbolType symbol_type
Definition libtpcsvg.h:128
char text[MAX_SVG_LEGEND_LEN+1]
Definition libtpcsvg.h:134
svgSymbolFill symbol_fill
Definition libtpcsvg.h:130
SVG_LEGEND * l
Definition libtpcsvg.h:146
int SVG_INLINE
Definition svg_file.c:12
char * svg_str_encode(const char *s)
Definition svg_legend.c:256

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_create_main_title()

int svg_create_main_title ( FILE * fp,
const char * main_title_text,
const char * sub_title_text,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Create SVG plot main title.

See also
svg_create_xaxis_title, svg_create_yaxis_title
Returns
Returns 0 if successful, <>0 in case of error.
Parameters
fpSVG graphics file pointer.
main_title_textText for main title.
sub_title_textText for sub title.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 17 of file svg_title.c.

31 {
32 char tmp[1024], line[128], ilc[9];
33 double main_pos, sub_pos;
34
35 if(verbose>0) printf("%s(fp, mtt, stt, vp, errmsg, %d)\n", __func__, verbose);
36 if(verbose>1) {
37 printf("main_title_text := '%s'\n", main_title_text);
38 printf("sub_title_text := '%s'\n", sub_title_text);
39 }
40
41 /* Check the input */
42 if(vp->main_title_viewport.is==0) return(0);
43 if(fp==NULL) {
44 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
45 return(1);
46 }
47
48 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
49
50 /* Set title of the SVG document */
51 sprintf(tmp, "\n <%stitle>%s</%stitle>\n", ilc, main_title_text, ilc);
52 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
53
54 /* Create a new viewport for main title */
55 strcpy(tmp, "\n <!-- Main title viewport -->\n");
56 sprintf(line, " <%ssvg x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%d\"",
58 vp->main_title_viewport.w, vp->main_title_viewport.h); strcat(tmp, line);
59 sprintf(line, "\n viewBox=\"0 0 %d %d\"",
60 vp->main_title_viewport.w, vp->main_title_viewport.h); strcat(tmp, line);
61 strcat(tmp, "\n preserveAspectRatio=\"xMidYMid meet\"");
62 strcat(tmp, ">\n");
63 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
64
65
66#if(0)
67 sprintf(tmp, " <rect width=\"%dpx\" height=\"%dpx\" stroke=\"none\" fill=\"lime\" fill-opacity=\"0.3\" />\n",
69 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
70#endif
71
72 /* Determine the y positions for titles */
73 if(strlen(main_title_text)>0) { /* main title exists */
74 if(strlen(sub_title_text)==0) { /* no subtitle */
75 main_pos=0.75*(double)vp->main_title_viewport.h;
76 sub_pos=vp->main_title_viewport.h;
77 } else { /* also subtitle */
78 main_pos=0.52*(double)vp->main_title_viewport.h;
79 sub_pos=0.9*(double)vp->main_title_viewport.h;
80 }
81 } else { /* no main title */
82 if(strlen(sub_title_text)==0) { /* no subtitle either */
83 main_pos=0.5*(double)vp->main_title_viewport.h;
84 sub_pos=vp->main_title_viewport.h;
85 } else { /* only subtitle */
86 main_pos=0.0;
87 sub_pos=0.4*(double)vp->main_title_viewport.h;
88 }
89 }
90
91 /* Set main title text */
92 sprintf(tmp, " <%stext x=\"%d\" y=\"%g\"\n", ilc, vp->main_title_viewport.w/2, main_pos);
93 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
94 sprintf(line, " text-anchor=\"middle\""); strcat(tmp, line);
95 sprintf(line, " font-size=\"%d\"", vp->main_title_viewport.chr_size); strcat(tmp, line);
96 sprintf(line, " fill=\"black\""); strcat(tmp, line);
97 sprintf(line, ">\n"); strcat(tmp, line);
98 sprintf(line, " %s\n", main_title_text); strcat(tmp, line);
99 sprintf(line, " </%stext>\n", ilc); strcat(tmp, line);
100 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
101
102 /* Set sub title text */
103 sprintf(tmp, " <%stext x=\"%d\" y=\"%g\"\n", ilc, vp->main_title_viewport.w/2, sub_pos);
104 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
105 sprintf(line, " text-anchor=\"middle\""); strcat(tmp, line);
106 sprintf(line, " font-size=\"%d\"", 2*vp->main_title_viewport.chr_size/3); strcat(tmp, line);
107 sprintf(line, " fill=\"black\""); strcat(tmp, line);
108 sprintf(line, ">\n"); strcat(tmp, line);
109 sprintf(line, " %s\n", sub_title_text); strcat(tmp, line);
110 sprintf(line, " </%stext>\n", ilc); strcat(tmp, line);
111 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(8);}
112
113 /* Close the view port */
114 sprintf(tmp, " </%ssvg>\n", ilc);
115 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
116
117 return(0);
118}
struct svg_viewport_pos main_title_viewport
Definition libtpcsvg.h:106
int SVG_INLINE
Definition svg_file.c:12

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_create_xaxis_title()

int svg_create_xaxis_title ( FILE * fp,
const char * title_text,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Create SVG plot x axis title.

See also
svg_create_main_title, svg_create_yaxis_title
Returns
Returns 0 if successful, <>0 in case of error.
Parameters
fpSVG graphics file pointer.
title_textText for x axis title.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 126 of file svg_title.c.

138 {
139 char tmp[1024], line[128], ilc[9];
140
141 if(verbose>0) printf("%s(fp, tt, vp, errmsg, %d)\n", __func__, verbose);
142 if(verbose>1) printf("title_text := '%s'\n", title_text);
143
144 /* Check the input */
145 if(vp->xaxis_title_viewport.is==0) return(0);
146 if(fp==NULL) {
147 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
148 return(1);
149 }
150
151 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
152
153 /* Create a new viewport for x axis title */
154 strcpy(tmp, "\n <!-- X axis title viewport -->\n");
155 sprintf(line, " <%ssvg x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%d\"",
157 vp->xaxis_title_viewport.w, vp->xaxis_title_viewport.h); strcat(tmp, line);
158 sprintf(line, "\n viewBox=\"0 0 %d %d\"",
159 vp->xaxis_title_viewport.w, vp->xaxis_title_viewport.h); strcat(tmp, line);
160 strcat(tmp, "\n preserveAspectRatio=\"xMidYMid meet\"");
161 strcat(tmp, ">\n");
162 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
163
164
165#if(0)
166 sprintf(tmp, " <rect width=\"%dpx\" height=\"%dpx\" stroke=\"none\" fill=\"green\" fill-opacity=\"0.3\" />\n",
168 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
169#endif
170
171
172 /* Set the text contents */
173 sprintf(tmp, " <%stext x=\"%d\" y=\"%g\"\n",
174 ilc, vp->xaxis_title_viewport.w/2, 0.75*(double)vp->xaxis_title_viewport.h);
175 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
176 sprintf(line, " text-anchor=\"middle\""); strcat(tmp, line);
177 sprintf(line, " font-size=\"%d\"", vp->xaxis_title_viewport.chr_size); strcat(tmp, line);
178 sprintf(line, " fill=\"black\""); strcat(tmp, line);
179 sprintf(line, ">\n"); strcat(tmp, line);
180 sprintf(line, " %s\n", title_text); strcat(tmp, line);
181 sprintf(line, " </%stext>\n", ilc); strcat(tmp, line);
182 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
183
184 /* Close the view port */
185 sprintf(tmp, " </%ssvg>\n", ilc);
186 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
187
188 return(0);
189}
struct svg_viewport_pos xaxis_title_viewport
Definition libtpcsvg.h:110

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_create_yaxis_title()

int svg_create_yaxis_title ( FILE * fp,
const char * title_text,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Create SVG plot y axis title.

See also
svg_create_main_title, svg_create_xaxis_title
Returns
Returns 0 if successful, <>0 in case of error.
Parameters
fpSVG graphics file pointer.
title_textText for y axis title.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 197 of file svg_title.c.

209 {
210 char tmp[1024], line[128], ilc[9];
211 double xpos, ypos;
212
213 if(verbose>0) printf("%s(fp, tt, vp, errmsg, %d)\n", __func__, verbose);
214 if(verbose>1) printf("title_text := '%s'\n", title_text);
215
216 /* Check the input */
217 if(vp->yaxis_title_viewport.is==0) return(0);
218 if(fp==NULL) {
219 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
220 return(1);
221 }
222
223 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
224
225 /* Create a new viewport for y axis title */
226 strcpy(tmp, "\n <!-- Y axis title viewport -->\n");
227 sprintf(line, " <%ssvg x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%d\"",
229 vp->yaxis_title_viewport.w, vp->yaxis_title_viewport.h); strcat(tmp, line);
230 sprintf(line, "\n viewBox=\"0 0 %d %d\"",
231 vp->yaxis_title_viewport.w, vp->yaxis_title_viewport.h); strcat(tmp, line);
232 strcat(tmp, "\n preserveAspectRatio=\"xMidYMid meet\"");
233 strcat(tmp, ">\n");
234 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
235
236
237#if(0)
238 sprintf(tmp, " <rect width=\"%dpx\" height=\"%dpx\" stroke=\"none\" fill=\"red\" fill-opacity=\"0.3\" />\n",
240 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
241#endif
242
243 /* Set the text contents */
244 xpos=0.75*(double)vp->yaxis_title_viewport.w;
245 ypos=0.5*(double)vp->yaxis_title_viewport.h;
246 sprintf(tmp, " <%stext x=\"%g\" y=\"%g\"\n", ilc, xpos, ypos);
247 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
248 sprintf(line, " text-anchor=\"middle\""); strcat(tmp, line);
249 sprintf(line, " font-size=\"%d\"", vp->yaxis_title_viewport.chr_size); strcat(tmp, line);
250 sprintf(line, " fill=\"black\""); strcat(tmp, line);
251 sprintf(line, " transform=\"rotate(270,%g,%g)\"", xpos, ypos); strcat(tmp, line);
252 sprintf(line, ">\n"); strcat(tmp, line);
253 sprintf(line, " %s\n", title_text); strcat(tmp, line);
254 sprintf(line, " </%stext>\n", ilc); strcat(tmp, line);
255 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
256
257 /* Close the view port */
258 sprintf(tmp, " </%ssvg>\n", ilc);
259 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
260
261 return(0);
262}
struct svg_viewport_pos yaxis_title_viewport
Definition libtpcsvg.h:108

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_define_symbols()

int svg_define_symbols ( FILE * fp,
char * errmsg,
int verbose )
extern

Define the curve symbols for SVG graphics file.

See also
svgSymbolName, svgColorName
Returns
Returns 0 if successful, <>0 in case of error.
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 83 of file svg_defs.c.

91 {
92 char tmp[2048], ilc[9], line[256], allsame[128];
93
94 if(verbose>0) printf("svg_define_symbols(fp, errmsg, %d)\n", verbose);
95
96 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
97 strcpy(allsame, "viewBox=\"0 0 120 120\" preserveAspectRatio=\"xMinYMin meet\"");
98
99 strcpy(tmp, "");
100 sprintf(line, " <%sdefs>\n", ilc); strcat(tmp, line);
101
102 sprintf(line, " <%ssymbol id=\"sym-rect\" %s >\n", ilc, allsame);
103 strcat(tmp, line);
104 //sprintf(line, " <%srect x=\"2\" y=\"2\" width=\"116\" height=\"116\" style=\"stroke-width:40\" />\n", ilc);
105 sprintf(line, " <%spolygon points=\"13 13, 13 107, 107 107, 107 13\" />\n", ilc);
106 strcat(tmp, line);
107 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
108
109 sprintf(line, " <%ssymbol id=\"sym-circ\" %s >\n", ilc, allsame);
110 strcat(tmp, line);
111// sprintf(line, " <%scircle cx=\"60\" cy=\"60\" r=\"50\" />\n", ilc);
112 sprintf(line, " <%scircle cx=\"60\" cy=\"60\" r=\"46\" />\n", ilc);
113 strcat(tmp, line);
114 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
115
116 sprintf(line, " <%ssymbol id=\"sym-uptr\" %s >\n", ilc, allsame);
117 strcat(tmp, line);
118// sprintf(line, " <%spolygon points=\" 10 17, 110 17, 60 103 \" />\n", ilc);
119 sprintf(line, " <%spolygon points=\" 14 14, 106 14, 60 106 \" />\n", ilc);
120 strcat(tmp, line);
121 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
122
123 sprintf(line, " <%ssymbol id=\"sym-dotr\" %s >\n", ilc, allsame);
124 strcat(tmp, line);
125// sprintf(line, " <%spolygon points=\" 10 103, 110 103, 60 17 \" />\n", ilc);
126 sprintf(line, " <%spolygon points=\" 14 106, 106 106, 60 14 \" />\n", ilc);
127 strcat(tmp, line);
128 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
129
130 sprintf(line, " <%ssymbol id=\"sym-letr\" %s >\n", ilc, allsame);
131 strcat(tmp, line);
132// sprintf(line, " <%spolygon points=\" 103 10, 103 110, 17 60 \" />\n", ilc);
133 sprintf(line, " <%spolygon points=\" 106 14, 106 106, 14 60 \" />\n", ilc);
134 strcat(tmp, line);
135 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
136
137 sprintf(line, " <%ssymbol id=\"sym-ritr\" %s >\n", ilc, allsame);
138 strcat(tmp, line);
139// sprintf(line, " <%spolygon points=\" 17 10, 17 110, 103 60 \" />\n", ilc);
140 sprintf(line, " <%spolygon points=\" 14 14, 14 106, 106 60 \" />\n", ilc);
141 strcat(tmp, line);
142 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
143
144 sprintf(line, " <%ssymbol id=\"sym-diam\" %s >\n", ilc, allsame);
145 strcat(tmp, line);
146// sprintf(line, " <%spolygon points=\" 60 10, 110 60, 60 110, 10 60 \" />\n", ilc);
147 sprintf(line, " <%spolygon points=\" 60 14, 106 60, 60 106, 14 60 \" />\n", ilc);
148 strcat(tmp, line);
149 sprintf(line, " </%ssymbol>\n", ilc); strcat(tmp, line);
150
151 sprintf(line, " </%sdefs>\n", ilc); strcat(tmp, line);
152
153 return(svg_write(fp, tmp, errmsg, verbose-5));
154}
int SVG_INLINE
Definition svg_file.c:12

Referenced by svg_initiate(), svg_start_plot_viewport(), and svg_xhtml_initiate().

◆ svg_define_viewports()

int svg_define_viewports ( const int main_viewport_width,
const int main_viewport_height,
const int is_main_title,
const int is_yaxis_title,
const int is_xaxis_title,
const int is_label_area,
struct svg_viewports * vp,
int verbose )
extern

Define the viewport positions for further use. All measures are in pixels.

Axis tick labels (y axis at least) should be set before calling this, so that enough room for the labels can be reserved.

Returns
Returns pointer to the file if successful and NULL in case of an error.
Parameters
main_viewport_widthMain viewport width (zero if default is used)
main_viewport_heightMain viewport height (zero if default is used)
is_main_titleIs there main title? no=0, yes<>0
is_yaxis_titleIs there y axis title? no=0, yes<>0
is_xaxis_titleIs there x axis title? no=0, yes<>0
is_label_areaIs there label area? no=0, yes<>0
vpPointer to structure which will be filled with viewport positions and sizes
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 65 of file svg_vport.c.

82 {
83 int ti, m, n;
84
85 if(verbose>0)
86 printf("%s(%d, %d, %d, %d, %d, %d, vp, %d)\n", __func__,
87 main_viewport_width, main_viewport_height, is_main_title, is_yaxis_title,
88 is_xaxis_title, is_label_area, verbose);
89
90 if(vp==NULL) return(1);
91 /* Set main viewport */
93 if(main_viewport_width<1) vp->main_viewport.w=SVG_VIEWPORT_WIDTH;
94 if(main_viewport_height<1) vp->main_viewport.h=SVG_VIEWPORT_HEIGHT;
95 /* Set viewport for main title(s) */
96 if(is_main_title==0) {
101 } else {
107 }
108 /* Set viewport for x axis title */
109 if(is_xaxis_title==0) {
116 } else {
123 }
124 /* Set viewport for y axis title */
125 if(is_yaxis_title==0) {
132 } else {
140 else
142 }
143 /* Set viewport for label area */
144 if(is_label_area==0) {
150 } else {
156 }
157 /* Set viewport for plot area */
163 /* Set plot area contents (inside plot area) */
165 for(ti=m=0; ti<vp->y.tick_nr; ti++) {n=strlen(vp->y.tick_label[ti]); if(n>m) m=n;}
166 if(verbose>2) printf("max_yaxis_label_len=%d\n", m);
168 else if(m<5) vp->coordinate_area_viewport.x=vp->plot_area_viewport.w/10;
169 else if(m<7) vp->coordinate_area_viewport.x=vp->plot_area_viewport.w/7; // prev 8
170 else vp->coordinate_area_viewport.x=vp->plot_area_viewport.w/4; // prev 5
174 /* Calculate the character size for tick labels etc */
176 ceil(0.62*(double)(vp->plot_area_viewport.h-vp->coordinate_area_viewport.h)); // prev 0.67
177
178 if(verbose>3) printf("coordinate_area_viewport.h := %d\n", vp->coordinate_area_viewport.h);
179
180 return(0);
181}
#define SVG_VIEWPORT_WIDTH
Definition libtpcsvg.h:21
#define SVG_VIEWPORT_HEIGHT
Definition libtpcsvg.h:23
struct svg_viewport_pos coordinate_area_viewport
Definition libtpcsvg.h:116
struct svg_viewport_pos main_viewport
Definition libtpcsvg.h:104
struct svg_viewport_pos plot_area_viewport
Definition libtpcsvg.h:114

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_end_coordinate_viewport()

int svg_end_coordinate_viewport ( FILE * fp,
char * errmsg,
int verbose )
extern

End coordinate area viewport.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_start_coordinate_viewport
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 327 of file svg_plot.c.

335 {
336 char tmp[1024];
337
338 if(verbose>0) {
339 printf("%s(fp, errmsg, %d)\n", __func__, verbose);
340 fflush(stdout);
341 }
342
343 /* Check the input */
344 if(fp==NULL) {
345 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
346 return(1);
347 }
348
349 if(SVG_INLINE) strcpy(tmp, " </svg:svg>\n\n");
350 else strcpy(tmp, " </svg>\n\n");
351 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(2);}
352
353 return(0);
354}
int SVG_INLINE
Definition svg_file.c:12

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_end_plot_viewport()

int svg_end_plot_viewport ( FILE * fp,
char * errmsg,
int verbose )
extern

End plot viewport.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_start_plot_viewport
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 238 of file svg_plot.c.

248 {
249 char tmp[1024];
250
251 if(verbose>0) {
252 printf("%s(fp, vp, errmsg, %d)\n", __func__, verbose);
253 fflush(stdout);
254 }
255
256 /* Check the input */
257 if(fp==NULL) {
258 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
259 return(1);
260 }
261
262 /* Write the end tag */
263 if(SVG_INLINE) strcpy(tmp, " </svg:svg>\n"); else strcpy(tmp, " </svg>\n");
264
265 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(2);}
266
267 return(0);
268}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_init_coord()

void svg_init_coord ( struct svg_coord * p)
extern

Initiate struct svg_coord contents to all-zeroes before use.

Definition at line 26 of file svg_vport.c.

28 {
29 p->min=0;
30 p->max=0;
31 p->scale=0.0;
32 p->origo=0.0;
33 p->tick_nr=0;
34 for(int i=0; i<MAX_TICK_NR; i++) p->tick[i]=0.0;
35 p->tickscale=0.0;
36 p->tick_decimals=0.0;
37 for(int i=0; i<MAX_TICK_NR; i++) p->tick_label[i][0]=(char)0;
38 p->upper_margin=0;
39 p->fixed_min=0;
40 p->fixed_max=0;
41}
int upper_margin
Definition libtpcsvg.h:93
double origo
Definition libtpcsvg.h:81
double scale
Definition libtpcsvg.h:79

Referenced by svg_init_viewports().

◆ svg_init_legends()

void svg_init_legends ( SVG_LEGENDS * legends)
extern

Initiate SVG plot legends struct contents; call this once before usage

Parameters
legendsPointer to legends struct

Definition at line 14 of file svg_legend.c.

17 {
18 legends->_init=1;
19 legends->n=0;
20 legends->l=NULL;
21}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_init_viewport_pos()

void svg_init_viewport_pos ( struct svg_viewport_pos * p)
extern

Initiate struct svg_viewport_pos contents to all-zeroes before use.

Definition at line 15 of file svg_vport.c.

17 {
18 p->is=0;
19 p->x=0;
20 p->y=0;
21 p->w=0;
22 p->h=0;
23 p->chr_size=0;
24}

Referenced by svg_init_viewports().

◆ svg_init_viewports()

void svg_init_viewports ( struct svg_viewports * p)
extern

Initiate struct svg_viewports contents to all-zeroes before use.

Definition at line 43 of file svg_vport.c.

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_initiate()

FILE * svg_initiate ( const char * filename,
const double height,
const double width,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Initiate a new SVG graphics file.

If file with same name exists, it is overwritten without backup.

Returns
Returns pointer to the file if successful and NULL in case of an error.
See also
svg_close, svg_xhtml_initiate
Parameters
filenameFile name for SVG graphics.
heightPlot height in cm; 0, if not predefined.
widthPlot width in cm; 0, if not predefined.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 22 of file svg_file.c.

36 {
37 FILE *fp;
38 char tmp[1024], *cptr;
39
40 if(verbose>0)
41 printf("%s(%s, %g, %g, vp, errmsg, %d)\n", __func__, filename, height, width, verbose);
42
43 /* Check input */
44 if(filename==NULL || vp==NULL) return(NULL);
45 if(vp->main_viewport.w<3 || vp->main_viewport.h<3) return(NULL);
46
47 /* Open file for write */
48 fp=fopen(filename, "w");
49 if(fp==NULL) {
50 if(errmsg!=NULL) strcpy(errmsg, "cannot open file for write");
51 return(fp);
52 }
53
54 strcpy(tmp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
55 //strcat(tmp, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
56 //strcat(tmp, " \"https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
57 strcat(tmp, "<svg version=\"1.1\" baseProfile=\"full\"\n");
58 strcat(tmp, " xmlns=\"https://www.w3.org/2000/svg\"\n");
59 strcat(tmp, " xmlns:xlink=\"https://www.w3.org/1999/xlink\"\n");
60 strcat(tmp, " xmlns:ev=\"https://www.w3.org/2001/xml-events\"");
61 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
62 fclose(fp); remove(filename); return(NULL);}
63
64 sprintf(tmp, "\n viewBox=\"0 0 %d %d\"",
66 strcat(tmp, "\n preserveAspectRatio=\"xMinYMin meet\"");
67 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
68 fclose(fp); remove(filename); return(NULL);}
69
70 if(width>0.0) {
71 sprintf(tmp, "\n width=\"%gcm\"", width);
72 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
73 fclose(fp); remove(filename); return(NULL);}
74 }
75 if(height>0.0) {
76 sprintf(tmp, "\n height=\"%gcm\"", height);
77 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
78 fclose(fp); remove(filename); return(NULL);}
79 }
80
81 strcpy(tmp, ">\n");
82 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
83 fclose(fp); remove(filename); return(NULL);}
84
85 /* Write file name as title element */
86 cptr=strrchr(filename, '/'); if(cptr==NULL) cptr=strrchr(filename, '\\');
87 if(cptr!=NULL) cptr++; else cptr=(char*)filename;
88 if(cptr!=NULL) {
89 sprintf(tmp, " <title>%s</title>\n", cptr);
90 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
91 fclose(fp); remove(filename); return(NULL);}
92 }
93
94 /* Create plot symbols for possible later use */
95 if(svg_define_symbols(fp, errmsg, verbose)!=0) {
96 fclose(fp); remove(filename); return(NULL);}
97
98 return(fp);
99}
int svg_define_symbols(FILE *fp, char *errmsg, int verbose)
Definition svg_defs.c:83

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_legend_add()

int svg_legend_add ( SVG_LEGENDS * legends,
const int plot_type,
const int symbol_type,
const svgSymbolFill symbol_fill,
const int color,
const char * text )
extern

Add information of one legend item to legends struct.

Memory will be allocated here.

Returns
Returns 0 when successful, otherwise <>0.
Parameters
legendsPointer to legends structure.
plot_typePlot type: 1=line, 2=symbols, 0=both line and symbols.
symbol_typeSymbol type: RECTANGLE, CIRCLE, UPTRIANGLE, DOWNTRIANGLE, DIAMOND, LEFTTRIANGLE, RIGHTTRIANGLE
symbol_fillSymbol filling: SYMBOLOPEN, SYMBOLFILLED
colorSVG color index.
textPointer to Legend text.

Definition at line 43 of file svg_legend.c.

56 {
57 if(legends==NULL || legends->_init!=1) return 1;
58 if(legends->n==0) legends->l=malloc((legends->n+1)*sizeof(SVG_LEGEND));
59 else legends->l=realloc(legends->l, (legends->n+1)*sizeof(SVG_LEGEND));
60 if(legends->l==NULL) {legends->n=0; return 2;}
61 legends->n++;
62 legends->l[legends->n-1].plot_type=plot_type;
63 legends->l[legends->n-1].symbol_type=symbol_type;
64 legends->l[legends->n-1].symbol_fill=symbol_fill;
65 legends->l[legends->n-1].color=color;
66 strncpy(legends->l[legends->n-1].text, text, MAX_SVG_LEGEND_LEN);
67 legends->l[legends->n-1].text[MAX_SVG_LEGEND_LEN]=(char)0;
68 return 0;
69}
#define MAX_SVG_LEGEND_LEN
Definition libtpcsvg.h:29

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_legend_empty()

void svg_legend_empty ( SVG_LEGENDS * legends)
extern

Empty the legends struct contents and free the allocated memory.

Parameters
legendsPointer to legends struct

Definition at line 26 of file svg_legend.c.

29 {
30 if(legends==NULL) return;
31 if(legends->_init==0 || legends->n<1) return;
32 free(legends->l);
33 legends->n=0;
34}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_start_coordinate_viewport()

int svg_start_coordinate_viewport ( FILE * fp,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Start coordinate area viewport.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_end_coordinate_viewport, svg_define_viewports
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 276 of file svg_plot.c.

286 {
287 char tmp[1024], line[128];
288
289 if(verbose>0) {
290 printf("%s(fp, vp, errmsg, %d)\n", __func__, verbose);
291 fflush(stdout);
292 }
293
294 /* Check the input */
295 if(fp==NULL) {
296 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
297 return(1);
298 }
299
300 /* Create a new viewport for plot area */
301 strcpy(tmp, "\n <!-- Coordinate area viewport -->\n");
302 if(SVG_INLINE) strcat(tmp, " <svg:"); else strcat(tmp, " <");
303 sprintf(line, "svg x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%d\"",
305 vp->coordinate_area_viewport.w, vp->coordinate_area_viewport.h); strcat(tmp, line);
306 sprintf(line, "\n viewBox=\"0 0 %d %d\"",
307 vp->coordinate_area_viewport.w, vp->coordinate_area_viewport.h); strcat(tmp, line);
308 strcat(tmp, "\n preserveAspectRatio=\"xMidYMid meet\"");
309 strcat(tmp, ">\n");
310 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
311
312#if(0)
313 sprintf(tmp, " <rect width=\"%dpx\" height=\"%dpx\" stroke=\"none\" fill=\"silver\" fill-opacity=\"0.3\" />\n",
315 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
316#endif
317
318 return(0);
319}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_start_plot_viewport()

int svg_start_plot_viewport ( FILE * fp,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Start plot area viewport.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_end_plot_viewport, svg_define_viewports
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 181 of file svg_plot.c.

191 {
192 char tmp[1024], line[128], ilc[9];
193
194 if(verbose>0) {
195 printf("%s(fp, vp, errmsg, %d)\n", __func__, verbose);
196 fflush(stdout);
197 }
198
199 /* Check the input */
200 if(fp==NULL) {
201 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
202 return(1);
203 }
204 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
205
206 /* Create a new viewport for plot area */
207 strcpy(tmp, "\n <!-- Plot area viewport -->\n");
208 strcat(tmp, " <"); strcat(tmp, ilc); strcat(tmp, "svg");
209 sprintf(line, " x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%d\"",
211 vp->plot_area_viewport.w, vp->plot_area_viewport.h); strcat(tmp, line);
212 sprintf(line, "\n viewBox=\"0 0 %d %d\"",
213 vp->plot_area_viewport.w, vp->plot_area_viewport.h); strcat(tmp, line);
214 strcat(tmp, "\n preserveAspectRatio=\"xMidYMid meet\"");
215 strcat(tmp, ">\n");
216 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
217
218#if(0)
219 sprintf(tmp, " <%srect width=\"%dpx\" height=\"%dpx\" stroke=\"none\" fill=\"yellow\" fill-opacity=\"0.1\" />\n",
221 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
222#endif
223
224#if(0) // moved elsewhere
225 /* Create symbols for later use */
226 if(svg_define_symbols(fp, errmsg)!=0) {return(6);}
227#endif
228
229 return(0);
230}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_str_encode()

char * svg_str_encode ( const char * s)
extern

Encode special characters for XML, including SVG.

Postcondition
Free the memory of returned string pointer.
Returns
Returns pointer to encoded string. NULL is returned in case of an error, or if encoding is not necessary.
See also
strEncodeForXML
Parameters
sPointer to the string to be encoded.

Definition at line 256 of file svg_legend.c.

259 {
260 if(s==NULL) return(NULL);
261 /* Count the characters needing encoding */
262 int n=0;
263 for(size_t i=0; i<strlen(s); i++) {
264 if(s[i]=='&') {n++; continue;}
265 if(s[i]=='\'') {n++; continue;}
266 if(s[i]=='\"') {n++; continue;}
267 if(s[i]=='<') {n++; continue;}
268 if(s[i]=='>') {n++; continue;}
269 }
270 if(n==0) return(NULL);
271 /* Allocate memory for new string (one char to max 6 chars) */
272 n*=5; n+=strlen(s);
273 char *ns=(char*)malloc(n*sizeof(char));
274 if(ns==NULL) return(NULL);
275 /* Process the string */
276 for(int i=0; i<n; i++) ns[i]=(char)0;
277 for(size_t i=0; i<strlen(s); i++) {
278 if(s[i]=='&') {strcat(ns, "&amp;"); continue;}
279 if(s[i]=='\'') {strcat(ns, "&apos;"); continue;}
280 if(s[i]=='\"') {strcat(ns, "&quot;"); continue;}
281 if(s[i]=='<') {strcat(ns, "&lt;"); continue;}
282 if(s[i]=='>') {strcat(ns, "&gt;"); continue;}
283 ns[strlen(ns)]=s[i];
284 }
285 return(ns);
286}

Referenced by svg_create_legends(), and svg_write_tac().

◆ svg_write()

int svg_write ( FILE * fp,
const char * svg_string,
char * errmsg,
int verbose )
extern

Write given string into open SVG file

Returns
Returns 0 if successful, <>0 in case of an error.
See also
svg_initiate, svg_xhtml_initiate, svg_xhtml_svg_open
Parameters
fpSVG graphics file pointer.
svg_stringChar pointer to NULL terminated string to be written into file.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 304 of file svg_file.c.

314 {
315 int pnr, len;
316
317 if(verbose>0) printf("%s(fp, svg_string, errmsg, %d)\n", __func__, verbose);
318 if(verbose>1) printf("svg_string := %s\n", svg_string);
319
320 len=strlen(svg_string); if(len<1) return(0);
321 if(fp==NULL) {
322 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
323 return(1);
324 }
325 pnr=fprintf(fp, "%s", svg_string);
326 if(pnr<len) {
327 if(errmsg!=NULL) sprintf(errmsg, "cannot write into file");
328 return(2);
329 }
330 return(0);
331}

Referenced by svg_close(), svg_create_legends(), svg_create_main_title(), svg_create_xaxis_title(), svg_create_yaxis_title(), svg_define_symbols(), svg_end_coordinate_viewport(), svg_end_plot_viewport(), svg_initiate(), svg_start_coordinate_viewport(), svg_start_plot_viewport(), svg_write_axes(), svg_write_tac(), svg_write_xticks(), svg_write_yticks(), svg_xhtml_close(), svg_xhtml_initiate(), svg_xhtml_svg_close(), and svg_xhtml_svg_open().

◆ svg_write_axes()

int svg_write_axes ( FILE * fp,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Draw the axes into SVG plot coordinate area.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_define_viewports, svg_calculate_axes, svg_write_xticks, svg_write_yticks, axis_tick_positions
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
errmsgPointer to string where error message is written; NULL if not needed.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 437 of file svg_plot.c.

446 {
447 int linew=20, coordw=10;
448 char tmp[1024], line[128], ilc[9];
449 double f;
450
451 if(verbose>0) {
452 printf("%s(fp, vp, errmsg, %d)\n", __func__, verbose);
453 fflush(stdout);
454 }
455
456 /* Check the input */
457 if(fp==NULL) {
458 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
459 return(1);
460 }
461 if(vp->x.min>=vp->x.max || vp->y.min>=vp->y.max) {
462 if(errmsg!=NULL) sprintf(errmsg, "invalid plot range");
463 if(verbose>1) {
464 printf("vp->x.min=%g vp->x.max=%g\n", vp->x.min, vp->x.max);
465 printf("vp->y.min=%g vp->y.max=%g\n", vp->y.min, vp->y.max);
466 }
467 return(2);
468 }
469
470 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
471
472 /* Draw the lines around the plot */
473 sprintf(tmp, " <%s", ilc);
474 sprintf(line, "polyline fill=\"none\" stroke=\"%s\" stroke-width=\"%d\"\n", "black", linew);
475 strcat(tmp, line);
476#if(0)
477 sprintf(line, " points=\"%d,%d %d,%d %d,%d %d,%d %d,%d\" />\n",
478 linew/2, linew/2,
479 linew/2, vp->coordinate_area_viewport.h-linew,
481 vp->coordinate_area_viewport.w-linew/2, linew/2,
482 linew/2, linew/2);
483 strcat(tmp, line);
484#else
485 sprintf(line, " points=\"%d,%d %d,%d %d,%d %d,%d %d,%d\" />\n",
486 linew/2, linew/2,
487 linew/2, vp->coordinate_area_viewport.h-linew/2,
488 vp->coordinate_area_viewport.w-linew/2, vp->coordinate_area_viewport.h-linew/2,
489 vp->coordinate_area_viewport.w-linew/2, linew/2,
490 linew/2, linew/2);
491 strcat(tmp, line);
492#endif
493 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(4);}
494
495 /* Set the upper margins for both axes */
496 vp->y.upper_margin=0.04*(double)vp->coordinate_area_viewport.h;
497#if(1)
498 if(vp->label_area_viewport.is)
499 vp->x.upper_margin=0.02*(double)vp->coordinate_area_viewport.w;
500 else // If no legends, then more room may be needed to fit x tick labels
501 vp->x.upper_margin=0.08*(double)vp->coordinate_area_viewport.w;
502#else
503 vp->x.upper_margin=0.08*(double)vp->coordinate_area_viewport.w;
504#endif
505
506 /* Calculate the scale factors */
508 vp->x.scale=f/(vp->x.max-vp->x.min);
509 if(verbose>0) printf("xscalef:=%g (%g vs %g-%g)\n", vp->x.scale, f, vp->x.min, vp->x.max);
511 vp->y.scale=f/(vp->y.max-vp->y.min);
512 if(verbose>1) printf("yscalef:=%g (%g vs %g-%g)\n", vp->y.scale, f, vp->y.min, vp->y.max);
513
514 /* Calculate the origo in plot coordinates */
515 vp->x.origo=-vp->x.scale*vp->x.min;
516 if(verbose>1) printf("x.origo := %g\n", vp->x.origo);
517 vp->y.origo=-vp->y.scale*vp->y.min;
518 if(verbose>1) printf("y.origo := %g\n", vp->y.origo);
519
520 /* Draw the x=0 line, if necessary */
521 if(vp->x.origo>0 && vp->x.origo<vp->coordinate_area_viewport.w/*-vp->x.upper_margin*/) {
522 if(verbose>1) printf("drawing x=0 line\n");
523 sprintf(tmp, " <%s", ilc);
524 sprintf(line, "line fill=\"none\" stroke=\"%s\" stroke-width=\"%d\"\n", "black", coordw);
525 strcat(tmp, line);
526 sprintf(line, " x1=\"%g\" x2=\"%g\" y1=\"%d\" y2=\"%d\" />\n",
527 vp->x.origo, vp->x.origo, 0, vp->coordinate_area_viewport.h);
528 strcat(tmp, line);
529 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
530 }
531
532 /* Draw the y=0 line, if necessary */
533 if(vp->y.origo>0 && vp->y.origo<vp->coordinate_area_viewport.h /*- vp->y.upper_margin*/) {
534 if(verbose>1) printf("drawing y=0 line\n");
535 sprintf(tmp, " <%s", ilc);
536 sprintf(line, "line fill=\"none\" stroke=\"%s\" stroke-width=\"%d\"\n", "black", coordw);
537 strcat(tmp, line);
538 sprintf(line, " x1=\"%d\" x2=\"%d\" y1=\"%g\" y2=\"%g\" />\n",
542 strcat(tmp, line);
543 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(7);}
544 }
545
546 return(0);
547}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_write_tac()

int svg_write_tac ( FILE * fp,
struct svg_viewports * vp,
const int plot_type,
const char * tac_id,
const char * tac_title,
double * x,
double * y,
const int data_nr,
const char * color,
const svgSymbolType symbol_type,
const svgSymbolFill symbol_fill,
char * errmsg,
int verbose )
extern

Draw sample curve in an SVG file.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_initiate, svg_write, svg_define_viewports
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
plot_typePlot type: 1=line, 2=symbols, 0=both line and symbols.
tac_idUnique ID for the curve.
tac_titleTitle of the curve, which may be shown in the graph.
xPointer to the polyline data x array (original quantities).
yPointer to the polyline data y array (original quantities).
data_nrNr of data points in the array (half of array length).
colorSVG color name as a string, e.g. aqua,black,blue,fuchsia,gray, green,lime,maroon,navy,olive,purple,red,silver,teal,yellow. Note that this string is not tested.
symbol_typeSymbol type: RECTANGLE,CIRCLE,UPTRIANGLE,DOWNTRIANGLE,DIAMOND, LEFTTRIANGLE, RIGHTTRIANGLE
symbol_fillSymbol filling: SYMBOLOPEN, SYMBOLFILLED
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 746 of file svg_plot.c.

777 {
778 char tmp[1024], line[128], ilc[9];
779 int i, j;
780 double px, py, size=100.0, trgsize=140.0, circsize=120.0;
781
782 if(verbose>0) {
783 printf("%s(fp, vp, %d, %s, %s, x, y, %d, %s, %d, %d, errmsg, %d)\n", __func__,
784 plot_type, tac_id, tac_title, data_nr, color, (int)symbol_type, (int)symbol_fill, verbose);
785 fflush(stdout);
786 }
787
788 /* Check the input */
789 if(data_nr<1) return(0);
790 if(fp==NULL) {if(errmsg!=NULL) sprintf(errmsg, "file was closed too early"); return(1);}
791 if(color==NULL || strlen(color)<2) {
792 if(errmsg!=NULL) sprintf(errmsg, "invalid color");
793 return(1);
794 }
795
796 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
797
798 /* Initiate the curve object group */
799 sprintf(tmp, "\n <!-- %s : %s -->\n <%sg", tac_id, tac_title, ilc);
800 sprintf(line, " stroke=\"%s\"", color); strcat(tmp, line);
801 sprintf(line, " stroke-width=\"%g\"", 0.25*size); strcat(tmp, line);
802 sprintf(line, " fill=\"%s\"", color); strcat(tmp, line);
803 if(symbol_fill==SYMBOLOPEN) sprintf(line, " fill-opacity=\"0.02\"");
804 else sprintf(line, " fill-opacity=\"0.92\"");
805 strcat(tmp, line); strcat(tmp, ">\n");
806 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(11);}
807 /* Set the curve title */
808 sprintf(tmp, " <%s", ilc);
809 sprintf(line, "title>"); strcat(tmp, line);
810 char *senc=svg_str_encode(tac_title);
811 if(senc==NULL) strcat(tmp, tac_title); else {strcat(tmp, senc); free(senc);}
812 strcat(tmp, "</"); strcat(tmp, ilc); strcat(tmp, "title>\n");
813 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(12);}
814
815 /* Plot the line, if required */
816 strcpy(tmp, ""); strcpy(line, "");
817 if(plot_type==0 || plot_type==1) {
818 int lineon=0, path_started=0, prev_exists=0, cross_nr;
819 double prev_px=-1, prev_py=-1, nx1, ny1, nx2, ny2;
820 /* Start a new print line */
821 strcpy(line, "\n ");
822 /* Write line coordinates */
823 for(i=j=0; i<data_nr; i++) {
824 if(isnan(x[i]) || isnan(y[i])) {lineon=0; continue;}
825 /* Print recent line coordinates in file */
826 if(j>=5) { /* line end */
827 if(path_started==0) {
828 sprintf(tmp, " <%spath fill=\"none\" d=\"", ilc);
829 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(14);}
830 path_started=1;
831 }
832 if(svg_write(fp, line, errmsg, verbose-5)!=0) {return(15);}
833 /* Start a new line */
834 j=0; strcpy(line, "\n ");
835 }
836 /* Compute the point coordinates in viewport */
837 if(verbose>3) printf("x[%d]=%g y[%d]=%g\n", i, x[i], i, y[i]);
838 px=vp->x.origo+x[i]*vp->x.scale;
839 py=vp->coordinate_area_viewport.h-(vp->y.origo+vp->y.scale*y[i]);
840 /* Make sure that prev point exists */
841 if(prev_exists==0) {prev_px=px; prev_py=py; prev_exists=1;}
842 /* Check if line would cross viewport border(s) */
843 cross_nr=check_intersection_with_viewport(prev_px, prev_py, px, py,
844 &vp->coordinate_area_viewport, &nx1, &ny1, &nx2, &ny2, verbose);
845 if(verbose>2 &&cross_nr>0)
846 printf("new line coordinates (%g,%g) -> (%g,%g)\n", nx1, ny1, nx2, ny2);
847 if(cross_nr==2) {
848 // Move to nx1,ny1 and draw line from nx1,ny1 to nx2,ny2
849 if(j>0) strcat(line, " ");
850 sprintf(tmp, "M%.0f %.0f L%.0f %.0f", nx1, ny1, nx2, ny2);
851 if(verbose>4) printf(" write %s\n", tmp);
852 strcat(line, tmp); j+=2;
853 lineon=0;
854 // Proceed to next sample
855 prev_px=px; prev_py=py;
856 continue;
857 } else if(cross_nr==1) {
858 if(nx1!=prev_px || ny1!=prev_py) lineon=0;
859 // Draw line from nx1,ny1 to nx2,ny2
860 if(j>0) strcat(line, " ");
861 if(lineon==0) {sprintf(tmp, "M%.0f %.0f L%.0f %.0f", nx1, ny1, nx2, ny2); j+=2;}
862 else if(lineon==1) {sprintf(tmp, "L%.0f %.0f", nx2, ny2); j++;}
863 else {sprintf(tmp, "%.0f %.0f", nx2, ny2); j++;}
864 if(verbose>4) printf(" write %s\n", tmp);
865 strcat(line, tmp);
866 if(nx2!=px || ny2!=py) lineon=0; else lineon++;
867 // Proceed to next sample
868 prev_px=px; prev_py=py;
869 continue;
870 } else {
871 nx1=prev_px; ny1=prev_py; nx2=px; ny2=py;
872 }
873 /* Draw line if coordinates are within viewport */
874 if(nx1>=0 && nx1<=vp->coordinate_area_viewport.w+1 &&
875 nx2>=0 && nx2<=vp->coordinate_area_viewport.w+1 &&
876 ny1>=0 && ny1<=vp->coordinate_area_viewport.h+1 &&
877 ny2>=0 && ny2<=vp->coordinate_area_viewport.h+1)
878 {
879 if(j>0) strcat(line, " ");
880 if(lineon==0) {
881 if(nx1!=nx2 && ny1!=ny2) sprintf(tmp, "M%.0f %.0f L%.0f %.0f", nx1, ny1, nx2, ny2);
882 else sprintf(tmp, "M%.0f %.0f", nx2, ny2);
883 j+=2;
884 }
885 else if(lineon==1) {sprintf(tmp, "L%.0f %.0f", nx2, ny2); j++;}
886 else {sprintf(tmp, "%.0f %.0f", nx2, ny2); j++;}
887 if(verbose>4) printf(" write %s\n", tmp);
888 strcat(line, tmp);
889 lineon++;
890 } else {
891 lineon=0;
892 }
893 prev_px=px; prev_py=py;
894 }
895 /* Write into file the remaining (if any) points */
896 if(j>0) {
897 if(path_started==0) {
898 sprintf(tmp, " <%spath fill=\"none\" d=\"", ilc);
899 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(14);}
900 }
901 if(svg_write(fp, line, errmsg, verbose-5)!=0) {return(16);}
902 }
903 /* Close line */
904 strcpy(tmp, "\" />\n");
905 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(17);}
906 }
907
908 /* Plot the symbols, if required */
909 strcpy(tmp, "");
910 if(plot_type==0 || plot_type==2) {
911 int prev_px=-1, prev_py=-1;
912 for(i=0; i<data_nr; i++) {
913 if(isnan(x[i]) || isnan(y[i])) continue;
914 px=vp->x.origo+x[i]*vp->x.scale;
915 py=vp->coordinate_area_viewport.h-(vp->y.origo+vp->y.scale*y[i]);
916 /* Do not plot points outside viewport */
917 if(px<0 || py<0) continue;
918 if(px>vp->coordinate_area_viewport.w+1 || py>vp->coordinate_area_viewport.h+1) continue;
919 /* Do not plot 2nd time the same point */
920 if(px==prev_px && py==prev_py) continue;
921 prev_px=px; prev_py=py;
922 /* Draw the symbol */
923 sprintf(tmp, " <%suse ", ilc);
924 switch(symbol_type) {
925 case RECTANGLE:
926 sprintf(line, "xlink:href=\"#sym-rect\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
927 px-0.5*size, py-0.5*size, size, size);
928 break;
929 case UPTRIANGLE:
930 sprintf(line, "xlink:href=\"#sym-uptr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
931 px-0.5*trgsize, py-0.5*trgsize, trgsize, trgsize);
932 break;
933 case DOWNTRIANGLE:
934 sprintf(line, "xlink:href=\"#sym-dotr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
935 px-0.5*trgsize, py-0.5*trgsize, trgsize, trgsize);
936 break;
937 case DIAMOND:
938 sprintf(line, "xlink:href=\"#sym-diam\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
939 px-0.5*trgsize, py-0.5*trgsize, trgsize, trgsize);
940 break;
941 case LEFTTRIANGLE:
942 sprintf(line, "xlink:href=\"#sym-letr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
943 px-0.5*trgsize, py-0.5*trgsize, trgsize, trgsize);
944 break;
945 case RIGHTTRIANGLE:
946 sprintf(line, "xlink:href=\"#sym-ritr\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
947 px-0.5*trgsize, py-0.5*trgsize, trgsize, trgsize);
948 break;
949 case CIRCLE:
950 default:
951 sprintf(line, "xlink:href=\"#sym-circ\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"",
952 px-0.5*circsize, py-0.5*circsize, circsize, circsize);
953 break;
954 }
955 strcat(tmp, line);
956 strcat(tmp, " />\n");
957 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(18);}
958 }
959 }
960
961 /* Close the curve object group */
962 sprintf(tmp, " </%sg>\n", ilc);
963 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(19);}
964
965 return(0);
966}
char * svg_str_encode(const char *s)
Definition svg_legend.c:256
int check_intersection_with_viewport(const double x1, const double y1, const double x2, const double y2, struct svg_viewport_pos *cavp, double *nx1, double *ny1, double *nx2, double *ny2, int verbose)
Definition svg_plot.c:73

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_write_xticks()

int svg_write_xticks ( FILE * fp,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Create SVG plot x axis ticks.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_define_viewports, svg_write_axes, svg_write_yticks, svg_calculate_axes
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 555 of file svg_plot.c.

565 {
566 char tmp[1024], line[128], ilc[9];
567 int ti;
568 double pos, ypos, yheight;
569
570 if(verbose>0) {printf("%s(fp, vp, errmsg, %d)\n", __func__, verbose); fflush(stdout);}
571
572 /* Check the input */
573 if(vp->x.tick_nr<1 || vp->plot_area_viewport.h==vp->coordinate_area_viewport.h) return(0);
574 if(fp==NULL) {if(errmsg!=NULL) sprintf(errmsg, "file was closed too early"); return(1);}
575
576 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
577
580
581 strcpy(tmp, "\n <!-- X axis ticks inside plot area -->\n");
582#if(0)
583 sprintf(tmp, " <%srect x=\"0px\" y=\"%dpx\" width=\"%dpx\" height=\"%gpx\" stroke=\"none\" fill=\"aqua\" fill-opacity=\"0.3\" />\n",
584 ilc, vp->coordinate_area_viewport.h, vp->plot_area_viewport.w, yheight);
585#endif
586 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
587
588 /* Write ticks */
589 sprintf(tmp, " <%sg", ilc);
590 sprintf(line, " stroke=\"%s\"", "black"); strcat(tmp, line);
591 sprintf(line, " stroke-width=\"%g\"", 20.); strcat(tmp, line);
592 sprintf(line, " fill=\"%s\"", "none"); strcat(tmp, line);
593 strcat(tmp, ">\n");
594 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(5);}
595
596 if(verbose>9) {
597 printf("vp->plot_area_viewport.w := %d\n", vp->plot_area_viewport.w);
598 printf("vp->coordinate_area_viewport.w := %d\n", vp->coordinate_area_viewport.w);
599 }
600 for(ti=0; ti<vp->x.tick_nr; ti++) {
601 pos=vp->x.origo+vp->x.scale*vp->x.tick[ti];
603 if(verbose>1) printf("ti=%d: x tick pos=%g\n", ti, pos);
604 sprintf(tmp, " <%s", ilc);
605 sprintf(line, "line x1=\"%g\" x2=\"%g\" y1=\"%g\" y2=\"%g\" />\n",
606 pos, pos, ypos, ypos+yheight/8);
607 strcat(tmp, line);
608 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
609 }
610 // try to write one extra tick, if there is space
611 pos=vp->x.origo+vp->x.scale*(2.0*vp->x.tick[vp->x.tick_nr-1] - vp->x.tick[vp->x.tick_nr-2]);
613 if(pos<vp->plot_area_viewport.w) {
614 if(verbose>1) printf("extra ti=%d: x tick pos=%g\n", ti, pos);
615 sprintf(tmp, " <%s", ilc);
616 sprintf(line, "line x1=\"%g\" x2=\"%g\" y1=\"%g\" y2=\"%g\" />\n",
617 pos, pos, ypos, ypos+yheight/8);
618 strcat(tmp, line);
619 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
620 }
621 sprintf(tmp, " </%sg>\n", ilc);
622 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(7);}
623
624 /* Write ticks labels */
625 sprintf(tmp, " <%sg", ilc);
626 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
627 sprintf(line, " text-anchor=\"middle\""); strcat(tmp, line);
628 sprintf(line, " font-size=\"%d\"", vp->coordinate_area_viewport.chr_size); strcat(tmp, line);
629 sprintf(line, " fill=\"black\""); strcat(tmp, line);
630 strcat(tmp, ">\n");
631 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(8);}
632 for(ti=0; ti<vp->x.tick_nr; ti++) {
633 pos=vp->x.origo+vp->x.scale*vp->x.tick[ti];
635 if(verbose>1) printf("ti=%d: x tick pos=%g\n", ti, pos);
636 sprintf(tmp, " <%s", ilc);
637 sprintf(line, "text x=\"%g\" y=\"%g\"", pos, ypos+0.92*(double)yheight); strcat(tmp, line);
638 sprintf(line, ">"); strcat(tmp, line);
639 strcat(tmp, vp->x.tick_label[ti]);
640 strcat(tmp, "</"); strcat(tmp, ilc); strcat(tmp, "text>\n");
641 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
642 }
643 sprintf(tmp, " </%sg>\n", ilc);
644 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(10);}
645
646 return(0);
647}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_write_yticks()

int svg_write_yticks ( FILE * fp,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Create SVG plot y axis ticks.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_define_viewports, svg_write_axes, svg_write_xticks, svg_calculate_axes, axis_tick_positions
Parameters
fpSVG graphics file pointer.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 655 of file svg_plot.c.

665 {
666 char tmp[1024], line[128], ilc[9];
667 int ti;
668 double pos, xwidth;
669
670 if(verbose>0) {printf("%s(fp, vp, errmsg, %d)\n", __func__, verbose); fflush(stdout);}
671
672 /* Check the input */
673 if(vp->y.tick_nr<1 || vp->plot_area_viewport.w==vp->coordinate_area_viewport.w) return(0);
674 if(fp==NULL) {if(errmsg!=NULL) sprintf(errmsg, "file was closed too early"); return(1);}
675
676 if(SVG_INLINE) strcpy(ilc, "svg:"); else strcpy(ilc, "");
677
679
680 strcpy(tmp, "\n <!-- Y axis ticks inside plot area -->\n");
681#if(0)
682 sprintf(tmp,
683 " <%srect width=\"%gpx\" height=\"%dpx\" stroke=\"none\" fill=\"aqua\" fill-opacity=\"0.3\" />\n",
684 ilc, xwidth, vp->plot_area_viewport.h);
685#endif
686 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(99);}
687
688 if(verbose>0) {
689 printf("vp->y.tick_nr=%d\n", vp->y.tick_nr);
690 printf("vp->y.tickscale=%g\n", vp->y.tickscale);
691 printf("vp->y.tick_decimals=%d\n", vp->y.tick_decimals);
692 }
693
694 /* Write ticks */
695 sprintf(tmp, " <%sg", ilc);
696 sprintf(line, " stroke=\"%s\"", "black"); strcat(tmp, line);
697 sprintf(line, " stroke-width=\"%g\"", 20.); strcat(tmp, line);
698 sprintf(line, " fill=\"%s\"", "none"); strcat(tmp, line);
699 strcat(tmp, ">\n");
700 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(5);}
701 for(ti=0; ti<vp->y.tick_nr; ti++) {
702 pos=vp->coordinate_area_viewport.h-(vp->y.origo+vp->y.scale*vp->y.tick[ti]);
703 if(verbose>1) printf("ti=%d: y tick pos=%g\n", ti, pos);
704 sprintf(tmp, " <%s", ilc);
705 sprintf(line, "line x1=\"%g\" x2=\"%g\" y1=\"%g\" y2=\"%g\" />\n",
706 xwidth,
707 xwidth-(double)vp->coordinate_area_viewport.chr_size/8.0,
708 pos, pos); strcat(tmp, line);
709 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(6);}
710 }
711 sprintf(tmp, " </%sg>\n", ilc);
712 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(7);}
713
714 /* Write ticks labels */
715 sprintf(tmp, " <%sg", ilc);
716 sprintf(line, " font-family=\"Sans-serif\""); strcat(tmp, line);
717 sprintf(line, " text-anchor=\"end\""); strcat(tmp, line);
718 sprintf(line, " font-size=\"%d\"", vp->coordinate_area_viewport.chr_size); strcat(tmp, line);
719 sprintf(line, " fill=\"black\""); strcat(tmp, line);
720 strcat(tmp, ">\n");
721 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(8);}
722 for(ti=0; ti<vp->y.tick_nr; ti++) {
723 pos=vp->coordinate_area_viewport.h-(vp->y.origo+vp->y.scale*vp->y.tick[ti]);
724 if(verbose>1) printf("ti=%d: y tick pos=%g\n", ti, pos);
725 sprintf(tmp, " <%s", ilc);
726 sprintf(line, "text x=\"%g\" y=\"%g\"",
727 0.92*xwidth, pos+0.4*(double)vp->coordinate_area_viewport.chr_size);
728 strcat(tmp, line);
729 sprintf(line, ">"); strcat(tmp, line);
730 strcat(tmp, vp->y.tick_label[ti]);
731 strcat(tmp, "</"); strcat(tmp, ilc); strcat(tmp, "text>\n");
732 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(9);}
733 }
734 sprintf(tmp, " </%sg>\n", ilc);
735 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {return(10);}
736
737 return(0);
738}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_xhtml_close()

int svg_xhtml_close ( FILE * fp,
char * errmsg,
int verbose )
extern

Close XHTML file containing inline SVG.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_xhtml_initiate
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 206 of file svg_file.c.

214 {
215 if(verbose>0) printf("%s(fp, errmsg, %d)\n", __func__, verbose);
216
217 if(svg_write(fp, "</body>\n</html>\n", errmsg, verbose-5)!=0) {fclose(fp); return(1);}
218 fflush(fp); fclose(fp);
219
220 SVG_INLINE = 0;
221
222 return(0);
223}
int SVG_INLINE
Definition svg_file.c:12

◆ svg_xhtml_initiate()

FILE * svg_xhtml_initiate ( const char * filename,
const char * XHTML_title,
char * errmsg,
int verbose )
extern

Initiate a new XHTML file for one or more inline SVG graphics files.

If file with same name exists, it is overwritten without backup.

Returns
Returns pointer to the file if successful and NULL in case of an error.
See also
svg_initiate, svg_xhtml_close, svg_xhtml_svg_open
Parameters
filenameFile name for SVG graphics.
XHTML_titleXHTML title; if NULL, then file name is used.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 131 of file svg_file.c.

141 {
142 FILE *fp;
143 char tmp[2048], line[256];
144
145 if(verbose>0)
146 printf("%s(%s, %s, errmsg, %d)\n", __func__, filename, XHTML_title, verbose);
147
148 SVG_INLINE = 1;
149
150 /* Check input */
151 if(filename==NULL) return(NULL);
152 if(XHTML_title==NULL) XHTML_title=filename;
153
154 /* Open file for write */
155 fp=fopen(filename, "w");
156 if(fp==NULL) {
157 if(errmsg!=NULL) strcpy(errmsg, "cannot open file for write");
158 return(fp);
159 }
160
161 strcpy(tmp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
162 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
163 fclose(fp); remove(filename); return(NULL);}
164
165 strcpy(tmp, "<!DOCTYPE html PUBLIC\n");
166 strcat(tmp, " \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n");
167 strcat(tmp, " \"https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
168 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
169 fclose(fp); remove(filename); return(NULL);}
170
171 strcpy(tmp, "<html xmlns=\"https://www.w3.org/1999/xhtml\"\n");
172 strcat(tmp, " xmlns:svg=\"https://www.w3.org/2000/svg\"\n");
173 strcat(tmp, " xmlns:xlink=\"https://www.w3.org/1999/xlink\"\n");
174 strcat(tmp, " xmlns:ev=\"https://www.w3.org/2001/xml-events\"\n");
175 strcat(tmp, " xml:lang=\"en\" lang=\"en\">\n");
176 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
177 fclose(fp); remove(filename); return(NULL);}
178
179 strcpy(tmp, "<head>\n");
180 sprintf(line, " <title>%s</title>\n", XHTML_title); strcat(tmp, line);
181 sprintf(line, " <meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />\n"); strcat(tmp, line);
182 sprintf(line, " <meta http-equiv=\"content-language\" content=\"en-gb\" />\n"); strcat(tmp, line);
183 strcat(tmp, " <object id=\"AdobeSVG\" classid=\"clsid:78156a80-c6a1-4bbf-8e6a-3cd390eeb4e2\"> </object>\n");
184 strcat(tmp, " <?import namespace=\"svg\" urn=\"https://www.w3.org/2000/svg\" implementation=\"#AdobeSVG\"?>\n");
185 strcat(tmp, "</head>\n\n");
186 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
187 fclose(fp); remove(filename); return(NULL);}
188
189 strcpy(tmp, "<body>\n\n");
190 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
191 fclose(fp); remove(filename); return(NULL);}
192
193 /* Create plot symbols for possible later use */
194 if(svg_define_symbols(fp, errmsg, verbose)!=0) {
195 fclose(fp); remove(filename); return(NULL);}
196
197 return(fp);
198}

◆ svg_xhtml_svg_close()

int svg_xhtml_svg_close ( FILE * fp,
char * errmsg,
int verbose )
extern

Close SVG graphics inline in XHTML file. Leaves the file open.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_xhtml_svg_open, svg_write
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 283 of file svg_file.c.

291 {
292 if(verbose>0) printf("%s(fp, errmsg, %d)\n", __func__, verbose);
293
294 if(svg_write(fp, "</svg:svg>\n", errmsg, verbose-5)!=0) return(1);
295 return(0);
296}

◆ svg_xhtml_svg_open()

int svg_xhtml_svg_open ( FILE * fp,
const double height,
const double width,
struct svg_viewports * vp,
char * errmsg,
int verbose )
extern

Open a new SVG inline XHTML file.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_xhtml_svg_close, svg_xhtml_initiate
Parameters
fpSVG graphics file pointer.
heightPlot height in cm; 0, if not predefined.
widthPlot width in cm; 0, if not predefined.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 231 of file svg_file.c.

245 {
246 char tmp[1024];
247
248 if(verbose>0)
249 printf("%s(fp, %g, %g, vp, errmsg, %d)\n", __func__, height, width, verbose);
250
251 /* Check input */
252 if(fp==NULL || vp==NULL) return(1);
253 if(vp->main_viewport.w<3 || vp->main_viewport.h<3) return(1);
254
255 strcpy(tmp, "<svg:svg version=\"1.1\" baseProfile=\"full\"");
256 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(2);
257
258 sprintf(tmp, "\n viewBox=\"0 0 %d %d\"", vp->main_viewport.w, vp->main_viewport.h);
259 strcat(tmp, "\n preserveAspectRatio=\"xMinYMin meet\"");
260 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(3);
261
262 if(width>0.0) {
263 sprintf(tmp, "\n width=\"%gcm\"", width);
264 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(4);
265 }
266 if(height>0.0) {
267 sprintf(tmp, "\n height=\"%gcm\"", height);
268 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(5);
269 }
270
271 strcpy(tmp, ">\n");
272 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(6);
273
274 return(0);
275}

◆ svgColorName()

char * svgColorName ( const svgColor i)
extern

Return pointer to string describing the color, or NULL if outside of limits.

See also
svgSymbolName
Parameters
iindex of color

Definition at line 38 of file svg_defs.c.

41 {
42 unsigned int n=0;
43 while(svgcolor[n]!=0) n++;
44 if(/*i<0 ||*/ i>n-1) return(NULL); //return(svgcolor[BLACK]);
45 else return(svgcolor[i]);
46}

Referenced by plot_fit_svg(), plot_fitrange_svg(), plot_svg(), and svg_create_legends().

◆ svgSymbolName()

char * svgSymbolName ( const svgSymbolType i)
extern

Return pointer to string describing the symbol, or NULL if outside of limits.

See also
svgColorName, svg_define_symbols
Parameters
iindex of symbol

Definition at line 67 of file svg_defs.c.

70 {
71 unsigned int n=0;
72 while(svgsymbol[n]!=0) n++;
73 if(/*i<0 ||*/ i>n-1) return(NULL);
74 else return(svgsymbol[i]);
75}

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().