Related
how i can sort my List by one of my values from list?
here is a example of my list
List data = [
{'name': 'Example1', 'value1': 10, 'value2': 0},
{'name': 'Example2', 'value1': 0, 'value2': 10},
{'name': 'Example3', 'value1': 15, 'value2': 5},
{'name': 'Example4', 'value1': 22.5, 'value2': 10},
];
and so I call it
Column(
children: data.map((info) {
return Container(
child: SizedBox(
child: Row(
children: [
Text(info['name'],),
Text(info['value1'],),
],
),
),
);
}).toList())
this is how my list is listed from top to bottom
how can i sort it by value?
How can I hide an entry if the value is 0?
list.sort() takes a comparator function. A comparator takes two values from the list and compares them to see if swapping is required. Based on what you return, you can control how the lists get sorted. When a positive value is returned, swapping occurs otherwise not.
In your case, let's say you want to sort using value1 in increasing order. You need to tell the comparator to return a positive value when a > b. If you want decreasing order, return a positive value when b > a:
List data = [
{'name': 'Example1', 'value1': 15},
{'name': 'Example2', 'value1': 10},
{'name': 'Example3', 'value1': 5},
{'name': 'Example4', 'value1': 0},
];
// sort in place w.r.t. value1
// CompareTo method just returns first value - second value in case of double
// Try returning b['value1'].compareTo(a['value1']) or b['value1'] - a['value1'] and the result should be in descending order w.r.t value1 property.
data.sort((a,b) => a['value1'].compareTo(b['value1'])); // You can also write a['value1'] - b['value1']
print(data.toString());
// To filter (remove all those elements whose value1 is 0)
List filtered = data.where((a) => a['value1'] != 0).toList(); // Where method takes a function which should return true if you want to keep the item in the filtered list.
print(filtered.toString()); // filtered is the new list with all those elements removed.
and here is the output:
[{name: Example4, value1: 0}, {name: Example3, value1: 5}, {name: Example2, value1: 10}, {name: Example1, value1: 15}]
[{name: Example3, value1: 5}, {name: Example2, value1: 10}, {name: Example1, value1: 15}]
Update:
You can use the filter like this:
Column(
children: data
.where((d) => d['value1'] != 0) // <----- Here :)
.map((info) {
return Container(
child: SizedBox(
child: Row(
children: [
Text(info['name'],),
Text(info['value1'],),
],
),
),
);
}).toList())
This is the code for my list:
Future<List<Map>> queryDb() async {
List localList;
final db = await database;
final allRows = await db.query(TABLE_FAVORITE);
localList = allRows.toList(growable: true);
localList.removeWhere((item) => item[COLUMN_ISFAVORITE] == 0);
publicFavoriteList = localList;
print(localList);
return localList;
}
whenever it gets called it prints:
[{id: 0, isFavorite: 1}, {id: 1, isFavorite: 1}, {id: 2, isFavorite: 1}, {id: 4, isFavorite: 1}]
How can I "cut" this to
[{0}, {1}, {2}, {4}]
Use map to extract the values from the inner Map objects:
var localValues = localList.map((o) => o['id'] as int).toList();
print(localVales);
// Prints: [0, 1, 2, 4]
I have a List<Comment> about food from users.
It looks like this:
[
{userId: 1,
rating: 4.5
},
{userId: 2,
rating: 4.0
},
{userId: 3,
rating: 3.5
},
{userId: 4,
rating: 3.0
}
...
];
I want to get the average rating. AVERAGE = Number of ratings : total user, how do I apply this in dart?
var values = [
{'userId': 1, 'rating': 4.5},
{'userId': 2, 'rating': 4.0},
{'userId': 3, 'rating': 3.5},
{'userId': 4, 'rating': 3.0}
];
var result = values.map((m) => m['rating']).reduce((a, b) => a + b) / values.length;
print(result);
In 2021 dart has the built-in average method from the collection package which can be used on an Iterable.
import 'package:collection/collection.dart';
void main() {
var values = [
{'userId': 1, 'rating': 4.5},
{'userId': 2, 'rating': 4.0},
{'userId': 3, 'rating': 3.5},
{'userId': 4, 'rating': 3.0}
];
var result = values.map((m) => m['rating']!).average;
print(result); // prints 3.75
}
You might also be interested in the .sum method from the same package.
I did this by adding the values with the fold function and then dividing by the total. Here is an example:
var values = [
{'userId': 1, 'rating': 4.5},
{'userId': 2, 'rating': 4.0},
{'userId': 3, 'rating': 3.5},
{'userId': 4, 'rating': 3.0}
];
List<double> ratings = values.map((e) => e.price!).toList();
double sum = ratings.fold(0, (p, c) => p + c);
if (sum > 0) {
double average = sum / ratings.length;
}
Trying to do a chart to show/hide the lines on click (I think the original code is from #asgallant), but it only works after the first interaction.
PS.: Is it possible to gray out the text in the legend too?
FIDDLE
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable
([
['Time', 'One', 'Two', 'Three', 'Four', 'Five'],
['05/2013', 0, 3, 0, 0, 8],
['06/2013', 0, 3, 1, 0, 7],
['07/2013', 0, 3, 1, 0, 7],
['08/2013', 0, 3, 1, 0, 7],
['09/2013', 0, 3, 1, 0, 7],
['10/2013', 0, 3, 1, 0, 7],
['11/2013', 0, 3, 1, 0, 7],
['12/2013', 0, 3, 1, 0, 7],
['01/2014', 0, 3, 1, 0, 7],
['02/2014', 0, 3, 1, 0, 7],
['03/2014', 0, 3, 1, 0, 7],
['04/2014', 0, 2, 1, 0, 7],
['05/2014', 0, 2, 1, 0, 7]
]);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
var columns = [];
var series = {};
for (var i = 0; i < data.getNumberOfColumns(); i++) {
columns.push(i);
if (i > 0) {
series[i - 1] = {};
}
}
var options = {
chartArea: {width: '80%', height: '70%'},
fontSize: ['13'],
lineWidth: 3,
pointSize: 10,
series: series
};
google.visualization.events.addListener(chart, 'select', function () {
var sel = chart.getSelection();
// if selection length is 0, we deselected an element
if (sel.length > 0) {
// if row is undefined, we clicked on the legend
if (sel[0].row === null) {
var col = sel[0].column;
if (columns[col] == col) {
// hide the data series
columns[col] = {
label: data.getColumnLabel(col),
type: data.getColumnType(col),
calc: function () {
return null;
}
};
// grey out the legend entry
series[col - 1].color = '#CCCCCC';
}
else {
// show the data series
columns[col] = col;
series[col - 1].color = null;
}
var view = new google.visualization.DataView(data);
view.setColumns(columns);
chart.draw(view, options);
}
}
});
}
I am having an issue with the Google Visualization API in that some of the data in the chart is not showing. The chart is fairly simple, it has 4 columns and two rows.
http://savedbythegoog.appspot.com/?id=ae0853b788af3292b5547a5b7f1224aed76abfff
function drawVisualization() {
// Create and populate the data table.
var data_table = new google.visualization.DataTable();
data_table.addColumn({"type": "date","label": "Date"});
data_table.addColumn({"type": "number","label": "A"});
data_table.addColumn({"type": "number","label": "B"});
data_table.addColumn({"type": "number","label": "C"});
data_table.addRow([{v: new Date(2013, 5, 26)}, {v: 1}, {v: 0}, {v: 0}]);
data_table.addRow([{v: new Date(2013, 5, 27)}, {v: 2}, {v: 1}, {v: 0.5}]);
var chart = new google.visualization.ColumnChart(document.getElementById('visualization'));
chart.draw(data_table, {
legend: "bottom"
});
}
When generated, the chart shows nothing for the first row (2013-5-26), and shows only the values of 2 and 1 for the second row (omitting 0.5).
I suspect this could be similar to Google Column Chart Missing data
Does anyone have any ideas?
So it seems Google have provided the solution somewhat...
https://developers.google.com/chart/interactive/docs/customizing_axes#Discrete_vs_Continuous
Help! My chart has gone wonky!
My domain axis type is not string but I still want a discrete domain axis:
and this makes you terribly upset, then you can do one of the
following:
Change the type of your first data table column to string.
Use a DataView as adapter to convert the type of your first data table
column to string:
So the solution to the above chart was to add:
//Create a DataView from the data_table
var dataView = new google.visualization.DataView(data_table);
//Set the first column of the dataview to format as a string, and return the other columns [1, 2 and 3]
dataView.setColumns([{calc: function(data, row) { return data.getFormattedValue(row, 0); }, type:'string'}, 1, 2, 3]);
So the whole function became:
function drawVisualization() {
var data_table = new google.visualization.DataTable();
data_table.addColumn({"type": "date","label": "Date"});
data_table.addColumn({"type": "number","label": "A"});
data_table.addColumn({"type": "number","label": "B"});
data_table.addColumn({"type": "number","label": "C"});
data_table.addRow([{v: new Date(2013, 5, 26)}, {v: 1}, {v: 0}, {v: 0}]);
data_table.addRow([{v: new Date(2013, 5, 27)}, {v: 2}, {v: 1}, {v: 0.5}]);
//Create a DataView from the data_table
var dataView = new google.visualization.DataView(data_table);
//Set the first column of the dataview to format as a string, and return the other columns [1, 2 and 3]
dataView.setColumns([{calc: function(data, row) { return data.getFormattedValue(row, 0); }, type:'string'}, 1, 2, 3]);
var chart = new google.visualization.ColumnChart(document.getElementById('visualization'));
chart.draw(dataView, {
legend: "bottom"
});
}