Arc charts are circular visualizations that display data as segments of a circle. Each segment represents a proportion of the whole. They are similar to pie charts, but often have a hole in the center, creating a donut - like appearance. Arc charts are useful for showing relative proportions of different categories within a dataset.
Responsive design is an approach to web design that ensures web pages look and function well on a variety of devices, including desktops, tablets, and mobile phones. Key principles of responsive design include using flexible grids and layouts, media queries, and flexible images. When applying these principles to arc charts, we need to make sure the chart resizes and remains legible across different screen sizes.
To create an arc chart, we can use basic HTML elements. A simple approach is to use <div>
elements to represent the different segments of the chart. Here is a basic example:
<div class="arc-chart">
<div class="arc-segment" style="--arc-percentage: 30;"></div>
<div class="arc-segment" style="--arc-percentage: 70;"></div>
</div>
In this example, we have a parent <div>
with the class arc - chart
and two child <div>
elements representing the segments of the arc chart. The --arc - percentage
custom property is used to define the proportion of each segment.
We can use CSS to style the arc chart and make it responsive. Here is an example of CSS code:
.arc-chart {
position: relative;
width: 200px;
height: 200px;
border-radius: 50%;
background: lightgray;
margin: 0 auto;
/* Make it responsive */
width: 50vw;
height: 50vw;
max-width: 300px;
max-height: 300px;
}
.arc-segment {
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
clip: rect(0, 100px, 200px, 0);
transform: rotate(calc(var(--arc-percentage) * 3.6deg));
}
.arc-segment:nth-child(1) {
background: blue;
}
.arc-segment:nth-child(2) {
background: red;
}
In this CSS code, we first style the arc - chart
container to make it a circle using border - radius: 50%
. We also set the width and height to be responsive using vw
units and limit the maximum size. The arc - segment
elements are positioned absolutely within the chart and rotated based on the --arc - percentage
custom property.
When creating responsive arc charts, it is important to use percentages or relative units like vw
and vh
for sizing. This ensures that the chart resizes proportionally as the screen size changes. For example, instead of setting a fixed width and height for the chart, we can use percentages or viewport units as shown in the CSS code above.
Media queries can be used to adjust the styling of the arc chart based on the screen size. For example, we can change the size of the chart or the font size of any labels when the screen is smaller.
@media (max-width: 600px) {
.arc-chart {
width: 80vw;
height: 80vw;
}
}
In this media query, when the screen width is less than or equal to 600px, the width and height of the arc chart are set to 80% of the viewport width.
role="img"
to the arc - chart
container and aria - label
to describe the data represented by the chart.Here is the complete code example combining the HTML and CSS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.arc-chart {
position: relative;
width: 50vw;
height: 50vw;
max-width: 300px;
max-height: 300px;
border-radius: 50%;
background: lightgray;
margin: 0 auto;
}
.arc-segment {
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
clip: rect(0, 50%, 100%, 0);
transform: rotate(calc(var(--arc-percentage) * 3.6deg));
}
.arc-segment:nth-child(1) {
background: blue;
}
.arc-segment:nth-child(2) {
background: red;
}
@media (max-width: 600px) {
.arc-chart {
width: 80vw;
height: 80vw;
}
}
</style>
</head>
<body>
<div class="arc-chart">
<div class="arc-segment" style="--arc-percentage: 30;"></div>
<div class="arc-segment" style="--arc-percentage: 70;"></div>
</div>
</body>
</html>
Creating responsive arc charts using CSS and HTML is a great way to visualize data on the web. By understanding the fundamental concepts, using the right HTML and CSS techniques, and following common and best practices, we can create engaging and accessible arc charts that work well on a variety of devices. Remember to focus on responsiveness, accessibility, and performance optimization to provide the best user experience.