2020-08-10 02:18:51 +03:00
|
|
|
import * as React from "react";
|
2020-08-17 05:44:48 +03:00
|
|
|
import * as Constants from "~/common/constants";
|
|
|
|
import ReactDOM from "react-dom";
|
2020-08-08 05:21:07 +03:00
|
|
|
|
2020-11-30 08:24:22 +03:00
|
|
|
import { css } from "@emotion/react";
|
2020-08-17 05:44:48 +03:00
|
|
|
|
|
|
|
const STYLES_GRAPH_CONTAINER = css`
|
|
|
|
display: flex;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_GRAPH = css`
|
2020-08-17 06:23:13 +03:00
|
|
|
height: 600px;
|
|
|
|
margin: auto;
|
2020-08-17 05:44:48 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_X_LINE = css`
|
2020-08-17 06:23:13 +03:00
|
|
|
stroke: ${Constants.system.black};
|
2020-08-17 05:44:48 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_CHART_CIRCLE = css`
|
2020-08-17 06:23:13 +03:00
|
|
|
stroke: none;
|
2021-07-07 22:14:51 +03:00
|
|
|
fill: ${Constants.system.blue};
|
2020-08-17 05:44:48 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_CHART_LINE = css`
|
2021-07-07 22:14:51 +03:00
|
|
|
stroke: ${Constants.system.blue};
|
2020-08-17 06:23:13 +03:00
|
|
|
fill: none;
|
2020-08-17 05:44:48 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_CHART_TEXT = css`
|
2020-08-17 06:23:13 +03:00
|
|
|
fill: ${Constants.system.black};
|
2020-08-17 05:44:48 +03:00
|
|
|
`;
|
2020-08-08 05:21:07 +03:00
|
|
|
|
2020-08-17 06:23:13 +03:00
|
|
|
export default class CreateChart extends React.Component {
|
2020-08-10 01:40:03 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.myRef = React.createRef();
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:44:48 +03:00
|
|
|
componentDidMount() {
|
2020-08-17 06:23:13 +03:00
|
|
|
this.createLines();
|
|
|
|
this.createCircles();
|
|
|
|
this.createTicks();
|
|
|
|
}
|
2020-08-17 05:44:48 +03:00
|
|
|
|
2020-11-04 20:55:48 +03:00
|
|
|
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
2020-08-17 05:44:48 +03:00
|
|
|
|
2020-08-17 06:23:13 +03:00
|
|
|
createCircles = () => {
|
|
|
|
const { organizedData } = this.props;
|
|
|
|
let oData = organizedData.flat(2);
|
|
|
|
let allCircles = oData.map((g, index) => {
|
2020-11-04 20:55:48 +03:00
|
|
|
return <circle key={index} cx={g.x} cy={g.y} r="2" css={STYLES_CHART_CIRCLE} />;
|
2020-08-17 06:23:13 +03:00
|
|
|
});
|
|
|
|
ReactDOM.render(allCircles, document.getElementById("circles"));
|
|
|
|
};
|
|
|
|
|
|
|
|
createLines = () => {
|
|
|
|
const { organizedData } = this.props;
|
|
|
|
|
|
|
|
if (organizedData.length) {
|
|
|
|
let allLines = [];
|
|
|
|
for (let groups of organizedData) {
|
|
|
|
for (let group of groups) {
|
|
|
|
let polyLines = this.loopData(group);
|
|
|
|
allLines.push(polyLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(allLines, document.getElementById("lines"));
|
2020-08-08 05:21:07 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-17 06:23:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
loopData = (g) => {
|
|
|
|
let coordinates = [];
|
|
|
|
let i = {};
|
|
|
|
g.map((o, index) => {
|
|
|
|
coordinates.push(o.x);
|
|
|
|
coordinates.push(o.y);
|
|
|
|
i[`id`] = o.id;
|
|
|
|
});
|
|
|
|
let polyLine = (
|
2020-11-04 20:55:48 +03:00
|
|
|
<polyline css={STYLES_CHART_LINE} key={i.id} points={this.drawPoints(coordinates)} />
|
2020-08-17 05:44:48 +03:00
|
|
|
);
|
2020-08-17 06:23:13 +03:00
|
|
|
return polyLine;
|
|
|
|
};
|
|
|
|
|
|
|
|
drawPoints = (a) => {
|
|
|
|
const c = a.toString();
|
|
|
|
const regex = /([0-9]+),\s([0-9]+),\s/g;
|
|
|
|
const cOrganized = c.replace(regex, "$1,$2 ");
|
|
|
|
return cOrganized;
|
|
|
|
};
|
|
|
|
|
|
|
|
createTicks = () => {
|
|
|
|
const { ticks } = this.props;
|
|
|
|
const fTicks = ticks.flat(1);
|
|
|
|
const tickLines = [];
|
|
|
|
|
|
|
|
for (let tick of fTicks) {
|
|
|
|
const tDate = new Date(tick.date);
|
|
|
|
const month = this.monthNames[tDate.getMonth()];
|
|
|
|
const year = tDate.getUTCFullYear();
|
|
|
|
|
|
|
|
let tickLine = (
|
|
|
|
<g>
|
|
|
|
<line css={STYLES_X_LINE} x1={tick.x} y1="550" x2={tick.x} y2="560" />
|
|
|
|
<text css={STYLES_CHART_TEXT} textAnchor="middle" x={tick.x} y="575">
|
|
|
|
{`${month} ${year}`}
|
|
|
|
</text>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
tickLines.push(tickLine);
|
|
|
|
}
|
|
|
|
ReactDOM.render(tickLines, document.getElementById("tickContainer"));
|
|
|
|
};
|
2020-08-17 05:44:48 +03:00
|
|
|
|
|
|
|
render() {
|
2020-08-08 05:21:07 +03:00
|
|
|
return (
|
2020-08-17 05:44:48 +03:00
|
|
|
<div css={STYLES_GRAPH_CONTAINER}>
|
|
|
|
<svg css={STYLES_GRAPH} viewBox="0 0 600 600">
|
2020-11-04 20:55:48 +03:00
|
|
|
<g id="circles" />
|
|
|
|
<g id="lines" />
|
2020-08-08 05:21:07 +03:00
|
|
|
<g>
|
2020-08-17 05:44:48 +03:00
|
|
|
<line css={STYLES_X_LINE} x1="25" y1="550" x2="575" y2="550" />
|
2020-08-08 05:21:07 +03:00
|
|
|
</g>
|
2020-11-04 20:55:48 +03:00
|
|
|
<g id="tickContainer" />
|
2020-08-08 05:21:07 +03:00
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|