Personalización
Personaliza la gráfica con plantillas tipadas de Angular. El texto también se usa en servidor, accesibilidad y exportación.
Prueba la personalización
Cambia los formatos y explora la gráfica para ver los cinco puntos de personalización en uso. El código debajo se actualiza con los ajustes aplicados.
Configura un gráfico real. El código inferior muestra los datos y opciones de entrada, incluidas las reglas responsive.
Media actual: 4081,82 EUR
Ingresos por periodo · personalizado
| Serie | Categoría X | id | x | y | Visible |
|---|---|---|---|---|---|
| Serie 1 | Periodo 1 | series-0-0 | period-0 | 4200 | Sí |
| Serie 1 | Periodo 2 | series-0-1 | period-1 | 6100 | Sí |
| Serie 1 | Periodo 3 | series-0-2 | period-2 | 5300 | Sí |
| Serie 1 | Periodo 4 | series-0-3 | period-3 | Medición ausente | Sí |
| Serie 1 | Periodo 5 | series-0-4 | period-4 | 7200 | Sí |
| Serie 1 | Periodo 6 | series-0-5 | period-5 | 6800 | Sí |
| Serie 2 | Periodo 1 | series-1-0 | period-0 | 3300 | Sí |
| Serie 2 | Periodo 2 | series-1-1 | period-1 | 4800 | Sí |
| Serie 2 | Periodo 3 | series-1-2 | period-2 | 0 | Sí |
| Serie 2 | Periodo 4 | series-1-3 | period-3 | -5100 | Sí |
| Serie 2 | Periodo 5 | series-1-4 | period-4 | 6400 | Sí |
| Serie 2 | Periodo 6 | series-1-5 | period-5 | 5900 | Sí |
La media incluye cada valor válido de las series elegidas, también cero; omite los ausentes. En modo visible se recalcula al ocultar series. Se actualiza con los datos y no cambia con el zoom. Un objetivo fuera del dominio no se dibuja. Cambiar moneda o unidad formatea el valor: no hace conversiones.
const data = {
"type": "column",
"axes": {
"x": {
"type": "category",
"values": [
{
"id": "period-0",
"label": "Periodo 1"
},
{
"id": "period-1",
"label": "Periodo 2"
},
{
"id": "period-2",
"label": "Periodo 3"
},
{
"id": "period-3",
"label": "Periodo 4"
},
{
"id": "period-4",
"label": "Periodo 5"
},
{
"id": "period-5",
"label": "Periodo 6"
}
]
},
"y": {
"type": "linear"
}
},
"series": [
{
"id": "series-0",
"name": "Serie 1",
"data": [
{
"id": "series-0-0",
"x": "period-0",
"y": 4200
},
{
"id": "series-0-1",
"x": "period-1",
"y": 6100
},
{
"id": "series-0-2",
"x": "period-2",
"y": 5300
},
{
"id": "series-0-3",
"x": "period-3",
"y": null
},
{
"id": "series-0-4",
"x": "period-4",
"y": 7200
},
{
"id": "series-0-5",
"x": "period-5",
"y": 6800
}
]
},
{
"id": "series-1",
"name": "Serie 2",
"data": [
{
"id": "series-1-0",
"x": "period-0",
"y": 3300
},
{
"id": "series-1-1",
"x": "period-1",
"y": 4800
},
{
"id": "series-1-2",
"x": "period-2",
"y": 0
},
{
"id": "series-1-3",
"x": "period-3",
"y": -5100
},
{
"id": "series-1-4",
"x": "period-4",
"y": 6400
},
{
"id": "series-1-5",
"x": "period-5",
"y": 5900
}
]
}
]
};
const options = {
"title": "Ingresos por periodo",
"locale": "es",
"theme": "light",
"height": 380,
"animation": {
"enabled": false
},
"formatting": {
"locale": "es-ES",
"number": {
"minimumFractionDigits": 2,
"maximumFractionDigits": 2,
"style": "currency",
"currency": "EUR",
"currencyDisplay": "code",
"currencySign": "standard"
}
},
"appearance": {
"colors": [
"#2c6ed5",
"#698e37"
],
"markers": true,
"dataLabels": true,
"labelLimit": 8
},
"hiddenSeriesIds": [],
"legend": {
"position": "bottom"
},
"tooltip": {
"crosshair": true,
"content": "shared"
},
"references": [
{
"id": "comparison",
"axis": "y",
"value": 4081.818181818182
}
],
"referenceLabels": [
{
"id": "comparison",
"text": "Media: 4081,82 EUR"
}
],
"referenceStyles": [
{
"id": "comparison",
"color": "#875310",
"dash": "dashed",
"width": 1.5
}
],
"responsive": [
{
"maxWidth": 430,
"options": {
"height": 320,
"tooltip": {
"placement": "docked"
},
"appearance": {
"dataLabels": false
},
"axes": {
"y": {
"numberFormat": {
"minimumFractionDigits": 0,
"maximumFractionDigits": 0,
"style": "currency",
"currency": "EUR",
"currencyDisplay": "narrowSymbol",
"currencySign": "standard",
"notation": "compact"
}
}
}
}
}
]
};Copia el archivo completo para reproducir los datos, opciones y personalización que ves ahora. Elige Líneas en esta página para probar un eje de fechas UTC.
import {Component} from '@angular/core';
import {NeuralCartesianChart, NeuralChartTitleTemplate, NeuralChartTooltipTemplate, NeuralChartLegendTemplate, NeuralChartAxisLabelTemplate, NeuralChartDataLabelTemplate, type CartesianChartData} from '@neural-charts/angular';
import {ChartContentSlotName, type ChartContentPorts, type ChartOptions} from '@neural-charts/angular';
export function createContent(options: ChartOptions, timeAxis: boolean, browserTemplates = false): ChartContentPorts {
const region = options.formatting?.locale ?? options.locale ?? 'en';
const numberOptions = options.formatting?.number;
const number = new Intl.NumberFormat(region, numberOptions);
const compact = new Intl.NumberFormat(region, {
...numberOptions,
notation: 'compact',
minimumFractionDigits: 0,
maximumFractionDigits: 1,
});
const date = new Intl.DateTimeFormat(region, {day: 'numeric', month: 'short', timeZone: 'UTC'});
const text = {
[ChartContentSlotName.Title]: {text: context => context.defaultText + (context.locale === 'es' ? ' · personalizado' : ' · custom')},
[ChartContentSlotName.Tooltip]: {text: context => {
const value = context.observation.xValue;
const heading = timeAxis && value !== undefined ? date.format(value) : context.observation.label;
const title = context.locale === 'es' ? 'Detalle personalizado' : 'Custom detail';
const missing = context.locale === 'es' ? 'Medición ausente' : 'Missing measurement';
const rows = context.rows?.map(row => row.name + ': ' + (row.point ? number.format(row.point.value) : missing))
?? context.points.map(point => point.seriesName + ': ' + number.format(point.value));
return [title + ' · ' + heading, ...rows].join('\n');
}},
[ChartContentSlotName.Legend]: {text: context => context.name + (context.visible ? '' : context.locale === 'es' ? ' · oculta' : ' · hidden')},
[ChartContentSlotName.AxisLabel]: {text: context => {
const {tick, axis, defaultText} = context;
if (typeof tick.value !== 'number') return defaultText;
if (axis === 'x' && tick.type === 'time') return date.format(tick.value);
if (axis === 'y' && tick.type === 'number') return compact.format(tick.value);
return defaultText;
}},
[ChartContentSlotName.DataLabel]: {text: context => {
const value = context.observation?.value;
if (value === undefined) return context.defaultText;
const label = compact.format(value);
return options.appearance?.points ? (context.index + 1) + ' · ' + label : label;
}},
} satisfies ChartContentPorts;
if (!browserTemplates) return text;
const html = (tag: 'strong' | 'em', attribute: string) => (host: HTMLElement, context: {readonly text: string}) => {
const node = host.ownerDocument.createElement(tag);
node.setAttribute(attribute, '');
node.textContent = context.text;
host.append(node);
return () => node.remove();
};
const svg = (attribute: string) => (host: SVGTextElement, context: {readonly text: string}) => {
const node = host.ownerDocument.createElementNS('http://www.w3.org/2000/svg', 'tspan');
node.setAttribute(attribute, '');
node.setAttribute('font-weight', '600');
node.textContent = context.text;
host.append(node);
return () => node.remove();
};
return {
[ChartContentSlotName.Title]: {...text[ChartContentSlotName.Title], render: html('strong', 'data-custom-title')},
[ChartContentSlotName.Tooltip]: {...text[ChartContentSlotName.Tooltip], render: html('strong', 'data-custom-tooltip')},
[ChartContentSlotName.Legend]: {...text[ChartContentSlotName.Legend], render: html('em', 'data-custom-legend')},
[ChartContentSlotName.AxisLabel]: {...text[ChartContentSlotName.AxisLabel], render: svg('data-custom-axis')},
[ChartContentSlotName.DataLabel]: {...text[ChartContentSlotName.DataLabel], render: svg('data-custom-label')},
};
}
const data: CartesianChartData = {
"type": "column",
"axes": {
"x": {
"type": "category",
"values": [
{
"id": "period-0",
"label": "Periodo 1"
},
{
"id": "period-1",
"label": "Periodo 2"
},
{
"id": "period-2",
"label": "Periodo 3"
},
{
"id": "period-3",
"label": "Periodo 4"
},
{
"id": "period-4",
"label": "Periodo 5"
},
{
"id": "period-5",
"label": "Periodo 6"
}
]
},
"y": {
"type": "linear"
}
},
"series": [
{
"id": "series-0",
"name": "Serie 1",
"data": [
{
"id": "series-0-0",
"x": "period-0",
"y": 4200
},
{
"id": "series-0-1",
"x": "period-1",
"y": 6100
},
{
"id": "series-0-2",
"x": "period-2",
"y": 5300
},
{
"id": "series-0-3",
"x": "period-3",
"y": null
},
{
"id": "series-0-4",
"x": "period-4",
"y": 7200
},
{
"id": "series-0-5",
"x": "period-5",
"y": 6800
}
]
},
{
"id": "series-1",
"name": "Serie 2",
"data": [
{
"id": "series-1-0",
"x": "period-0",
"y": 3300
},
{
"id": "series-1-1",
"x": "period-1",
"y": 4800
},
{
"id": "series-1-2",
"x": "period-2",
"y": 0
},
{
"id": "series-1-3",
"x": "period-3",
"y": -5100
},
{
"id": "series-1-4",
"x": "period-4",
"y": 6400
},
{
"id": "series-1-5",
"x": "period-5",
"y": 5900
}
]
}
]
};
const options: ChartOptions = {
"title": "Ingresos por periodo",
"locale": "es",
"theme": "light",
"height": 380,
"animation": {
"enabled": false
},
"formatting": {
"locale": "es-ES",
"number": {
"minimumFractionDigits": 2,
"maximumFractionDigits": 2,
"style": "currency",
"currency": "EUR",
"currencyDisplay": "code",
"currencySign": "standard"
}
},
"appearance": {
"colors": [
"#2c6ed5",
"#698e37"
],
"markers": true,
"dataLabels": true,
"labelLimit": 8
},
"hiddenSeriesIds": [],
"legend": {
"position": "bottom"
},
"tooltip": {
"crosshair": true,
"content": "shared"
},
"references": [
{
"id": "comparison",
"axis": "y",
"value": 4081.818181818182
}
],
"referenceLabels": [
{
"id": "comparison",
"text": "Media: 4081,82 EUR"
}
],
"referenceStyles": [
{
"id": "comparison",
"color": "#875310",
"dash": "dashed",
"width": 1.5
}
],
"responsive": [
{
"maxWidth": 430,
"options": {
"height": 320,
"tooltip": {
"placement": "docked"
},
"appearance": {
"dataLabels": false
},
"axes": {
"y": {
"numberFormat": {
"minimumFractionDigits": 0,
"maximumFractionDigits": 0,
"style": "currency",
"currency": "EUR",
"currencyDisplay": "narrowSymbol",
"currencySign": "standard",
"notation": "compact"
}
}
}
}
}
]
};
@Component({
selector: 'app-custom-chart',
standalone: true,
imports: [NeuralCartesianChart, NeuralChartTitleTemplate, NeuralChartTooltipTemplate, NeuralChartLegendTemplate, NeuralChartAxisLabelTemplate, NeuralChartDataLabelTemplate],
template: `
<nc-cartesian-chart [data]="data" [options]="options" [content]="content" instanceId="custom">
<ng-template ncChartTitle let-title><strong>{{title.text}}</strong></ng-template>
<ng-template ncChartTooltip let-tip><strong>{{tip.text}}</strong></ng-template>
<ng-template ncChartLegend let-series><em>{{series.text}}</em></ng-template>
<ng-template ncChartAxisLabel let-label><svg:tspan font-weight="600">{{label.text}}</svg:tspan></ng-template>
<ng-template ncChartDataLabel let-label><svg:tspan font-weight="600">{{label.text}}</svg:tspan></ng-template>
</nc-cartesian-chart>
`,
})
export class CustomChartComponent {
readonly data = data;
readonly options = options;
readonly content = createContent(options, false);
}Título
Añade jerarquía visual al título. Usa el texto portable para conservar el mismo nombre accesible y título al exportar.
Tooltip
Presenta la observación activa. El contexto contiene sus datos y puede incluir series de la misma posición cuando el tooltip es compartido.
Leyenda
Personaliza el nombre visible de la serie. El gráfico conserva los botones, el estado de visibilidad y su navegación por teclado.
Etiquetas de los ejes
El contexto distingue el eje X, el eje Y y el secundario, y aporta el valor original de la marca. Utiliza un elemento tspan para la presentación SVG.
Etiquetas de datos
Presenta el valor junto a su marca. Activa appearance.dataLabels en una gráfica compatible. El texto de partida ya incorpora el formato aplicado.