In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to achieve a beautiful chart in Flutter, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Set up the Flutter Charts project
First, we need to create a new Flutter project. To do this, make sure that Flutter SDK and other requirements related to Flutter application development are installed correctly. If everything is set up correctly, to create the project, we can simply run the following command in the desired local directory:
Java:
Flutter create chartpost
After the project is set up, we can navigate through the project directory and execute the following command in the terminal to run the project in an available simulator or actual device:
Java:
Flutter run create Home Page UI
Now, we will create a home page, Scaffold. To do this, we need to create a file called home_page.dart. In the file, we can use the code in the following code snippet:
Dart:
Import 'package:flutter/material.dart';import' package:charts_flutter/flutter.dart' as charts;class HomePage extends StatelessWidget {@ override return Scaffold (appBar: AppBar (backgroundColor: Colors.red, title: Text ("Bitcoin price from Jan to March (2021)"),), body: Center (child: Text ("Hello, Charts!"),);}}
Here, we return a simple Scaffold widget with an application bar and a body HomePage from a stateless widget class named.
Now we need to import the stateless widget class in the main.dart file and assign it to the widget's home option, MaterialApp as shown in the following code snippet:
Dart:
Import 'package:chartpost/home.dart';import' package:flutter/material.dart';void main () = > runApp (MyApp ()); class MyApp extends StatelessWidget {@ override Widget build (BuildContext context) {return MaterialApp (debugShowCheckedModeBanner: false, home: HomePage (),);}}
Now, if we rerun the application, we will get the following results on the simulator screen:
Add a charts_flutter plug-in
Since we are adding diagrams to our application, we will use a package called charts_flutter. This package is a Material Design data visualization library written natively in Dart. It provides a wide range of charts for data visualization, which are lightweight and easy to configure. Now to install this library into our flutter project, we need to add the charts_flutter: ^ 0.8.1 line to our pubspec.yaml file as directed in the following code snippet:
Java:
Dependencies: flutter: sdk: flutter charts_flutter: ^ 0.8.1
After the installation is successful, we can use the chart widget provided by this package.
Bar Chart in Flutter
In this section, we will learn how to add a bar chart to a Flutter application. First, we will create a model file that defines the properties of the data to be displayed in the bar chart. Here, we will name the file bitcoin_price_series.dart. Inside, we will define a class called BitcoinPriceSeries, which accepts three parameters: month, price, and color. The overall code implementation is shown in the following code snippet:
Dart:
Import 'package:charts_flutter/flutter.dart' as charts;import' package:chartpost/bitcoin_price_series.dart';import 'package:flutter/material.dart';class BitcoinPriceChart extends StatelessWidget {final List data; BitcoinPriceChart ({@ required this.data}) Override Widget build (BuildContext context) {List series = [charts.Series (id: "Price", data: data, domainFn: (BitcoinPriceSeries series, _) = > series.month, measureFn: (BitcoinPriceSeries series, _) = > series.price, colorFn: (BitcoinPriceSeries series, _) = > series.barColor)]
Now we need to create a new file called bitcoin_price_chart.dart to define the chart structure. Here, we will implement a stateless widget called BitcoinPriceChart, which returns a bar chart with series values. The configuration charts provided by the library Series helps us define the data for each series. The following code snippet provides an exact implementation of how to configure series values:
Dart:
Return Container (height: 400, padding: EdgeInsets.all (20), child: Card (child: Padding (padding: const EdgeInsets.all (8.0), child: Column (children: [Text ("Bitcoin price from Jan to March (2021)",) Expanded (child: (charts.BarChart (series, animate: true)),)],),) }}
Now that we have the list data to hold the bar chart series data, we can apply it to the UI template. Now we will return a Container widget, where the Card widget is a child widget that contains BarChart widgets that take list and animate Boolean values as parameters. The following code snippet provides the overall implementation:
Dart:
Import 'package:flutter/material.dart';import' package:chartpost/bitcoin_price_series.dart';import 'package:chartpost/bitcoin_price_chart.dart';import' package:charts_flutter/flutter.dart' as charts
Now we are going to add the bar chart to our home page. To do this, we need to import the BitcoinPriceChart stateless class widget into the home_page.dart file, as shown in the following code snippet:
Import 'package:flutter/material.dart'; import' package:chartpost/bitcoin_price_series.dart'; import 'package:chartpost/bitcoin_price_chart.dart'; import' package:charts_flutter/flutter.dart' as charts
After import, we can define a list of stored data based on the BitcoinPriceSeries model, as shown in the following code snippet:
Dart:
Override Widget build (BuildContext context) {final List data = [BitcoinPriceSeries (month: "Jan", price: 50000, barColor: charts.ColorUtil.fromDartColor (Colors.red),), BitcoinPriceSeries (month: "Feb", price: 60000, barColor: charts.ColorUtil.fromDartColor (Colors.blue),), BitcoinPriceSeries (month: "March", price: 58000 BarColor: charts.ColorUtil.fromDartColor (Colors.green),),]
Finally, we need to add the BitcoinPriceChart widget to body's Scaffold on the home page as shown in the following code snippet to bypass the required list data:
Dart:
Return Scaffold (appBar: AppBar (backgroundColor: Colors.red, title: Text ("Bitcoin price from Jan to March (2021)"),), body: Center (child: BitcoinPriceChart (data: data,),);}
Therefore, we will get a bar chart on the home screen, as shown in the following simulator screenshot:
Therefore, we have successfully added the bar chart to our Flutter application.
Pie Chart in Flutter
Now that we know how to configure data for bar charts, we can easily add pie charts using exactly the same series of list data. Bar chart data series and pie chart data series share the same data format. Therefore, we can simply add a pie chart using the widgets provided by the charts library that provides a series list and animated Boolean parameter PieChart, as shown in the following code snippet:
Dart:
Expanded (child: (charts.BarChart (series, animate: true)),), Expanded (child: (charts.PieChart (series, animate: true)),)
Notice that this PieChart widget is added directly to the bitcoin_price_chart.dart file below the widget inside the widget. BarChartColumn
Therefore, we also successfully added the pie chart to our Flutter application.
Thank you for reading this article carefully. I hope the article "how to achieve a beautiful chart in Flutter" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.