-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDataChart.dart
124 lines (113 loc) · 3.68 KB
/
DataChart.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* ******************************************************************************
* * Copyright (c) 2017 Arthur Deschamps
* *
* * All rights reserved. This program and the accompanying materials
* * are made available under the terms of the Eclipse Public License v1.0
* * which accompanies this distribution, and is available at
* * http://www.eclipse.org/legal/epl-v10.html
* *
* * Contributors:
* * Arthur Deschamps
* ******************************************************************************
*/
import 'dart:async';
import 'dart:html';
import 'package:angular2/angular2.dart';
import 'package:chartjs/chartjs.dart';
import 'package:webapp_angular/src/data_services/app/company/ChartData.service.dart';
/// A Chart component displaying the evolution of the stores sizes.
@Component(
selector: 'data-chart',
styleUrls: const ["styles/dataChart.component.css"],
templateUrl: 'templates/dataChart.component.html',
directives: const [CORE_DIRECTIVES]
)
class DataChartComponent implements AfterViewInit {
final ChartDataService _chartDataService;
Chart _companyDataChart;
DataChartComponent(this._chartDataService);
@override
ngAfterViewInit() {
_chartDataService.initChartData().then((_) {
_initChart();
new Timer.periodic(new Duration(seconds: 1), (_) => _updateChart());
});
}
/// Initializes the chart.
void _initChart() {
// Initiate the chart
var data = new LinearChartData(
labels: _chartDataService.timeline,
datasets: _getDataSets()
);
var config = new ChartConfiguration(
type: "line",
data: data,
options: new ChartOptions(
title: new ChartTitleOptions(
display: true,
text: "Evolution of the company over time",
fontColor: "#e6e8ed",
fontSize: 16,
fontStyle: "normal"
),
animation: new ChartAnimationOptions(duration: 0),
responsive: true,
maintainAspectRatio: false,
scales: new LogarithmicScale(
yAxes: [new ChartYAxe(
type: "logarithmic",
scaleLabel: new ScaleTitleOptions(
display: true,
labelString: "Quantity in store"
)
)],
xAxes: [new ChartXAxe(
scaleLabel: new ScaleTitleOptions(
display: true,
labelString: "Time"
)
)]
)
)
);
_companyDataChart = new Chart(querySelector('#chart') as CanvasElement, config);
}
/// Returns all the data the shall be displayed in the chart.
///
/// Time against store size will be displayed, for each defined store (orders,
/// customers, etc).
List<ChartDataSets> _getDataSets() {
// Map of border colors for the lines
Map<String, String> colorOf = {
"customers" : "#7b93d1",
"products" : "#FF4136",
"productTypes" : "#B10DC9",
"deliveries" : "#FF851B",
"orders" : "#AAAAAA",
"transportation" : "#DDDDDD"
};
List<ChartDataSets> data = new List<ChartDataSets>();
_chartDataService.storesQuantities.forEach((name, quantities) {
String labelName = name.replaceRange(0,1,name.substring(0,1).toUpperCase());
if (labelName == "ProductTypes")
labelName = "Product Types";
data.add(new ChartDataSets(
data: quantities,
label: labelName,
borderColor: colorOf[name]
));
}
);
return data;
}
/// Updates the chart data.
void _updateChart() {
_companyDataChart.data = new LinearChartData(
labels: _chartDataService.timeline,
datasets: _getDataSets()
);
_companyDataChart.update();
}
}