Skip to content

SfCartesianChart (Multiple LineSeries display errors when refresh the widget) #40

Closed

Description

@baliejun
  • flutter version any
  • syncfusion_flutter_charts : ^18.3.44
    1. It's normal to run the app the first time
    image

**2.when refresh the widget or perform hot reload , it will display errors(end to end),The data is obtained by network request **
image

code
`class EchartsWidget extends StatefulWidget{

@OverRide
State createState() {
return EchartsWidgetState();
}

}

class EchartsWidgetState extends State{
RefreshController _refreshController =
RefreshController(initialRefresh: false);

void _onRefresh() async {
// monitor network fetch
await Future.delayed(Duration(milliseconds: 1000));
if (mounted)
setState(() {
});
// if failed,use refreshFailed()
_refreshController.refreshCompleted();
}

List resultDataList = [];
List todayDataListOld = [];
List yesterdayDataListOld = [];
var unit;
List todayDataList = List();
List yesterdayDataList = List();

timestampToDateechart(timestamp) {
var date = DateTime.fromMillisecondsSinceEpoch(timestamp);
return date.toString().substring(0, 16);
}

getEchartData(List resultDataList) {

  ResultData data1 = resultDataList[0];
  ResultData data2 = resultDataList[1];
  if (data1.name == "今日") {
    todayDataListOld = data1.data;
    yesterdayDataListOld = data2.data;
  } else {
    todayDataListOld = data2.data;
    yesterdayDataListOld = data1.data;
  }


  for (int i = 0; i < todayDataListOld.length; i++) {
    String timestamp = timestampToDateechart(todayDataListOld[i].date-(24 * 60 * 60 * 1000));
    print("今日" + timestamp);
    var time = DateTime.parse(timestamp);
    var value = double.parse(todayDataListOld[i].value);
    todayDataList.add(new EchartsData(time, value));
  }

  for (int i = 0; i < yesterdayDataListOld.length; i++) {
    String timestamp = timestampToDateechart(yesterdayDataListOld[i].date);
    var time = DateTime.parse(timestamp);
    print("昨日" + timestamp);
    var value = double.parse(yesterdayDataListOld[i].value) ;
    yesterdayDataList.add(new EchartsData(time, value));
  }

}

@OverRide
Widget build(BuildContext context) {
return SmartRefresher(
enablePullDown: true,
header: MaterialClassicHeader(),
controller: _refreshController,
onRefresh: _onRefresh,
child: Container(
child: FutureBuilder(
future: WholeFactoryService().getEchartsData(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.connectionState == ConnectionState.done){
resultDataList = snapshot.data;
getEchartData(resultDataList);
return Container(
width: 300,
height: 300,
child: SfCartesianChart(
title: ChartTitle(
text: '发电负荷(MW)',
alignment: ChartAlignment.near
),
legend: Legend(
isVisible:true,
position: LegendPosition.auto,
),
backgroundColor: Color(0xff2775c7),
// Initialize category axis
primaryXAxis: DateTimeAxis(
//isVisible: true,
dateFormat: DateFormat.Hm(),

                  ),
                  primaryYAxis: NumericAxis(
                    axisLine: AxisLine(width: 0),
                  ),

                  series: <LineSeries<EchartsData, DateTime>>[
                    LineSeries<EchartsData, DateTime>(
                        color: Color(0xff05fcff),
                        // Bind data source
                        dataSource: todayDataList,
                        xValueMapper: (EchartsData data, _) => data.time,
                        yValueMapper: (EchartsData data, _) => data.value,
                        legendItemText: "今日",

                    ),
                    LineSeries<EchartsData, DateTime>(

                        color: Color(0xfff5a92f),
                        // Bind data source
                        dataSource: yesterdayDataList,
                        xValueMapper: (EchartsData data, _) => data.time,
                        yValueMapper: (EchartsData data, _) => data.value,
                        legendItemText: "昨日",
                        isVisible: true,

                    ),
                  ]
              ),
            );

          }else{
            return Container();
          }
        },
      ),
    ),
);

}
}

class EchartsData {
EchartsData(this.time, this.value);

DateTime time;
num value;
}`

Activity

  1. SriramKiranSenthilkumar commented on Nov 3, 2020

    @SriramKiranSenthilkumar
    Contributor

    Hi @baliejun,

    Greetings from Syncfusion. We have analyzed your scenario with the provided information and we tried to replicate the reported issue at our end regarding the chart displays errors (end to end) when hot-reloading or refreshing the widget. Unfortunately, we are not able to replicate the reported issue at our end. We also found that in the provided screenshot, you have told that the chart renders a line from end to start of the chart when hot-reloading. So we suspect that the data source which you are passing into the chart on hot-reload or refreshing the widget is the cause for the issue. So, we kindly request you to share the data source values which you are passing to the chart from the network request when hot-reloading or refreshing the widget or you can print the data source values in the console when hot-reloading or refreshing the widget and share us the console log for checking whether the data source values are passed correctly into the chart and also if possible, kindly share us a simple sample that can replicate the reported issue so that it will be helpful in providing the solutions sooner.

    Regards,
    Sriram Kiran

  2. baliejun commented on Nov 4, 2020

    @baliejun
    Author

    Hi,SriramKiranSenthilkumar ,

    Thanks for your quick response,I've found that the issue is caused by data.

  3. baliejun commented on Nov 4, 2020

    @baliejun
    Author

    Hi,SriramKiranSenthilkumar ,

    There is another question, How to achieve this effect(One of them is shaded)?
    image

  4. SriramKiranSenthilkumar commented on Nov 4, 2020

    @SriramKiranSenthilkumar
    Contributor

    Hi @baliejun,

    We have analyzed your query at our end and we have created a simple chart sample in which we have rendered two chart series (area series and line series) and we have used the gradient property of the AreaSeries in order to add shaded colours for the chart region and with the help of the borderColor, borderWidth and borderDrawMode properties of the Area series, we have set the border colour and width for the area series and we have set the value of the borderDrawMode property to BorderDrawMode.top so that the border for the area series will get rendered only in the top. Please refer the code snippet below for further reference.

    SfCartesianChart(
                    backgroundColor: Colors.blue[600],
                    primaryXAxis: DateTimeAxis(),
                    series: <ChartSeries<_ChartData, DateTime>>[
                      AreaSeries<_ChartData, DateTime>(
                          dataSource: chartData1,
                           // Added liner gradient to render the series region in shaded colors
                          gradient: LinearGradient(
                               // Provided colors for the shades
                              colors: [
                                Colors.lightBlueAccent.withOpacity(0.9),
                                Colors.lightBlueAccent[100].withOpacity(0.5),
                                Colors.lightBlue[50].withOpacity(0.3)
                              ],
                              stops: [
                                0.0,
                                0.5,
                                1.0
                              ],
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter
                          ),
                          // set the border mode as top to render the border for the series only in the top.
                          borderDrawMode: BorderDrawMode.top,
                          borderWidth: 3,
                          borderColor: Colors.cyanAccent.shade200,
                          xValueMapper: (_ChartData data, _) => data.xValue,
                          yValueMapper: (_ChartData data, _) => data.yValue),
                      LineSeries<_ChartData, DateTime>(
                          dataSource: chartData2,
                          width: 3,
                          color: Colors.amber[700],
                          xValueMapper: (_ChartData data, _) => data.xValue,
                          yValueMapper: (_ChartData data, _) => data.yValue)
                    ]
    )
    

    Screenshot:
    Screenshot_20201104-120947

    The sample for reference can be found below.
    sample.zip

    Please check with the above-attached sample and revert us if have further concerns.

    For further reference on the gradient support and border customization in area series, please check the user guides below.

    Regards,
    Sriram Kiran

  5. baliejun commented on Nov 5, 2020

    @baliejun
    Author

    Hi,Sriram Kiran ,

    Thank you very much.

    Regards,
    Liejun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions