Flutter蜡烛图绘制插件flutter_simple_candlesticks的使用

发布于 1周前 作者 ionicwang 来自 Flutter

Flutter蜡烛图绘制插件flutter_simple_candlesticks的使用

插件介绍

flutter_simple_candlesticks 是一个用于在Flutter中绘制蜡烛图的插件。它提供了优雅的蜡烛图图表,支持非滚动和可滚动两种模式。

示例代码

下面是一个完整的示例代码,展示了如何使用flutter_simple_candlesticks插件来绘制蜡烛图。

import 'package:flutter/material.dart';
import 'package:flutter_simple_candlesticks/flutter_simple_candlesticks.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter candlestick',
      theme: ThemeData(
          scaffoldBackgroundColor: const Color(0xFF0b112e),
          appBarTheme: const AppBarTheme(backgroundColor: Color(0xFF0b112e), elevation: 0)),
      home: const MyHomePage(title: 'Flutter candlestick'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    const style = CandlestickChartStyle(
        yLegendStyle: CandlestickChartYLegendStyle(
            textStyle: TextStyle(color: Colors.white),
            numberOfLabels: 5,
            lineStyle: CandlestickChartLineStyle(width: 11, color: Colors.white)),
        candlestickStyle: CandlestickStyle(bullishColor: Color(0xFF38bbbf), bearishColor: Color(0xFFed7b9e)),
        candlestickSelectedStyle: CandlestickStyle(bullishColor: Color(0xff25787b), bearishColor: Color(0xff9d4963)));

    const style2 = CandlestickChartStyle(
        yLegendStyle: CandlestickChartYLegendStyle(
            textStyle: TextStyle(color: Colors.white),
            numberOfLabels: 5,
            lineStyle: CandlestickChartDashedLineStyle(width: 11, color: Colors.white)),
        candlestickStyle: CandlestickStyle(bullishColor: Color(0xFF38bbbf), bearishColor: Color(0xFFed7b9e)),
        candlestickSelectedStyle: CandlestickStyle(bullishColor: Color(0xff25787b), bearishColor: Color(0xff9d4963)));

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              CandlesticksChart&lt;CandleModel&gt;(
                height: 200,
                data: data,
                style: style,
                options: const CandlestickChartOptions(isScrollable: true),
                getHightCallback: (e) =&gt; e.hight,
                getCloseCallback: (e) =&gt; e.close,
                getLowCallback: (e) =&gt; e.low,
                getOpenCallback: (e) =&gt; e.open,
                getTimeCallback: (e) =&gt; e.date,
              ),
              CandlesticksChart&lt;CandleModel&gt;(
                height: 200,
                data: data,
                style: style2,
                getHightCallback: (e) =&gt; e.hight,
                getCloseCallback: (e) =&gt; e.close,
                getLowCallback: (e) =&gt; e.low,
                getOpenCallback: (e) =&gt; e.open,
                getTimeCallback: (e) =&gt; e.date,
              )
            ],
          ),
        ),
      ),
    );
  }
}

示例说明

  1. 数据定义:首先定义了蜡烛图的数据。
const data = List&lt;CandleModel&gt;.from(List.generate(10, (index) =&gt; generateData(index))));

这里生成了一些示例数据,你可以根据需要替换为实际的数据。

  1. 样式设置:设置了蜡烛图的样式,包括阳线和阴线的颜色、标签样式等。
const style = CandlestickChartStyle(
    yLegendStyle: CandlestickChartYLegendStyle(
        textStyle: TextStyle(color: Colors.white),
        numberOfLabels: 5,
        lineStyle: CandlestickChartLineStyle(width: 11, color: Colors.white)),
    candlestickStyle: CandlestickStyle(bullishColor: Color(0xFF38bbbf), bearishColor: Color(0xFFed7b9e)),
    candlestickSelectedStyle: CandlestickStyle(bullishColor: Color(0xff25787j), bearishColor: Color(0xff9d4963)));
  1. 创建蜡烛图实例:通过CandlesticksChart构建蜡烛图实例,并传入相应的参数。
return Scaffold(
    appBar: AppBar(
      title: Text(widget.title),
    ),
    body: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            CandlesticksChart&lt;CandleModel&gt;(
              height: 200,
              data: data,
              style: style,
              options: const CandlestickChartOptions(isScrollable: true),
              getHightCallback: (e) =&gt; e.hight,
              getCloseCallback: (e) =&gt; e.close,
              getLowCallback: (e) =&gt; e.low,
              getOpenCallback: (e) =&gt; e.open,
              getTimeCallback: (e) =&gt; e.date,
            ),
            CandlesticksChart&lt;CandleModel&gt;(
              height: 200,
              data: data,
              style: style2,
              getHightCallback: (e) =&gt; e.hight,
              getCloseCallback: (e) =&gt; e.close,
              getLowCallback: (e) =&gt; e.low,
              getOpenCallback: (e) =&gt; e.open,
              getTimeCallback: (e) =&gt; e.date,
            )
          ],
        ),
      ),
    ),
  );
);

更多关于Flutter蜡烛图绘制插件flutter_simple_candlesticks的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter蜡烛图绘制插件flutter_simple_candlesticks的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用 flutter_simple_candlesticks 插件来绘制蜡烛图的示例代码。这个插件专门用于在Flutter应用中绘制金融市场中的蜡烛图(K线图)。

首先,确保你已经在 pubspec.yaml 文件中添加了 flutter_simple_candlesticks 依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_simple_candlesticks: ^最新版本号  # 请替换为当前最新版本号

然后运行 flutter pub get 来安装依赖。

接下来是一个完整的Flutter应用示例,展示了如何使用 flutter_simple_candlesticks 来绘制蜡烛图:

import 'package:flutter/material.dart';
import 'package:flutter_simple_candlesticks/flutter_simple_candlesticks.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Candlestick Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CandlestickChartScreen(),
    );
  }
}

class CandlestickChartScreen extends StatefulWidget {
  @override
  _CandlestickChartScreenState createState() => _CandlestickChartScreenState();
}

class _CandlestickChartScreenState extends State<CandlestickChartScreen> {
  // 示例数据,你可以根据实际需要替换为真实数据
  final List<CandlestickData> candlestickData = [
    CandlestickData(
      date: DateTime(2023, 10, 1),
      open: 100,
      high: 110,
      low: 95,
      close: 105,
    ),
    CandlestickData(
      date: DateTime(2023, 10, 2),
      open: 105,
      high: 115,
      low: 100,
      close: 110,
    ),
    // 更多数据...
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Candlestick Chart Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: SimpleCandlesticks(
          data: candlestickData,
          padding: EdgeInsets.symmetric(horizontal: 8.0),
          candlestickWidth: 4.0,
          candlestickGap: 2.0,
          dayFormat: 'yyyy-MM-dd',
          colorUp: Colors.green,
          colorDown: Colors.red,
          borderColor: Colors.black,
          showVolume: true,
          volumeColorUp: Colors.lightGreen,
          volumeColorDown: Colors.lightCoral,
          volumeBorderColor: Colors.grey,
        ),
      ),
    );
  }
}

// 蜡烛图数据模型
class CandlestickData {
  DateTime date;
  double open;
  double high;
  double low;
  double close;

  CandlestickData({
    required this.date,
    required this.open,
    required this.high,
    required this.low,
    required this.close,
  });
}

在这个示例中:

  1. 我们定义了一个 CandlestickData 类来表示每个蜡烛图的数据点。
  2. CandlestickChartScreen 中,我们创建了一些示例数据。
  3. 使用 SimpleCandlesticks 组件来绘制蜡烛图,并配置了各种样式参数,如蜡烛条的宽度、间距、颜色等。

你可以根据需要调整数据和样式参数来适应你的具体需求。希望这个示例对你有帮助!

回到顶部