

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>d3js &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/d3js/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Mon, 15 Jul 2024 09:26:49 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>d3js &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>修改 d3js 坐標顏色</title>
		<link>https://stackoverflow.max-everyday.com/2024/07/alter-the-color-of-axis-components-in-d3-js/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/07/alter-the-color-of-axis-components-in-d3-js/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 10 Jul 2024 09:44:53 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5860</guid>

					<description><![CDATA[在直接套用別人的範例後，預期的坐標上的顏色不是預...]]></description>
										<content:encoded><![CDATA[
<p>在直接套用別人的範例後，預期的坐標上的顏色不是預期的黑色，很奇怪：</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="466" height="443" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/07/Screenshot-2024-07-10-at-5.36.53 PM.png?v=1720604422" alt="" class="wp-image-5862"/></figure>



<p>文字可以反白，似乎是灰色的字。</p>



<p>範例來源：<br><a href="https://d3-graph-gallery.com/graph/heatmap_basic.html">https://d3-graph-gallery.com/graph/heatmap_basic.html</a></p>



<p>d3js 官方的說明：d3-axis<br><a href="https://d3js.org/d3-axis">https://d3js.org/d3-axis</a></p>



<p>使用解法：<br><a href="https://stackoverflow.com/questions/38414926/looking-for-a-better-way-to-alter-the-color-of-axis-components-in-d3-js-v4">https://stackoverflow.com/questions/38414926/looking-for-a-better-way-to-alter-the-color-of-axis-components-in-d3-js-v4</a></p>



<p>解法是輸出 axis 之後，再套用 selectAll() 即可：</p>



<pre class="wp-block-code"><code>// Add the Y Axis
var axis = svg.append("g")
  .call(d3.axisLeft(y));

axis.selectAll("text")
  .style("stroke", "blue");</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/07/alter-the-color-of-axis-components-in-d3-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>d3js plot textAnchor</title>
		<link>https://stackoverflow.max-everyday.com/2024/05/d3js-plot-textanchor/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/05/d3js-plot-textanchor/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 23 May 2024 04:00:15 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5778</guid>

					<description><![CDATA[在使用 plot 畫圖的時候，預設 text-a...]]></description>
										<content:encoded><![CDATA[
<p>在使用 plot 畫圖的時候，預設 text-anchor 會被設成 medd, 造成 plot 畫出來的圖會長的怪怪的：</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="648" height="683" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-23-at-11.50.13 AM-side.png?v=1716436565" alt="" class="wp-image-5779" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-23-at-11.50.13 AM-side.png?v=1716436565 648w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-23-at-11.50.13 AM-side-569x600.png?v=1716436565 569w" sizes="(max-width: 648px) 100vw, 648px" /></figure>
</div>


<p>plot 官方說明：<a href="https://observablehq.com/plot/marks/text#text-options">https://observablehq.com/plot/marks/text#text-options</a></p>



<p>The following text-specific constant options are also supported:</p>



<ul class="wp-block-list">
<li><strong>textAnchor</strong>&nbsp;&#8211; the&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor" target="_blank" rel="noreferrer noopener">text anchor</a>&nbsp;for horizontal position;&nbsp;<em>start</em>,&nbsp;<em>end</em>, or&nbsp;<em>middle</em></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>text-anchor 說明文件：<a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor">https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor</a></p>



<p>plot source code: <br><a href="https://github.com/observablehq/plot/blob/4bbad40906e851fe5ece146f34ecc854342b08da/src/plot.js#L251">https://github.com/observablehq/plot/blob/4bbad40906e851fe5ece146f34ecc854342b08da/src/plot.js#L251</a></p>



<pre class="wp-block-code"><code>  select(svg)
    .attr("class", className)
    .attr("fill", "currentColor")
    .attr("font-family", "system-ui, sans-serif")
    .attr("font-size", 10)
    .attr("text-anchor", "middle")
    .attr("width", width)
    .attr("height", height)
    .attr("viewBox", `0 0 ${width} ${height}`)
    .attr("aria-label", ariaLabel)
    .attr("aria-description", ariaDescription)
    .call((svg) =&gt;
      // Warning: if you edit this, change defaultClassName.
      svg.append("style").text(
        `:where(.${className}) {
  --plot-background: white;
  display: block;
  height: auto;
  height: intrinsic;
  max-width: 100%;
}
:where(.${className} text),
:where(.${className} tspan) {
  white-space: pre;
}`
      )
    )
    .call(applyInlineStyles, style);</code></pre>



<p>從 plot source code 來看，是hard code, 沒有辦法從外部傳入的參數做設定。</p>



<p>寫死（hard-coding）是指在軟體實作上，將輸出或輸入的相關參數（例如：路徑、輸出的形式或格式）</p>



<ul class="wp-block-list">
<li>解法1，針對新加的圖層，指定 .attr(&#8220;text-anchor&#8221;, &#8220;start&#8221;) 就可以不被 middle 影響。</li>



<li>解法2，透過 javascript selector 移除 plot source code 裡的 .attr(&#8220;text-anchor&#8221;, &#8220;middle&#8221;)。</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/05/d3js-plot-textanchor/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>D3.js Basic Pie Chart with Legend</title>
		<link>https://stackoverflow.max-everyday.com/2024/05/d3-js-basic-pie-chart-with-legend/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/05/d3-js-basic-pie-chart-with-legend/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 13 May 2024 03:41:58 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5728</guid>

					<description><![CDATA[遇到一個參考性佳的pie chart with ...]]></description>
										<content:encoded><![CDATA[
<p>遇到一個參考性佳的pie chart with legend 的範例：<br><a href="https://observablehq.com/@sengokyu/d3-js-basic-pie-chart-with-legend">https://observablehq.com/@sengokyu/d3-js-basic-pie-chart-with-legend</a></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="1308" height="756" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-13-at-11.40.09 AM.png?v=1715571622" alt="" class="wp-image-5729" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-13-at-11.40.09 AM.png?v=1715571622 1308w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-13-at-11.40.09 AM-600x347.png?v=1715571622 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-13-at-11.40.09 AM-1024x592.png?v=1715571622 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-13-at-11.40.09 AM-768x444.png?v=1715571622 768w" sizes="(max-width: 1308px) 100vw, 1308px" /></figure>
</div>


<pre class="wp-block-code"><code>{
  const svg = d3
    .create('svg')
    .attr('width', width)
    .attr('height', height);
  const chart = svg
    .append('g')
    .attr('transform', `translate(${radius},${radius})`);

  chart
    .selectAll(null)
    .data(pieData)
    .enter()
    .append('path')
    .attr('d', arc)
    .attr('fill', d => colorSeq(d.index))
    .attr('stroke', 'grey')
    .style('stroke-width', '1px');

  const legend = svg
    .append('g')
    .attr('transform', `translate(${radius * 2 + 20},0)`);

  legend
    .selectAll(null)
    .data(pieData)
    .enter()
    .append('rect')
    .attr('y', d => labelHeight * d.index * 1.8)
    .attr('width', labelHeight)
    .attr('height', labelHeight)
    .attr('fill', d => colorSeq(d.index))
    .attr('stroke', 'grey')
    .style('stroke-width', '1px');

  legend
    .selectAll(null)
    .data(pieData)
    .enter()
    .append('text')
    .text(d => d.data.key)
    .attr('x', labelHeight * 1.2)
    .attr('y', d => labelHeight * d.index * 1.8 + labelHeight)
    .style('font-family', 'sans-serif')
    .style('font-size', `${labelHeight}px`);

  return svg.node();
}</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>常數們：</p>



<pre class="wp-block-code"><code>labelHeight = 18
radius = 150
height = 300
width = 600</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">相關文章</h2>



<p>Building legends in d3.js<br><a href="https://d3-graph-gallery.com/graph/custom_legend.html">https://d3-graph-gallery.com/graph/custom_legend.html</a></p>



<p>Color legend<br><a href="https://observablehq.com/@d3/color-legend">https://observablehq.com/@d3/color-legend</a></p>



<p>d3js Legends<br><a href="https://observablehq.com/plot/features/legends#legend-options">https://observablehq.com/plot/features/legends#legend-options</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/05/d3-js-basic-pie-chart-with-legend/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to generate 1000 unique color in d3.js</title>
		<link>https://stackoverflow.max-everyday.com/2024/04/how-to-generate-1000-unique-color-in-d3-js/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/04/how-to-generate-1000-unique-color-in-d3-js/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 25 Apr 2024 01:22:32 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5670</guid>

					<description><![CDATA[最快的解法是直接拿別人已經產生好的 array ...]]></description>
										<content:encoded><![CDATA[
<p>最快的解法是直接拿別人已經產生好的 array 就做完了。</p>



<p>為避免資料真的 out of index, 請使用 Modulus (Remainder) 餘數功能。 </p>



<pre class="wp-block-code"><code>console.log(13 % 5);
// Expected output: 3

console.log(-13 % 5);
// Expected output: -3

console.log(4 % 2);
// Expected output: 0

console.log(-4 % 2);
// Expected output: -0</code></pre>



<p></p>



<p>在 d3js 裡，也很一些預設的 color scheme 可以使用。<br><a href="https://observablehq.com/@d3/color-schemes">https://observablehq.com/@d3/color-schemes</a></p>



<p>或參考這篇：<br><a href="https://d3js.org/d3-scale-chromatic/sequential#interpolateTurbo">https://d3js.org/d3-scale-chromatic/sequential#interpolateTurbo</a></p>



<p>或這篇：<br><a href="https://d3js.org/d3-interpolate/color">https://d3js.org/d3-interpolate/color</a></p>



<p>或這篇：<br><a href="https://observablehq.com/@d3/working-with-color">https://observablehq.com/@d3/working-with-color</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>解法：</p>



<p>Here&#8217;s a related answer in case it helps someone:&nbsp;<a href="https://stackoverflow.com/a/25302128/9972273">https://stackoverflow.com/a/25302128/9972273</a></p>



<p>There you have random color generator and can generate infinitely many colors. Below you&#8217;ve 1000.</p>



<pre class="wp-block-code"><code>"#3957ff", "#d3fe14", "#c9080a", "#fec7f8", "#0b7b3e", "#0bf0e9", "#c203c8", "#fd9b39", "#888593", "#906407", "#98ba7f", "#fe6794", "#10b0ff", "#ac7bff", "#fee7c0", "#964c63", "#1da49c", "#0ad811", "#bbd9fd", "#fe6cfe", "#297192", "#d1a09c", "#78579e", "#81ffad", "#739400", "#ca6949", "#d9bf01", "#646a58", "#d5097e", "#bb73a9", "#ccf6e9", "#9cb4b6", "#b6a7d4", "#9e8c62", "#6e83c8", "#01af64", "#a71afd", "#cfe589", "#d4ccd1", "#fd4109", "#bf8f0e", "#2f786e", "#4ed1a5", "#d8bb7d", "#a54509", "#6a9276", "#a4777a", "#fc12c9", "#606f15", "#3cc4d9", "#f31c4e", "#73616f", "#f097c6", "#fc8772", "#92a6fe", "#875b44", "#699ab3", "#94bc19", "#7d5bf0", "#d24dfe", "#c85b74", "#68ff57", "#b62347", "#994b91", "#646b8c", "#977ab4", "#d694fd", "#c4d5b5", "#fdc4bd", "#1cae05", "#7bd972", "#e9700a", "#d08f5d", "#8bb9e1", "#fde945", "#a29d98", "#1682fb", "#9ad9e0", "#d6cafe", "#8d8328", "#b091a7", "#647579", "#1f8d11", "#e7eafd", "#b9660b", "#a4a644", "#fec24c", "#b1168c", "#188cc1", "#7ab297", "#4468ae", "#c949a6", "#d48295", "#eb6dc2", "#d5b0cb", "#ff9ffb", "#fdb082", "#af4d44", "#a759c4", "#a9e03a", "#0d906b", "#9ee3bd", "#5b8846", "#0d8995", "#f25c58", "#70ae4f", "#847f74", "#9094bb", "#ffe2f1", "#a67149", "#936c8e", "#d04907", "#c3b8a6", "#cef8c4", "#7a9293", "#fda2ab", "#2ef6c5", "#807242", "#cb94cc", "#b6bdd0", "#b5c75d", "#fde189", "#b7ff80", "#fa2d8e", "#839a5f", "#28c2b5", "#e5e9e1", "#bc79d8", "#7ed8fe", "#9f20c3", "#4f7a5b", "#f511fd", "#09c959", "#bcd0ce", "#8685fd", "#98fcff", "#afbff9", "#6d69b4", "#5f99fd", "#aaa87e", "#b59dfb", "#5d809d", "#d9a742", "#ac5c86", "#9468d5", "#a4a2b2", "#b1376e", "#d43f3d", "#05a9d1", "#c38375", "#24b58e", "#6eabaf", "#66bf7f", "#92cbbb", "#ddb1ee", "#1be895", "#c7ecf9", "#a6baa6", "#8045cd", "#5f70f1", "#a9d796", "#ce62cb", "#0e954d", "#a97d2f", "#fcb8d3", "#9bfee3", "#4e8d84", "#fc6d3f", "#7b9fd4", "#8c6165", "#72805e", "#d53762", "#f00a1b", "#de5c97", "#8ea28b", "#fccd95", "#ba9c57", "#b79a82", "#7c5a82", "#7d7ca4", "#958ad6", "#cd8126", "#bdb0b7", "#10e0f8", "#dccc69", "#d6de0f", "#616d3d", "#985a25", "#30c7fd", "#0aeb65", "#e3cdb4", "#bd1bee", "#ad665d", "#d77070", "#8ea5b8", "#5b5ad0", "#76655e", "#598100", "#86757e", "#5ea068", "#a590b8", "#c1a707", "#85c0cd", "#e2cde9", "#dcd79c", "#d8a882", "#b256f9", "#b13323", "#519b3b", "#dd80de", "#f1884b", "#74b2fe", "#a0acd2", "#d199b0", "#f68392", "#8ccaa0", "#64d6cb", "#e0f86a", "#42707a", "#75671b", "#796e87", "#6d8075", "#9b8a8d", "#f04c71", "#61bd29", "#bcc18f", "#fecd0f", "#1e7ac9", "#927261", "#dc27cf", "#979605", "#ec9c88", "#8c48a3", "#676769", "#546e64", "#8f63a2", "#b35b2d", "#7b8ca2", "#b87188", "#4a9bda", "#eb7dab", "#f6a602", "#cab3fe", "#ddb8bb", "#107959", "#885973", "#5e858e", "#b15bad", "#e107a7", "#2f9dad", "#4b9e83", "#b992dc", "#6bb0cb", "#bdb363", "#ccd6e4", "#a3ee94", "#9ef718", "#fbe1d9", "#a428a5", "#93514c", "#487434", "#e8f1b6", "#d00938", "#fb50e1", "#fa85e1", "#7cd40a", "#f1ade1", "#b1485d", "#7f76d6", "#d186b3", "#90c25e", "#b8c813", "#a8c9de", "#7d30fe", "#815f2d", "#737f3b", "#c84486", "#946cfe", "#e55432", "#a88674", "#c17a47", "#b98b91", "#fc4bb3", "#da7f5f", "#df920b", "#b7bbba", "#99e6d9", "#a36170", "#c742d8", "#947f9d", "#a37d93", "#889072", "#9b924c", "#23b4bc", "#e6a25f", "#86df9c", "#a7da6c", "#3fee03", "#eec9d8", "#aafdcb", "#7b9139", "#92979c", "#72788a", "#994cff", "#c85956", "#7baa1a", "#de72fe", "#c7bad8", "#85ebfe", "#6e6089", "#9b4d31", "#297a1d", "#9052c0", "#5c75a5", "#698eba", "#d46222", "#6da095", "#b483bb", "#04d183", "#9bcdfe", "#2ffe8c", "#9d4279", "#c909aa", "#826cae", "#77787c", "#a96fb7", "#858f87", "#fd3b40", "#7fab7b", "#9e9edd", "#bba3be", "#f8b96c", "#7be553", "#c0e1ce", "#516e88", "#be0e5f", "#757c09", "#4b8d5f", "#38b448", "#df8780", "#ebb3a0", "#ced759", "#f0ed7c", "#e0eef1", "#0969d2", "#756446", "#488ea8", "#888450", "#61979c", "#a37ad6", "#b48a54", "#8193e5", "#dd6d89", "#8aa29d", "#c679fe", "#a4ac12", "#75bbb3", "#6ae2c1", "#c4fda7", "#606877", "#b2409d", "#5874c7", "#bf492c", "#4b88cd", "#e14ec0", "#b39da2", "#fb8300", "#d1b845", "#c2d083", "#c3caef", "#967500", "#c56399", "#ed5a05", "#aadff6", "#6685f4", "#1da16f", "#f28bff", "#c9c9bf", "#c7e2a9", "#5bfce4", "#e0e0bf", "#e8e2e8", "#ddf2d8", "#9108f8", "#932dd2", "#c03500", "#aa3fbc", "#547c79", "#9f6045", "#04897b", "#966f32", "#d83212", "#039f27", "#df4280", "#ef206e", "#0095f7", "#a5890d", "#9a8f7f", "#bc839e", "#88a23b", "#e55aed", "#51af9e", "#5eaf82", "#9e91fa", "#f76c79", "#99a869", "#d2957d", "#a2aca6", "#e3959e", "#adaefc", "#5bd14e", "#df9ceb", "#fe8fb1", "#87ca80", "#fc986d", "#2ad3d9", "#e8a8bb", "#a7c79c", "#a5c7cc", "#7befb7", "#b7e2e0", "#85f57b", "#f5d95b", "#dbdbff", "#fddcff", "#6e56bb", "#226fa8", "#5b659c", "#58a10f", "#e46c52", "#62abe2", "#c4aa77", "#b60e74", "#087983", "#a95703", "#2a6efb", "#427d92", "#78735c", "#c13c4b", "#437dae", "#9f5f95", "#51886f", "#74838a", "#8e7c5c", "#6e8f60", "#ae7764", "#e6409e", "#be7376", "#9790a8", "#da763c", "#d178c0", "#cf9641", "#66c466", "#94b0ec", "#9db6c6", "#72c2e4", "#70c3ff", "#94ce44", "#bcb8e8", "#73d1e5", "#accbb9", "#ddbb99", "#eeb91b", "#1efcaf", "#bef255", "#926b7c", "#fe6ab0", "#76e4e6", "#ebf219", "#5b4aff", "#bd61dc", "#9e969f", "#bda441", "#e18bcc", "#a9b0b8", "#fc81c6", "#eecc7d", "#eac6fc", "#fecfb3", "#5e7a4e", "#c3268c", "#8d6b48", "#3a8639", "#a66b09", "#877cbd", "#a579a4", "#e74654", "#b97901", "#d772a9", "#25bf83", "#8db15d", "#17bae3", "#e48e3a", "#5abea2", "#b0a892", "#d39e02", "#5cc2c5", "#caa3ed", "#d9a0c8", "#cda9b5", "#a9bfe3", "#ccc9dd", "#6fec8d", "#e9cdc9", "#b2ecb0", "#aa3a41", "#68716c", "#a43fd1", "#0b81a5", "#b34487", "#bc4f03", "#86719c", "#e00251", "#7a70fa", "#a76d8f", "#937b79", "#898687", "#e008ff", "#9388b5", "#709d4f", "#af8e3c", "#81a0a7", "#bc8c6d", "#fd563a", "#5ab166", "#93b391", "#91b5a8", "#b5b43c", "#c9a7d4", "#18d4b9", "#2ddb73", "#ccbdcc", "#e2bc5f", "#cac7a7", "#e5bde3", "#f8b4fc", "#d5d8d8", "#b9f5f4", "#ba1f2e", "#98557f", "#807002", "#825cd0", "#048953", "#b65076", "#7077b0", "#b35b64", "#aa672c", "#c040b9", "#6e81a7", "#249494", "#6a8d84", "#9e7e4b", "#6891d8", "#848bc6", "#d27010", "#a48acb", "#829ab9", "#8e97a9", "#76a285", "#98996c", "#fb7161", "#fd7a2c", "#c0a8a1", "#a7b762", "#b2aec8", "#a5ca79", "#aed8b2", "#cfe5fc", "#b2f1d7", "#6a6475", "#647761", "#358182", "#996abb", "#169987", "#3e99c2", "#ac928c", "#b68bf4", "#f1ac49", "#c1c6cd", "#1ce7ce", "#895391", "#4865db", "#45804c", "#597d2a", "#9c54a9", "#6c6bd3", "#83752e", "#9e52e2", "#b5569a", "#e31c34", "#848b12", "#ac6adc", "#948493", "#1aa34a", "#957be4", "#c664b1", "#dc596a", "#bf76c4", "#ab9927", "#be8cb4", "#a39bc8", "#e76cda", "#12c131", "#84aacb", "#c39a6b", "#84afbd", "#e38ead", "#84bb8b", "#6dce94", "#83cccc", "#fda4c4", "#30e5af", "#ffb1a8", "#b5d5e0", "#e2d037", "#38f1fd", "#beec77", "#9cf65e", "#e6e593", "#ebdff5", "#d9f892", "#6a5e9e", "#3d745f", "#4c7509", "#706c9c", "#83677e", "#be345f", "#695dfa", "#5c7987", "#797170", "#966459", "#d40c62", "#628564", "#b65c45", "#528b2c", "#b76e4c", "#e93d30", "#6f909f", "#7e9989", "#7f9a77", "#f615e0", "#53a5bc", "#f2528f", "#fd4a5e", "#9c9e83", "#bf9186", "#c786d3", "#2abd68", "#a3a3a7", "#8ab340", "#e48f6c", "#b2aeab", "#f4908c", "#b5b697", "#6fcbb6", "#14d1f5", "#e9a5a3", "#d5b0a1", "#f799df", "#c7bdbb", "#8ad5b5", "#f2b6bc", "#dcc99b", "#f4bd97", "#a8dcca", "#8beecd", "#d1dbcb", "#bfea08", "#e2d9d0", "#cfe3de", "#c7e8c4", "#e2e64f", "#acefff", "#5efdfb", "#f2e4a9", "#4a6b95", "#a1388c", "#6e51dc", "#a04a51", "#9a617d", "#d0533b", "#e8148f", "#7d8b4f", "#c16833", "#b8742c", "#a566fa", "#ad815e", "#c57561", "#919434", "#e25aad", "#3da2fd", "#ce70dd", "#e17d04", "#af9c6d", "#7eaca5", "#cc8d8e", "#cd83ec", "#b897bf", "#ada561", "#ae9cde", "#7fbb6f", "#c4a98b", "#acb47a", "#d2a55d", "#a5c141", "#92cee7", "#cdc47c", "#85e508", "#08fd6a", "#63fd10", "#feceeb", "#d5f4aa", "#5a61b2", "#88537f", "#556c76", "#4b6f6f", "#795f64", "#8c5560", "#805d55", "#566f4f", "#6f6838", "#a6422b", "#905806", "#b203cc", "#776d79", "#33816b", "#547a6b", "#71744d", "#c72d29", "#856c6d", "#3f79dc", "#c54962", "#d5324b", "#86778b", "#2c9436", "#6d881c", "#b24be3", "#9770a1", "#827c96", "#b5617a", "#c7592b", "#967b30", "#4b940b", "#7a8672", "#4e944f", "#e54702", "#4b8cf0", "#8588a5", "#7d8db4", "#848e98", "#4f9c91", "#c8688a", "#dd40e2", "#6c9f32", "#c3749c", "#1da890", "#c563fe", "#a888aa", "#4fa4a8", "#e66635", "#659fca", "#6ca3ef", "#11b3cc", "#a898af", "#93a57c", "#9aa0bd", "#95a4ab", "#ea7977", "#ff5acc", "#be97a5", "#f07d5a", "#64b6c4", "#6bbd92", "#fe6ce4", "#83c1a8", "#8fbfbf", "#c1b805", "#d8ae02", "#a9bcb6", "#eca27a", "#8ec0fe", "#bbb8c4", "#c9b78f", "#fc9e5c", "#a0cc02", "#c9b2e7", "#e3b178", "#c8c544", "#e4b7c9", "#73dead", "#b7d349", "#bbd19b", "#6feada", "#e6d5e2", "#eed7ad", "#e7d6fe", "#fcd6dc", "#73ffce", "#a8fbb3", "#9eff99", "#c9f8d7", "#f3ead6", "#a9307b", "#a3405f", "#34774d", "#8a40f0", "#7e6297", "#b815ad", "#c90a4c", "#856926", "#727223", "#825eb5", "#816c58", "#627694", "#39845f", "#cf3574", "#4f8bb4", "#468f9b", "#7b8483", "#8185da", "#dd5c51", "#a78485", "#de5a80", "#ff2e6a", "#bd8739", "#2fa8e2", "#929c93", "#8c9ad3", "#21b3aa", "#fe6616", "#fc4dfc", "#46b8f3", "#a3ad98", "#80c03e", "#a5b0c5", "#68d17e", "#aebdc5", "#99dc87", "#bccee6", "#ebc74d", "#b5e18b", "#dedd74", "#a44c73", "#6c7ad7", "#ec2f05", "#bd5bc6", "#549772", "#b57c87", "#ab7dc6", "#f823ae", "#929d51", "#73a966", "#61af32", "#d48543", "#43c808", "#fb7ca1", "#e49c3d", "#f69704", "#97c0a2", "#b3c27b", "#b9bfae", "#c5c062", "#48e246", "#d6c0b4", "#b7cdfe", "#d7c3ec", "#c7e24f", "#a0ebed", "#c9e0e6", "#ae390e", "#607435", "#943ce1", "#a809e8", "#6c6e78", "#8d5f8e", "#84698d", "#99653a", "#7f6ac3", "#c24944", "#577eba", "#698080", "#9f696a", "#cd03e8", "#a36e7e", "#3c9179", "#5a82da", "#987b86", "#ca5686", "#bf675b", "#de2fb7", "#cf51be", "#c97485", "#6ca1ae", "#7d9cfe", "#ec6c9c", "#e57fbe", "#b0a5b2", "#ca96df", "#abaee3", "#a5b992", "#e0a7e0", "#e2a7fe", "#0cdfd9", "#a2d1cc", "#8ad7ca", "#dabafd", "#eac0b1", "#9ce96c", "#fed46d", "#f1e106", "#96409b", "#5d6a6c", "#915533", "#8756a0", "#826170", "#6d6c64", "#a35123", "#0e7d93", "#6d6d83", "#9c5660", "#3f8000", "#4f61ff", "#097cb0", "#4c779a", "#a54d89", "#c12876", "#a45751", "#af4f2f", "#73776c", "#db0118", "#6c834f", "#0685df", "#8668df", "#028daa", "#a164a6", "#6a8193", "#4c7aff", "#d23699", "#93758a", "#d14856", "#c55c05", "#9172c2", "#b0659a", "#858367", "#a8753a", "#0c90d9", "#93816e", "#6d914b", "#c53cfe", "#668eaa", "#d05198", "#5d9481", "#c3666c", "#3a9d5f", "#f61236", "#b76bb2", "#9a884d", "#4ea456", "#45a634", "#b4841a", "#cf5ae2", "#a58895", "#92918e", "#869d02", "#b572f6", "#7e99ab", "#a285e5", "#e553d6", "#d867c1", "#33af78", "#a89275", "#b382df", "#5faa90", "#f06082", "#cf7e82", "#7ea695", "#8fa2ca", "#ca8d9f", "#e47c90", "#5cb5b3", "#07bea3", "#c099b5", "#96ad9b", "#a3a0fa", "#a0b33f", "#c496f7", "#07c99d", "#e7945b", "#83b9d2", "#0ad13c", "#7dc513", "#baabec", "#e992e4", "#18cfc7", "#b0b6d7", "#c2b1c6", "#ef9ab1", "#c2b87b", "#d1b361", "#82cd5b"</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>上面的解法，是有解決顏色不重覆的問題，但也不易閱讀，也不美。</p>



<p>最佳解法，是使用 d3js 提供的 color, 以 pie chart 的範例來說：</p>



<pre class="wp-block-code"><code>// Create the color scale.
const color = d3.scaleOrdinal()
.domain(formated_data.map(d =&gt; d&#91;chart_config.column_name]))
.range(d3.quantize(t =&gt; d3.interpolateSpectral(t * 0.8 + 0.1), formated_data.length).reverse())</code></pre>



<p>填入color 的 code:</p>



<pre class="wp-block-code"><code>// pop-over
svg.append("g")
.attr("stroke", "white")
.attr("font-size", font_size_int)
.selectAll("path")
.data(arcs)
.join("path")
.attr("fill", d =&gt; color(d.data&#91;chart_config.column_name]))
.attr("d", arc)
.append("title")
.text(function(d) {
  return d.data&#91;chart_config.column_name];
});</code></pre>



<p>就可以做出下圖效果：</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="720" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Pie_chart.png?v=1714354834" alt="" class="wp-image-5685" style="width:840px;height:auto" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Pie_chart.png?v=1714354834 720w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Pie_chart-600x417.png?v=1714354834 600w" sizes="(max-width: 720px) 100vw, 720px" /></figure>



<p>完整範例，參考：<br><a href="https://stackoverflow.max-everyday.com/2024/02/d3js-pie-chart-example/">https://stackoverflow.max-everyday.com/2024/02/d3js-pie-chart-example/</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Grouped Bar Chart 範例：<br></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="986" height="682" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-02-at-2.24.25 PM.png?v=1714631132" alt="" class="wp-image-5703" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-02-at-2.24.25 PM.png?v=1714631132 986w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-02-at-2.24.25 PM-600x415.png?v=1714631132 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Screenshot-2024-05-02-at-2.24.25 PM-768x531.png?v=1714631132 768w" sizes="(max-width: 986px) 100vw, 986px" /></figure>



<p>程式碼：</p>



<pre class="wp-block-code"><code>  // Both x and color encode the age class.
  const ages = new Set(data.map(d =&gt; d.age));

  const x = d3.scaleBand()
      .domain(ages)
      .rangeRound(&#91;0, fx.bandwidth()])
      .padding(0.05);

  const color = d3.scaleOrdinal()
      .domain(ages)
      .range(d3.schemeSpectral&#91;ages.size])
      .unknown("#ccc");

  // Y encodes the height of the bar.
  const y = d3.scaleLinear()
      .domain(&#91;0, d3.max(data, d =&gt; d.population)]).nice()
      .rangeRound(&#91;height - marginBottom, marginTop]);

  // A function to format the value in the tooltip.
  const formatValue = x =&gt; isNaN(x) ? "N/A" : x.toLocaleString("en")

  // Create the SVG container.
  const svg = d3.create("svg")
      .attr("width", width)
      .attr("height", height)
      .attr("viewBox", &#91;0, 0, width, height])
      .attr("style", "max-width: 100%; height: auto;");

  // Append a group for each state, and a rect for each age.
  svg.append("g")
    .selectAll()
    .data(d3.group(data, d =&gt; d.state))
    .join("g")
      .attr("transform", (&#91;state]) =&gt; `translate(${fx(state)},0)`)
    .selectAll()
    .data((&#91;, d]) =&gt; d)
    .join("rect")
      .attr("x", d =&gt; x(d.age))
      .attr("y", d =&gt; y(d.population))
      .attr("width", x.bandwidth())
      .attr("height", d =&gt; y(0) - y(d.population))
      .attr("fill", d =&gt; color(d.age));
</code></pre>



<p>製作 rainbow 範例：<br><a href="https://ithelp.ithome.com.tw/m/articles/10244168">https://ithelp.ithome.com.tw/m/articles/10244168</a></p>



<p>javascript:</p>



<pre class="wp-block-code"><code>const rainbow = d3.scaleSequential(d3.interpolateRainbow).domain(&#91;1, 100]);
console.log(rainbow(50)) // "rgb(177, 238, 87)"</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>D3JS 的官方說明：<br><a href="https://d3js.org/d3-scale/ordinal">https://d3js.org/d3-scale/ordinal</a></p>



<h2 class="wp-block-heading" id="ordinal_range"><em>ordinal</em>.range(<em>range</em>)<a href="https://d3js.org/d3-scale/ordinal#ordinal_range">​</a></h2>



<p><a href="https://observablehq.com/@d3/d3-scaleordinal" target="_blank" rel="noreferrer noopener">Examples</a>&nbsp;·&nbsp;<a href="https://github.com/d3/d3-scale/blob/main/src/ordinal.js" target="_blank" rel="noreferrer noopener">Source</a>&nbsp;· If&nbsp;<em>range</em>&nbsp;is specified, sets the range of the ordinal scale to the specified array of values.</p>



<p>js</p>



<pre class="wp-block-code"><code>const color = d3.scaleOrdinal().range(&#91;"red", "green", "blue"]);</code></pre>



<p>The first element in the domain will be mapped to the first element in&nbsp;<em>range</em>, the second domain value to the second range value, and so on. If there are fewer elements in the range than in the domain, the scale will reuse values from the start of the range. If&nbsp;<em>range</em>&nbsp;is not specified, this method returns the current range.</p>



<h2 class="wp-block-heading" id="026e">scaleOrdinal：序數比例尺</h2>



<pre class="wp-block-preformatted">const fruit = ["蘋果", "檸檬", "香蕉", "葡萄", "橘子"]<br>const color = ['red', 'green', 'yellow', 'purple', 'orange'];<br><br>const scale = d3.scaleOrdinal()<br>  .domain(fruit).range(color);<br><br>console.log(scale("橘子")) // orange</pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Domain ＆ Range 輸入域與輸出域<br><a href="https://ithelp.ithome.com.tw/articles/10274653">https://ithelp.ithome.com.tw/articles/10274653</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">scaleLinear</h2>



<p>使用d3.scaleLinear()創造一個線性比例尺，而domain()是定義域，range()是值域，相當於將domain中的數據集映射到range的數據集中。</p>



<p>允許 overflow, 以下例說來，每加減1就是相差25.</p>



<pre class="wp-block-code"><code>var dataset = var dataset = &#91;1, 2, 3, 4, 5];
&#91;1, 2, 3, 4, 5];
var min = d3.min(dataset);
var max = d3.max(dataset);

var linear = d3.scaleLinear()
    .domain(&#91;min, max])
    .range(&#91;0, 100]);

console.log(linear(1))  // 0
console.log(linear(2))  // 25
console.log(linear(5))  // 100
console.log(linear(-1)) // -50
console.log(linear(6))  // 125</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">scaleBand</h2>



<p>不允許 overflow, 以下例說來，每加減1就是相差25.</p>



<pre class="wp-block-code"><code>let scale = d3.scaleBand().domain(&#91;1,2,3,4]).range(&#91;0,100])

console.log(scale(1))  // 0
console.log(scale(2))  // 25
console.log(scale(3))  // 50
console.log(scale(4))  // 100
console.log(scale(5))  // undefined
console.log(scale(0))  // undefined</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">scaleOrdinal</h2>



<pre class="wp-block-code"><code>let scale = d3.scaleOrdinal().domain(&#91;'A','B']).range(&#91;1,2,3,4])
console.log(scale('A'))     // 1
console.log(scale('B'))     // 2
console.log(scale('C'))     // 3

// 當「輸入不存在的值」，若定義域的值的數量小於值域，
// 則會接著定義域的最後一個的輸出結果進行輸出，直至結束，然後從新再來一遍
console.log(scale('D'))     // 4
console.log(scale('E'))     // 1
console.log(scale('F'))     // 2

let scale = d3.scaleOrdinal().domain(&#91;'A','B','C','E']).range(&#91;1,2,3])
console.log(scale('A'))     // 1
console.log(scale('B'))     // 2
console.log(scale('C'))     // 3

// 這裡的E，屬於存在於定義域內，但沒有應該對應的值，所以會從頭對應一下
console.log(scale('E'))     // 1

// 當「輸入不存在的值」，
// 會接著E走，所以輸出的是2
console.log(scale('F'))     // 2
console.log(scale('G'))     // 3</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">scaleQuantize</h2>



<p>domain 是連續，range 分散。允許overflow.</p>



<pre class="wp-block-code"><code>let scale = d3.scaleQuantize().domain(&#91;0,10]).range(&#91;'A','B','C'])
console.log(scale(0))  //A
console.log(scale(1))  //A
console.log(scale(2))  //A
console.log(scale(3))  //A
console.log(scale(4))  //B
console.log(scale(11)) //C
console.log(scale(-1)) //A</code></pre>



<p></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>使用下面的程式碼，就可以拿到彩色的清單：</p>



<pre class="wp-block-code"><code>const color = d3.scaleOrdinal()
  .domain(sorted_legend_data.map(d =&gt; d&#91;"title"]))
  .range(d3.quantize(t =&gt; d3.interpolateSpectral(t), sorted_legend_data.length).reverse())

sorted_legend_data.forEach((d)=&gt; {
  const fill_color = color(d.title)
});</code></pre>



<p></p>



<p>interpolateSpectral 內容：</p>



<pre class="wp-block-code"><code>d3.interpolateSpectral(0.5) // rgb(251, 248, 176)
d3.interpolateSpectral(0) // rgb(158, 1, 66)
d3.interpolateSpectral(1) // rgb(94, 79, 162)</code></pre>



<p></p>



<p>分別的效果如下：</p>



<h2 class="wp-block-heading">d3.interpolateSpectral</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart.png?v=1717058794" alt="" class="wp-image-5789" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart.png?v=1717058794 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-600x429.png?v=1717058794 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolateHsl(&#8220;red&#8221;,&#8221;blue&#8221;)</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-1.png?v=1717058816" alt="" class="wp-image-5790" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-1.png?v=1717058816 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-1-600x429.png?v=1717058816 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolateHslLong(&#8220;red&#8221;,&#8221;blue&#8221;)</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-2.png?v=1717058880" alt="" class="wp-image-5791" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-2.png?v=1717058880 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-2-600x429.png?v=1717058880 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolateLab(&#8220;red&#8221;,&#8221;blue&#8221;)</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-3.png?v=1717058898" alt="" class="wp-image-5792" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-3.png?v=1717058898 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-3-600x429.png?v=1717058898 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolatePRGn</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-4.png?v=1717058919" alt="" class="wp-image-5793" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-4.png?v=1717058919 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-4-600x429.png?v=1717058919 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolateViridis</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-5.png?v=1717058937" alt="" class="wp-image-5794" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-5.png?v=1717058937 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-5-600x429.png?v=1717058937 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolateCubehelixDefault</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-6.png?v=1717058953" alt="" class="wp-image-5795" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-6.png?v=1717058953 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-6-600x429.png?v=1717058953 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<h2 class="wp-block-heading">d3.interpolateRainbow</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="700" height="500" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-7.png?v=1717058967" alt="" class="wp-image-5796" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-7.png?v=1717058967 700w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/Pie_chart-7-600x429.png?v=1717058967 600w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
</div>


<p></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">相關文章</h2>



<p>d3 &#8211; 力引导图(二) 为节点设置多种唯一颜色方案<br><a href="https://blog.csdn.net/Charonmomo/article/details/122084806">https://blog.csdn.net/Charonmomo/article/details/122084806</a></p>



<p>Working with Color<br><a href="https://observablehq.com/@d3/working-with-color">https://observablehq.com/@d3/working-with-color</a></p>



<p>Color interpolation<br><a href="https://d3js.org/d3-interpolate/color">https://d3js.org/d3-interpolate/color</a></p>



<p>Color legend<br><a href="https://observablehq.com/@d3/color-legend">https://observablehq.com/@d3/color-legend</a></p>



<p>d3-scale-chromatic<br><a href="https://observablehq.com/@d3/color-schemes?collection=@d3/d3-scale-chromatic">https://observablehq.com/@d3/color-schemes?collection=@d3/d3-scale-chromatic</a></p>



<p>Color Scales<br><a href="https://observablehq.com/plot/features/scales">https://observablehq.com/plot/features/scales</a></p>



<p>d3.scaleSequential + continuous color legend example<br><a href="https://observablehq.com/@tmcw/d3-scalesequential-continuous-color-legend-example">https://observablehq.com/@tmcw/d3-scalesequential-continuous-color-legend-example</a></p>



<p>Legend with smooth gradient and corresponding labels<br><a href="https://stackoverflow.com/questions/49739119/legend-with-smooth-gradient-and-corresponding-labels">https://stackoverflow.com/questions/49739119/legend-with-smooth-gradient-and-corresponding-labels</a></p>



<p>d3-axis<br><a href="https://d3js.org/d3-axis">https://d3js.org/d3-axis</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/04/how-to-generate-1000-unique-color-in-d3-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>學習d3js 最佳資源</title>
		<link>https://stackoverflow.max-everyday.com/2024/04/the-d3-js-graph-gallery/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/04/the-d3-js-graph-gallery/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 23 Apr 2024 06:13:43 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5661</guid>

					<description><![CDATA[雖然 d3js 官方的網站寫的很好，範例也寫的很...]]></description>
										<content:encoded><![CDATA[
<p>雖然 d3js 官方的網站寫的很好，範例也寫的很簡單，但還有做的更好，差別在預期的落差，d3js 官方的教學有點預設看的人略懂javascript/html, 所以理解上偏難，還有對範例的解說也是比較精簡或或幾乎沒有，跳過了很多動作，很容易讓人卡關。</p>



<p>這個網站就寫的很好：The D3.js Graph Gallery<br><a href="https://d3-graph-gallery.com/index.html">https://d3-graph-gallery.com/index.html</a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="879" height="757" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Screenshot-2024-04-23-at-2.12.51 PM.png?v=1713852821" alt="" class="wp-image-5663" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Screenshot-2024-04-23-at-2.12.51 PM.png?v=1713852821 879w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Screenshot-2024-04-23-at-2.12.51 PM-600x517.png?v=1713852821 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/Screenshot-2024-04-23-at-2.12.51 PM-768x661.png?v=1713852821 768w" sizes="(max-width: 879px) 100vw, 879px" /></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<ul class="wp-block-list">
<li>Official
<ul class="wp-block-list">
<li><a href="https://d3js.org/getting-started">https://d3js.org/getting-started</a></li>



<li><a href="https://observablehq.com/plot/getting-started">https://observablehq.com/plot/getting-started</a></li>
</ul>
</li>



<li>Non-official
<ul class="wp-block-list">
<li><a href="https://observablehq.com/@sengokyu/d3-js-basic-pie-chart-with-legend">https://observablehq.com/@sengokyu/d3-js-basic-pie-chart-with-legend</a></li>



<li>The D3.js Graph Gallery<br><a href="https://d3-graph-gallery.com/index.html">https://d3-graph-gallery.com/index.html</a></li>



<li>Create Pie Chart using D3<br><a href="https://www.tutorialsteacher.com/d3js/create-pie-chart-using-d3js">https://www.tutorialsteacher.com/d3js/create-pie-chart-using-d3js</a></li>
</ul>
</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/04/the-d3-js-graph-gallery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>d3js stacked bar chart / grouped bar chart 的坑</title>
		<link>https://stackoverflow.max-everyday.com/2024/04/d3js-stacked-bar-chart-grouped-bar-chart-data-type/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/04/d3js-stacked-bar-chart-grouped-bar-chart-data-type/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 19 Apr 2024 13:52:30 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5648</guid>

					<description><![CDATA[今天鬼打牆，花了2小時多，trace code ...]]></description>
										<content:encoded><![CDATA[
<p>今天鬼打牆，花了2小時多，trace code 才發現，又是文字型別造成的坑。實在吐血。</p>



<p>上一次遇到，因為已經隔了一個月，完全忘了有這件事，所以重複犯了一樣的錯誤。</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="878" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/截圖-2024-04-19-下午9.39.27-1024x878.png?v=1713534166" alt="" class="wp-image-5650" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/截圖-2024-04-19-下午9.39.27-1024x878.png?v=1713534166 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/截圖-2024-04-19-下午9.39.27-600x515.png?v=1713534166 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/截圖-2024-04-19-下午9.39.27-768x659.png?v=1713534166 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/截圖-2024-04-19-下午9.39.27-1536x1318.png?v=1713534166 1536w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/04/截圖-2024-04-19-下午9.39.27.png?v=1713534166 1576w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>d3js grouped bar chart sample:<br><a href="https://observablehq.com/@d3/grouped-bar-chart/2?intent=fork">https://observablehq.com/@d3/grouped-bar-chart/2?intent=fork</a></p>



<p>d3js stacked bar char sample:<br><a href="https://observablehq.com/@d3/stacked-bar-chart/2?intent=fork">https://observablehq.com/@d3/stacked-bar-chart/2?intent=fork</a></p>



<p>從範例看不出來是型別會造成的錯誤，如果x 軸是數字型別，會無法讓 grouped / stacked 成立，會只剩下x 軸，沒有 bar.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>但是，相同的情況無法使用在 line chart / dot chart 上，因為是連續性的數值資料，如果變成「字串」型別，則會讓 x 軸的每一個單位，都無法被隱藏。</p>



<p>詳細的 warning:</p>



<pre class="wp-block-code"><code>plot.js:1807 Warning: some data associated with the x scale are strings that appear to be numbers. If these strings represent numbers, you should parse or coerce them to numbers. Numbers are typically associated with a "linear" scale rather than a "point" scale. If you want to treat this data as ordinal, you can specify the interval of the x scale (e.g., 1 for integers), or you can suppress this warning by setting the type of the x scale to "point".</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/04/d3js-stacked-bar-chart-grouped-bar-chart-data-type/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Javascript 文數字的排序</title>
		<link>https://stackoverflow.max-everyday.com/2024/03/extract-a-number-from-a-string-using-javascript/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/03/extract-a-number-from-a-string-using-javascript/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 11 Mar 2024 09:03:43 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5550</guid>

					<description><![CDATA[以字母排序時, 會遇到排成 chr1, chr1...]]></description>
										<content:encoded><![CDATA[
<p>以字母排序時, 會遇到排成 chr1, chr17, chr9 , 如果想排成 chr1,chr9,ch17 則需要取出內容再轉成數字, 解法:</p>



<pre class="wp-block-code"><code>let a = "chr17_gl0000204";
let matches = str.match(/\d+/g);
// &#91;'17', '0000204']</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="812" height="396" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/03/chrome_2024-03-11_17-03_cg.png?v=1710147808" alt="" class="wp-image-5551" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/03/chrome_2024-03-11_17-03_cg.png?v=1710147808 812w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/03/chrome_2024-03-11_17-03_cg-600x293.png?v=1710147808 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/03/chrome_2024-03-11_17-03_cg-768x375.png?v=1710147808 768w" sizes="(max-width: 812px) 100vw, 812px" /></figure>



<p></p>



<p>How to split alphanumeric string but keep last alpha value javascript:</p>



<pre class="wp-block-code"><code>string.match(/&#91;a-z]+|\d+/ig)</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>完整範例：</p>



<pre class="wp-block-code"><code>function isNumeric(value) {
    return /^-?\d+(\.\d+)?$/.test(value);
}

function format_alpha_to_num(s) {
    let new_s = &#91;];
    if(s.length) {
        let num_matches = (s).match(/&#91;a-zA-Z]+|\d+/g);
        if(num_matches) {
            num_matches.forEach((element) => {
                if(isNumeric(element)) {
                    let t = "00000000" + element;
                    new_s.push(t.substring(t.length-8, t.length));
                } else {
                    new_s.push(element);
                }
            });
        }
    }
    return new_s.join('');
}

sort_value = format_alpha_to_num(label_value);</code></pre>



<p></p>



<p>實際執行畫面比較：</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1400" height="1400" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/collage-sort-alpha.jpg?v=1716429681" alt="" class="wp-image-5775" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/collage-sort-alpha.jpg?v=1716429681 1400w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/collage-sort-alpha-600x600.jpg?v=1716429681 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/collage-sort-alpha-1024x1024.jpg?v=1716429681 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/collage-sort-alpha-300x300.jpg?v=1716429681 300w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/05/collage-sort-alpha-768x768.jpg?v=1716429681 768w" sizes="(max-width: 1400px) 100vw, 1400px" /></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/03/extract-a-number-from-a-string-using-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>save/export an SVG file after creating an SVG with D3.js</title>
		<link>https://stackoverflow.max-everyday.com/2024/02/save-export-an-svg-file-after-creating-an-svg-with-d3-js/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/02/save-export-an-svg-file-after-creating-an-svg-with-d3-js/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 16 Feb 2024 06:50:13 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5428</guid>

					<description><![CDATA[把程式產生的SVG 另存新檔的方法:https:...]]></description>
										<content:encoded><![CDATA[
<p>把程式產生的SVG 另存新檔的方法:<br><a href="https://stackoverflow.com/questions/23218174/how-do-i-save-export-an-svg-file-after-creating-an-svg-with-d3-js-ie-safari-an">https://stackoverflow.com/questions/23218174/how-do-i-save-export-an-svg-file-after-creating-an-svg-with-d3-js-ie-safari-an</a></p>



<pre class="wp-block-code"><code>const svgData = $("#you_chart_div")&#91;0].outerHTML;
const svgBlob = new Blob(&#91;svgData], {type:"image/svg+xml;charset=utf-8"});
const objectUrl = URL.createObjectURL(svgBlob);
const downloadLink = document.createElement("a");
downloadLink.href = objectUrl;
downloadLink.download = "your_filename.svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);</code></pre>



<ul class="wp-block-list">
<li>該不該用 outerHTML 還是 innerHTML, 取決於有沒有 append SVG 到 div 之中, 如果是用 append, 則使用 innerHTML 即可.</li>



<li>innerHTML / outerHTML 是 javascript 寫法, 如果全用 jQuery object, 請使用 .html() 即可取得 innerHTML.</li>
</ul>



<p></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>svg 轉 png, 請參考:<br>How to Convert an SVG to an Image in the Browser?<br><a href="https://stackoverflow.max-everyday.com/2023/03/how-to-convert-an-svg-to-an-image-in-the-browser/">https://stackoverflow.max-everyday.com/2023/03/how-to-convert-an-svg-to-an-image-in-the-browser/</a></p>



<p></p>



<p>網路上的範例: SVG D3 to PNG<br><a href="https://gist.github.com/vicapow/758fce6aa4c5195d24be">https://gist.github.com/vicapow/758fce6aa4c5195d24be</a></p>



<pre class="wp-block-code"><code>var doctype = '&lt;?xml version="1.0" standalone="no"?>'
  + '&lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';

// serialize our SVG XML to a string.
var source = (new XMLSerializer()).serializeToString(d3.select('svg').node());

// create a file blob of our SVG.
var blob = new Blob(&#91; doctype + source], { type: 'image/svg+xml;charset=utf-8' });

var url = window.URL.createObjectURL(blob);

// Put the svg into an image tag so that the Canvas element can read it in.
var img = d3.select('body').append('img')
 .attr('width', 100)
 .attr('height', 100)
 .node();


img.onload = function(){
  // Now that the image has loaded, put the image into a canvas element.
  var canvas = d3.select('body').append('canvas').node();
  canvas.width = 100;
  canvas.height = 100;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);
  var canvasUrl = canvas.toDataURL("image/png");
  var img2 = d3.select('body').append('img')
    .attr('width', 100)
    .attr('height', 100)
    .node();
  // this is now the base64 encoded version of our PNG! you could optionally 
  // redirect the user to download the PNG by sending them to the url with 
  // `window.location.href= canvasUrl`.
  img2.src = canvasUrl; 
}
// start loading the image.
img.src = url;</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/02/save-export-an-svg-file-after-creating-an-svg-with-d3-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>d3js pie chart example</title>
		<link>https://stackoverflow.max-everyday.com/2024/02/d3js-pie-chart-example/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/02/d3js-pie-chart-example/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 15 Feb 2024 03:16:49 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[d3js]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5423</guid>

					<description><![CDATA[在使用 d3js 畫圓餅圖, 範例雖然看起來有點...]]></description>
										<content:encoded><![CDATA[
<p>在使用 d3js 畫圓餅圖, 範例雖然看起來有點難, 實際上還滿簡單的, 而且javascript 的元件寫的很物件化, 所以操作起來, 很快就可以理解與上手。</p>



<h2 class="wp-block-heading">d3js官方範例</h2>



<ul class="wp-block-list">
<li>Pie chart component<br><a href="https://observablehq.com/@d3/pie-chart-component">https://observablehq.com/@d3/pie-chart-component</a></li>



<li>Donut chart<br><a href="https://observablehq.com/@d3/donut-chart">https://observablehq.com/@d3/donut-chart</a></li>
</ul>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="868" height="780" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/02/chrome_2024-02-15_11-04_as.png?v=1707966517" alt="" class="wp-image-5424" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/02/chrome_2024-02-15_11-04_as.png?v=1707966517 868w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/02/chrome_2024-02-15_11-04_as-600x539.png?v=1707966517 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2024/02/chrome_2024-02-15_11-04_as-768x690.png?v=1707966517 768w" sizes="(max-width: 868px) 100vw, 868px" /></figure>



<h2 class="wp-block-heading">相關教學</h2>



<p>Most basic pie chart in d3.js<br><a href="https://d3-graph-gallery.com/graph/pie_basic.html">https://d3-graph-gallery.com/graph/pie_basic.html</a></p>



<p>Create Pie Chart using D3<br><a href="https://www.tutorialsteacher.com/d3js/create-pie-chart-using-d3js">https://www.tutorialsteacher.com/d3js/create-pie-chart-using-d3js</a></p>



<p>使用心得</p>



<p>如果你的伺服器程式語言是JSP, 直接使用 d3js 的範例程式:</p>



<pre class="wp-block-code"><code>svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "middle")
.selectAll("text")
.data(arcs)
.join("text")
.attr("transform", d =&gt; `translate(${arcLabel.centroid(d)})`)
.selectAll("tspan")
.data(d =&gt; {
const lines = `${title(d.data)}`.split(/\n/);
return (d.endAngle - d.startAngle) &gt; 0.25 ? lines : lines.slice(0, 1);
})
.join("tspan")
.attr("x", 0)
.attr("y", (_, i) =&gt; `${i * 1.1}em`)
.attr("font-weight", (_, i) =&gt; i ? null : "bold")
.text(d =&gt; d);</code></pre>



<p>會造成 JSP EL( Expression Language, 表達式語言) 的錯誤.  解法, 是避免使用 ${&#8230;} 即可, 修改後的範例:</p>



<pre class="wp-block-code"><code>const width = 928;
const height = Math.min(width, 500);

// Create the color scale.
const color = d3.scaleOrdinal()
.domain(data.map(d =&gt; d.name))
.range(d3.quantize(t =&gt; d3.interpolateSpectral(t * 0.8 + 0.1), data.length).reverse())

// Create the pie layout and arc generator.
const pie = d3.pie()
.sort(null)
.value(d =&gt; d.value);

const arc = d3.arc()
.innerRadius(0)
.outerRadius(Math.min(width, height) / 2 - 1);

const labelRadius = arc.outerRadius()() * 0.8;

// A separate arc generator for labels.
const arcLabel = d3.arc()
.innerRadius(labelRadius)
.outerRadius(labelRadius);

const arcs = pie(data);

// Create the SVG container.
const svg = d3.select("#ifilter_data_visualization_chart").append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", &#91;-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

svg.append("g")
.attr("stroke", "white")
.selectAll("path")
.data(arcs)
.join("path")
.attr("fill", d =&gt; color(d.data.name))
.attr("d", arc)
.append("title")
.text(function(d) { return d.data.name; });

svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.attr("text-anchor", "middle")
.selectAll("text")
.data(arcs)
.join("text")
.attr("transform", function(d) { return "translate(" + arcLabel.centroid(d) + ")"; })
.call(text =&gt; text.append("tspan")
    .attr("y", "-0.4em")
    .attr("font-weight", "bold")
    .text(d =&gt; d.data.name))
.call(text =&gt; text.filter(d =&gt; (d.endAngle - d.startAngle) &gt; 0.25).append("tspan")
    .attr("x", 0)
    .attr("y", "0.7em")
    .attr("fill-opacity", 0.7)          
    .text(d =&gt; d.data.value));</code></pre>



<p></p>



<h2 class="wp-block-heading">pie 與 donut 的差異在 innerRadius</h2>



<p><strong>pie:</strong></p>



<pre class="wp-block-code"><code><strong>innerRadius = 0</strong>, // inner radius of pie, in pixels (non-zero for donut)
outerRadius = Math.min(width, height) / 2, // outer radius of pie, in pixels
labelRadius = (innerRadius * 0.2 + outerRadius * 0.8), // center radius of labels
format = ",", // a format specifier for values (in the label)
names, // array of names (the domain of the color scale)
colors, // array of colors for names
stroke = innerRadius &gt; 0 ? "none" : "white", // stroke separating widths
strokeWidth = 1, // width of stroke separating wedges
strokeLinejoin = "round", // line join of stroke separating wedges
padAngle = stroke === "none" ? 1 / outerRadius : 0, // angular separation between wedges, in radians
</code></pre>



<p><strong>donut:</strong></p>



<pre class="wp-block-code"><code><strong>innerRadius = Math.min(width, height) / 3,</strong> // inner radius of pie, in pixels (non-zero for donut)
outerRadius = Math.min(width, height) / 2, // outer radius of pie, in pixels
labelRadius = (innerRadius + outerRadius) / 2, // center radius of labels
format = ",", // a format specifier for values (in the label)
names, // array of names (the domain of the color scale)
colors, // array of colors for names
stroke = innerRadius &gt; 0 ? "none" : "white", // stroke separating widths
strokeWidth = 1, // width of stroke separating wedges
strokeLinejoin = "round", // line join of stroke separating wedges
padAngle = stroke === "none" ? 1 / outerRadius : 0, // angular separation between wedges</code></pre>



<p><strong>差異的程式碼:</strong></p>



<p>innerRadius=0, 就會變成 pie</p>



<pre class="wp-block-code"><code>//let innerRadius=0;
let innerRadius = Math.min(width, height) / 3;

const arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(Math.min(width, height) / 2 - 1);</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>控制顏色，參考：<br><a href="https://stackoverflow.max-everyday.com/2024/04/how-to-generate-1000-unique-color-in-d3-js/">https://stackoverflow.max-everyday.com/2024/04/how-to-generate-1000-unique-color-in-d3-js/</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/02/d3js-pie-chart-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
