Built with blockbuilder.org
Last active
June 21, 2018 08:59
-
-
Save alteist/9a955a8d53db9b840d91ee2e66cfa0f8 to your computer and use it in GitHub Desktop.
fresh block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
const toRadians = function (angle) { | |
return angle * (Math.PI / 180); | |
} | |
var svg = d3.select("body").append("svg") | |
.attr("width", 500) | |
.attr("height", 500) | |
var data = [['Занятость',150],['KPI2',120],['KPI3',100]] | |
// var center = [205,205] | |
var width = +svg.attr("width"), | |
radius = width/4, | |
height = +svg.attr("height") | |
var g = svg.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + radius*1.5 + ")"); | |
svg.append("text") | |
.attr("x", 10) | |
.attr("y", 40) | |
.attr("font-size","20px") | |
.attr("fill", "black") | |
.attr("text-anchor", "start") | |
.text('% исполнения показателей программ') | |
var angleStep = 360/data.length | |
var log = function (t) { | |
svg.append("text") | |
.attr("x", 10) | |
.attr("y", 100) | |
.attr("font-size","30px") | |
.attr("fill", "black") | |
.attr("text-anchor", "start") | |
.text(t) | |
} | |
var gridCount = 4 | |
var fontSize = 16 | |
g.selectAll("circle.grid-circle") | |
.data(d3.range(gridCount)) | |
.enter() | |
.append("circle") | |
.attr("cx",0) | |
.attr("cy",0) | |
.attr("r", (d,i)=>(radius*(i+1)/gridCount)) | |
.attr("fill", "none") | |
.attr("stroke", "black") | |
.attr('class', 'grid-circle') | |
var points = [] | |
data.forEach((d,i)=>{ | |
// var k = radius/d3.max(data,(d)=>(d[1])) | |
var k = radius/100 | |
var angleRad = toRadians(-i*angleStep) | |
points.push({ | |
title: d[0], | |
value: d[1], | |
a: angleRad, | |
r: d[1]*k, | |
x: d[1]*k*Math.cos(angleRad), | |
y: d[1]*k*Math.sin(angleRad), | |
rx: radius*Math.cos(angleRad), | |
ry: radius*Math.sin(angleRad), | |
}) | |
}) | |
var pointsPath = d3.line().x((d)=>(d.x)).y((d)=>(d.y)) | |
g | |
.append("path") | |
.attr("d", pointsPath(points.concat(points[0]))) | |
.attr("stroke", "red") | |
.attr("stroke-width", "2px") | |
.attr("fill", "none") | |
.attr("class", "points-path") | |
g.selectAll("line.rays") | |
.data(points) | |
.enter() | |
.append("line") | |
.attr("x1",0) | |
.attr("y1",0) | |
.attr("x2",(d)=>(d.rx)) | |
.attr("y2",(d)=>(d.ry)) | |
.attr("stroke", "black") | |
.attr("class", "rays") | |
var pointRadius = width/100 | |
var pointMouseOver = function(d, i) { | |
d3.selectAll('.data-point-real') | |
.attr('fill','black') | |
d3.select(this) | |
.select('.data-point-real') | |
.transition() | |
.attr('r',pointRadius*2) | |
.attr('fill','red') | |
context.select('.context-key').text(d.title) | |
context.select('.context-value').text(d.value) | |
} | |
var pointMouseOut = function(d, i) { | |
d3.select(this) | |
.select('.data-point-real') | |
.transition() | |
.attr('r',pointRadius) | |
context.selectAll('text').text('') | |
} | |
var dataPoints = g.selectAll("circle.data-point") | |
.data(points) | |
.enter() | |
.append("g") | |
.style("pointer-events", "bounding-box") | |
.attr("class", "data-point") | |
.on('mouseover', pointMouseOver) | |
.on('mouseout', pointMouseOut) | |
dataPoints | |
.append("circle") | |
.attr('fill', 'none') | |
.attr("cx",(d)=>(d.x)) | |
.attr("cy",(d)=>(d.y)) | |
.attr("r", pointRadius*2) | |
dataPoints.append("circle") | |
.attr("cx",(d)=>(d.x)) | |
.attr("cy",(d)=>(d.y)) | |
.attr("r",pointRadius) | |
.attr("class", "data-point-real") | |
context = svg.append('g') | |
.attr('class','context') | |
.attr("transform", "translate(" + 1 / 2 + "," + (height - 100) + ")") | |
context.append('line') | |
.attr('x1',0) | |
.attr('x2',width) | |
.style('stroke', 'red') | |
context | |
.append('text') | |
.attr("x", fontSize*2) | |
.attr("y", fontSize*3) | |
.attr("font-size",fontSize*1.5) | |
.attr("fill", "black") | |
.attr("text-anchor", "start") | |
.attr('class','context-key') | |
context | |
.append('text') | |
.attr("x", width-fontSize*2) | |
.attr("y", fontSize*3.5) | |
.attr("font-size",fontSize*3) | |
.attr("fill", "black") | |
.attr("text-anchor", "end") | |
.attr('class','context-value') | |
g.selectAll("text.grid-text") | |
.data(d3.range(gridCount)) | |
.enter() | |
.append("text") | |
.text((d,i)=>(100*i/gridCount)) | |
.attr("x", 0) | |
.attr("y", (d,i)=>(-radius*i/gridCount-fontSize/2)) | |
.attr("text-anchor", "middle") | |
.attr("class", 'grid-text') | |
txt = [ | |
[radius+fontSize,0], | |
[0,radius+fontSize], | |
[-radius-fontSize,0], | |
[0,-radius-fontSize/2] | |
] | |
txt.forEach((d)=>{ | |
g.append("text") | |
.text(100) | |
.attr("x", d[0]) | |
.attr("y", d[1]) | |
.attr("text-anchor", "middle") | |
.attr("class", 'grid-text') | |
}) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment