import { ChangeDetectionStrategy, Component, Inject, Input, LOCALE_ID, OnInit } from '@angular/core'; import { EChartsOption, graphic } from 'echarts'; import { Observable } from 'rxjs'; import { map, share, startWith, switchMap, tap } from 'rxjs/operators'; import { ApiService } from '../../services/api.service'; import { SeoService } from '../../services/seo.service'; import { formatCurrency, formatNumber, getCurrencySymbol } from '@angular/common'; import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { download, formatterXAxis, formatterXAxisLabel, formatterXAxisTimeCategory } from '../../shared/graphs.utils'; import { MiningService } from '../../services/mining.service'; import { StorageService } from '../../services/storage.service'; import { ActivatedRoute } from '@angular/router'; import { FiatShortenerPipe } from '../../shared/pipes/fiat-shortener.pipe'; @Component({ selector: 'app-block-rewards-graph', templateUrl: './block-rewards-graph.component.html', styleUrls: ['./block-rewards-graph.component.scss'], styles: [` .loadingGraphs { position: absolute; top: 50%; left: calc(50% - 15px); z-index: 100; } `], changeDetection: ChangeDetectionStrategy.OnPush, }) export class BlockRewardsGraphComponent implements OnInit { @Input() right: number | string = 45; @Input() left: number | string = 75; miningWindowPreference: string; radioGroupForm: UntypedFormGroup; chartOptions: EChartsOption = {}; chartInitOptions = { renderer: 'svg', }; statsObservable$: Observable; isLoading = true; formatNumber = formatNumber; timespan = ''; chartInstance: any = undefined; constructor( @Inject(LOCALE_ID) public locale: string, private seoService: SeoService, private apiService: ApiService, private formBuilder: UntypedFormBuilder, private miningService: MiningService, private storageService: StorageService, private route: ActivatedRoute, private fiatShortenerPipe: FiatShortenerPipe, ) { } ngOnInit(): void { this.seoService.setTitle($localize`:@@8ba8fe810458280a83df7fdf4c614dfc1a826445:Block Rewards`); this.miningWindowPreference = this.miningService.getDefaultTimespan('3m'); this.radioGroupForm = this.formBuilder.group({ dateSpan: this.miningWindowPreference }); this.radioGroupForm.controls.dateSpan.setValue(this.miningWindowPreference); this.route .fragment .subscribe((fragment) => { if (['3m', '6m', '1y', '2y', '3y', 'all'].indexOf(fragment) > -1) { this.radioGroupForm.controls.dateSpan.setValue(fragment, { emitEvent: false }); } }); this.statsObservable$ = this.radioGroupForm.get('dateSpan').valueChanges .pipe( startWith(this.radioGroupForm.controls.dateSpan.value), switchMap((timespan) => { this.storageService.setValue('miningWindowPreference', timespan); this.timespan = timespan; this.isLoading = true; return this.apiService.getHistoricalBlockRewards$(timespan) .pipe( tap((response) => { this.prepareChartOptions({ blockRewards: response.body.map(val => [val.timestamp * 1000, val.avgRewards / 100000000, val.avgHeight]), blockRewardsUSD: response.body.filter(val => val.USD > 0).map(val => [val.timestamp * 1000, val.avgRewards / 100000000 * val.USD, val.avgHeight]), }); this.isLoading = false; }), map((response) => { return { blockCount: parseInt(response.headers.get('x-total-count'), 10), }; }), ); }), share() ); } prepareChartOptions(data) { let title: object; if (data.blockRewards.length === 0) { title = { textStyle: { color: 'grey', fontSize: 15 }, text: $localize`:@@23555386d8af1ff73f297e89dd4af3f4689fb9dd:Indexing blocks`, left: 'center', top: 'center' }; } const scaleFactor = 0.1; this.chartOptions = { title: title, animation: false, color: [ new graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: '#FDD835' }, { offset: 1, color: '#FB8C00' }, ]), new graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: '#C0CA33' }, { offset: 1, color: '#1B5E20' }, ]), ], grid: { top: 20, bottom: 80, right: this.right, left: this.left, }, tooltip: { show: !this.isMobile(), trigger: 'axis', axisPointer: { type: 'line' }, backgroundColor: 'rgba(17, 19, 31, 1)', borderRadius: 4, shadowColor: 'rgba(0, 0, 0, 0.5)', textStyle: { color: '#b1b1b1', align: 'left', }, borderColor: '#000', formatter: function (data) { if (data.length <= 0) { return ''; } let tooltip = ` ${formatterXAxis(this.locale, this.timespan, parseInt(data[0].axisValue, 10))}
`; for (const tick of data) { if (tick.seriesIndex === 0) { tooltip += `${tick.marker} ${tick.seriesName}: ${formatNumber(tick.data[1], this.locale, '1.3-3')} BTC
`; } else if (tick.seriesIndex === 1) { tooltip += `${tick.marker} ${tick.seriesName}: ${formatCurrency(tick.data[1], this.locale, getCurrencySymbol('USD', 'narrow'), 'USD', '1.0-0')}
`; } } tooltip += `* On average around block ${data[0].data[2]}`; return tooltip; }.bind(this) }, xAxis: data.blockRewards.length === 0 ? undefined : { type: 'time', splitNumber: this.isMobile() ? 5 : 10, axisLabel: { hideOverlap: true, } }, legend: data.blockRewards.length === 0 ? undefined : { data: [ { name: 'Rewards BTC', inactiveColor: 'rgb(110, 112, 121)', textStyle: { color: 'white', }, icon: 'roundRect', }, { name: 'Rewards USD', inactiveColor: 'rgb(110, 112, 121)', textStyle: { color: 'white', }, icon: 'roundRect', }, ], }, yAxis: data.blockRewards.length === 0 ? undefined : [ { type: 'value', axisLabel: { color: 'rgb(110, 112, 121)', formatter: (val) => { return `${val} BTC`; } }, min: (value) => { return Math.round(value.min * (1.0 - scaleFactor) * 10) / 10; }, max: (value) => { return Math.round(value.max * (1.0 + scaleFactor) * 10) / 10; }, splitLine: { lineStyle: { type: 'dotted', color: '#ffffff66', opacity: 0.25, } }, }, { min: (value) => { return Math.round(value.min * (1.0 - scaleFactor) * 10) / 10; }, max: (value) => { return Math.round(value.max * (1.0 + scaleFactor) * 10) / 10; }, type: 'value', position: 'right', axisLabel: { color: 'rgb(110, 112, 121)', formatter: function(val) { return this.fiatShortenerPipe.transform(val); }.bind(this) }, splitLine: { show: false, }, }, ], series: data.blockRewards.length === 0 ? undefined : [ { legendHoverLink: false, zlevel: 0, yAxisIndex: 0, name: 'Rewards BTC', data: data.blockRewards, type: 'line', smooth: 0.25, symbol: 'none', }, { legendHoverLink: false, zlevel: 1, yAxisIndex: 1, name: 'Rewards USD', data: data.blockRewardsUSD, type: 'line', smooth: 0.25, symbol: 'none', lineStyle: { width: 2, opacity: 0.75, }, areaStyle: { opacity: 0.05, } }, ], dataZoom: data.blockRewards.length === 0 ? undefined : [{ type: 'inside', realtime: true, zoomLock: true, maxSpan: 100, minSpan: 5, moveOnMouseMove: false, }, { showDetail: false, show: true, type: 'slider', brushSelect: false, realtime: true, left: 20, right: 15, selectedDataBackground: { lineStyle: { color: '#fff', opacity: 0.45, }, areaStyle: { opacity: 0, } }, }], }; } onChartInit(ec) { this.chartInstance = ec; } isMobile() { return (window.innerWidth <= 767.98); } onSaveChart() { // @ts-ignore const prevBottom = this.chartOptions.grid.bottom; const now = new Date(); // @ts-ignore this.chartOptions.grid.bottom = 40; this.chartOptions.backgroundColor = '#11131f'; this.chartInstance.setOption(this.chartOptions); download(this.chartInstance.getDataURL({ pixelRatio: 2, excludeComponents: ['dataZoom'], }), `block-rewards-${this.timespan}-${Math.round(now.getTime() / 1000)}.svg`); // @ts-ignore this.chartOptions.grid.bottom = prevBottom; this.chartOptions.backgroundColor = 'none'; this.chartInstance.setOption(this.chartOptions); } }