【发布时间】:2019-06-08 08:48:39
【问题描述】:
此外,如果我指定部分的数量,调色板将被平均分配。例如,如果我将部分的数量指定为 3,则颜色均分为 3 个相等的百分比(每个百分比为 33.333%)。我需要将自定义范围设置为 20% 红色、50% 黄色和 30% 绿色。我怎样才能做到这一点?
我正在构建此代码:Codepen link
Javascript
percent = .65
barWidth = 60
numSections = 3
# / 2 for HALF circle
sectionPerc = 1 / numSections / 2
padRad = 0
chartInset = 10
# start at 270deg
totalPercent = .75
el = d3.select('.chart-gauge')
margin = { top: 20, right: 20, bottom: 30, left: 20 }
width = el[0][0].offsetWidth - margin.left - margin.right
height = width
radius = Math.min(width, height) / 2
percToDeg = (perc) ->
perc * 360
percToRad = (perc) ->
degToRad percToDeg perc
degToRad = (deg) ->
deg * Math.PI / 180
svg = el.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
chart = svg.append('g')
.attr('transform', "translate(#{(width + margin.left) / 2}, #{(height + margin.top) / 2})")
# build gauge bg
for sectionIndx in [1..numSections]
arcStartRad = percToRad totalPercent
arcEndRad = arcStartRad + percToRad sectionPerc
totalPercent += sectionPerc
startPadRad = if sectionIndx is 0 then 0 else padRad / 2
endPadRad = if sectionIndx is numSections then 0 else padRad / 2
arc = d3.svg.arc()
.outerRadius(radius - chartInset)
.innerRadius(radius - chartInset - barWidth)
.startAngle(arcStartRad + startPadRad)
.endAngle(arcEndRad - endPadRad)
chart.append('path')
.attr('class', "arc chart-color#{sectionIndx}")
.attr('d', arc)
class Needle
constructor: (@len, @radius) ->
drawOn: (el, perc) ->
el.append('circle')
.attr('class', 'needle-center')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', @radius)
el.append('path')
.attr('class', 'needle')
.attr('d', @mkCmd(perc))
animateOn: (el, perc) ->
self = this
el
.transition()
.delay(500)
.ease('elastic')
.duration(3000)
.selectAll('.needle')
.tween('progress', ->
(percentOfPercent) ->
progress = percentOfPercent * perc
d3
.select(this)
.attr('d', self.mkCmd progress)
)
mkCmd: (perc) ->
thetaRad = percToRad perc / 2 # half circle
centerX = 0
centerY = 0
topX = centerX - @len * Math.cos(thetaRad)
topY = centerY - @len * Math.sin(thetaRad)
leftX = centerX - @radius * Math.cos(thetaRad - Math.PI / 2)
leftY = centerY - @radius * Math.sin(thetaRad - Math.PI / 2)
rightX = centerX - @radius * Math.cos(thetaRad + Math.PI / 2)
rightY = centerY - @radius * Math.sin(thetaRad + Math.PI / 2)
"M #{leftX} #{leftY} L #{topX} #{topY} L #{rightX} #{rightY}"
needle = new Needle 140, 15
needle.drawOn chart, 0
needle.animateOn chart, percent
CSS:
@import compass
.chart-gauge
width: 400px
margin: 10px auto
.chart-color1
fill: #D82724
.chart-color2
fill: #FCBF02
.chart-color3
fill: #92D14F
.needle,
.needle-center
fill: #464A4F
.prose
text-align: center
font-family: sans-serif
color: #ababab
HTML:
<div class="chart-gauge"></div>
谢谢
【问题讨论】:
-
在我看来,链接的 Pen 不是你的。我编辑了这个问题以明确这一点。
标签: javascript d3.js charts coffeescript data-visualization