水平条形图单一系列

huangapple go评论69阅读模式
英文:

d3 horizontal bar chart single series

问题

我有一个水平条形图,它针对一个数组系列起作用。但是我想修改它,使其看起来更像这里的图片,其中只有一个项目具有圆角边框,并且百分比值显示在侧边。

当我将数组删除只保留1个时,它占据了100%的区域。

当前代码:

// 你的 JavaScript 代码部分

新进展,通过修复域:

// 你的 JavaScript 代码部分

水平条形图单一系列

英文:

I've got a horizontal bar chart that works against an array series. However I want to modify this to look more akin to the picture here - where there is just 1 item rounded border with the percentage value off to the side.

水平条形图单一系列

When I remove the array to just 1 - it takes up 100% of the area.

current code

https://jsfiddle.net/csa1w2fm/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;multibar d3&lt;/title&gt;
		&lt;script src=&quot;https://d3js.org/d3.v4.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
		&lt;style&gt;
			
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;



	&lt;script&gt;
 
		
		d3.select(&quot;body&quot;)
			.selectAll(&#39;svg&#39;)
			.remove();

		var data = [{&quot;label&quot;: &quot;Toblerone&quot;, &quot;value&quot;: 10}, {&quot;label&quot;: &quot;Mars Bar&quot;, &quot;value&quot;: 30}];

		var w = parseInt(900, 10),
			h = parseInt(800, 10);


		var color = d3.scaleOrdinal().range([&#39;#046395&#39;, &#39;#d62828&#39;, &#39;#f77f00&#39;, &#39;#fcbf49&#39;, &#39;#eae2b7&#39;]);


		data.forEach(function(d) {
			d.total = +d.value;
		});


		var margin = {top: 20, right: 5, bottom: 30, left: 20};

	    var width = w - margin.left - margin.right;
	    var height = h - margin.top - margin.bottom;


		var x = d3.scaleLinear()
		          .range([0, width]);
		var y = d3.scaleBand()
		          .range([height, 0])
		          .padding(0.1);

		x.domain([0, d3.max(data, function(d) { return d.value; })]);
		y.domain(data.map(function(d) { return d.total; }));

        var svg = d3.select(&quot;body&quot;)
        	.append(&quot;svg&quot;)
            .attr(&quot;width&quot;, width + margin.left + margin.right)
            .attr(&quot;height&quot;, height + margin.top + margin.bottom)
			.append(&quot;g&quot;)
			.attr(&#39;class&#39;, &#39;barchart&#39;)
			.attr(&quot;transform&quot;, &quot;translate(&quot; + margin.left + &quot;,&quot; + margin.top + &quot;)&quot;);

		var bars = svg.append(&#39;g&#39;).attr(&#39;class&#39;, &#39;bars&#39;);

		bars.selectAll(&quot;.bar&quot;)
			.data(data)
			.enter().append(&quot;rect&quot;)
			.attr(&quot;class&quot;, &quot;bar&quot;)
			.attr(&#39;fill&#39;, function(d, i) {
				return color(i);
			})
			.attr(&quot;width&quot;, function(d) { return x(d.total); })
			.attr(&quot;y&quot;, function(d) { return y(d.value); })
			.attr(&quot;height&quot;, y.bandwidth());

		bars.selectAll(&quot;.label&quot;)
			.data(data)
			.enter().append(&quot;text&quot;)
			.attr(&quot;class&quot;, &quot;label&quot;)
			.attr(&#39;text-anchor&#39;, function(d) {
			  return d.total &lt;= 20 ? &#39;start&#39; : &#39;end&#39;;
			})
			.attr(&quot;x&quot;, function(d) {
			  return d.total &lt;= 20 ? x(d.total) + 20 : x(d.total) - 20;
			})
			.attr(&quot;y&quot;, function(d) {
			  return y(d.value) + y.bandwidth() / 2;
			})
			.attr(&quot;dy&quot;, 5)
			.text(function(d) {
			  return d.label;
			});

		bars.append(&quot;g&quot;)
			.attr(&quot;transform&quot;, &quot;translate(0,&quot; + height + &quot;)&quot;)
			.call(d3.axisBottom(x));

		bars.append(&quot;g&quot;)
			.call(d3.axisLeft(y));



	&lt;/script&gt;



	&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->


new progress by fixing the domain

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;multibar d3&lt;/title&gt;
		&lt;script src=&quot;https://d3js.org/d3.v4.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
		&lt;style&gt;
			
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;



	&lt;script&gt;
 
		
		d3.select(&quot;body&quot;)
			.selectAll(&#39;svg&#39;)
			.remove();

		var data = [{&quot;label&quot;: &quot;Toblerone&quot;, &quot;value&quot;: 70}];

		var w = parseInt(280, 10),
			h = parseInt(80, 10);


		var color = d3.scaleOrdinal().range([&#39;#046395&#39;, &#39;#d62828&#39;, &#39;#f77f00&#39;, &#39;#fcbf49&#39;, &#39;#eae2b7&#39;]);


		data.forEach(function(d) {
			d.total = +d.value;
		});


		var margin = {top: 20, right: 10, bottom: 20, left: 20};

	    var width = w - margin.left - margin.right;
	    var height = h - margin.top - margin.bottom;

	
		var x = d3.scaleLinear()
		          .range([0, width]);
		var y = d3.scaleBand()
		          .range([height, 0])
		          .padding(0.1);

		x.domain([0, 100])
		y.domain(data.map(function(d) { return d.total; }));

        var svg = d3.select(&quot;body&quot;)
        	.append(&quot;svg&quot;)
            .attr(&quot;width&quot;, width + margin.left + margin.right)
            .attr(&quot;height&quot;, height + margin.top + margin.bottom)
			.append(&quot;g&quot;)
			.attr(&#39;class&#39;, &#39;barchart&#39;)
			.attr(&quot;transform&quot;, &quot;translate(&quot; + margin.left + &quot;,&quot; + margin.top + &quot;)&quot;);

		var bars = svg.append(&#39;g&#39;).attr(&#39;class&#39;, &#39;bars&#39;);

		bars.selectAll(&quot;.bar&quot;)
			.data(data)
			.enter().append(&quot;rect&quot;)
			.attr(&quot;class&quot;, &quot;bar&quot;)
			.attr(&#39;fill&#39;, function(d, i) {
				return color(i);
			})
			.attr(&quot;width&quot;, function(d) { return x(d.total); })
			.attr(&quot;y&quot;, function(d) { return y(d.value); })
			.attr(&quot;height&quot;, y.bandwidth());

		bars.selectAll(&quot;.label&quot;)
			.data(data)
			.enter().append(&quot;text&quot;)
			.attr(&quot;class&quot;, &quot;label&quot;)
			.attr(&#39;text-anchor&#39;, function(d) {
			  return d.total &lt;= 20 ? &#39;start&#39; : &#39;end&#39;;
			})
			.attr(&quot;x&quot;, function(d) {
			  return d.total &lt;= 20 ? x(d.total) + 20 : x(d.total) - 20;
			})
			.attr(&quot;y&quot;, function(d) {
			  return y(d.value) + y.bandwidth() / 2;
			})
			.attr(&quot;dy&quot;, 5)
			.text(function(d) {
			  return d.label;
			});

		bars.append(&quot;g&quot;)
			.attr(&quot;transform&quot;, &quot;translate(0,&quot; + height + &quot;)&quot;)
			.call(d3.axisBottom(x));

		bars.append(&quot;g&quot;)
			.call(d3.axisLeft(y));



	&lt;/script&gt;



	&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

目前,您的x比例域采用数组的最小值和最大值。

只需将x比例域更改为 x.domain([0, 100]);

这样,比例的跨度将从0到100,与其range一起。

英文:

Currently, your x scale domain take min and max value of your array.

Just changes the x scale domain to x.domain([0, 100]);.

So that the span of the scale goes from 0 to 100 alongside it's range

huangapple
  • 本文由 发表于 2023年7月27日 21:04:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定