Fl_chart: How to assign an array to spots

Created on 18 Feb 2020  Â·  7Comments  Â·  Source: imaNNeoFighT/fl_chart

Hi,

I just started using fl_charts which really looks fantastic. However I am struggling to move beyond demo LineChartSample1.

The sample code assigns values to spots as follows:

      spots: [
        FlSpot(1, 1.4),
        FlSpot(2, 3.4),
        FlSpot(3, 2),
        FlSpot(4, 2.2),
        FlSpot(5, 1.8),
      ],

In real life this obviously will have to be dynamic. Hence I need to assign values from an array of numbers like this:
myArray = [7578, 7547, 3578, 9875]

I can't figure out how to assign this array to spots assuming that first parameter to FlSpot is just a sequence like 0, 1, 2, 3 ... and second parameter coming from myArray.

I need something like FlSpot(x++, myArray[x]) in a loop.

Thanks

question

All 7 comments

I also tried this:

spots: List<FlSpot>.generate(myArray.length, (i) => FlSpot(i.toDouble(), myArray[i])),

However it says
"The constructor being called isn't a cons constructor. try using new to call the constructor"
When I add new before (new List"Invalid constant value"

List<FlSpot> mySpots = [];
myArray.asMap().forEach((index, value) {
  mySpots.add(FlSpot(index, value));
}

Thanks Amund,

Will check if I went back to fl_chart. But thanks anyway for taking time to respond.

Best Regards

Anjum

From: Amund Sivertsen [mailto:[email protected]]
Sent: Thursday, February 20, 2020 4:07 PM
To: imaNNeoFighT/fl_chart fl_chart@noreply.github.com
Cc: anjumraheel anjum.raheel@gmail.com; Author author@noreply.github.com
Subject: Re: [imaNNeoFighT/fl_chart] How to assign an array to spots (#204)

List mySpots = [];
myArray.asMap().forEach((index, value) {
mySpots.add(FlSpot(index, value));
}

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub https://github.com/imaNNeoFighT/fl_chart/issues/204?email_source=notifications&email_token=ABOCCXHHPLYQSIGGIE3P3X3RDZP4TA5CNFSM4KXDSAYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMNOLQY#issuecomment-588965315 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOCCXHVL2UM2ZSRUSTHEV3RDZP4TANCNFSM4KXDSAYA .

This is really a programming question for which you can always try Stack Overflow. In general in these cases no matter what you're using (Dart, Javascript, whatever) you will use transforms on your models to make them conform to the data type that the dependency you're trying to use asks for. You can do this on the spot using map() or forEach with variables or you can formalize it using constructors, models and entirely new models you create elsewhere. That will depend on your personal tastes and preferences.

For instance myArray.map( (item)=>FlSpot(item.x, item.y) );

Thank you so much for your guidance. Much appreciated.

Regards

Anjum

On Fri, Feb 21, 2020, 2:10 AM Timothy Shamilov notifications@github.com
wrote:

This is really a programming question for which you can always try Stack
Overflow. In general in these cases no matter what you're using (Dart,
Javascript, whatever) you will use transforms on your models to make them
conform to the data type that the dependency you're trying to use asks for.
You can do this on the spot using map() or forEach with variables or you
can formalize it using constructors, models and entirely new models you
create elsewhere. That will depend on your personal tastes and preferences.

For instance myArray.map( (item)=>FlSpot(item.x, item.y) );

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/imaNNeoFighT/fl_chart/issues/204?email_source=notifications&email_token=ABOCCXEF3LXRDQNSCGSHEBTRD3WUPA5CNFSM4KXDSAYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMQE6ZQ#issuecomment-589320038,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABOCCXEFYSQR36BXCI5QJBDRD3WUPANCNFSM4KXDSAYA
.

final spots = [1.0, 2.0, 3.0]
  .asMap()
  .entries
  .map((it) => FlSpot(it.key.toDouble(), it.value))
  .toList();

or

final spots = [1.0, 2.0, 3.0]
  .mapIndexed((i, it) => FlSpot(i.toDouble(), it))
  .toList();

extension IndexedIterableX<E> on Iterable<E> { 
  Iterable<R> mapIndexed<R>(R Function(int index, E element) transform) sync* {
    var index = 0;
    for (var element in this) {
      yield transform(index++, element);
    }
  }
}

Thanks, @yongjhih @shamilovtim.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jitenders859 picture jitenders859  Â·  5Comments

kw2019ltd picture kw2019ltd  Â·  6Comments

Saran90 picture Saran90  Â·  5Comments

enyo picture enyo  Â·  4Comments

davidmarinangeli picture davidmarinangeli  Â·  3Comments