Published on

How to Create A Chart Filter With Tailwind CSS?

Tags
Chart Filter

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Chart Filter ui component

Chart filter inside of basic dashboard

Why use Tailwind CSS to create a Chart Filter ui component?

  • It can make the building process of Chart Filter ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Chart Filter component file.

The preview of Chart Filter ui component

Free download of the Chart Filter's source code

The source code of Chart Filter ui component

<div class="block">

            <div x-data="{ open : false }">
                <div class="bg-white shadow">
                    <div class="container mx-auto px-4 py-4">
						<div class="flex justify-between">
							<div class="flex justify-center">
								<a class="flex items-center px-3 py-1 mr-3 border font-medium rounded  bg-red-500 text-white">Loa Status</a>
								<a class="flex items-center px-3 py-1 mr-3 border font-medium rounded text-gray-700">Shipment</a>
								<a class="flex items-center px-3 py-1 mr-3 border font-medium rounded text-gray-700">Inventory</a>
								<a class="flex items-center px-3 py-1 mr-3 border font-medium rounded text-gray-700">Distribution</a>
								<a class="flex items-center px-3 py-1 mr-3 border font-medium rounded text-gray-700">Loss</a>
							</div>
							<div class="">
								<button type="button" @click="open = !open" class="flex items-center text-gray-700 px-3 py-1 border font-medium rounded"> 
									<svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" class="w-5 h-5 mr-1">
										<g class="">
											<path d="M0 0h24v24H0z" fill="none" class=""></path>
											<path d="M3 17v2h6v-2H3zM3 5v2h10V5H3zm10 16v-2h8v-2h-8v-2h-2v6h2zM7 9v2H3v2h4v2h2V9H7zm14 4v-2H11v2h10zm-6-4h2V7h4V5h-4V3h-2v6z" class=""></path>
										</g>
									</svg>
									Filter 
								</button>
							</div>
						</div>
                    </div>
                </div>
                <div class="container mx-auto">
                    <div class="flex flex-col mx-4 px-8 py-6 bg-white rounded-b-md shadow"
                        x-show="open"
                        x-transition:enter="transition ease-out origin-top duration-300"
                        x-transition:enter-start="opacity-0 transform scale-90"
                        x-transition:enter-end="opacity-100 transform scale-100"
                        x-transition:leave="transition ease-in origin-top duration-300"
                        x-transition:leave-start="opacity-100 transform scale-100"
                        x-transition:leave-end="opacity-0 transform scale-90"
                        >
						<div class="flex justify-center text-sm text-gray-700">
							<div class="w-1/5 mr-10">
								<h3 class="pb-2 border-b mb-3 text-md text-gray-900 font-semibold">Country</h3>
								<div class="flex flex-col">
									<div class="flex py-2">
										<svg class="h-5 w-5 mr-2" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
											viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
											<circle style="fill:#496E2D;" cx="256" cy="256" r="256"/>
											<circle style="fill:#D80027;" cx="200.348" cy="256" r="111.304"/>
										</svg>
										<a href="#" class="flex-1">Bangladesh</a>
									</div>
									<div class="flex py-2 font-bold">
										<svg class="h-5 w-5 mr-2" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
											viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
											<circle style="fill:#496E2D;" cx="256" cy="256" r="256"/>
											<circle style="fill:#D80027;" cx="200.348" cy="256" r="111.304"/>
										</svg>
										<a href="#" class="flex-1">Bangladesh</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<svg class="h-5 w-5 mr-2" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
											viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
											<circle style="fill:#496E2D;" cx="256" cy="256" r="256"/>
											<circle style="fill:#D80027;" cx="200.348" cy="256" r="111.304"/>
										</svg>
										<a href="#" class="flex-1">Bangladesh</a>
									</div>
								</div>
							</div>
							<div class="w-1/5 mr-10">
								<h3 class="pb-2 border-b mb-3 text-md text-gray-900 font-semibold">Item</h3>
								<div class="flex flex-col">
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 1</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 3</a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 1</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 3</a>
									</div>
								</div>
							</div>
							<div class="w-1/5 mr-10">
								<h3 class="pb-2 border-b mb-3 text-md text-gray-900 font-semibold">Item</h3>
								<div class="flex flex-col">
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 1</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 3</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 3</a>
									</div>
								</div>
							</div>
							<div class="w-1/5 mr-10">
								<h3 class="pb-2 border-b mb-3 text-md text-gray-900 font-semibold">Item</h3>
								<div class="flex flex-col">
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 1</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 3</a>
									</div>
								</div>
							</div>
							<div class="w-1/5">
								<h3 class="pb-2 border-b mb-3 text-md text-gray-900 font-semibold">Item</h3>
								<div class="flex flex-col">
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 1</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 3</a>
									</div>
									<div class="flex py-2">
										<a href="#" class="flex-1">Item 1</a>
									</div>
									<div class="flex py-2 font-bold">
										<a href="#" class="flex-1">Item 2</a>
										<a href="#"><svg class="h-5 py-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 8.586L2.929 1.515 1.515 2.929 8.586 10l-7.071 7.071 1.414 1.414L10 11.414l7.071 7.071 1.414-1.414L11.414 10l7.071-7.071-1.414-1.414L10 8.586z"/></svg></a>
									</div>
								</div>
							</div>
						</div>
						<div class="flex">
							<a class="text-gray-700 font-normal text-sm hover:underline">Clear Filter</a>
						</div>
                    </div>
                </div>
            </div>
            <div class="container px-4 py-4 mx-auto">
				<h3 class="block text-xl text-gray-700 font-semibold mb-3"></h3>
				<div class="flex">
					<div class="w-1/2">
						<div class="rounded-md p-6 bg-white shadow">
							<div class="mb-2 pb-2">
								<h3 class="font-semibold text-lg text-gray-600">Bar Chart</h3>
								<p class="text-sm text-gray-500">Profit Share between customers</p>
							</div>
							<div id="chartdiv" class="w-full" style="height: 240px"></div>
						</div>
					</div>
					<div class="w-1/2 ml-4">
						<div class="rounded-md p-6 bg-white shadow">
							<div class="mb-2 pb-2">
								<h3 class="font-semibold text-lg text-gray-600">Bar Chart</h3>
								<p class="text-sm text-gray-500">Profit Share between customers</p>
							</div>
							<div id="chartdiv2" class="w-full" style="height: 240px"></div>
						</div>
					</div>
				</div>
            </div>
        </div>

		<!-- Resources -->
        <script src="https://www.amcharts.com/lib/4/core.js"></script>
        <script src="https://www.amcharts.com/lib/4/charts.js"></script>
        <script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>

        <script>
			function bar_chart(selector, data) {
				// Themes begin
				// Themes end
				am4core.useTheme(am4themes_animated);

				// Create chart instance
				var chart = am4core.create(selector, am4charts.XYChart);

				// Add data
				chart.data = data;

				// Create axes

				var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
				categoryAxis.dataFields.category = "country";
				categoryAxis.renderer.grid.template.location = 0;
				categoryAxis.renderer.minGridDistance = 30;

				categoryAxis.renderer.labels.template.adapter.add("dy", function (dy, target) {
					if (target.dataItem && target.dataItem.index & 2 == 2) {
						return dy + 25;
					}
					return dy;
				});

				var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

				// Create series
				var series = chart.series.push(new am4charts.ColumnSeries());
				series.dataFields.valueY = "visits";
				series.dataFields.categoryX = "country";
				series.name = "Visits";
				series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/]";
				series.columns.template.fillOpacity = .8;

				var columnTemplate = series.columns.template;
				columnTemplate.strokeWidth = 2;
				columnTemplate.strokeOpacity = 1;
			}

			function radar_chart(selector, data) {
				// Themes begin
				// Themes end
				am4core.useTheme(am4themes_animated);

				// Create chart instance
				var chart = am4core.create(selector, am4charts.RadarChart);


				chart.data = data;

				// Make chart not full circle
				chart.startAngle = -90;
				chart.endAngle = 180;
				chart.innerRadius = am4core.percent(20);

				// Set number format
				chart.numberFormatter.numberFormat = "#.#'%'";

				// Create axes
				var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
				categoryAxis.dataFields.category = "category";
				categoryAxis.renderer.grid.template.location = 0;
				categoryAxis.renderer.grid.template.strokeOpacity = 0;
				categoryAxis.renderer.labels.template.horizontalCenter = "right";
				categoryAxis.renderer.labels.template.fontWeight = 500;
				categoryAxis.renderer.labels.template.adapter.add("fill", function (fill, target) {
					return (target.dataItem.index >= 0) ? chart.colors.getIndex(target.dataItem.index) : fill;
				});
				categoryAxis.renderer.minGridDistance = 10;

				var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
				valueAxis.renderer.grid.template.strokeOpacity = 0;
				valueAxis.min = 0;
				valueAxis.max = 100;
				valueAxis.strictMinMax = true;

				// Create series
				var series1 = chart.series.push(new am4charts.RadarColumnSeries());
				series1.dataFields.valueX = "full";
				series1.dataFields.categoryY = "category";
				series1.clustered = false;
				series1.columns.template.fill = new am4core.InterfaceColorSet().getFor("alternativeBackground");
				series1.columns.template.fillOpacity = 0.08;
				series1.columns.template.cornerRadiusTopLeft = 20;
				series1.columns.template.strokeWidth = 0;
				series1.columns.template.radarColumn.cornerRadius = 20;

				var series2 = chart.series.push(new am4charts.RadarColumnSeries());
				series2.dataFields.valueX = "value";
				series2.dataFields.categoryY = "category";
				series2.clustered = false;
				series2.columns.template.strokeWidth = 0;
				series2.columns.template.tooltipText = "{category}: [bold]{value}[/]";
				series2.columns.template.radarColumn.cornerRadius = 20;

				series2.columns.template.adapter.add("fill", function (fill, target) {
					return chart.colors.getIndex(target.dataItem.index);
				});

				// Add cursor
				chart.cursor = new am4charts.RadarCursor();
			}

			am4core.ready(function () {
				bar_chart("chartdiv", [{
					"country": "USA",
					"visits": 2025
				}, {
					"country": "China",
					"visits": 1882
				}, {
					"country": "Japan",
					"visits": 1809
				}]);

				radar_chart("chartdiv2", [{
					"category": "Research",
					"value": 80,
					"full": 100
				}, {
					"category": "Human Resources",
					"value": 68,
					"full": 100
				}]);
			}); // end am4core.ready()

        </script>

How to create a Chart Filter with Tailwind CSS?

Install tailwind css of verion 1.3.4

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.3.4

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to create a Chart Filter component

  • block
  • bg-white
  • mx-auto
  • px-4
  • py-4
  • flex
  • px-3
  • py-1
  • mr-3
  • bg-red-500
  • text-white
  • text-gray-700
  • w-5
  • h-5
  • mr-1
  • flex-col
  • mx-4
  • px-8
  • py-6
  • text-sm
  • w-1/5
  • mr-10
  • pb-2
  • border-b
  • mb-3
  • text-md
  • text-gray-900
  • py-2
  • mr-2
  • flex-1
  • text-xl
  • w-1/2
  • p-6
  • mb-2
  • text-lg
  • text-gray-600
  • text-gray-500
  • w-full
  • ml-4

39 steps to create a Chart Filter component with Tailwind CSS

  1. Use inline utilities to put the element on its own line and fill its parent.

  2. Control the background color of an element to white using the bg-white utilities.

  3. Control the horizontal margin of an element to auto using the mx-auto utilities.

  4. Control the horizontal padding of an element to 1rem using the px-4 utilities.

  5. Control the vertical padding of an element to 1rem using the py-4 utilities.

  6. Use flex to create a block-level flex container.

  7. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  8. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

  9. Control the margin on right side of an element to 0.75rem using the mr-3 utilities.

  10. Control the background color of an element to red-500 using the bg-red-500 utilities.

  11. Control the text color of an element to white using the text-white utilities.

  12. Control the text color of an element to gray-700 using the text-gray-700 utilities.

  13. Use w-5 to set an element to a fixed width(1.25rem).

  14. Use h-5 to set an element to a fixed height(1.25rem).

  15. Control the margin on right side of an element to 0.25rem using the mr-1 utilities.

  16. Use flex to create a block-level flex container.

  17. Control the horizontal margin of an element to 1rem using the mx-4 utilities.

  18. Control the horizontal padding of an element to 2rem using the px-8 utilities.

  19. Control the vertical padding of an element to 1.5rem using the py-6 utilities.

  20. Control the text color of an element to sm using the text-sm utilities.

  21. Use w-1/5 to set an element to a fixed width(1/5).

  22. Control the margin on right side of an element to 2.5rem using the mr-10 utilities.

  23. Control the padding on bottom side of an element to 0.5rem using the pb-2 utilities.

  24. Control the border color of an element to b using the border-b utilities.

  25. Control the margin on bottom side of an element to 0.75rem using the mb-3 utilities.

  26. Control the text color of an element to md using the text-md utilities.

  27. Control the text color of an element to gray-900 using the text-gray-900 utilities.

  28. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  29. Control the margin on right side of an element to 0.5rem using the mr-2 utilities.

  30. Use flex to create a block-level flex container.

  31. Control the text color of an element to xl using the text-xl utilities.

  32. Use w-1/2 to set an element to a fixed width(1/2).

  33. Control the padding on all sides of an element to 1.5rem using the p-6 utilities.

  34. Control the margin on bottom side of an element to 0.5rem using the mb-2 utilities.

  35. Control the text color of an element to lg using the text-lg utilities.

  36. Control the text color of an element to gray-600 using the text-gray-600 utilities.

  37. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  38. Use w-full to set an element to a 100% based width.

  39. Control the margin on left side of an element to 1rem using the ml-4 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Chart Filter components, learn and follow along to implement your own components.