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'; 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; 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; var svg = d3.select("#category-diagram").append('svg') .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 + ")"); var color = d3.scaleOrdinal(['#4daf4a', '#377eb8', '#ff7f00', '#984ea3', '#e41a1c']); var pie = d3.pie().value(function (d) { return d.count; }); var path = d3.arc() .outerRadius(radius - 10) // .innerRadius(0); .innerRadius(130); var label = d3.arc() .outerRadius(radius) .innerRadius(radius - 80); var arc = g.selectAll(".arc") .data(pie(data)) .enter().append("g") .attr("class", "arc"); 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) .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) arc.append("text") .attr("transform", function (d) { return "translate(" + label.centroid(d) + ")"; }) .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") } render() { return (