FLCharts: Create Bar Chart easily
FLCharts is a flexible, easy-to-use third-party library, allowing you to highly customise your chart.
FLCharts is an easy-to-use library to build highly customizable bar charts. It allows you to create your own chart bar.
Here I used some gas consumptions data to demonstrate how to set up a basic bar chart using
Setting Up a Chart View
Firstly, make sure to import
FLCharts to the top of the file.
Setting Up Data
let monthsData = [MultiPlotable(name: "jan", values: ),
MultiPlotable(name: "feb", values: ),
MultiPlotable(name: "mar", values: ),
MultiPlotable(name: "apr", values: [45, 30]),
MultiPlotable(name: "may", values: ),
MultiPlotable(name: "jun", values: [46, 40]),
MultiPlotable(name: "jul", values: ),
MultiPlotable(name: "aug", values: ),
MultiPlotable(name: "set", values: ),
MultiPlotable(name: "oct", values: ),
MultiPlotable(name: "nov", values: ),
MultiPlotable(name: "dec", values: [55, 50, 20])]
MultiPlotable object represents the data of one month. The name value is the name of the month on the x axis and the values property is their consumptions on the y axis.
let chartData = FLChartData(title: "Consumptions",
legendKeys: [Key(key: "F1", color: .Gradient.purpleCyan),
Key(key: "F2", color: .green),
Key(key: "F3", color: .Gradient.sunset)],
Here, we are setting up the data for the chart.
FLChartData contains all the data the chart needs to configure itself.
The title is shown when the chart is embedded in a
The legendKeys define the color of the sections of the bar and the keys of the legend, if it is embedded in a
FLCharts has a collection of colors and gradients to use in your chart. And you can create your own using the
Then we create the actual chart with this simple line of code.
let chart = FLChart(data: chartData, type: .bar())
The bar chart allows you to create your own bar view simply creating a class that conforms to
Now we have to layout the chart, in this example we will do it using AutoLayout.
chart.translatesAutoresizingMaskIntoConstraints = false
And here is our bar chart.
Embed chart inside FLCard
Now, we can embed the chart inside a card.
let card = FLCard(chart: chart, style: .rounded)
card.showAverage = true
card.showLegend = false
This is all we need for this task.
As you may notice
FLCard has some additional properties and the ability to customize the card itself through the
FLCharts has two predefined styles:
.rounded, but you can create your own using the
There is just one last thing to change in order to show the card. We must replace
card in the auto layout code.
card.translatesAutoresizingMaskIntoConstraints = false
FLCharts allows you to display a view when tapping on a bar. The library has a default one but you can create your own creating a
class that conforms to the protocol
Here’s an example:
FLCharts has additional properties the allows you to further customize your charts.
You can assign your own configuration through the
chart.config = FLChartConfig(granularityY: 20)
Aware x FLCharts
Check out the library here.
For your time and attention! I hope you enjoyed this article and
If you have suggestions or feedbacks, they are really welcomed and they help me improve the library.
- Mobile App Development (297)