Files
hpos-web/src/components/TestAnalyticsPie.js

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-07-23 09:58:23 +05:30
import React, { Component } from 'react';
import * as d3 from 'd3';
import { sgg } from 'ml-savitzky-golay-generalized';
import axios from 'axios';
import moment from 'moment';
import './TestAnalyticsPie.css';
2023-07-23 09:58:23 +05:30
class TestAnalyticsPie extends Component {
constructor(props) {
super(props);
this.state = { data: this.props.data, show: this.props.show };
}
componentDidMount() {
this.drawChart(this.state.data);
}
drawChart(data) {
if (!data) return;
2023-07-27 10:31:15 +05:30
var margin = { top: 60, right: 20, bottom: 30, left: 50 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
radius = Math.min(width, height) / 2;
2023-07-23 09:58:23 +05:30
var svg = d3.select("#category-diagram").append('svg')
2023-07-23 09:58:23 +05:30
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g").attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
2023-07-23 09:58:23 +05:30
var color = d3.scaleOrdinal(['#4daf4a', '#377eb8', '#ff7f00', '#984ea3', '#e41a1c']);
var pie = d3.pie().value(function (d) {
return d.count;
});
2023-07-23 09:58:23 +05:30
var path = d3.arc()
.outerRadius(radius - 10)
// .innerRadius(0);
.innerRadius(130);
2023-07-23 09:58:23 +05:30
var label = d3.arc()
.outerRadius(radius)
.innerRadius(radius - 80);
var arc = g.selectAll(".arc")
2023-07-23 09:58:23 +05:30
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
2023-07-27 10:31:15 +05:30
var tooltip = d3.select("#category-diagram")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("background", "#fff")
.text("a simple tooltip");
arc.append("path")
.attr("d", path)
2023-07-27 10:31:15 +05:30
.attr("fill", function (d) { return color(d.data.category); })
.on('mouseover', (event, d) => { tooltip.text(`${d.data.category} (${d.data.count})`); console.log(d.data.count); return tooltip.style("visibility", "visible"); });
console.log(arc)
2023-07-23 09:58:23 +05:30
2023-07-27 10:31:15 +05:30
arc.append("text")
.attr("transform", function (d) {
return "translate(" + label.centroid(d) + ")";
2023-07-23 09:58:23 +05:30
})
.text(function (d) { return `${d.data.category} (${d.data.count})`; });
// svg.append("g")
// .attr("transform", "translate(" + (width / 2 - 170) + "," + 1 + ")")
// .append("text")
// .text("category counts per day")
// .attr("class", "title")
2023-07-23 09:58:23 +05:30
}
render() {
return (
<div>
<div className='graph-card'>
<div id="category-diagram" style={{ margin: '1em' }}>
2023-07-23 09:58:23 +05:30
</div>
<div className='graph-title'>category data on daily basis</div>
2023-07-23 09:58:23 +05:30
</div>
</div>
)
};
}
export default TestAnalyticsPie;