/
chartView.js
181 lines (171 loc) · 4.78 KB
/
chartView.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React from 'react'
import PropTypes from 'prop-types';
import ReactChartkick, { LineChart } from 'react-chartkick'
import Chart from 'chart.js'
import * as zoom from 'chartjs-plugin-zoom'
export default class ChartView extends React.Component {
constructor(props) {
super(props);
ReactChartkick.addAdapter(Chart)
this.state = {
chartData: [],
}
this.timer = null;
this.lastsongid = "";
console.log(zoom);
}
componentDidUpdate = (prevProps) => {
if (prevProps === this.props) { return }
if (this.props.startTrack) {
if (this.timer) { return; }
//this.startCollecting()
}
if (this.props.stopTrack) {
if (this.timer) clearInterval(this.timer);
const chartData = [
{
name: "Accuracy",
data: {
},
dataset: { yAxisID: 'y-axis-1' },
},
{
name: "Notes Missed",
data: {
},
dataset: { yAxisID: 'y-axis-2' },
},
]
this.setState({ chartData })
}
}
componentWillUnmount = () => {
if (this.timer) clearInterval(this.timer);
}
getRandomInt = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
startCollecting = async () => {
const chartData = [
{
name: "Accuracy",
data: {
},
dataset: { yAxisID: 'y-axis-1' },
},
{
name: "Notes Missed",
data: {
},
dataset: { yAxisID: 'y-axis-2' },
},
]
this.setState({ chartData })
await this.DataTimer(chartData);
}
DataTimer = async (chartData) => {
if (this.timer) clearInterval(this.timer);
this.timer = setInterval(async () => {
const songData = await window.fetch("http://127.0.0.1:9938");
if (!songData) return;
if (typeof songData === 'undefined') { return; }
const jsonObj = await songData.json();
if (jsonObj.memoryReadout && jsonObj.memoryReadout.songTimer > 0) {
const { memoryReadout } = jsonObj;
if (typeof memoryReadout === 'undefined') { return; }
if (this.lastsongid !== memoryReadout.songID) {
this.lastsongid = memoryReadout.songID;
chartData[0].data = {}
chartData[1].data = {}
}
const newTime = Math.round(memoryReadout.songTimer);
if (newTime >= this.props.timeTotal && this.timer) clearInterval(this.timer);
//const { minutes, seconds } = getMinutesSecs(newTime)
//const timeKey = `${minutes}:${seconds}`
const tnh = memoryReadout ? memoryReadout.totalNotesHit : 0;
const tnm = memoryReadout ? memoryReadout.totalNotesMissed : 0;
let accuracy = tnh / (tnh + tnm);
accuracy *= 100;
if (Number.isNaN(accuracy)) {
accuracy = 0;
}
const notesRandom = jsonObj.memoryReadout.totalNotesMissed;
chartData[0].data[newTime] = accuracy;
chartData[1].data[newTime] = notesRandom;
this.setState({ chartData })
console.log(chartData);
}
}, 1000);
}
render = () => {
return (
<LineChart
library={{
legend: {
labels: {
// This more specific font property overrides the global property
fontFamily: "Roboto Condensed",
},
},
scales: {
xAxes: [{
type: 'time',
time: {
displayFormats: {
quarter: 'mm:ss',
},
unit: 'seconds',
},
}],
yAxes: [{
type: 'linear',
display: true,
position: 'left',
id: 'y-axis-1',
}, {
type: 'linear',
display: true,
position: 'right',
id: 'y-axis-2',
gridLines: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
ticks: {
suggestedMin: 0,
},
}],
},
pan: {
enabled: true,
mode: 'x',
},
zoom: {
enabled: true,
mode: 'x',
},
responsive: true,
stacked: false,
hoverMode: 'index',
}}
messages={{ empty: "Waiting for data..." }}
data={this.state.chartData}
className="lineChart" />
);
}
}
ChartView.propTypes = {
timeTotal: PropTypes.number,
//eslint-disable-next-line
startTrack: PropTypes.bool,
//eslint-disable-next-line
stopTrack: PropTypes.bool,
}
ChartView.defaultProps = {
timeTotal: 0,
//eslint-disable-next-line
startTrack: false,
//eslint-disable-next-line
stopTrack: false,
}