# Radar & Gauge Charts ## Required imports ### ES modules ```ts import * as am5 from "@amcharts/amcharts5"; import * as am5xy from "@amcharts/amcharts5/xy"; import * as am5radar from "@amcharts/amcharts5/radar"; import am5themes_Animated from "@amcharts/amcharts5/themes/Animated"; ``` ### CDN ```html ``` ## Radar chart setup ```js const root = am5.Root.new("chartdiv"); root.setThemes([am5themes_Animated.new(root)]); const chart = root.container.children.push( am5radar.RadarChart.new(root, { panX: false, panY: false, wheelX: "panX", wheelY: "zoomX", radius: am5.percent(80), innerRadius: am5.percent(20), // hole in center (0 = solid) startAngle: -90, // default: -90 (top) endAngle: 270 // default: 270 (full circle) }) ); ``` ### RadarChart key settings | Setting | Type | Description | |---------|------|-------------| | `radius` | percent/number | Outer radius (default: `am5.percent(80)`) | | `innerRadius` | percent/number | Inner radius / hole size (default: 0) | | `startAngle` | number | Start angle in degrees (default: -90) | | `endAngle` | number | End angle in degrees (default: 270) | ## Axes — use circular + radial renderers ```js // Circular axis (X — goes around the perimeter) const xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, { categoryField: "category", renderer: am5radar.AxisRendererCircular.new(root, { minGridDistance: 30 }), tooltip: am5.Tooltip.new(root, {}) })); xAxis.data.setAll(data); // CategoryAxis needs data // Radial axis (Y — goes from center outward) const yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, { renderer: am5radar.AxisRendererRadial.new(root, {}), min: 0 })); ``` ### Circular renderer label options ```js am5radar.AxisRendererCircular.new(root, { minGridDistance: 30 }); // Label text type: xAxis.get("renderer").labels.template.setAll({ textType: "circular", // "circular" | "radial" | "adjusted" fontSize: 12 }); ``` ## Series types | Class | Use for | |-------|---------| | `am5radar.RadarLineSeries` | Lines on radar chart (spider chart) | | `am5radar.RadarColumnSeries` | Columns on radar chart | | `am5radar.SmoothedRadarLineSeries` | Smoothed/curved radar lines | ```js // Radar line series (spider chart) const series = chart.series.push(am5radar.RadarLineSeries.new(root, { name: "Performance", xAxis: xAxis, yAxis: yAxis, valueYField: "value", categoryXField: "category", tooltip: am5.Tooltip.new(root, { labelText: "{categoryX}: {valueY}" }), connectEnds: true // connect first and last point (default: true) })); // Make it an area chart series.fills.template.setAll({ visible: true, fillOpacity: 0.3 }); series.strokes.template.setAll({ strokeWidth: 2 }); // Radar column series const columnSeries = chart.series.push(am5radar.RadarColumnSeries.new(root, { name: "Values", xAxis: xAxis, yAxis: yAxis, valueYField: "value", categoryXField: "category", stacked: false })); columnSeries.columns.template.setAll({ strokeOpacity: 0, fillOpacity: 0.8, width: am5.percent(80) }); ``` ## Gauge chart Gauges are built with RadarChart + a single axis + axis ranges for colored bands. ```js const chart = root.container.children.push(am5radar.RadarChart.new(root, { panX: false, panY: false, startAngle: -180, endAngle: 0, // semi-circle innerRadius: -25, // fixed-width band (negative = from outer edge) radius: am5.percent(95) })); // Single value axis (the gauge scale) const xAxis = chart.xAxes.push(am5xy.ValueAxis.new(root, { min: 0, max: 100, strictMinMax: true, renderer: am5radar.AxisRendererCircular.new(root, { strokeOpacity: 0.1, minGridDistance: 30 }) })); // Enable ticks xAxis.get("renderer").ticks.template.setAll({ visible: true, strokeOpacity: 0.5 }); // Disable grid xAxis.get("renderer").grid.template.setAll({ visible: false }); // Colored bands via axis ranges function createRange(start, end, color, label) { var rangeDataItem = xAxis.makeDataItem({ value: start, endValue: end }); xAxis.createAxisRange(rangeDataItem); rangeDataItem.get("axisFill").setAll({ visible: true, fill: color, fillOpacity: 0.8 }); rangeDataItem.get("label").setAll({ text: label, inside: true, radius: 8, fontSize: "0.9em", fill: am5.color(0xffffff) }); } createRange(0, 33, am5.color(0x4caf50), "Good"); createRange(33, 66, am5.color(0xffc107), "Warning"); createRange(66, 100, am5.color(0xf44336), "Danger"); // Needle (clock hand) — uses an axis data item var handDataItem = xAxis.makeDataItem({ value: 42 }); xAxis.createAxisRange(handDataItem); var hand = handDataItem.set("bullet", am5xy.AxisBullet.new(root, { sprite: am5radar.ClockHand.new(root, { radius: am5.percent(99), innerRadius: am5.percent(40), pinRadius: 10 }) })); // Animate needle to a new value // handDataItem.animate({ key: "value", to: 75, duration: 1000, easing: am5.ease.out(am5.ease.cubic) }); chart.appear(1000, 100); ``` **Tip: Avoiding needle/label overlap** — When adding a value label in the gauge center, the clock hand pin and inner portion will overlap the label. Hide the pin and increase `innerRadius` to leave space: ```js am5radar.ClockHand.new(root, { radius: am5.percent(99), innerRadius: am5.percent(55), // keep needle away from center pinRadius: 0 // hide the center pin circle }) ``` ## Cursor ```js chart.set("cursor", am5radar.RadarCursor.new(root, { behavior: "zoomX" // "none" | "zoomX" | "zoomY" | "zoomXY" })); ``` ## Legend ```js const legend = chart.children.push(am5.Legend.new(root, { centerX: am5.percent(50), x: am5.percent(50) })); legend.data.setAll(chart.series.values); ``` ## Partial-circle radar chart ```js // Half radar (180 degrees) var chart = root.container.children.push(am5radar.RadarChart.new(root, { startAngle: -90, endAngle: 90 })); // Three-quarter radar (270 degrees) var chart = root.container.children.push(am5radar.RadarChart.new(root, { startAngle: -135, endAngle: 135 })); ``` ## Dynamic gauge value update ```js // After creating the gauge hand and data item: // Update value from external event (button, API, timer, etc.) function setGaugeValue(newValue) { handDataItem.animate({ key: "value", to: newValue, duration: 800, easing: am5.ease.out(am5.ease.cubic) }); } // Example: update every 2 seconds setInterval(function() { setGaugeValue(Math.random() * 100); }, 2000); ``` ## Axis label formatting for gauges ```js // Append "%" to gauge axis labels xAxis.get("renderer").labels.template.adapters.add("text", function(text) { return text + "%"; }); // Custom number format xAxis.set("numberFormat", "#.#'%'"); ``` ## Events on radar elements ```js // Click on radar columns columnSeries.columns.template.events.on("click", function(ev) { var di = ev.target.dataItem; console.log("Category:", di.get("categoryX"), "Value:", di.get("valueY")); }); // Pointer over/out on radar line data points (via bullets) series.bullets.push(function() { var circle = am5.Circle.new(root, { radius: 5, fill: series.get("fill"), cursorOverStyle: "pointer" }); circle.events.on("click", function(ev) { console.log("Clicked data point:", ev.target.dataItem.get("valueY")); }); return am5.Bullet.new(root, { sprite: circle }); }); // Gauge needle animation complete handDataItem.animate({ key: "value", to: 75, duration: 1000, easing: am5.ease.out(am5.ease.cubic) }).events.on("stopped", function() { console.log("Needle animation complete"); }); ``` ## Disposal ```js root.dispose(); ``` --- # amCharts 5 Radar & Gauge Charts — Working Examples ## Example 1: Spider chart (radar line chart) ```html