Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
CS 499
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hamid Choucha
CS 499
Commits
fade031c
Commit
fade031c
authored
2 years ago
by
francoisdillinger
Browse files
Options
Downloads
Patches
Plain Diff
API works!
parent
5ed4a37c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/chartComponent.js
+50
-27
50 additions, 27 deletions
frontend/src/chartComponent.js
with
50 additions
and
27 deletions
frontend/src/chartComponent.js
+
50
−
27
View file @
fade031c
import
*
as
React
from
"
react
"
;
import
{
useState
}
from
'
react
'
;
import
{
useState
,
useEffect
}
from
'
react
'
;
import
{
Chart
as
ChartJS
,
CategoryScale
,
...
...
@@ -11,7 +11,6 @@ import {
Legend
,
}
from
'
chart.js
'
;
import
{
Line
}
from
'
react-chartjs-2
'
;
import
{
faker
}
from
"
@faker-js/faker
"
;
ChartJS
.
register
(
CategoryScale
,
...
...
@@ -36,35 +35,59 @@ export const options = {
},
};
const
labels
=
[
'
January
'
,
'
February
'
,
'
March
'
,
'
April
'
,
'
May
'
,
'
June
'
,
'
July
'
];
const
ChartComponent
=
(
props
)
=>
{
export
const
data
=
{
labels
,
datasets
:
[
{
label
:
'
Power Usage
'
,
data
:
labels
.
map
(()
=>
faker
.
datatype
.
number
({
min
:
-
1000
,
max
:
1000
})),
borderColor
:
'
red
'
,
backgroundColor
:
'
red
'
,
},
{
label
:
'
Water Usage
'
,
data
:
labels
.
map
(()
=>
faker
.
datatype
.
number
({
min
:
-
1000
,
max
:
1000
})),
borderColor
:
'
blue
'
,
backgroundColor
:
'
blue
'
,
},
],
};
const
[
chartData
,
setChartData
]
=
useState
(
null
);
const
ChartComponent
=
(
props
)
=>
{
const
fetchData
=
async
()
=>
{
try
{
const
response
=
await
fetch
(
'
http://127.0.0.1:5000/api/costData
'
);
if
(
!
response
.
ok
)
{
throw
new
Error
(
'
Network response was not ok
'
);
}
const
chartData
=
await
response
.
json
();
setChartData
(
chartData
);
}
catch
(
error
)
{
console
.
error
(
'
There was a problem with the fetch operation:
'
,
error
);
}
};
useEffect
(()
=>
{
fetchData
();
},
[]);
const
data
=
{
labels
:
chartData
?
chartData
.
map
(
data
=>
data
[
0
])
:
[],
datasets
:
[
{
label
:
'
Power Usage (MW)
'
,
data
:
chartData
?
chartData
.
map
(
data
=>
(
data
[
1
]
/
1000
))
:
[],
borderColor
:
'
red
'
,
backgroundColor
:
'
red
'
,
},
{
label
:
'
Cost ($)
'
,
data
:
chartData
?
chartData
.
map
(
data
=>
data
[
2
])
:
[],
borderColor
:
'
green
'
,
backgroundColor
:
'
green
'
,
},
{
label
:
'
Water Usage
'
,
data
:
chartData
?
chartData
.
map
(
data
=>
data
[
3
])
:
[],
borderColor
:
'
blue
'
,
backgroundColor
:
'
blue
'
,
},
],
};
return
(
<
div
id
=
"
chart
"
>
<
Line
options
=
{
options
}
data
=
{
data
}
/
>
<
/div
>
);
}
return
(
<
div
id
=
"
chart
"
>
{
chartData
&&
<
Line
options
=
{
options
}
data
=
{
data
}
/>
}
<
/div
>
);
}
;
export
default
ChartComponent
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment