Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {createRef} from 'react';
- import * as d3 from "d3";
- import {scaleLinear, scaleBand, scaleTime, scaleRadial, schemeCategory20c } from 'd3-scale';
- import { select as d3Select } from 'd3-selection'
- import moment from 'moment'
- import * as dataParser from '../dataParser.js';
- const margin = {top: 20, right: 10, bottom: 20, left: 10};
- const width = 150 - margin.left - margin.right,
- height = 150 - margin.top - margin.bottom;
- const innerRadius = 20,
- outerRadius = Math.min(width, height) / 2 - 6;
- const formatHour = d3.timeFormat("%I %p")
- const fullCircle = 2 * Math.PI * 23/24;
- const y = scaleRadial()
- .range([innerRadius, outerRadius]);
- class CalendarRadial extends React.Component {
- componentDidMount() {
- this.renderRadial()
- }
- renderRadial = () => {
- console.log('XXXXXXXXXX', this.props)
- const x = scaleLinear()
- if (this.props.clockConfig === 'Midnight Up') {
- x.range([0, fullCircle]);
- }
- else {
- x.range([0 + Math.PI, fullCircle + Math.PI]);
- }
- const line = d3.lineRadial()
- .angle(function(d) { return x(d.key); })
- .radius(function(d) { return y(d.value); })
- .curve(this.props.lineType)
- const data = this.props.dataDayHours;
- const svg = d3.select(this.refs[this.props.currentDay])
- const gSelect = svg.selectAll('.radial').data(data);
- gSelect.exit()
- .classed('radial', false)
- .attr('opacity', 0.8)
- .transition()
- .attr('opacity', 0)
- .remove();
- var gEnter = gSelect.enter().append("g")
- .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
- .classed('radial', true);
- const exit = gSelect.exit().classed('radial', false);
- exit
- .attr('opacity', 0.8)
- .transition()
- .attr('opacity', 0)
- .remove();
- x.domain(d3.extent(data, function(d) { return d.key; }));
- y.domain(d3.extent(data, function(d) { return d.value; }));
- var yAxis = d3.selectAll('.radial').append("g")
- .attr("text-anchor", "middle");
- var yTick = yAxis
- .selectAll(".radial")
- .data(y.ticks(6))
- .enter().append("g");
- yTick.append("circle")
- .attr("fill", "none")
- .attr("stroke", "#edf1f4")
- .attr("r", function(d) {return y(d)});
- var numColors = 10;
- var colorScale = d3.scaleLinear()
- .domain([0,(numColors-1)/2,numColors-1])
- .range(["#F5D801", "#74D877", "#2A4858"])
- .interpolate(d3.interpolateHcl);
- var gradient = d3.selectAll('.radial').append("defs").append("radialGradient")
- .attr("id", "gradientRainbow")
- .attr("gradientUnits", "userSpaceOnUse")
- .attr("cx", "0%")
- .attr("cy", "0%")
- .attr("r", "45%")
- .selectAll("stop")
- .data(d3.range(numColors))
- .enter().append("stop")
- .attr("offset", function(d,i) { return (i/(numColors-1)*50 + 40) + "%"; })
- .attr("stop-color", function(d) { return colorScale(d); });
- var linePlot = d3.selectAll('.radial').append("path")
- .datum(data)
- .attr("fill", "url(#gradientRainbow)")
- .attr("stroke", "#213946")
- .attr("stroke-width", 1)
- .attr("d", line);
- var yAxisWhite = d3.selectAll('.radial').append("g")
- .attr("text-anchor", "middle");
- yAxisWhite.append("circle")
- .attr("fill", "white")
- .attr("stroke", "black")
- .attr("opacity", 1)
- .attr("r", function() { return y(y.domain()[0])});
- var xAxis = svg.selectAll('.radial').append("g");
- }
- render() {
- return(
- <div className="center">
- <svg width={150} height={150}
- ref={this.props.currentDay} id={this.props.currentDay}>
- </svg>
- </div>
- )
- }
- }
- export default CalendarRadial;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement