Advertisement
Guest User

Untitled

a guest
Nov 27th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {createRef} from 'react';
  2. import * as d3 from "d3";
  3. import {scaleLinear, scaleBand, scaleTime, scaleRadial, schemeCategory20c } from 'd3-scale';
  4. import { select as d3Select } from 'd3-selection'
  5. import moment from 'moment'
  6. import *  as dataParser from '../dataParser.js';
  7.  
  8.  
  9. const margin = {top: 20, right: 10, bottom: 20, left: 10};
  10.  
  11. const width = 150 - margin.left - margin.right,
  12.   height = 150 - margin.top - margin.bottom;
  13.  
  14. const innerRadius = 20,
  15.     outerRadius = Math.min(width, height) / 2 - 6;
  16.  
  17. const formatHour = d3.timeFormat("%I %p")
  18.  
  19. const fullCircle = 2 * Math.PI * 23/24;
  20.  
  21. const y = scaleRadial()
  22.     .range([innerRadius, outerRadius]);
  23.  
  24.  
  25. class CalendarRadial extends React.Component {
  26.  
  27.  componentDidMount() {
  28.    this.renderRadial()
  29.  }
  30.  
  31.   renderRadial = () => {
  32.     console.log('XXXXXXXXXX', this.props)
  33.  
  34.     const x = scaleLinear()
  35.  
  36.     if (this.props.clockConfig === 'Midnight Up') {
  37.       x.range([0, fullCircle]);
  38.     }
  39.     else {
  40.       x.range([0 + Math.PI, fullCircle + Math.PI]);
  41.     }
  42.  
  43.  
  44.     const line = d3.lineRadial()
  45.         .angle(function(d) { return x(d.key); })
  46.         .radius(function(d) { return y(d.value); })
  47.       .curve(this.props.lineType)
  48.  
  49.     const data = this.props.dataDayHours;
  50.  
  51.     const svg = d3.select(this.refs[this.props.currentDay])
  52.     const gSelect = svg.selectAll('.radial').data(data);
  53.  
  54.     gSelect.exit()
  55.     .classed('radial', false)
  56.     .attr('opacity', 0.8)
  57.     .transition()
  58.     .attr('opacity', 0)
  59.     .remove();
  60.  
  61.     var gEnter = gSelect.enter().append("g")
  62.     .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
  63.     .classed('radial', true);
  64.  
  65.     const exit = gSelect.exit().classed('radial', false);
  66.     exit
  67.     .attr('opacity', 0.8)
  68.     .transition()
  69.     .attr('opacity', 0)
  70.     .remove();
  71.  
  72.     x.domain(d3.extent(data, function(d) { return d.key; }));
  73.  
  74.     y.domain(d3.extent(data, function(d) { return d.value; }));
  75.  
  76.     var yAxis = d3.selectAll('.radial').append("g")
  77.     .attr("text-anchor", "middle");
  78.  
  79.     var yTick = yAxis
  80.     .selectAll(".radial")
  81.     .data(y.ticks(6))
  82.     .enter().append("g");
  83.  
  84.     yTick.append("circle")
  85.       .attr("fill", "none")
  86.       .attr("stroke", "#edf1f4")
  87.       .attr("r", function(d) {return y(d)});
  88.  
  89.  
  90.     var numColors = 10;
  91.     var colorScale = d3.scaleLinear()
  92.       .domain([0,(numColors-1)/2,numColors-1])
  93.       .range(["#F5D801", "#74D877", "#2A4858"])
  94.       .interpolate(d3.interpolateHcl);
  95.  
  96.     var gradient = d3.selectAll('.radial').append("defs").append("radialGradient")
  97.       .attr("id", "gradientRainbow")
  98.       .attr("gradientUnits", "userSpaceOnUse")
  99.       .attr("cx", "0%")
  100.       .attr("cy", "0%")
  101.       .attr("r", "45%")
  102.       .selectAll("stop")
  103.       .data(d3.range(numColors))
  104.       .enter().append("stop")
  105.       .attr("offset", function(d,i) { return (i/(numColors-1)*50 + 40) + "%"; })
  106.       .attr("stop-color", function(d) { return colorScale(d); });
  107.  
  108.     var linePlot = d3.selectAll('.radial').append("path")
  109.       .datum(data)
  110.       .attr("fill", "url(#gradientRainbow)")
  111.       .attr("stroke", "#213946")
  112.       .attr("stroke-width", 1)
  113.       .attr("d", line);
  114.  
  115.  
  116.     var yAxisWhite = d3.selectAll('.radial').append("g")
  117.       .attr("text-anchor", "middle");
  118.  
  119.     yAxisWhite.append("circle")
  120.       .attr("fill", "white")
  121.       .attr("stroke", "black")
  122.       .attr("opacity", 1)
  123.       .attr("r", function() { return y(y.domain()[0])});
  124.  
  125.  
  126.       var xAxis = svg.selectAll('.radial').append("g");
  127.  
  128.   }
  129.  
  130.   render() {
  131.     return(
  132.       <div className="center">
  133.  
  134.         <svg width={150} height={150}
  135.             ref={this.props.currentDay} id={this.props.currentDay}>
  136.         </svg>
  137.  
  138.       </div>
  139.     )
  140.   }
  141. }
  142.  
  143. export default CalendarRadial;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement