add-week-month-view #62
@@ -37,13 +37,11 @@ export default function Chart(props) {
|
|||||||
q7: true,
|
q7: true,
|
||||||
});
|
});
|
||||||
const [lines, setLines] = React.useState([])
|
const [lines, setLines] = React.useState([])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (data !== props.data) {
|
if (data !== props.data) {
|
||||||
setData(data)
|
setData(props.data)
|
||||||
}
|
}
|
||||||
}, [data, setData, props.data]);
|
}, [data, setData, props.data]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
var output = []
|
var output = []
|
||||||
const colors = ["#8884d8", "#000000", "#82ca9d", "#ff0066", "#660066", "#99cc00", "#669999", "#996633"]
|
const colors = ["#8884d8", "#000000", "#82ca9d", "#ff0066", "#660066", "#99cc00", "#669999", "#996633"]
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import {
|
|
||||||
LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend,
|
|
||||||
} from 'recharts';
|
|
||||||
import {
|
import {
|
||||||
useParams
|
useParams
|
||||||
} from "react-router-dom";
|
} from "react-router-dom";
|
||||||
@@ -13,6 +10,10 @@ import { makeStyles } from '@material-ui/core/styles';
|
|||||||
import Grid from '@material-ui/core/Grid';
|
import Grid from '@material-ui/core/Grid';
|
||||||
import Button from '@material-ui/core/Button';
|
import Button from '@material-ui/core/Button';
|
||||||
import Chart from './chart';
|
import Chart from './chart';
|
||||||
|
import FormHelperText from '@material-ui/core/FormHelperText';
|
||||||
|
import MenuItem from '@material-ui/core/MenuItem';
|
||||||
|
import FormControl from '@material-ui/core/FormControl';
|
||||||
|
import Select from '@material-ui/core/Select';
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
root: {
|
root: {
|
||||||
@@ -38,9 +39,10 @@ const output = (data) => {
|
|||||||
export default function ResourceChart(props) {
|
export default function ResourceChart(props) {
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const [data, setData] = React.useState(null);
|
const [data, setData] = React.useState(null);
|
||||||
|
const [interval, setInterval] = React.useState('day');
|
||||||
let { id } = useParams();
|
let { id } = useParams();
|
||||||
const loadData = async () => {
|
const loadData = async () => {
|
||||||
let dayData = await fetch(`/simcompanies/API/day?kind=${id}`);
|
let dayData = await fetch(`/simcompanies/API/${interval}?kind=${id}`);
|
||||||
let dataWithDate = await dayData.json();
|
let dataWithDate = await dayData.json();
|
||||||
let qualitySortedData = [];
|
let qualitySortedData = [];
|
||||||
for (let i = 0; i < dataWithDate.length; i++) {
|
for (let i = 0; i < dataWithDate.length; i++) {
|
||||||
@@ -48,44 +50,56 @@ export default function ResourceChart(props) {
|
|||||||
else {
|
else {
|
||||||
if (dataWithDate[i]["time"] === qualitySortedData[qualitySortedData.length - 1]["time"]) {
|
if (dataWithDate[i]["time"] === qualitySortedData[qualitySortedData.length - 1]["time"]) {
|
||||||
switch (dataWithDate[i].quality) {
|
switch (dataWithDate[i].quality) {
|
||||||
case 0: break;
|
case 0:
|
||||||
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q1"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q1"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q2"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q2"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q3"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q3"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q4"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q4"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q5"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q5"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q6"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q6"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q7"] = dataWithDate[i]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q7"] = dataWithDate[i]["price"].toFixed(3);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qualitySortedData.push(dataWithDate[i]);
|
qualitySortedData.push(dataWithDate[i]);
|
||||||
qualitySortedData[qualitySortedData.length - 1]["q0"] = qualitySortedData[qualitySortedData.length - 1]["price"];
|
qualitySortedData[qualitySortedData.length - 1]["q0"] = qualitySortedData[qualitySortedData.length - 1]["price"].toFixed(3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let i = 0; i < qualitySortedData.length; i++) {
|
for (let i = 0; i < qualitySortedData.length; i++) {
|
||||||
qualitySortedData[i]["time"] = new Date(qualitySortedData[i]["time"]);
|
qualitySortedData[i]["time"] = new Date(qualitySortedData[i]["time"]);
|
||||||
|
if (interval === "day") {
|
||||||
qualitySortedData[i]["time"] = qualitySortedData[i]["time"].toLocaleTimeString();
|
qualitySortedData[i]["time"] = qualitySortedData[i]["time"].toLocaleTimeString();
|
||||||
|
} else if (interval === "week") {
|
||||||
|
qualitySortedData[i]["time"] = qualitySortedData[i]["time"].toLocaleString();
|
||||||
|
} else {
|
||||||
|
qualitySortedData[i]["time"] = qualitySortedData[i]["time"].toLocaleDateString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
setData(qualitySortedData)
|
setData(qualitySortedData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadData()
|
||||||
|
}, [interval])
|
||||||
|
|
||||||
if (data === null) {
|
if (data === null) {
|
||||||
loadData();
|
loadData();
|
||||||
}
|
}
|
||||||
@@ -93,6 +107,10 @@ export default function ResourceChart(props) {
|
|||||||
const handleClick = (e) => {
|
const handleClick = (e) => {
|
||||||
loadData()
|
loadData()
|
||||||
}
|
}
|
||||||
|
const handleChange = (event) => {
|
||||||
|
setInterval(event.target.value);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
<Grid container spacing={2}>
|
<Grid container spacing={2}>
|
||||||
@@ -103,6 +121,19 @@ export default function ResourceChart(props) {
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Card className={classes.card}>
|
<Card className={classes.card}>
|
||||||
|
|
||||||
|
<FormControl style={{ margin: 20 }}>
|
||||||
|
<Select
|
||||||
|
id="select-interval"
|
||||||
|
value={interval}
|
||||||
|
onChange={handleChange}
|
||||||
|
>
|
||||||
|
<MenuItem value={"day"}>Day</MenuItem>
|
||||||
|
<MenuItem value={"week"}>Week</MenuItem>
|
||||||
|
<MenuItem value={"month"}>Month</MenuItem>
|
||||||
|
</Select>
|
||||||
|
<FormHelperText>Select Interval</FormHelperText>
|
||||||
|
</FormControl>
|
||||||
<CardContent style={{ padding: 0 }}>
|
<CardContent style={{ padding: 0 }}>
|
||||||
{output(data)}
|
{output(data)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
|
|||||||
Reference in New Issue
Block a user