public code v1

This commit is contained in:
Francisco Jesús Martínez Mimbrera
2026-05-23 00:32:57 +02:00
commit 759a8968a2
4357 changed files with 163763 additions and 0 deletions
@@ -0,0 +1,179 @@
package flintstones.helper.html.svg;
import flintstones.helper.html.tags.Text;
public class Svg {
public static enum LineStroke{ Normal, Dotted, Dashed };
public static enum CircleStroke{ Normal, Small, Big, Hollow };
public static Tag getVerticalLine(int x, int ystart, int size) {
return getLine(x, ystart, x, ystart + size);
}
public static Tag getHorizontalLine(int xstart, int y, int size) {
return getLine(xstart, y, xstart+size, y);
}
public static Tag getLine(int x, int y, int nx, int ny) {
// <line y1="40" x1="60" y2="1000" x2="60" stroke-width="5" fill="none" stroke="#000" >
Tag line = (Tag) new Tag("line")
.attr("y1",y)
.attr("y2",ny)
.attr("x1",x)
.attr("x2",nx)
.attr("stroke-width", 2)
.attr("fill", "none")
.attr("stroke", "#000");
return line;
}
public static void addStroke(Tag tag, int textsize, LineStroke stroke) {
if(stroke.equals(LineStroke.Dashed))
tag.attr("stroke-dasharray",(textsize/4)*5);
if(stroke.equals(LineStroke.Dotted))
tag.attr("stroke-dasharray",(textsize/4)*1.5);
}
public static Tag getText(int x, int y, int size, String textcontent) {
// <text dx="-22.5" x="810" y="1040" font-size="30" >555</text>
// int chars = textcontent.length();
// double mchars = (double)chars/4;
double mheight = (size*0.6)/2; // 0.6 -> Ratio alto-ancho de fuentes habitual.
Tag text = (Tag) new Tag("text")
.attr("x", x)
.attr("y", y )
// .attr("dx",-(mchars*size) )
.attr("dy", mheight)
.attr("text-anchor","middle")
.attr("font-size", size);
text.add(new Text(textcontent));
return text;
}
public static Tag getLeftMarker(int x, int y, int size, String textcontent) {
Tag group = new Tag("g");
Tag line = getHorizontalLine(x-(size/2), y, size);
Tag text = getText(x - size - size/2, y ,size, textcontent);
group.add(line);
group.add(text);
return group;
}
public static Tag getBottomMarker(int x, int y, int size, String textcontent) {
// <g ><line y1="990" x1="810" y2="1005" x2="810" stroke-width="2" fill="none" stroke="#000" ></line><text dx="-22.5" x="810" y="1040" font-size="30" >555</text></g>
Tag group = new Tag("g");
Tag line = getVerticalLine(x, y-(size/2), size);
Tag text = getText(x, y + size ,size, textcontent);
group.add(line);
group.add(text);
return group;
}
public static Tag getStar(int x, int y, int size) {
//<polygon transform="translate(795,525) scale(0.14285714285714285,0.14285714285714285)" points="100,10 40,198 190,78 10,78 160,198" ></polygon>
String points = "100,10 40,198 190,78 10,78 160,198";
int originalSize = 210;
double prod = (double) size/originalSize;
int tx = x - size/2;
int ty = y - size/2;
String translate = "translate("+tx+","+ty+")";
String scale = "scale("+prod+","+prod+")";
Tag star = (Tag) new Tag("polygon")
.attr("transform", translate + " " + scale )
.attr("points", points);
return star;
}
public static Tag getDot(int x, int y, int size) {
return getDot(x,y,size,CircleStroke.Normal);
}
public static Tag getDot(int x, int y, int size, CircleStroke stroke) {
if(stroke.equals(CircleStroke.Small))
size *= 2;
if(stroke.equals(CircleStroke.Normal))
size *= 5;
if(stroke.equals(CircleStroke.Big))
size *= 10;
// <circle cx="46" cy="45" r="40"></circle>
Tag circle = (Tag) new Tag("circle")
.attr("cx", x )
.attr("cy", y )
.attr("r", size);
return circle;
}
public static Tag getEllipse(int x, int y, int rx, int ry, CircleStroke stroke) {
Tag circle = (Tag) new Tag("ellipse")
.attr("cx", x )
.attr("cy", y )
.attr("rx", rx)
.attr("ry", ry)
.attr("fill","none")
.attr("stroke","black")
.attr("stroke-width","2");
return circle;
}
public static Tag getRectangle(int x, int y, int x2, int y2) {
int width = x2-x;
int height = y2-y;
Tag rect = (Tag) new Tag("rect")
.attr("x",x)
.attr("y",y)
.attr("width",width)
.attr("height",height)
.attr("fill","none")
.attr("style","stroke:black; stroke-width: 2");
return rect;
}
public static Tag getBox(int x, int y, int x2, int y2) {
int width = x2-x;
int height = y2-y;
Tag rect = (Tag) new Tag("rect")
.attr("x",x)
.attr("y",y)
.attr("width",width)
.attr("height",height);
// .attr("style","stroke:black; stroke-width: 2");
return rect;
}
}
@@ -0,0 +1,44 @@
package flintstones.helper.html.svg;
import org.eclipse.swt.widgets.Composite;
import flintstones.helper.html.HtmlWidget;
import flintstones.helper.html.svg.chart.SvgChartCanvas;
public class SvgBrowser extends HtmlWidget {
SvgCanvas canvas;
public SvgBrowser(Composite base) {
super(base);
}
public SvgBrowser(Composite base, SvgChartCanvas canvas) {
super(base);
this.canvas = canvas;
}
public void setCanvas(SvgCanvas canvas){
this.canvas = canvas;
}
@Override
protected String getResult() {
System.out.println(canvas);
return canvas.toString();
}
@Override
protected String getStyle() {
return "html, body { margin:0; padding:0; overflow:hidden; background-color: white; } svg { position: absolute; top: 5%; left: 5%; width: 90%; height: 90%; } text:hover {\r\n" +
" paint-order: stroke;\r\n" +
" stroke: #000000;\r\n" +
" stroke-width: 1px;\r\n" +
" stroke-linecap: butt;\r\n" +
" stroke-linejoin: miter;\r\n" +
"}";
};
}
@@ -0,0 +1,5 @@
package flintstones.helper.html.svg;
public class SvgCanvas {
}
@@ -0,0 +1,11 @@
package flintstones.helper.html.svg;
import flintstones.helper.html.tags.HtmlTag;
public class Tag extends HtmlTag {
public Tag(String name) {
super(name);
}
}
@@ -0,0 +1,217 @@
package flintstones.helper.html.svg.chart;
import flintstones.helper.DoubleHelper;
import flintstones.helper.html.svg.Svg;
import flintstones.helper.html.svg.Tag;
import flintstones.helper.html.svg.Svg.CircleStroke;
import flintstones.helper.html.svg.components.LineSeries;
import flintstones.helper.html.svg.components.Point;
public class SvgChart extends SvgChartCanvas {
double maxX;
double maxY;
public SvgChart(int sizeX, int sizeY, double maxX, double maxY) {
super(sizeX,sizeY);
this.maxX = maxX;
this.maxY = maxY;
Tag vline = (Tag) Svg.getVerticalLine(ix, iy, canvasHeigth).attr("stroke-width", 5);
Tag hline = (Tag) Svg.getHorizontalLine(ix, tHeight, canvasWidth).attr("stroke-width", 5);
svg.add(vline);
svg.add(hline);
}
public void drawMarkers(boolean left, boolean bottom, double distance) {
if(bottom) {
for(double i = 0.0; i < maxX; i+= distance) {
Double xper = getXPer(i);
Tag x = getBottonMarker(xper, DoubleHelper.Draw(i));
svg.add(x);
}
}
if(left) {
for(double i = 0.0; i < maxY; i+= distance) {
Double yper = getYPer(i);
Tag x = getLeftMarker(yper, DoubleHelper.Draw(i));
svg.add(x);
}
}
}
public Tag drawHorizontalLine(double ypos, double xposStart, double xposEnd) {
return drawLine(xposStart, ypos, xposEnd, ypos);
}
public Tag drawHorizontalLine(double ypos) {
return drawHorizontalLine(ypos,0,maxX);
}
//
public Tag drawVerticalLine(double xpos) {
return drawVerticalLine(xpos,0,maxY);
}
public Tag drawVerticalLine(double xpos, double yposStart, double yposEnd) {
return drawLine(xpos, yposStart, xpos, yposEnd);
}
//
public Tag drawLine(double xposStart, double yposStart, double xposEnd, double yposEnd) {
Double xperStart = getXPer(xposStart);
Double xperEnd = getXPer(xposEnd);
Double yperStart = getYPer(yposStart);
Double yperEnd = getYPer(yposEnd);
Tag line = getLine(xperStart, yperStart, xperEnd, yperEnd);
svg.add(line);
return line;
}
//
public Tag drawText(double xpos, double ypos, double yperExtra, String text) {
Double xper = getXPer(xpos);
Double yper = getYPer(ypos);
Tag textE = getText(xper, yper + yperExtra, text);
svg.add(textE);
return textE;
}
//
public Tag drawBottonMarker(double xpos, String text) {
Double xper = getXPer(xpos);
Tag x = getBottonMarker(xper, text);
svg.add(x);
return x;
}
//
public Tag drawLeftMarker(double ypos, String text) {
Double yper = getYPer(ypos);
Tag x = getLeftMarker(yper, text);
svg.add(x);
return x;
}
public Tag drawStar(double xpos, double ypos) {
Double xper = getXPer(xpos);
Double yper = getYPer(ypos);
Tag x = getStar(xper, yper);
svg.add(x);
return x;
}
public Tag drawBox(double xposStart, double yposStart, double xposEnd, double yposEnd) {
Double xperStart = getXPer(xposStart);
Double xperEnd = getXPer(xposEnd);
Double yperStart = getYPer(yposStart);
Double yperEnd = getYPer(yposEnd);
Tag rect = getBox(xperStart, yperStart, xperEnd, yperEnd);
svg.add(rect);
return rect;
}
//
// public Tag drawHollowCircle(double xper, double yper, int size ) {
// Tag x = getHollowCircle(xper, yper, size);
// svg.add(x);
// return x;
// }
//
// public Tag drawDot(double xper, double yper) {
// return drawDot(xper, yper, ts/10, CircleStroke.Normal);
// }
//
public Tag drawPoint(Point point, CircleStroke stroke) {
Double xper = getXPer(point.getFirst());
Double yper = getYPer(point.getSecond());
Tag x = getDot(xper, yper, ts, stroke);
svg.add(x);
return x;
}
public Tag drawLineSeries(LineSeries series) {
Double x = null;
Double y = null;
String color = series.getColor();
boolean showLegend = series.showLegend();
Tag g = new Tag("g");
for(Point point : series.getPoints()) {
Double nx = getXPer(point.getFirst());
Double ny = getYPer(point.getSecond());
Tag dot = (Tag) getDot(nx, ny, ts/10, CircleStroke.Small)
.attr("stroke",color)
.attr("stroke-width",5)
.attr("fill", color);
g.add(dot);
if(x != null && y != null) {
Tag line = getLine(x, y, nx, ny);
g.add(line)
.attr("stroke",color)
.attr("stroke-width",5);
}
// Valus on the top of the point
String p1Name = DoubleHelper.Draw(point.getFirst());
String p2Name = DoubleHelper.Draw(point.getSecond());
if(showLegend) {
Tag pointName = getText(nx, ny+3, p1Name + " | " + p2Name );
g.add(pointName);
}
if(series.showMarkers()) {
Tag bottomMarker = getBottonMarker(nx, p1Name);
svg.add(bottomMarker);
}
if(series.showMarkers()) {
Tag leftMarker = getLeftMarker(ny, p2Name);
svg.add(leftMarker);
}
x = nx;
y = ny;
}
svg.add(g);
return g;
}
public double getXPer(double xval) {
return (xval / maxX)*100;
}
public double getYPer(double yval) {
return (yval / maxY)*100;
}
}
@@ -0,0 +1,169 @@
package flintstones.helper.html.svg.chart;
import flintstones.helper.html.svg.Svg;
import flintstones.helper.html.svg.SvgCanvas;
import flintstones.helper.html.svg.Tag;
import flintstones.helper.html.svg.Svg.CircleStroke;
public class SvgChartCanvas extends SvgCanvas {
// Data
protected int canvasHeigth;
protected int canvasWidth;
protected int ix;
protected int iy;
protected int ts;
protected Tag svg;
protected int tHeight;
protected int tWidth;
public SvgChartCanvas(int sizeX, int sizeY) {
// Fill vars
canvasHeigth = sizeY;
canvasWidth = sizeX;
ix = canvasWidth / 25;
iy = canvasHeigth / 25;
ts = canvasHeigth > canvasWidth ? canvasHeigth / 50 : canvasWidth / 50;
tHeight = canvasHeigth + iy;
tWidth = canvasWidth + ix;
svg = (Tag) new Tag("svg")
.attr("xmlns","http://www.w3.org/2000/svg")
.attr("viewbox", "0 0 " + tWidth + " " + tHeight)
.attr("width", "100%")
.attr("height", "100%");
}
protected Tag getHorizontalLine(double yper, double xperStart, double xperEnd) {
return getLine(xperStart, yper, xperEnd, yper);
}
protected Tag getHorizontalLine(double yper) {
return getHorizontalLine(yper,0,100);
}
protected Tag getVerticalLine(double xper) {
return getVerticalLine(xper,0,100);
}
protected Tag getVerticalLine(double xper, double yperStart, double yperEnd) {
return getLine(xper, yperStart, xper, yperEnd);
}
protected Tag getLine(double xperStart, double yperStart, double xperEnd, double yperEnd) {
int xmin = realocateX(xperStart);
int xmax = realocateX(xperEnd);
int ymin = realocateY(yperStart);
int yend = realocateY(yperEnd);
Tag line = Svg.getLine(xmin+ix, ymin+iy, xmax+ix, yend+iy);
return line;
}
protected Tag getText(double xper, double yper, String text) {
int x = realocateX(xper);
int y = realocateY(yper);
Tag textTag = Svg.getText(x+ix, y+iy, ts, text);
return textTag;
}
protected Tag getBottonMarker(double xper, String text) {
int x = realocateX(xper);
Tag marker = Svg.getBottomMarker(x+ix, canvasHeigth + iy, ts, text);
return marker;
}
protected Tag getLeftMarker(double yper, String text) {
int y = realocateY(yper);
Tag marker = Svg.getLeftMarker(ix, y+iy, ts, text);
return marker;
}
protected Tag getStar(double xper, double yper) {
int x = realocateX(xper);
int y = realocateY(yper);
Tag star = Svg.getStar(ix+x, y+iy,ts);
return star;
}
protected Tag getBox(double xperStart, double yperStart, double xperEnd, double yperEnd) {
int xmin = realocateX(xperStart);
int xmax = realocateX(xperEnd);
int ymin = realocateY(yperStart);
int yend = realocateY(yperEnd);
return Svg.getBox(xmin+ix, ymin+iy, xmax+ix, yend+iy);
}
protected Tag getHollowCircle(double xper, double yper, int size ) {
CircleStroke stroke = CircleStroke.Hollow;
int x = realocateX(xper);
int y = realocateY(yper);
int rx = realocateX(size);
int ry = realocateY(100-size);
Tag elip = Svg.getEllipse(ix+x, y+iy,rx,ry,stroke);
return elip;
}
protected Tag getDot(double xper, double yper) {
return getDot(xper, yper, ts/10, CircleStroke.Normal);
}
protected Tag getDot(double xper, double yper, int size, CircleStroke stroke) {
int x = realocateX(xper);
int y = realocateY(yper);
Tag circle = Svg.getDot(ix+x, y+iy,size,stroke);
return circle;
}
private int realocateX(double xper) {
if(xper < 0.0 || xper > 100.0)
throw new RuntimeException("XPer Between 0,100 :( -> " + xper);
double prod = (double)canvasWidth / 100.0;
return (int) (xper*prod);
}
private int realocateY(double yper) {
yper = 100.0 - yper;
if(yper < 0 || yper > 100.0)
throw new RuntimeException("YPer Between 0,100 :( -> " + yper);
double prod = (double)canvasHeigth / 100.0;
return (int) (yper*prod);
}
@Override
public String toString() {
return svg.toString();
}
}
@@ -0,0 +1,56 @@
package flintstones.helper.html.svg.components;
import java.util.ArrayList;
import java.util.List;
public class LineSeries {
List<Point> points = new ArrayList<>();
boolean showLegend = false;
boolean showMarkers = false;
String color = "#ffffff";
public LineSeries() {}
public LineSeries(double[][] pointsM) {
for(double[] arr : pointsM) {
Point p = new Point(arr[0], arr[1]);
points.add(p);
}
}
public void add(Point p) {
points.add(p);
}
public void add(double a, double b) {
Point p = new Point(a, b);
points.add(p);
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void showLegend(boolean x) {showLegend = x;}
public boolean showLegend() {return showLegend;}
public void showMarkers(boolean x) {showMarkers = x;}
public boolean showMarkers() {return showMarkers;}
public List<Point> getPoints(){
return points;
}
}
@@ -0,0 +1,16 @@
package flintstones.helper.html.svg.components;
public class Point {
double a;
double b;
public Point(double a, double b) {
this.a = a;
this.b = b;
}
public double getFirst() {return a;}
public double getSecond() {return b;}
}