flutter_xchart 1.0.0 copy "flutter_xchart: ^1.0.0" to clipboard
flutter_xchart: ^1.0.0 copied to clipboard

简易图表

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:flutter_xchart/flutter_xchart.dart';
import 'package:flutter_xchart/xchart.dart';
import 'package:flutter_xchart/screen_size_adaptation.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await FlutterXChart.platformVersion ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: XChart(
            yAxisHeight: 150.w,
            yData: ["500", "400", "300", "200", "100", "0"],
            xCount: 5,
            xItemBuild: (params) {///构建柱状图
              double maxHeight = params["maxHeight"];
              double margin = params["margin"];
              int index = params["index"];
              int curIndex = params["curIndex"];
              int xWidth = params["xWidth"];
              double xItemWidth = xWidth / 5;
              Widget bar = getBar(maxHeight,margin,index,curIndex,xItemWidth);
              Widget tag = getTag(index,curIndex,xItemWidth);
              return {
                "bar":bar,
                "tag":tag,
              };
            },
            popBuild: (params){///构建图表弹窗
              double maxHeight = params["maxHeight"];
              double margin = params["margin"];
              int curIndex = params["curIndex"];
              int xWidth = params["xWidth"];
              double xItemWidth = xWidth / 5;
              Widget popWindow = getPopWindow(maxHeight,margin,curIndex,xItemWidth);
              return {
                "widget": popWindow,
                "leftOffset": 25.w,
                "topOffset": 25.w,
                "popWidth": 100,
                "popHeight": 100,
              };
            },
            lineChartBuild: (){///构建曲线图折线图
              List<LineChartBean> lineData = [];
              return {
                "lineData":lineData,
              };
            },
            limitLineBuild: (){///构建限制线
              List<LimitLineBean> lineData = [];
              return {
                "lineData":lineData,
              };
            },
          ),
        ),
      ),
    );
  }

  Widget getBar(double maxHeight,double margin,int index,int curIndex,double xItemWidth){

    return Container(
      width: xItemWidth,
      height: maxHeight - margin,

    );
  }

  Widget getTag(int index,int curIndex,double xItemWidth){
    return Container(
      width: xItemWidth,
    );
  }

  Widget getPopWindow(double maxHeight,double margin,int curIndex,double xItemWidth){
    return Container();
  }

}