mempool/frontend/src/app/components/difficulty-chart/difficulty-chart.component.ts

133 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Component, Inject, LOCALE_ID, OnInit } from '@angular/core';
2022-02-16 21:20:28 +09:00
import { EChartsOption } from 'echarts';
import { Observable } from 'rxjs';
import { map, share, startWith, switchMap, tap } from 'rxjs/operators';
2022-02-16 21:20:28 +09:00
import { ApiService } from 'src/app/services/api.service';
import { SeoService } from 'src/app/services/seo.service';
import { formatNumber } from '@angular/common';
import { FormBuilder, FormGroup } from '@angular/forms';
2022-02-16 21:20:28 +09:00
@Component({
selector: 'app-difficulty-chart',
templateUrl: './difficulty-chart.component.html',
styleUrls: ['./difficulty-chart.component.scss'],
styles: [`
.loadingGraphs {
position: absolute;
top: 38%;
left: calc(50% - 15px);
z-index: 100;
}
`],
})
export class DifficultyChartComponent implements OnInit {
radioGroupForm: FormGroup;
2022-02-16 21:20:28 +09:00
chartOptions: EChartsOption = {};
chartInitOptions = {
renderer: 'svg'
};
difficultyObservable$: Observable<any>;
isLoading = true;
formatNumber = formatNumber;
2022-02-16 21:20:28 +09:00
constructor(
@Inject(LOCALE_ID) public locale: string,
2022-02-16 21:20:28 +09:00
private seoService: SeoService,
private apiService: ApiService,
private formBuilder: FormBuilder,
2022-02-16 21:20:28 +09:00
) {
this.seoService.setTitle($localize`:@@mining.difficulty:Difficulty`);
this.radioGroupForm = this.formBuilder.group({ dateSpan: '1y' });
this.radioGroupForm.controls.dateSpan.setValue('1y');
2022-02-16 21:20:28 +09:00
}
ngOnInit(): void {
this.difficultyObservable$ = this.radioGroupForm.get('dateSpan').valueChanges
2022-02-16 21:20:28 +09:00
.pipe(
startWith('1y'),
switchMap((timespan) => {
return this.apiService.getHistoricalDifficulty$(timespan)
.pipe(
tap(data => {
this.prepareChartOptions(data.adjustments.map(val => [val.timestamp * 1000, val.difficulty]));
this.isLoading = false;
}),
map(data => {
const availableTimespanDay = (
(new Date().getTime() / 1000) - (data.oldestIndexedBlockTimestamp / 1000)
) / 3600 / 24;
const tableData = [];
for (let i = 0; i < data.adjustments.length - 1; ++i) {
const change = (data.adjustments[i].difficulty / data.adjustments[i + 1].difficulty - 1) * 100;
tableData.push(Object.assign(data.adjustments[i], {
change: change
}));
}
return {
availableTimespanDay: availableTimespanDay,
data: tableData
};
}),
);
}),
share()
);
2022-02-16 21:20:28 +09:00
}
prepareChartOptions(data) {
this.chartOptions = {
title: {
text: $localize`:@@mining.difficulty:Difficulty`,
left: 'center',
textStyle: {
color: '#FFF',
},
},
tooltip: {
show: true,
trigger: 'axis',
},
axisPointer: {
type: 'line',
},
xAxis: [
{
type: 'time',
}
],
yAxis: {
type: 'value',
axisLabel: {
fontSize: 11,
formatter: (val) => {
2022-02-16 21:20:28 +09:00
const diff = val / Math.pow(10, 12); // terra
return diff.toString() + 'T';
}
},
splitLine: {
lineStyle: {
type: 'dotted',
color: '#ffffff66',
opacity: 0.25,
}
}
},
series: [
{
data: data,
type: 'line',
smooth: false,
lineStyle: {
width: 3,
},
areaStyle: {}
},
],
};
}
}