Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm attempting to use Angular-Chart in my AngularJS app.

From the documentation, I see that you can define Labels, Series and Data like this:

$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];

$scope.data = [
  [65, 59, 80, 81, 56, 55, 40],
  [28, 48, 40, 19, 86, 27, 90]
];

My problem is that in my case, the labels are key values from objects, and series and data are value and property from such objects.

For instance, given the following example:

enter image description here

Labels would be: ['324', '369', '391', '396', 'KE-22'] (Key from all objects in Cars)

Series would be: ['Trips','Total'] (Value from Cars)

And Data would be (Property from Cars):

[
   ['1','4','5','3','7'],
   ['00:00:08','00:00:13','00:00:24','00:00:03','00:00:01']
]

enter image description here

Is there some way I can create such arrays?

Right now I create the objects using LoDash, like so:

dataByMonth = _.mapValues(dataByMonth, function(month) {
    var obj = {};
    var carArray = [];
    carArray = _.groupBy(month, 'car');
    obj.Cars = _.groupBy(month, 'car');

    obj.Drivers = _.groupBy(month, 'driver');

    _.each(obj, function(groupsValue, groupKey) {
        obj[groupKey] = _.mapValues(groupsValue, function(groupValue) {
            return _.reduce(groupValue, function(sum, trip) {
                sum['trips']++;
                sum['duration']+= moment.utc(trip.duration, 'HH:mm:ss');
                sum['total'] = moment.utc(sum.duration). format('HH:mm:ss')
                //addDuration(sum.duration, car.duration); 
                return sum;
            }, {trips: 0, duration: 0, total:0})
        });
    })

    return obj;
});

$scope.statistics = dataByMonth;
share|improve this question
    
I didnt understand from your example what the input is and what the expected output is. Can you provide a sample input data and what youre trying to achieve? Does order in the arrays matter? –  Etai Nov 27 '14 at 18:28
    
Hi @Etai! The input is the object containing objects in the image (created by the last piece of code of the question). The output need to be 3 different arrays (labels, series & data) to populate a chart. The order in the arrays only matter for Series and data, as series-1 (Trips) will be showing the first array in Data and series-2 (Total) will be showing the second array in Data. I will update the question later with more diagrams to make it more clear anyway! –  Eric Mitjans Nov 28 '14 at 10:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.