Skip to content Skip to sidebar Skip to footer

Jquery+svg: How To Position Text On Top Of Circle?

I draw a circle using svg jquery _svg.circle(_X, _Y, _R, { fill: 'yellow', stroke: 'black', strokeWidth: 1 }); On top of it I want to put a text: var _svgGroup = _svg.group({ font

Solution 1:

When i tried with the same kind of code, it works fine.

My code:

$(function() {
    $('#svgbasics').svg({onLoad: drawInitial});
    functiondrawInitial(svg) {
        var _X = 200;
        var _Y = 200;
        var _Z = 100;
        svg.circle(_X, _Y, _Z, { fill: 'yellow', stroke: 'black', strokeWidth: 1 });
        var _svgGroup = svg.group({ fontSize: '11', fill: 'black' });
        svg.text(_X, _Y + 15, "Hello World");
    }
});

Also, before the code we may need to include the piece of css.

<styletype="text/css">@import"jquery.svg.css";
#svgbasics { width: 400px; height: 300px; border: 1px solid #484; }
</style>

I got this from the sample files (svgBasic.html) present inside the jquery.svg library.

Solution 2:

That code works:

var _svgTextStyle = { fontSize: '11', fill: 'red' };
var _svgCircleStyle = { fill: 'yellow', stroke: 'black', strokeWidth: 1 };
_svg.circle(pos.getPosX() + 7, pos.getPosY() + 0, 7, _svgCircleStyle);
_svg.text(pos.getPosX() + 0, pos.getPosY() + 10, pos.getId() + "", _svgTextStyle);

So the difference is that style should be passed not as 'group' object in a 1st parameter, but as a pure array in 4th.

Dunno actually, why "group" doesn't work as I see reference that recommended it.

Post a Comment for "Jquery+svg: How To Position Text On Top Of Circle?"