Skip to content

Commit

Permalink
Pie graph color diff
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrychjan committed Mar 25, 2022
1 parent b1825c8 commit f9a4b84
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#.env
GENERATE_SOURCEMAP=false
REACT_APP_JWTPK="token"
REACT_APP_API_URL="http://localhost:3000"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
.env
# dependencies
/node_modules
/.pnp
Expand All @@ -21,3 +20,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
test.txt
.env
33 changes: 28 additions & 5 deletions src/components/graphs/pieGraphComponent.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import React, { Component } from "react";
import Joi from "joi";
import { Pie, PieChart, ResponsiveContainer, Tooltip } from "recharts";
import { Cell, Pie, PieChart, ResponsiveContainer, Tooltip } from "recharts";

class PieGraphComponent extends Component {
state = {
colors: [],
};

componentDidMount() {
let c = [];
for (let i = 0; i < this.props.collectionData.length; i++)
c.push(this.genRandomColor());
this.setState({ colors: c });
}

validatePieChart(settingsPayload) {
const pieSchema = Joi.object({
dataKey: Joi.string().required(),
Expand All @@ -17,6 +28,13 @@ class PieGraphComponent extends Component {
return error ? false : true;
}

genRandomColor() {
let l = "0123456789ABCDEF";
let c = "#";
for (let i = 0; i < 6; i++) c += l[Math.floor(Math.random() * 16)];
return c;
}

render() {
const { settingsPreset, collectionData, collectionModel } = this.props;

Expand All @@ -26,9 +44,7 @@ class PieGraphComponent extends Component {
);
if (valueColumn.dataType === "number") {
for (let d of collectionData) {
d[settingsPreset.dataKey] = parseFloat(
d[settingsPreset.dataKey]
);
d[settingsPreset.dataKey] = parseFloat(d[settingsPreset.dataKey]);
}
} else {
return <p>Column of {settingsPreset.dataKey} is not of type number.</p>;
Expand All @@ -47,7 +63,14 @@ class PieGraphComponent extends Component {
dataKey={settingsPreset.dataKey}
fill={settingsPreset.fill}
label
/>
>
{collectionData.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={this.state.colors[index % this.state.colors.length]}
/>
))}
</Pie>
<Tooltip />
</PieChart>
</ResponsiveContainer>
Expand Down

0 comments on commit f9a4b84

Please sign in to comment.