score:0
The code below will do this. Later I will add the link to the tutorial.
import * as d3 from 'd3';
import { attrs } from 'd3-selection-multi'
let btn = d3.select('body').append('button').text('Change data')
.style('margin-bottom', '10px')
let dim = { 'width': 700, 'height': 400 }
let svg = d3.select('body').append('svg').attrs(dim)
.style('border', '1px solid black')
let g = svg.append('g').attr('transform', 'translate(350, 370)')
let arcGen = d3.arc()
arcGen.innerRadius(250).outerRadius(340)
let pth = arcGen({
startAngle: -Math.PI / 2,
endAngle: Math.PI / 2
})
let scale = d3.scaleLinear([0, 1], [-Math.PI / 2, Math.PI / 2])
let data = [Math.random(), Math.random()]
g.append('path').attrs({
'd': pth,
'fill': 'lightgray',
'stroke': 'black'
})
svg.append('clipPath').attr('id', 'mask')
.append('path').attr('d', pth)
let valuePth = arcGen({
startAngle: -Math.PI / 2,
endAngle: scale(data[0])
})
let value = g.append('path').attrs({
'd': valuePth,
'fill': data[0] < data[1]? 'aquamarine': 'lightcoral',
'stroke': 'black'
})
// target line
let pts = d3.pointRadial(scale(data[1]), 0).concat(d3.pointRadial(scale(data[1]), 1000))
let target = g.append('line').attrs({
'x1': pts[0], 'y1': pts[1], 'x2': pts[2], 'y2': pts[3],
'stroke': 'midnightblue',
'stroke-width': 4,
'stroke-dasharray': '6, 3',
'clip-path': 'url(#mask)'
})
btn.on('click', ()=>{
let oldAngle = scale(data[0])
data = [Math.random(), Math.random()]
pts = d3.pointRadial(scale(data[1]), 0).concat(d3.pointRadial(scale(data[1]), 1000))
value.transition().duration(2000).attrTween('d', ()=>{
let start = {startAngle: -Math.PI / 2, endAngle: oldAngle}
let interpolate = d3.interpolate(start, {startAngle: -Math.PI / 2, endAngle: scale(data[0])})
return (t)=>arcGen(interpolate(t))
}).attr('fill', data[0] < data[1]? 'aquamarine': 'lightcoral')
target.transition().duration(2000).attrs({
'x1': pts[0], 'y1': pts[1], 'x2': pts[2], 'y2': pts[3]
})
})
Source: stackoverflow.com
Related Query
- Show values for half donut pie chart in D3 JS
- How to tell d3js that this half donut chart should fill values from left to right corner
- Special donut chart with different rings/arcs for positive and negative values
- How to show values in C3.js in Donut Legend?
- How to make a C3 donut chart to a semi circle or a half donut chart in typescript?
- Half pie chart with multiple values
- d3 js axis Percentage Values with decimals
- D3 - Placing labels and lines on a half pie/half donut chart
- Multiple donut chart not showing percentage
- Donut chart: show percentage inside
- tool tip for half donut or half pie chart
- Sorting in vegalite screws when changing values from fraction to percentage
- Half-donut percentage values
- How to change randomised data used for graph values with my input data? D3js donut graph
- Donut chart is not updating with new values
- D3.js: Using images (with filenames specified in data) as tick values on axis
- Bar chart with negative values
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- Animating D3 donut chart on load
- nvd3 line chart with string values on x-axis
- How to add in zero values into a time series in d3.js / JavaScript
- How to add drop shadow to d3.js pie or donut chart
- Pass null values to SVG path (using d3.js) to suppress missing data
- d3.nest() key and values conversion to name and children
- How do we change the tick values generated by a linear scale in a d3.js line plot?
- Display only whole values along y axis in d3.js when range is 0-1 or 0-2
- how to create labels for data in donut chart using d3.js
- How to select unique values in d3.js from data
- D3.js - Donut charts with multiple rings
- How can I get numeric values instead of Logarthmic values in D3 axis scale?
More Query from same tag
- render bar chart with different colors in react native
- How to limit the d3.js drag behavior on a mercator map
- D3 radial bar chart adjust radius scaling
- What does D3's "empty" selection do?
- Using General update pattern in line graph
- Create a Graduated Symbol Map using D3
- Adding a chart legend in D3
- nvd3 formatting date returns always 1970-01-01
- Dynamically adding/removing Angular components to/from the DOM using D3
- D3js: How do I make my tooltip appear next to the selection?
- How to dynamically load multiple line segments using D3.js
- Grouping json data to create grouped bar chart
- Color d3 area based on computed value
- DC.js | How can I trigger my D3 updates?
- Sankey diagram transition
- Creating links between nodes d3.js currently returning Uncaught TypeError: Cannot read property 'weight' of undefined
- Applying Bostock's wrap function on text
- Using data from d3.js outside
- d3.js nodes and edges format
- How to get d3 instance from d3 selection?
- d3.select() returns null for a html element inside <ng-template>
- Get domain from range using ScaleBand
- D3.js not recognizing floats from CSV
- How to get rid of hairline from overlapping svg shapes
- Change the link color of ngx--graph
- Writing methods that call other methods inside d3 ember components?
- D3 - get current time and subtract by 2 hours
- Collapsible tidy horizontal tree from obervablehq migrated to html is rendering only a single large node in the middle
- Plotly js chart hover text not displaying correct number values
- d3js adding element using .append(function(){})