Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { NewsService } from './../../../services/news.service';
- import { Router } from '@angular/router';
- import { Stock } from './../../../classes/stock';
- import { SharedMemoryService } from './../../../services/sharedMemory.service';
- import { Component, SimpleChanges, OnChanges, Input, HostListener, AfterViewInit, Renderer2, ViewChild, OnInit } from '@angular/core';
- import { clone, reverse, chunk, max, sortBy } from 'lodash';
- import { StockAnalysisService } from './stock-analysis.service';
- import { AppStorageService } from '../../../services/appStorage.service';
- import { StockService } from '../stock.service';
- declare const google: any;
- @Component({
- selector: 'app-stock-analysis',
- templateUrl: './stock-analysis.component.html',
- styleUrls: ['./stock-analysis.component.css'],
- providers: []
- })
- export class StockAnalysisComponent implements OnChanges, AfterViewInit, OnInit {
- @Input() chartData: any;
- @Input() canDraw: boolean;
- @Input() stock: Stock;
- dataTable: any[];
- timeFrame: any[];
- @ViewChild('periodSort') periodSort: Element;
- period = 'Y';
- days = 1000;
- factor: number;
- dataTableLength = 0;
- timeFrameLength: number;
- preStockData = []; // holds all of the detailed data of the graph. each click on grph point will have its details.
- chosenGraphPoint;
- top4Indexes = [];
- tfCopy;
- firstTimerCounter = 0;
- constructor(
- private sharedMemoryService: SharedMemoryService,
- private renderer: Renderer2,
- private router: Router,
- private newsService: NewsService,
- private stockAnalysisService: StockAnalysisService,
- private appStorageService: AppStorageService,
- private stockService: StockService
- ) {
- google.charts.load('current', { packages: ['line', 'bar'] });
- google.charts.setOnLoadCallback(this.drawBasic.bind(this));
- }
- // this.stockAnalysisService.getOptionsY(); has the same hAxis as the rest of the getOptions (W\M)
- @HostListener('window:resize', ['$event']) onResize(event) {
- if (event.target['innerWidth'] < 420) {
- this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MMM';
- } else if (event.target['innerWidth'] < 760) {
- this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MM. yy\'';
- } else { this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MMM d, yyyy'; }
- this.drawBasic();
- }
- ngOnInit() {
- this.stockAnalysisService.changeViewListener()
- .subscribe(({ period: period, length: section }) => {
- this.changeView(period, section);
- });
- }
- ngOnChanges(simpleChanges: SimpleChanges) {
- // first initial draw (only to present graph first with no data):
- // data.addColumn({ type: 'string', role: 'annotation' }); // annotation role col.
- if (this.canDraw) {
- this.chartData = reverse(this.chartData); // to present on axis line the last week \ month
- const data = new google.visualization.DataTable();
- data.addColumn('date', 'Date');
- data.addColumn('number', 'Stock');
- const chart = new google.visualization.LineChart(document.getElementById('line_chart'));
- chart.draw(data, this.stockAnalysisService.getOptionsY());
- this.dataTable = this.sharedMemoryService.getToolTipData();
- this.fetchStockDaily(); // fetch and manipulate needed data for stock daily per point on graph.
- this.appStorageService.saveStockHistory(this.dataTable);
- this.changeView('M', -1);
- }
- }
- ngAfterViewInit() {
- // document.getElementById('line_chart').style.opacity = '1';
- const periodChildren = this.periodSort['nativeElement'].children;
- this.renderer.addClass(periodChildren[1], 'active');
- // this.onResize({ target: { innerWidth: window.innerWidth } });
- // this.changeToMonth();
- }
- changeToMonth() {
- setTimeout(() => {
- if (this.canDraw) {
- this.changeView('M', -1);
- } else { this.changeToMonth(); }
- }, 100);
- }
- resetAnnotations() { // used to clean all annotations from changeView to changeView ('M' to 'Y' for example)
- // const length = this.factor + this.dataTable['length'];
- const length = this.chartData['length'];
- for (let i = 0; i < length; i++) {
- this.chartData[i][2] = null;
- }
- // for (let i = 0; i < length; i++) {
- // if (this.chartData[i]['length'] === 3) {
- // this.chartData[i][2] = null;
- // } else {
- // this.chartData[i].push(null);
- // }
- // }
- }
- drawBasic() {
- // score - opponent - outcome
- if (this.canDraw) {
- const data = new google.visualization.DataTable();
- data.addColumn('date', 'Date');
- data.addColumn('number', 'Stock');
- data.addColumn({ type: 'string', role: 'annotation' }); // annotation role col.
- const chart = new google.visualization.LineChart(document.getElementById('line_chart'));
- // getting all 'x' highest Abs stock values (* I cannot sort since there are alot of same EXACT values, index is best solution.)
- this.resetAnnotations(); // so data from 'M' wont affect 'Y' because it still remains after changeView.
- // #### START OF TOP 4 ALGORITHM
- let indexCounter = 0;
- let stockValuesIndex = [];
- const tempStockValues = [];
- const tempStockValuesIndexes = [];
- for (let i = 1; i < this.dataTable['length']; i++) {
- const currentIndexValue = this.dataTable[i][1];
- const beforeIndexValue = this.dataTable[i - 1][1];
- const totalDifference = currentIndexValue - beforeIndexValue;
- if (currentIndexValue - beforeIndexValue > 0) {
- // tempStockValues.push(this.dataTable[i][1]);
- tempStockValues.push(totalDifference);
- tempStockValuesIndexes.push(this.factor + i);
- } else if (currentIndexValue - beforeIndexValue < 0) {
- tempStockValues.push(Math.abs(totalDifference));
- tempStockValuesIndexes.push(this.factor + i);
- }
- }
- let top4 = [];
- if (tempStockValues.length > 4) {
- for (let i = 0; i < 4; i++) {
- const currentMaxValue = max(tempStockValues); // find max value
- // delete current max value so we wont find duplicate max values:
- const currentMaxValueIndex = tempStockValues.indexOf(currentMaxValue);
- top4.push(tempStockValuesIndexes[currentMaxValueIndex]); // add max value
- tempStockValues.splice(currentMaxValueIndex, 1);
- tempStockValuesIndexes.splice(currentMaxValueIndex, 1);
- }
- } else {
- top4 = tempStockValuesIndexes;
- }
- stockValuesIndex = sortBy(top4);
- this.top4Indexes = stockValuesIndex;
- // #### END OF TOP 4 ALGORITHM
- let phrase = ''; // this will contain for each annotation its concatonated text for switch case statement.
- // const dtLength = this.dataTable['length'] - 1;
- for (let i = 0; i < this.dataTableLength; i++) {
- // const dTable = this.dataTable[i];
- // const outrights = this.dataTable[i][2]['outrights'];
- if (i === this.dataTableLength - 1) { // to handle last point
- //
- } else {
- if (this.dataTable[i][2]['games']) { // if there was a game on that date
- const game = this.dataTable[i][2]['games'][0];
- // for Factor 1 and for later use.
- const score = game['score'];
- const opponent = game['opponent'];
- const outcome = game['outcome'];
- // if league (in our case only EPL)
- if (game['competition'] === 'EPL') {
- // phrase = outcome + ' ' + game['competition'] + ' game';
- phrase = 'E';
- } else { // FA, EFL, CL and EL
- for (let t = 0; t < this.dataTable[i][2]['outrights']['length'] - 1; t++) {
- if (this.dataTable[i][2]['outrights'][t]['competition'] === 'premier-league') { // this means cell '0' is also premiere league
- if (this.dataTable[i + 1][2]['outrights'][t]['fulfilled'] === 1 &&
- typeof (this.dataTable[i][2]['outrights'][t]['fulfilled'] === 'undefined')) { // if league outrights was achieved
- // TODO: if there are more then 1 achievement, we should decide top achievement
- // phrase = `Secured ${this.dataTable[i][2]['outrights'][i]['competition']} ${this.dataTable[i][2]['outrights'][i]['outright']}`;
- phrase = 'E';
- break;
- } else if (this.dataTable[i + 1][2]['outrights'][t]['fulfilled'] === 0 &&
- typeof (this.dataTable[i][2]['outrights'][t]['fulfilled'] === 'undefined')) { // if league outrights was lost
- // phrase = `Lost ${this.dataTable[i][2]['outrights'][i]['competition']} ${this.dataTable[i][2]['outrights'][i]['outright']}`;
- phrase = 'E';
- break;
- } else {
- if (i === this.dataTable[i][2]['outrights']['length'] - 1) { // we want it only once. ???
- // phrase = outcome + ' ' + game['competition'] + ' game';
- phrase = 'E';
- break;
- }
- }
- }
- }
- }
- } else { // if there was no game
- for (let j = 0; j < this.dataTable[i][2]['outrights']['length']; j++) {
- if (this.dataTable[i + 1][2]['outrights'][j]['fulfilled'] === 1 &&
- typeof (this.dataTable[i][2]['outrights'][j]['fulfilled'] === 'undefined')) { // if league outrights was achieved
- // TODO: if there are more then 1 achievement, we should decide top achievement
- // phrase = `Qualified for ${this.dataTable[i][2]['outrights'][i]['competition']} NA`;
- phrase = 'E';
- break;
- } else if (this.dataTable[i + 1][2]['outrights'][j]['fulfilled'] === 0 &&
- typeof (this.dataTable[i][2]['outrights'][j]['fulfilled'] === 'undefined')) { // if league outrights was lost
- // phrase = `Relegation from ${this.dataTable[i][2]['outrights'][i]['competition']} NA`;
- phrase = 'E';
- break;
- } else { // for now we do not implement 'league outright was changed'
- let totalOutrightsSum = 0;
- for (let k = 0; k < this.dataTable[i][2]['outrights']['length'] - 1; k++) {
- totalOutrightsSum += parseFloat(this.dataTable[i][2]['outrights'][k]['impact']);
- }
- let direction = '';
- if (totalOutrightsSum > 0) {
- direction = 'Improved';
- } else if (totalOutrightsSum < 0) {
- direction = 'Worsened';
- }
- // phrase = `Expectations ${direction}`;
- phrase = 'E';
- break;
- }
- }
- }
- }
- switch (this.period) {
- case 'W': {
- if (this.factor + i === stockValuesIndex[indexCounter]) {
- this.chartData[this.factor + i][2] = phrase;
- indexCounter++;
- }
- break;
- }
- case 'M': {
- if (this.factor + i === stockValuesIndex[indexCounter]) {
- // this.chartData[this.factor + i][1] = tempStockValues[stockValuesIndex[indexCounter]];
- this.chartData[this.factor + i][2] = phrase;
- indexCounter++;
- }
- break;
- }
- case 'Y': {
- if (this.factor + i === stockValuesIndex[indexCounter]) {
- this.chartData[this.factor + i][2] = phrase;
- indexCounter++;
- }
- break;
- }
- }
- }
- const timeFrame = chunk(this.chartData, this.days);
- const length = this.timeFrameLength;
- data.addRows((timeFrame[length]));
- // set listener
- this.selectListener(chart, data);
- // this.readyListener(chart, data);
- chart.draw(data, this.stockAnalysisService.getOptionsW());
- // this.chosenGraphPoint = this.preStockData[this.factor + this.dataTable['length'] - 1];
- // console.log(chart.getSelection())
- // responsible to auto select a default graph point per each period. (last event)
- let topIndex: number;
- if (typeof (this.top4Indexes[3]) !== 'undefined') {
- topIndex = this.top4Indexes[3];
- } else if (typeof (this.top4Indexes[2]) !== 'undefined') {
- topIndex = this.top4Indexes[2];
- } else if (typeof (this.top4Indexes[1]) !== 'undefined') {
- topIndex = this.top4Indexes[1];
- } else if (typeof (this.top4Indexes[0]) !== 'undefined') {
- topIndex = this.top4Indexes[0];
- }
- chart.setSelection([{ row: topIndex - this.factor }]);
- this.chosenGraphPoint = this.preStockData[this.factor + chart.getSelection()[0]['row']];
- }
- }
- changeView(_period, section: number): void {
- if (this.canDraw) {
- // emit yield change:
- if (_period === 'Y') { // TODO: make 'Y' as an 'S' in the app?
- this.stockService.yieldNotifyer().next('S');
- } else {
- this.stockService.yieldNotifyer().next(_period);
- }
- // if (_period === 'W' && this.timeFrame[0] === 30) { // chosen 'W' after 'M' was selected.
- // const timeFrameCopy = this.timeFrame;
- // const dataTableLengthCopy = this.dataTableLength;
- // }
- let arr = clone(this.dataTable);
- arr = (arr);
- let days = 0;
- let index = 0;
- switch (_period) {
- case 'W': {
- days = 7;
- index = 0;
- this.period = 'W';
- break;
- }
- case 'M': { days = 30; index = 1; this.period = 'M'; break; }
- case 'Y': { days = this.dataTable['length']; index = 2; this.period = 'Y'; break; }
- }
- this.days = days;
- // responsible for 'W' 'M' 'Y' selection
- const periodChildren = this.periodSort['nativeElement'].children;
- for (let i = 0; i < periodChildren['length']; i++) {
- this.renderer.removeClass(periodChildren[i], 'active');
- }
- this.renderer.addClass(periodChildren[index], 'active');
- this.timeFrame = chunk(arr, days); // chunks of days according to period
- if (_period === 'M') {
- this.tfCopy = this.timeFrame['length'];
- } else if (_period === 'W' && this.firstTimerCounter > 0) {
- this.tfCopy = this.timeFrame['length'];
- }
- const dataCopy = clone(this.dataTable);
- // case for aggregation of week with month
- let monthDifference;
- let weeksInTotal;
- if (_period === 'W' && this.firstTimerCounter === 0) { // chosen 'W' after 'M' was selected.
- monthDifference = (this.tfCopy - this.timeFrameLength) - 1; // converting months to weeks difference.
- weeksInTotal = monthDifference * 4;
- this.firstTimerCounter++;
- } else if (_period === 'W' && this.firstTimerCounter > 0) {
- weeksInTotal = 0;
- }
- if (section === -1) {
- this.timeFrameLength = this.timeFrame['length'] - 1;
- } else {
- this.timeFrameLength = section;
- }
- this.stockAnalysisService.setTimeFrameLength(this.timeFrameLength);
- if (_period === 'W') {
- this.timeFrameLength -= weeksInTotal;
- }
- this.dataTable = (this.timeFrame[this.timeFrameLength]);
- this.factor = this.timeFrameLength * days; // not for use yet.
- this.dataTableLength = this.timeFrame[this.timeFrameLength]['length']; // used to track previous length of dataTable for onResize()
- this.showProperAxis();
- this.drawBasic();
- this.dataTable = dataCopy; // return to original full array ('Y' aka season)
- }
- }
- showProperAxis() {
- if (window.innerWidth < 420) {
- this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MMM';
- } else if (window.innerWidth < 760) {
- this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MM. yy\'';
- } else { this.stockAnalysisService.getOptionsY()['hAxis']['format'] = 'MMM d, yyyy'; }
- }
- selectListener(chart, data) {
- google.visualization.events.addListener(chart, 'select', (e) => {
- try {
- const stringDateArray1 = new Date(data.getValue(chart.getSelection()[0]['row'], 0)).toLocaleString().split(',')[0].split('/');
- const stringDateArray2 = new Date(data.getValue(chart.getSelection()[0]['row'], 0)).toLocaleString().split(',')[0].split('.');
- let stringDateArray: any;
- if (stringDateArray1.length > 1) {
- stringDateArray = stringDateArray1;
- } else { stringDateArray = stringDateArray2; }
- if (stringDateArray[0].length === 1) {
- stringDateArray[0] = '0' + stringDateArray[0];
- }
- if (stringDateArray[1]['length'] === 1) {
- stringDateArray[1] = '0' + stringDateArray[1];
- }
- const stringDate = stringDateArray[2] + '-' + stringDateArray[0] + '-' + stringDateArray[1];
- console.log(data.getValue(chart.getSelection()[0]['row'], 2));
- this.newsService.getNewsForTeamByDate(this.stock['id'].toString(), stringDate)
- .then(res => console.log(res));
- console.log(chart.getSelection()[0]['row']);
- this.chosenGraphPoint = this.preStockData[this.factor + chart.getSelection()[0]['row']];
- console.log(this.chosenGraphPoint);
- console.log(this.factor + chart.getSelection()[0]['row']);
- } catch (e) { }
- });
- }
- readyListener(chart, data) {
- // TODO: only in case of 'Y' condition later? or case 'letter' is enough...
- // if (this.period === 'Y') {
- // Array.prototype.forEach.call(document.getElementById('line_chart').getElementsByTagName('text'), (text, index) => {
- // for (let i = 0; i < data.getNumberOfRows(); i++) {
- // if ((text.getAttribute('text-anchor') === 'middle')) {
- // switch (text.innerHTML) {
- // case 'W':
- // text.parentNode.firstElementChild.attributes[8].value = '#7CFC00';
- // break;
- // case 'L':
- // text.parentNode.firstElementChild.attributes[8].value = '#FF0000';
- // break;
- // case 'D':
- // text.parentNode.firstElementChild.attributes[8].value = '#D3D3D3';
- // break;
- // }
- // }
- // }
- // });
- // } else if (this.period === 'W') {
- // Array.prototype.forEach.call(document.getElementById('line_chart').getElementsByTagName('text'), (text, index) => {
- // for (let i = 0; i < data.getNumberOfRows(); i++) {
- // if ((text.getAttribute('text-anchor') === 'middle')) {
- // // const score = text.innerHTML.split(' ');
- // // const newScore = score[0].substring(1, score[0].length - 2);
- // // const isolatedScore = newScore.split(':');
- // switch (text.innerHTML.split(' ')[2]) {
- // case 'W':
- // text.parentNode.firstElementChild.attributes[8].value = '#7CFC00';
- // break;
- // case 'L':
- // text.parentNode.firstElementChild.attributes[8].value = '#FF0000';
- // break;
- // case 'D':
- // text.parentNode.firstElementChild.attributes[8].value = '#D3D3D3';
- // break;
- // }
- // }
- // }
- // });
- // } else if (this.period === 'M') {
- // Array.prototype.forEach.call(document.getElementById('line_chart').getElementsByTagName('text'), (text, index) => {
- // for (let i = 0; i < data.getNumberOfRows(); i++) {
- // if ((text.getAttribute('text-anchor') === 'middle')) {
- // switch (text.innerHTML.split(' ')[2]) {
- // case 'W':
- // text.parentNode.firstElementChild.attributes[8].value = '#7CFC00';
- // text.innerHTML = text.innerHTML.substring(0, text.innerHTML.length - 2);
- // break;
- // case 'L':
- // text.parentNode.firstElementChild.attributes[8].value = '#FF0000';
- // text.innerHTML = text.innerHTML.substring(0, text.innerHTML.length - 2);
- // break;
- // case 'D':
- // text.parentNode.firstElementChild.attributes[8].value = '#D3D3D3';
- // text.innerHTML = text.innerHTML.substring(0, text.innerHTML.length - 2);
- // break;
- // }
- // }
- // }
- // });
- // }
- // });
- }
- fetchStockDaily() {
- const dt = reverse(this.dataTable);
- const dtArr = []; // will contain per each day its full data. array of stockItem
- const dtLength = dt['length'];
- for (let i = 0; i < dtLength; i++) { // TODO: solve 1 last missing index
- const stockItem = {};
- if (i === 0) {
- stockItem['date'] = dt[i][0];
- stockItem['stockValue'] = (dt[i][1]).toFixed(2);
- stockItem['dailyYield'] = '0.00%';
- stockItem['changeReason'] = [{ phrase: '', impact: 0.00 }];
- dtArr.push(stockItem);
- } else if (i === dt['length'] - 1) {
- stockItem['date'] = dt[i][0];
- stockItem['stockValue'] = (dt[i][1]).toFixed(2);
- let dailyYield;
- let yieldSign = '';
- if (dt[i][1] - dt[i - 1][1] > 0) {
- yieldSign = '+';
- }
- const fixedYield = (dt[i][1] - dt[i - 1][1]).toFixed(2);
- dailyYield = yieldSign + (fixedYield) + '%';
- stockItem['dailyYield'] = dailyYield;
- // change reason:
- const changeReason = [];
- const game = dt[i][2]['games'];
- if (game) {
- let outcome = game[0]['outcome'];
- if (outcome === 'W') {
- outcome = 'WON';
- } else if (outcome === 'L') {
- outcome = 'LOST';
- } else if (outcome === 'D') { outcome = 'DRAW'; }
- const competition = game[0]['competition'];
- const score = game[0]['score'];
- const opponent = game[0]['opponent'];
- const phrase = outcome + ' ' + competition + ' game by ' + score + ' against ' + opponent;
- const impact = game[0]['impact'];
- changeReason.push({ phrase: phrase, impact: impact });
- }
- stockItem['changeReason'] = changeReason;
- dtArr.push(stockItem);
- } else {
- // ###HEADER###
- stockItem['date'] = dt[i][0];
- stockItem['stockValue'] = (dt[i][1]).toFixed(2);
- let dailyYield;
- let yieldSign = '';
- if (dt[i][1] - dt[i - 1][1] > 0) {
- yieldSign = '+';
- }
- const fixedYield = (dt[i][1] - dt[i - 1][1]).toFixed(2);
- dailyYield = yieldSign + (fixedYield) + '%';
- stockItem['dailyYield'] = dailyYield;
- // ###CHANGE REASON###
- const changeReason = [];
- // Event outcome:
- const game = dt[i][2]['games'];
- if (game) {
- let outcome = game[0]['outcome'];
- if (outcome === 'W') {
- outcome = 'WON';
- } else if (outcome === 'L') {
- outcome = 'LOST';
- } else if (outcome === 'D') { outcome = 'DRAW'; }
- const competition = game[0]['competition'];
- const score = game[0]['score'];
- const opponent = game[0]['opponent'];
- const phrase = outcome + ' ' + competition + ' game by ' + score + ' against ' + opponent;
- const impact = parseFloat(game[0]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- const outrights = dt[i][2]['outrights'];
- // Outright changed:
- for (let j = 0; j < outrights['length']; j++) {
- if (parseFloat(outrights[j]['impact']) !== 0 && (typeof (outrights[j]['fulfilled']) === 'undefined')) {
- const competition = outrights[j]['competition'];
- const outright = outrights[j]['outright'];
- let direction;
- let phrase = '';
- if (parseFloat(outrights[j]['impact']) > 0) {
- direction = 'better';
- phrase = 'Expectations for the ' + competition + ' ' + outright + ' got ' + direction;
- } else if (parseFloat(outrights[j]['impact']) < 0) {
- direction = 'worse';
- phrase = 'Expectations for the ' + competition + ' ' + outright + ' got ' + direction;
- }
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- }
- // Outright achieved: League + Outright obsolete: League + Outright obsolete: League relegated
- for (let j = 0; j < outrights['length']; j++) {
- if (i > 0) {
- if (outrights[j]['competition'] === 'premier-league' &&
- (parseFloat(dt[i][2]['outrights'][j]['fulfilled']) === 1 &&
- (typeof (dt[i - 1][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // achieved
- if (outrights[j]['outright'] === 'to-stay-up') {
- const competition = outrights[j]['competition'];
- const phrase = 'Relegated from the ' + competition;
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- } else {
- const competition = outrights[j]['competition'];
- const outright = outrights[j]['outright'];
- const phrase = 'Secured ' + competition + ' ' + outright + ' ranking';
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- } else if (outrights[j]['competition'] === 'premier-league' &&
- (parseFloat(dt[i][2]['outrights'][j]['fulfilled']) === 0 &&
- (typeof (dt[i - 1][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // obsolete
- const competition = dt[i][2]['outrights'][j]['competition'];
- const outright = dt[i][2]['outrights'][j]['outright'];
- const phrase = competition + ' ' + outright + ' ranking was not secured';
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- } else {
- if (outrights[j]['competition'] === 'premier-league' &&
- (parseFloat(dt[i + 1][2]['outrights'][j]['fulfilled']) === 1 &&
- (typeof (dt[i][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // achieved
- if (outrights[j]['outright'] === 'to-stay-up') {
- const competition = outrights[j]['competition'];
- const phrase = 'Relegated from the ' + competition;
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- } else {
- const competition = outrights[j]['competition'];
- const outright = outrights[j]['outright'];
- const phrase = 'Secured ' + competition + ' ' + outright + ' ranking';
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- } else if (outrights[j]['competition'] === 'premier-league' &&
- (parseFloat(dt[i + 1][2]['outrights'][j]['fulfilled']) === 0 &&
- (typeof (dt[i][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // obsolete
- const competition = dt[i][2]['outrights'][j]['competition'];
- const outright = dt[i][2]['outrights'][j]['outright'];
- const phrase = competition + ' ' + outright + ' ranking was not secured';
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- }
- }
- // Outright achieved: Knockout stage + Outright obsolete: Cup (group or knockout)
- for (let j = 0; j < outrights['length']; j++) {
- if (i > 0) {
- if (outrights[j]['competition'] !== 'premier-league' &&
- (parseFloat(dt[i][2]['outrights'][j]['fulfilled']) === 1 &&
- (typeof (dt[i - 1][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // achieved
- const competition = outrights[j]['competition'];
- const phrase = 'Qualified for the ' + competition + ' knockout phase';
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- } else if (outrights[j]['competition'] !== 'premier-league' &&
- (parseFloat(dt[i][2]['outrights'][j]['fulfilled']) === 0 &&
- (typeof (dt[i - 1][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // obsolete
- const competition = outrights[j]['competition'];
- const phrase = 'Knocked-out of the ' + competition;
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- } else {
- if (outrights[j]['competition'] !== 'premier-league' &&
- (parseFloat(dt[i + 1][2]['outrights'][j]['fulfilled']) === 1 &&
- (typeof (dt[i][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // achieved
- const competition = outrights[j]['competition'];
- const phrase = 'Qualified for the ' + competition + ' knockout phase';
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- } else if (outrights[j]['competition'] !== 'premier-league' &&
- (parseFloat(dt[i + 1][2]['outrights'][j]['fulfilled']) === 0 &&
- (typeof (dt[i][2]['outrights'][j]['fulfilled']) === 'undefined'))) { // obsolete
- const competition = outrights[j]['competition'];
- const phrase = 'Knocked-out of the ' + competition;
- const impact = parseFloat(outrights[j]['impact']);
- let color: string;
- if (impact > 0) {
- color = 'green';
- } else if (impact < 0) {
- color = 'red';
- }
- changeReason.push({ phrase: phrase, impact: impact, color: color });
- }
- }
- }
- // adding all of the data of changeReason for specific item:
- stockItem['changeReason'] = changeReason;
- dtArr.push(stockItem);
- }
- // adding all data of the iterate as a stockItem:
- }
- this.preStockData = (dtArr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement