2022-02-19 22:09:35 +09:00
|
|
|
import { Component, Inject, Input, LOCALE_ID, OnInit } from '@angular/core';
|
2022-02-21 23:36:05 +09:00
|
|
|
import { EChartsOption, graphic } from 'echarts';
|
2022-02-19 22:09:35 +09:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { map, share, startWith, switchMap, tap } from 'rxjs/operators';
|
|
|
|
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-21 15:55:27 +09:00
|
|
|
import { selectPowerOfTen } from 'src/app/bitcoin.utils';
|
2022-02-18 15:26:15 +09:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-hashrate-chart',
|
|
|
|
templateUrl: './hashrate-chart.component.html',
|
2022-02-19 22:09:35 +09:00
|
|
|
styleUrls: ['./hashrate-chart.component.scss'],
|
|
|
|
styles: [`
|
|
|
|
.loadingGraphs {
|
|
|
|
position: absolute;
|
|
|
|
top: 38%;
|
|
|
|
left: calc(50% - 15px);
|
|
|
|
z-index: 100;
|
|
|
|
}
|
|
|
|
`],
|
2022-02-18 15:26:15 +09:00
|
|
|
})
|
|
|
|
export class HashrateChartComponent implements OnInit {
|
2022-02-19 22:09:35 +09:00
|
|
|
@Input() widget: boolean = false;
|
2022-02-22 15:50:14 +09:00
|
|
|
@Input() right: number | string = 45;
|
2022-02-21 14:02:41 +09:00
|
|
|
@Input() left: number | string = 75;
|
2022-02-18 15:26:15 +09:00
|
|
|
|
2022-02-19 22:09:35 +09:00
|
|
|
radioGroupForm: FormGroup;
|
|
|
|
|
|
|
|
chartOptions: EChartsOption = {};
|
|
|
|
chartInitOptions = {
|
2022-02-21 14:02:41 +09:00
|
|
|
renderer: 'svg',
|
|
|
|
width: 'auto',
|
|
|
|
height: 'auto',
|
2022-02-19 22:09:35 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
hashrateObservable$: Observable<any>;
|
|
|
|
isLoading = true;
|
|
|
|
formatNumber = formatNumber;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(LOCALE_ID) public locale: string,
|
|
|
|
private seoService: SeoService,
|
|
|
|
private apiService: ApiService,
|
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
) {
|
2022-02-22 15:50:14 +09:00
|
|
|
this.seoService.setTitle($localize`:@@mining.hashrate-difficulty:Hashrate and Difficulty`);
|
2022-02-19 22:09:35 +09:00
|
|
|
this.radioGroupForm = this.formBuilder.group({ dateSpan: '1y' });
|
|
|
|
this.radioGroupForm.controls.dateSpan.setValue('1y');
|
|
|
|
}
|
2022-02-18 15:26:15 +09:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2022-02-19 22:09:35 +09:00
|
|
|
this.hashrateObservable$ = this.radioGroupForm.get('dateSpan').valueChanges
|
|
|
|
.pipe(
|
|
|
|
startWith('1y'),
|
|
|
|
switchMap((timespan) => {
|
|
|
|
return this.apiService.getHistoricalHashrate$(timespan)
|
|
|
|
.pipe(
|
2022-02-22 16:06:27 +09:00
|
|
|
tap((data: any) => {
|
|
|
|
// We generate duplicated data point so the tooltip works nicely
|
2022-02-22 15:50:14 +09:00
|
|
|
const diffFixed = [];
|
2022-02-22 16:06:27 +09:00
|
|
|
let diffIndex = 0;
|
2022-02-22 15:50:14 +09:00
|
|
|
let hashIndex = 0;
|
|
|
|
while (hashIndex < data.hashrates.length) {
|
|
|
|
if (diffIndex >= data.difficulty.length) {
|
|
|
|
while (hashIndex < data.hashrates.length) {
|
|
|
|
diffFixed.push({
|
|
|
|
timestamp: data.hashrates[hashIndex].timestamp,
|
|
|
|
difficulty: data.difficulty[data.difficulty.length - 1].difficulty
|
|
|
|
});
|
|
|
|
++hashIndex;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (data.hashrates[hashIndex].timestamp < data.difficulty[diffIndex].timestamp) {
|
|
|
|
diffFixed.push({
|
|
|
|
timestamp: data.hashrates[hashIndex].timestamp,
|
|
|
|
difficulty: data.difficulty[diffIndex - 1].difficulty
|
|
|
|
});
|
|
|
|
++hashIndex;
|
|
|
|
}
|
|
|
|
++diffIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prepareChartOptions({
|
|
|
|
hashrates: data.hashrates.map(val => [val.timestamp * 1000, val.avgHashrate]),
|
2022-02-22 16:06:27 +09:00
|
|
|
difficulty: diffFixed.map(val => [val.timestamp * 1000, val.difficulty])
|
2022-02-22 15:50:14 +09:00
|
|
|
});
|
2022-02-19 22:09:35 +09:00
|
|
|
this.isLoading = false;
|
|
|
|
}),
|
2022-02-22 15:50:14 +09:00
|
|
|
map((data: any) => {
|
2022-02-19 22:09:35 +09:00
|
|
|
const availableTimespanDay = (
|
2022-02-22 15:50:14 +09:00
|
|
|
(new Date().getTime() / 1000) - (data.oldestIndexedBlockTimestamp)
|
2022-02-19 22:09:35 +09:00
|
|
|
) / 3600 / 24;
|
2022-02-22 16:06:27 +09:00
|
|
|
|
|
|
|
const tableData = [];
|
|
|
|
for (let i = data.difficulty.length - 1; i > 0; --i) {
|
|
|
|
const selectedPowerOfTen: any = selectPowerOfTen(data.difficulty[i].difficulty);
|
|
|
|
const change = (data.difficulty[i].difficulty / data.difficulty[i - 1].difficulty - 1) * 100;
|
|
|
|
|
|
|
|
tableData.push(Object.assign(data.difficulty[i], {
|
|
|
|
change: change,
|
|
|
|
difficultyShorten: formatNumber(
|
|
|
|
data.difficulty[i].difficulty / selectedPowerOfTen.divider,
|
|
|
|
this.locale, '1.2-2') + selectedPowerOfTen.unit
|
|
|
|
}));
|
|
|
|
}
|
2022-02-19 22:09:35 +09:00
|
|
|
return {
|
|
|
|
availableTimespanDay: availableTimespanDay,
|
2022-02-22 16:06:27 +09:00
|
|
|
difficulty: tableData
|
2022-02-19 22:09:35 +09:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
2022-02-21 12:22:20 +09:00
|
|
|
}),
|
|
|
|
share()
|
|
|
|
);
|
2022-02-18 15:26:15 +09:00
|
|
|
}
|
|
|
|
|
2022-02-19 22:09:35 +09:00
|
|
|
prepareChartOptions(data) {
|
|
|
|
this.chartOptions = {
|
2022-02-22 15:50:14 +09:00
|
|
|
color: [
|
|
|
|
new graphic.LinearGradient(0, 0, 0, 0.65, [
|
|
|
|
{ offset: 0, color: '#F4511E' },
|
|
|
|
{ offset: 0.25, color: '#FB8C00' },
|
|
|
|
{ offset: 0.5, color: '#FFB300' },
|
|
|
|
{ offset: 0.75, color: '#FDD835' },
|
|
|
|
{ offset: 1, color: '#7CB342' }
|
|
|
|
]),
|
|
|
|
'#D81B60',
|
|
|
|
],
|
2022-02-21 14:02:41 +09:00
|
|
|
grid: {
|
|
|
|
right: this.right,
|
|
|
|
left: this.left,
|
|
|
|
},
|
2022-02-19 22:09:35 +09:00
|
|
|
tooltip: {
|
|
|
|
trigger: 'axis',
|
2022-02-22 15:50:14 +09:00
|
|
|
axisPointer: {
|
|
|
|
type: 'line'
|
|
|
|
},
|
2022-02-21 23:36:05 +09:00
|
|
|
backgroundColor: 'rgba(17, 19, 31, 1)',
|
|
|
|
borderRadius: 4,
|
|
|
|
shadowColor: 'rgba(0, 0, 0, 0.5)',
|
|
|
|
textStyle: {
|
|
|
|
color: '#b1b1b1',
|
|
|
|
},
|
|
|
|
borderColor: '#000',
|
2022-02-19 22:09:35 +09:00
|
|
|
},
|
|
|
|
xAxis: {
|
|
|
|
type: 'time',
|
|
|
|
splitNumber: this.isMobile() ? 5 : 10,
|
|
|
|
},
|
2022-02-22 15:50:14 +09:00
|
|
|
legend: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
name: 'Hashrate',
|
|
|
|
inactiveColor: 'rgb(110, 112, 121)',
|
|
|
|
textStyle: {
|
|
|
|
color: 'white',
|
|
|
|
},
|
|
|
|
icon: 'roundRect',
|
|
|
|
itemStyle: {
|
|
|
|
color: '#FFB300',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Difficulty',
|
|
|
|
inactiveColor: 'rgb(110, 112, 121)',
|
|
|
|
textStyle: {
|
|
|
|
color: 'white',
|
|
|
|
},
|
|
|
|
icon: 'roundRect',
|
|
|
|
itemStyle: {
|
|
|
|
color: '#D81B60',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
yAxis: [
|
|
|
|
{
|
|
|
|
type: 'value',
|
|
|
|
name: 'Hashrate',
|
|
|
|
axisLabel: {
|
|
|
|
color: 'rgb(110, 112, 121)',
|
|
|
|
formatter: (val) => {
|
|
|
|
const selectedPowerOfTen: any = selectPowerOfTen(val);
|
|
|
|
const newVal = val / selectedPowerOfTen.divider;
|
|
|
|
return `${newVal} ${selectedPowerOfTen.unit}H/s`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
splitLine: {
|
|
|
|
show: false,
|
2022-02-19 22:09:35 +09:00
|
|
|
}
|
|
|
|
},
|
2022-02-22 15:50:14 +09:00
|
|
|
{
|
|
|
|
type: 'value',
|
|
|
|
name: 'Difficulty',
|
|
|
|
position: 'right',
|
|
|
|
axisLabel: {
|
|
|
|
color: 'rgb(110, 112, 121)',
|
|
|
|
formatter: (val) => {
|
|
|
|
const selectedPowerOfTen: any = selectPowerOfTen(val);
|
|
|
|
const newVal = val / selectedPowerOfTen.divider;
|
|
|
|
return `${newVal} ${selectedPowerOfTen.unit}`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
splitLine: {
|
|
|
|
show: false,
|
2022-02-19 22:09:35 +09:00
|
|
|
}
|
2022-02-22 15:50:14 +09:00
|
|
|
}
|
|
|
|
],
|
|
|
|
series: [
|
|
|
|
{
|
|
|
|
name: 'Hashrate',
|
|
|
|
showSymbol: false,
|
|
|
|
data: data.hashrates,
|
|
|
|
type: 'line',
|
|
|
|
lineStyle: {
|
|
|
|
width: 2,
|
|
|
|
},
|
2022-02-19 22:09:35 +09:00
|
|
|
},
|
2022-02-22 15:50:14 +09:00
|
|
|
{
|
|
|
|
yAxisIndex: 1,
|
|
|
|
name: 'Difficulty',
|
|
|
|
showSymbol: false,
|
|
|
|
data: data.difficulty,
|
|
|
|
type: 'line',
|
|
|
|
lineStyle: {
|
|
|
|
width: 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2022-02-19 22:09:35 +09:00
|
|
|
dataZoom: this.widget ? null : [{
|
|
|
|
type: 'inside',
|
|
|
|
realtime: true,
|
|
|
|
zoomLock: true,
|
|
|
|
zoomOnMouseWheel: true,
|
|
|
|
moveOnMouseMove: true,
|
|
|
|
maxSpan: 100,
|
|
|
|
minSpan: 10,
|
|
|
|
}, {
|
|
|
|
showDetail: false,
|
|
|
|
show: true,
|
|
|
|
type: 'slider',
|
|
|
|
brushSelect: false,
|
|
|
|
realtime: true,
|
|
|
|
bottom: 0,
|
|
|
|
selectedDataBackground: {
|
|
|
|
lineStyle: {
|
|
|
|
color: '#fff',
|
|
|
|
opacity: 0.45,
|
|
|
|
},
|
|
|
|
areaStyle: {
|
|
|
|
opacity: 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
isMobile() {
|
|
|
|
return (window.innerWidth <= 767.98);
|
|
|
|
}
|
2022-02-18 15:26:15 +09:00
|
|
|
}
|