d3实现堆叠柱状图图表效果代码
代码语言:html
所属分类:图表
代码描述:d3实现堆叠柱状图图表效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> * { box-sizing: border-box; padding: 0; margin: 0; } body { background: hsl(0, 0%, 97%); color: hsl(0, 0%, 7%); font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Ubuntu, roboto, noto, arial, sans-serif; } #root { max-width: 52rem; margin: 1rem auto; padding: 1rem; } #root > * + * { margin-top: 0.75em; } h1 { font-size: 1.43rem; } .chart { display: block; } .chart .axis-y path { opacity: 0; } .chart .axis-y text { color: hsl(0, 0%, 25%); font-size: 0.8rem; font-weight: 700; } .chart .axis-x path { color: hsl(0, 0%, 57%); } .chart .axis-x text { color: hsl(0, 0%, 37%); font-size: 0.6rem; font-weight: 700; } form { display: flex; justify-content: space-evenly; flex-wrap: wrap; gap: 0.5rem; } label { color: hsl(0, 0%, 25%); cursor: pointer; position: relative; display: inline-flex; align-items: center; gap: 0 0.5rem; } label input { width: 1em; height: 1em; opacity: 0; } label svg { display: block; width: 1em; height: 1em; top: 0; right: 0; position: absolute; } </style> </head> <body > <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/d3.7.7.0.js"></script> <script > const stackedBarChartComponent = () => { // prettier-ignore let data = [ { apples: 3840, bananas: 1920, cherries: 960, durians: 400 }, { apples: 1600, bananas: 1440, cherries: 960, durians: 400 }, { apples: 640, bananas: 960, cherries: 640, durians: 400 }, { apples: 320, bananas: 480, cherries: 640, durians: 400 }]; let keys = Object.keys(data[0]); let xAccessor = (_, i) => i; let xFormat = d => d; let valueFormat = d => d; let colorScale = d3.scaleOrdinal(d3.schemeSet2).domain(keys); let width = 500; let height = 250; const stack = d3.stack(); const xScale = d3.scaleBand().padding(0.4); const yScale = d3.scaleLinear(); const xAxis = d3.axisBottom(xScale).tickSize(0).tickPadding(10); const yAxis = d3.axisLeft(yScale).ticks(4).tickSize(0).tickPadding(6); const stackedBarChartComponent = context => { const series = stack.keys(keys)(data); const selection = context.selection ? context.selec.........完整代码请登录后点击上方下载按钮下载查看
网友评论0