An Angular component appears in the page as an element of its own, named after its selector: a component with the selector app-bar is an <app-bar> element. In HTML that's fine. The browser doesn't know the element, so it treats it much like a <span>, and whatever the component puts inside it is shown.
SVG is much stricter. It only draws the elements it knows, and an element it doesn't know isn't drawn, along with everything inside it. So a component used as a tag inside an <svg> drawing disappears, and you can't build a drawing out of components the way you build a page.
<g> to the rescue
SVG has one element that draws nothing of its own: <g>, the group. It draws whatever is inside it, and it can move, rotate or scale all of it with a transform.
An Angular selector doesn't have to be a tag name. It can be any CSS selector, including an element with an attribute. A component with the selector g[svg-translate] attaches itself to <g svg-translate>, so the component's element is a real SVG group, and the browser draws it and everything in it. That gives us components that are themselves part of the drawing.
Translate
The simplest is a group that moves everything inside it. It has no drawing of its own, just <ng-content> for its children, and it sets the group's transform attribute from its inputs:
@Component({
selector: 'g[svg-translate]',
template: '<ng-content></ng-content>'
})
export class SvgTranslateComponent {
@Input() x = 0;
@Input() y = 0;
@HostBinding('attr.transform')
get transform(): string {
return `translate(${this.x} ${this.y})`;
}
}
<g svg-translate [x]="100" [y]="50">
...
</g>
Rotate
Rotating works the same way, about the point (cx, cy):
@Component({
selector: 'g[svg-rotate]',
template: '<ng-content></ng-content>'
})
export class SvgRotateComponent {
@Input() angle = 0;
@Input() cx = 0;
@Input() cy = 0;
@HostBinding('attr.transform')
get transform(): string {
return `rotate(${this.angle} ${this.cx} ${this.cy})`;
}
}
Rectangle
The shapes draw an SVG element inside their group. Two things are different from an HTML template. A component's template is compiled on its own, without knowing it will end up inside an <svg>, so SVG elements in it need the svg: prefix to be made as SVG elements. And SVG attributes are bound with attr., because the properties of an SVG element, like a rectangle's x, aren't plain numbers that can be set:
@Component({
selector: 'g[svg-rect]',
template: `
<svg:rect
[attr.x]="x"
[attr.y]="y"
[attr.width]="width"
[attr.height]="height"
[attr.fill]="fill">
</svg:rect>
`
})
export class SvgRectComponent {
@Input() x = 0;
@Input() y = 0;
@Input() width = 0;
@Input() height = 0;
@Input() fill = '#0b1a1f';
}
Line
@Component({
selector: 'g[svg-line]',
template: `
<svg:line
[attr.x1]="x1"
[attr.y1]="y1"
[attr.x2]="x2"
[attr.y2]="y2"
[attr.stroke]="stroke"
[attr.stroke-width]="strokeWidth">
</svg:line>
`
})
export class SvgLineComponent {
@Input() x1 = 0;
@Input() y1 = 0;
@Input() x2 = 0;
@Input() y2 = 0;
@Input() stroke = '#0b1a1f';
@Input() strokeWidth = 1;
}
Text
The text component puts its own children inside the <text> element, so the words go between the tags where you use it:
@Component({
selector: 'g[svg-text]',
template: `
<svg:text
[attr.x]="x"
[attr.y]="y"
[attr.text-anchor]="anchor"
[attr.font-size]="size"
[attr.fill]="fill"><ng-content></ng-content></svg:text>
`
})
export class SvgTextComponent {
@Input() x = 0;
@Input() y = 0;
@Input() anchor = 'start';
@Input() size = 12;
@Input() fill = '#0b1a1f';
}
<g svg-text [x]="20" [y]="40" anchor="middle">Hello</g>
A bar chart
With those five, a chart is a component like any other. This one draws six months of sales as bars, with lines and labels for the scale. Its template is all groups: a translate to leave room for the labels, a translate for each line of the scale and for each bar, and a rotate to stand the axis title on its end:
@Component({
selector: 'g[sales-chart]',
templateUrl: './sales-chart.component.html'
})
export class SalesChartComponent {
@Input() months: string[] = [];
@Input() sales: number[] = [];
@Input() width = 600;
@Input() height = 300;
@Input() ticks = [0, 20, 40, 60, 80];
get plotWidth(): number {
return this.width - 80;
}
get plotHeight(): number {
return this.height - 60;
}
get slot(): number {
return this.plotWidth / this.months.length;
}
yFor(value: number): number {
const max = this.ticks[this.ticks.length - 1];
return this.plotHeight - value / max * this.plotHeight;
}
}
<svg:g svg-translate [x]="60" [y]="20">
<svg:g *ngFor="let tick of ticks" svg-translate [x]="0" [y]="yFor(tick)">
<svg:g svg-line [x2]="plotWidth" stroke="#c9d6da"></svg:g>
<svg:g svg-text [x]="-10" [y]="4" anchor="end" fill="#64777e">{{ tick }}</svg:g>
</svg:g>
<svg:g *ngFor="let value of sales; let i = index" svg-translate [x]="i * slot" [y]="0">
<svg:g svg-rect
[x]="slot * 0.2"
[y]="yFor(value)"
[width]="slot * 0.6"
[height]="plotHeight - yFor(value)"
fill="#0e7490"></svg:g>
<svg:g svg-text [x]="slot / 2" [y]="plotHeight + 20" anchor="middle">{{ months[i] }}</svg:g>
</svg:g>
<svg:g svg-line [y1]="plotHeight" [x2]="plotWidth" [y2]="plotHeight"></svg:g>
<svg:g svg-translate [x]="-44" [y]="plotHeight / 2">
<svg:g svg-rotate [angle]="-90">
<svg:g svg-text anchor="middle">Sales (£k)</svg:g>
</svg:g>
</svg:g>
</svg:g>
Plain attributes like fill="#0e7490" set an input to a fixed string, and the inputs that aren't given keep their defaults, so a scale line only needs its length. The chart itself goes inside an <svg> in the page, on a group like the rest:
<svg width="600" height="300">
<g sales-chart [months]="months" [sales]="sales"></g>
</svg>
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
sales = [42, 38, 51, 47, 60, 66];
And this is what it draws. Every group in the template is still in the drawing, each with its own transform:
Like any components, they're declared in a module, and from then on they can be used in any SVG in the application:
@NgModule({
imports: [CommonModule],
declarations: [
SvgTranslateComponent,
SvgRotateComponent,
SvgRectComponent,
SvgLineComponent,
SvgTextComponent,
SalesChartComponent
],
exports: [
SvgTranslateComponent,
SvgRotateComponent,
SvgRectComponent,
SvgLineComponent,
SvgTextComponent,
SalesChartComponent
]
})
export class SvgModule {}
See also
- DOM as data - components that are both what's shown and the data, in a CSV download
- Declarative charts in Angular - building charts out of components instead of data objects
- Angular best practices for large teams - the user interface library these components were part of