简体   繁体   English

如何使用复选框隐藏chart_flutter中的一行图形?

[英]How to I hide one line of graph in chart_flutter with checks box?

Uncheks 屏幕没有任何变化 检查图形丢失的真实性

Hi, sorry I was unable to come with solation for my flutter app嗨,对不起,我无法为我的颤振应用程序提供安慰

What trying to do hide one graph when the checkbox is true shown in two pictures above当上面两张图片中显示的复选框为真时,如何隐藏一张图表

This my grapgh codes:这是我的图形代码:

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;


 class AbminetCharts extends StatelessWidget {
  final List<charts.Series> seriesList;
  final bool animate;

  AbminetCharts(this.seriesList, {this.animate});

  /// Creates a [LineChart] with sample data and no transition.
  factory AbminetCharts.withSampleData() {
    return new AbminetCharts(
      _createSampleData(),
      // Disable animations for image tests.
      animate: false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return new charts.TimeSeriesChart(
      seriesList,
      animate: animate,
      // Optionally pass in a [DateTimeFactory] used by the chart. The factory
      // should create the same type of [DateTime] as the data provided. If none
      // specified, the default creates local date time.
      dateTimeFactory: const charts.LocalDateTimeFactory(),
    );
  }

  static List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {

    final myCo2data = [
      new TimeSeriesSales(new DateTime(2017, 9, 19), 1.0),
      new TimeSeriesSales(new DateTime(2017, 9, 26), 2.3),
      new TimeSeriesSales(new DateTime(2017, 10, 3), 2.2),
    ];

    final myhumditydata = [
      new TimeSeriesSales(new DateTime(2017, 9, 19), 3.5),
      new TimeSeriesSales(new DateTime(2017, 9, 26), 2.3),
      new TimeSeriesSales(new DateTime(2017, 10, 3), 1.2),
    ];


    final mytempturedata = [
      new TimeSeriesSales(new DateTime(2017, 9, 19), 25),
      new TimeSeriesSales(new DateTime(2017, 9, 26), 30),
      new TimeSeriesSales(new DateTime(2017, 10, 3), 17),
    ];

    return  [

     new charts.Series<TimeSeriesSales, DateTime>(
        id: 'Co2',
        colorFn: (_, __) => charts.MaterialPalette.gray.shadeDefault,
        domainFn: (TimeSeriesSales sales, _) => sales.time,
        measureFn: (TimeSeriesSales sales, _) => sales.data,
        data: myCo2data,
      ),


      new charts.Series<TimeSeriesSales, DateTime>(
        id: 'Humdity',
        colorFn: (_, __) => charts.MaterialPalette.deepOrange.shadeDefault,
        domainFn: (TimeSeriesSales sales, _) => sales.time,
        measureFn: (TimeSeriesSales sales, _) => sales.data,
        data: myhumditydata,
      ),

      new charts.Series<TimeSeriesSales, DateTime>(
      id: 'tempture',
      colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
      domainFn: (TimeSeriesSales sales, _) => sales.time,
      measureFn: (TimeSeriesSales sales, _) => sales.data,
      data: mytempturedata,     
      ),

          ];
        }
      }

/// time series data type.
class TimeSeriesSales {
  final DateTime time;
  final double data;

  TimeSeriesSales(this.time, this.data);
}

This is my Checkbox codes:这是我的复选框代码:

class MyAbmientCheckbox extends StatefulWidget {
  @override
  Selectcheckbox createState() => Selectcheckbox();
}

class Selectcheckbox extends State<MyAbmientCheckbox> {

bool co2Val = false;
bool humVal = false;
bool tempVal = false;

  @override
  Widget build(BuildContext context) {
  return 
  //padding effects
     Material(
            borderRadius: BorderRadius.circular(30),
            shadowColor: Colors.black,
            color: Colors.lightGreenAccent.shade100,
            elevation: 5,


  child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              // [Monday] checkbox
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Text("Co2/",
                  style: TextStyle(color: Colors.grey.withOpacity(1)),
                  ),
                  Checkbox(
                    value: co2Val,
                    onChanged: (bool value) {
                      setState(() {
                        co2Val = value;
                      });
                    },
                  ),
                ],
              ),
              // [Tuesday] checkbox
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Text("Huminty/",
                  style: TextStyle(color: Colors.orange.withOpacity(1)),
                  ),
                  Checkbox(
                    value: humVal ,
                    onChanged: (bool value) {
                      setState(() {
                        humVal  = value;
                      });
                    },
                  ),
                ],
              ),
              // [Temptures] checkbox
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Text("Temptures/",
                  style: TextStyle(color: Colors.red.withOpacity(1)),
                  ),
                  Checkbox(
                    value: tempVal,
                    onChanged: (bool value) {
                      setState(() {
                        tempVal = value;
                      });
                    },
                  ),
                ],
              ),
            ],
           )
          );
  }
}

I had to try out conditions within the list, with "if the case" but can't pass the checkbox boolean variable into the charts class我不得不尝试列表中的条件,使用“if the case”,但无法将复选框布尔变量传递到图表类中
Example:例子:

    return  [

if(humVal == false)(
      new charts.Series<TimeSeriesSales, DateTime>(
        id: 'Humdity',
        colorFn: (_, __) => charts.MaterialPalette.deepOrange.shadeDefault,
        domainFn: (TimeSeriesSales sales, _) => sales.time,
        measureFn: (TimeSeriesSales sales, _) => sales.data,
        data: myhumditydata,
      ),
)//end of if 

] // end of return ] // 返回结束

If your reading this thank you for time, really appreciate it.如果您阅读本文感谢您的时间,真的很感激。

You basically should add this to your charts.TimeSeriesChart你基本上应该把它添加到你的 charts.TimeSeriesChart

var _listHiddenIds = [];
// ex : _listHiddenIds = ["Humdity"]

return new charts.TimeSeriesChart(
      seriesList,
      animate: animate,
      // Optionally pass in a [DateTimeFactory] used by the chart. The factory
      // should create the same type of [DateTime] as the data provided. If none
      // specified, the default creates local date time.
      dateTimeFactory: const charts.LocalDateTimeFactory(),
      // add this attribute
      behaviors: [
        new charts.SeriesLegend(
          defaultHiddenSeries: _listHiddenIds, // list of ids to be hidden
        )
      ],
    );

Then you should setState everytime the checkbox is toggled.然后每次切换复选框时都应该设置状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM