import { Component, AfterViewInit, ElementRef } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'app-horizontal-group-bar-chart',
templateUrl: './horizontal-group-bar-chart.component.html',
styleUrls: ['./horizontal-group-bar-chart.component.css']
})
export class HorizontalGroupBarChartComponent implements AfterViewInit {
// Sample dataset for the chart
data = [
{ group: "A", series1: 10, series2: 15, series3: 7 },
{ group: "B", series1: 8, series2: 12, series3: 20 },
{ group: "C", series1: 12, series2: 18, series3: 5 },
{ group: "D", series1: 15, series2: 10, series3: 12 }
];
// Define margins and dimensions for the chart
margin = { top: 20, right: 20, bottom: 30, left: 60 };
width: number;
height: number;
// Reference to the SVG element
private svg: any;
constructor(private elementRef: ElementRef) {
// Calculate the width and height of the chart area
this.width = 600 - this.margin.left - this.margin.right;
this.height = 400 - this.margin.top - this.margin.bottom;
}
ngAfterViewInit(): void {
// Select the SVG element and add a group (g) to contain the chart
this.svg = d3.select(this.elementRef.nativeElement).select('.chart')
.append('g')
.attr('transform', `translate(${this.margin.left}, ${this.margin.top})`);
// Call the function to draw the chart
this.drawChart();
}
drawChart(): void {
// Define subgroups (series) and groups (categories) from the data
const subgroups = ["series1", "series2", "series3"];
const groups = this.data.map(d => d.group);
// Create scales for the x-axis (linear) and y-axis (band)
const xScale = d3.scaleLinear()
.domain([0, d3.max(this.data, d => d3.max(subgroups, key => d[key]))]).nice() // Set domain based on max value in data
.range([0, this.width]); // Map to the chart width
const yScale = d3.scaleBand()
.domain(groups) // Set domain to the group names
.range([0, this.height]) // Map to the chart height
.padding(0.2); // Add padding between groups
const xSubgroupScale = d3.scaleBand()
.domain(subgroups) // Set domain to subgroup names
.range([0, yScale.bandwidth()]) // Map within each group's bandwidth
.padding(0.05); // Add padding between subgroups
// Append x-axis to the chart
this.svg.append('g')
.attr('transform', `translate(0, ${this.height})`) // Position at the bottom
.call(d3.axisBottom(xScale).tickFormat(d3.format("d"))); // Use bottom-oriented axis
// Append y-axis to the chart
this.svg.append('g')
.call(d3.axisLeft(yScale).tickSize(-this.width).tickSizeOuter(0)); // Use left-oriented axis
// Create groups for each category
const groupsG = this.svg.selectAll("groups")
.data(this.data) // Bind data to each group
.enter()
.append("g")
.attr("transform", d => `translate(0, ${yScale(d.group)})`); // Position each group
// Create bars for each subgroup within the groups
const bars = groupsG.selectAll("rect")
.data(d => subgroups.map(key => ({ key, value: d[key] }))) // Map subgroups to their values
.enter()
.append("rect")
.attr("x", 0) // Start at the left edge
.attr("y", d => xSubgroupScale(d.key)) // Position vertically within the group
.attr("width", d => xScale(d.value)) // Set width based on value
.attr("height", xSubgroupScale.bandwidth()) // Set height to the subgroup bandwidth
.attr("fill", (d, i) => ['#ff69b4', '#33cc33', '#6666ff'][i]); // Color each subgroup differently
}
}
This guide provides a basic implementation. Depending on your requirements, you might want to enhance it with features like dynamic data updating, tooltips, or responsive design. You can further customize the chart by exploring more D3.js features, such as animations, transitions, or interactive elements.