调整 D3 SVG 元素并在窗口大小更改时溢出

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

Resize D3 SVG Element and Overflow on windows size change

问题

我仍在学习D3v7并创建热图,我会添加更多功能。

现在我遇到一个问题,当窗口大小改变时,无法设置SVG元素的高度和宽度。

通过论坛上的几篇帖子,我设法使我的SVG元素能够动态调整其高度和宽度。

.attr(
  "viewBox",
  `0 0 ${i_width + margin.right} ${i_height + margin.top + margin.bottom}`
)

但现在我希望在窗口太小以至于SVG元素中的字体不再可读时,我的SVG元素获得一个滚动条。我希望在可更改的宽度范围内具有最小宽度,并在超过此宽度时创建滚动条。

以下是您的JavaScript代码,已经翻译好:

const data = [
    { group: "A", variable: "v1", value: 98 }, { group: "A", variable: "v2", value: 95 },
    // ...(数据继续)
    { group: "F", variable: "v5", value: 48 }, { group: "F", variable: "v6", value: 102 }
];

// 设置维度和大小
var parentDiv = document.getElementById("heatmap")
var i_width = parentDiv.clientWidth;
var i_height = 450;

const margin = { top: 0, right: 50, bottom: 30, left: 100 };
const width = i_width - margin.left - margin.right;
const height = i_height - margin.top - margin.bottom;
const extent = d3.extent(data, d => d.value);
const max = d3.max(data, function (d) { return d.value })
// 获取数据源的唯一组和变量,用于X和Y轴
const myGroups = data.map(function (d) { return d.group })
const myVars = data.map(function (d) { return d.variable })

// 将SVG元素附加到#heatmap
const svg = d3.select("#heatmap")
    .append("svg") //将svg附加到#heatmap
    .attr("id", "hm_svg")
    .attr("viewBox", `0 0 ${i_width + margin.right} ${i_height + margin.top + margin.bottom}`)

// ...(其余的JavaScript代码)

请注意,我已经将注释部分删除,以便更容易阅读。如有需要,请根据您的要求添加注释。

英文:

I am still in the process of learning D3v7 and create a heatmap, which I expand with more functions.

Now I have a problem to set the height and width of my SVG element when the Windows Size is changed.

Through several posts from the forum, I managed to get my SVG element to dynamically adjust its height and width.

.attr(
  "viewBox",
  `0 0 ${i_width + margin.right} ${i_height + margin.top + margin.bottom}`
)

But now I would like that my SVG Element get an Scrollbar when the windows is to small that the font in the SVG element is no longer readable. I want a min width up to where the width is changeable and over it then an Scrollbar is created.

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

<!-- language: lang-js -->

const data = [
{ group: &quot;A&quot;, variable: &quot;v1&quot;, value: 98 }, { group: &quot;A&quot;, variable: &quot;v2&quot;, value: 95 },
{ group: &quot;A&quot;, variable: &quot;v3&quot;, value: 22 }, { group: &quot;A&quot;, variable: &quot;v4&quot;, value: 14 },
{ group: &quot;A&quot;, variable: &quot;v5&quot;, value: 59 }, { group: &quot;A&quot;, variable: &quot;v6&quot;, value: 52 },
{ group: &quot;A&quot;, variable: &quot;v7&quot;, value: 88 }, { group: &quot;A&quot;, variable: &quot;v8&quot;, value: 20 },
{ group: &quot;A&quot;, variable: &quot;v9&quot;, value: 99 }, { group: &quot;A&quot;, variable: &quot;v10&quot;, value: 66 },
{ group: &quot;B&quot;, variable: &quot;v1&quot;, value: 37 }, { group: &quot;B&quot;, variable: &quot;v2&quot;, value: 50 },
{ group: &quot;B&quot;, variable: &quot;v3&quot;, value: 81 }, { group: &quot;B&quot;, variable: &quot;v4&quot;, value: 79 },
{ group: &quot;B&quot;, variable: &quot;v5&quot;, value: 84 }, { group: &quot;B&quot;, variable: &quot;v6&quot;, value: 91 },
{ group: &quot;B&quot;, variable: &quot;v7&quot;, value: 82 }, { group: &quot;B&quot;, variable: &quot;v8&quot;, value: 89 },
{ group: &quot;B&quot;, variable: &quot;v9&quot;, value: 6 }, { group: &quot;B&quot;, variable: &quot;v10&quot;, value: 67 },
{ group: &quot;C&quot;, variable: &quot;v1&quot;, value: 96 }, { group: &quot;C&quot;, variable: &quot;v2&quot;, value: 13 },
{ group: &quot;C&quot;, variable: &quot;v3&quot;, value: 98 }, { group: &quot;C&quot;, variable: &quot;v4&quot;, value: 10 },
{ group: &quot;C&quot;, variable: &quot;v5&quot;, value: 86 }, { group: &quot;C&quot;, variable: &quot;v6&quot;, value: 23 },
{ group: &quot;C&quot;, variable: &quot;v7&quot;, value: 74 }, { group: &quot;C&quot;, variable: &quot;v8&quot;, value: 47 },
{ group: &quot;C&quot;, variable: &quot;v9&quot;, value: 73 }, { group: &quot;C&quot;, variable: &quot;v10&quot;, value: 40 },
{ group: &quot;D&quot;, variable: &quot;v1&quot;, value: 75 }, { group: &quot;D&quot;, variable: &quot;v2&quot;, value: 18 },
{ group: &quot;D&quot;, variable: &quot;v3&quot;, value: 92 }, { group: &quot;D&quot;, variable: &quot;v4&quot;, value: 43 },
{ group: &quot;D&quot;, variable: &quot;v5&quot;, value: 16 }, { group: &quot;D&quot;, variable: &quot;v6&quot;, value: 27 },
{ group: &quot;D&quot;, variable: &quot;v7&quot;, value: 76 }, { group: &quot;D&quot;, variable: &quot;v8&quot;, value: 24 },
{ group: &quot;D&quot;, variable: &quot;v9&quot;, value: 1 }, { group: &quot;D&quot;, variable: &quot;v10&quot;, value: 87 },
{ group: &quot;E&quot;, variable: &quot;v1&quot;, value: 44 }, { group: &quot;E&quot;, variable: &quot;v2&quot;, value: 29 },
{ group: &quot;E&quot;, variable: &quot;v3&quot;, value: 58 }, { group: &quot;E&quot;, variable: &quot;v4&quot;, value: 55 },
{ group: &quot;E&quot;, variable: &quot;v5&quot;, value: 65 }, { group: &quot;E&quot;, variable: &quot;v6&quot;, value: 56 },
{ group: &quot;E&quot;, variable: &quot;v7&quot;, value: 9 }, { group: &quot;E&quot;, variable: &quot;v8&quot;, value: 78 },
{ group: &quot;E&quot;, variable: &quot;v9&quot;, value: 49 }, { group: &quot;E&quot;, variable: &quot;v10&quot;, value: 36 },
{ group: &quot;F&quot;, variable: &quot;v1&quot;, value: 35 }, { group: &quot;F&quot;, variable: &quot;v2&quot;, value: 80 },
{ group: &quot;F&quot;, variable: &quot;v3&quot;, value: 8 }, { group: &quot;F&quot;, variable: &quot;v4&quot;, value: 46 },
{ group: &quot;F&quot;, variable: &quot;v5&quot;, value: 48 }, { group: &quot;F&quot;, variable: &quot;v6&quot;, value: 102 }
];
// Setze Dimension und Gr&#246;&#223;e
var parentDiv = document.getElementById(&quot;heatmap&quot;)
var i_width = parentDiv.clientWidth;
var i_height = 450;
//const margin = {top: 40, right: 60, bottom: 40, left: 50};
const margin = { top: 0, right: 50, bottom: 30, left: 100 };
const width = i_width - margin.left - margin.right;
const height = i_height - margin.top - margin.bottom;
const extent = d3.extent(data, d =&gt; d.value);
const max = d3.max(data, function (d) { return d.value })
// Holt mir die Unique Group und Variables von der Datenquelle f&#252;r die X- und Y-Achse
// d[value] ist wenn value eine Variable ist 
// d[&#39;value&#39;] ist das gleiche wie d.value um den Key aus dem Array zu erhalten
const myGroups = data.map(function (d) { return d.group })
const myVars = data.map(function (d) { return d.variable })
// Anf&#252;gen von svg Elementen zu #heatmap
const svg = d3.select(&quot;#heatmap&quot;)
.append(&quot;svg&quot;) //Anf&#252;gen von svg an #heatmap
.attr(&quot;id&quot;, &quot;hm_svg&quot;)
//.attr(&quot;width&quot;, width + margin.left + margin.right)
//.attr(&quot;height&quot;, height + margin.top + margin.bottom)
.attr(&quot;viewBox&quot;, `0 0 ${i_width + margin.right} ${i_height + margin.top + margin.bottom}`)
const g_area = d3.select(&quot;svg&quot;)
// Anf&#252;gen einer Gruppe an das SVG Element
.append(&quot;g&quot;) //Anf&#252;gen von g (Groups) an svg
.attr(&quot;id&quot;, &quot;hm_area&quot;)
.attr(&quot;transform&quot;, `translate(${margin.left},${margin.top})`);
// Color Skalierung
const myColor = d3.scaleLinear()
.range([&quot;#f9f9f9&quot;, &quot;#6e69b3&quot;, /*&quot;#69b3a2&quot;*/])
.domain([extent[0], (Math.ceil(extent[1] / 50) * 50)])
// X-Skalierung
const x_scale = d3.scaleBand()
.range([0, width])
.domain(myGroups)
.padding(0.01);
// X-Achse erstellen
const xAxis = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_x_axis&quot;)
xAxis.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_x_axis_value&quot;)
.attr(&quot;transform&quot;, `translate(0, ${height})`)
.style(&quot;color&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;16px&quot;)
.style(&quot;font-family&quot;, &quot;arial&quot;)
.call(d3.axisBottom(x_scale));
// Einf&#252;gen xAxis Label
xAxis.append(&quot;text&quot;)
.attr(&quot;id&quot;, &quot;hm_x_axis_label&quot;)
.attr(&quot;x&quot;, width / 2)
.attr(&quot;y&quot;, height + 40)
.style(&quot;fill&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;18px&quot;)
.style(&quot;font-family&quot;, &quot;sans-serif&quot;)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.text(&quot;Label X&quot;);
// Y-Skalierung
const y_scale = d3.scaleBand()
.range([height, 0])
.domain(myVars)
.padding(0.03);
// Y-Achse erstellen
const yAxis = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_y_axis&quot;)
yAxis.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_y_axis_value&quot;)
.style(&quot;color&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;16px&quot;)
.style(&quot;font-family&quot;, &quot;arial&quot;)
.call(d3.axisLeft(y_scale));
// Einf&#252;gen yAxis Label
yAxis.append(&quot;text&quot;)
.attr(&quot;id&quot;, &quot;hm_y_axis_label&quot;)
.attr(&quot;y&quot;, - 35)         //Y ist hierbei die horizontale ausrichtung
.attr(&quot;x&quot;, - height / 2) //X ist hierbei die vertikale ausrichtung
.attr(&quot;transform&quot;, &quot;rotate(-90)&quot;)
.style(&quot;fill&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;18px&quot;)
.style(&quot;font-family&quot;, &quot;sans-serif&quot;)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.text(&quot;Label Y&quot;);
// ColorScale Legend
const GradColors = [myColor(Math.ceil(extent[1] / 50) * 50), myColor(extent[0])];
const g_colorScale = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;colorLegend&quot;)
//console.log(Math.ceil(extent[1] / 100) * 100)
// Erstelle yColorscale
var yColorscale = d3.scaleLinear()
// Mit Math.ceil zum n&#228;chsten 100er Runden
.domain([0, Math.ceil(extent[1] / 50) * 50])
.range([height - 70, 0]);
// Add scales to axis
var y_axis_color = d3.axisRight(yColorscale)
.ticks(1);
//Append group and insert axis
g_colorScale.append(&quot;g&quot;)
.attr(&quot;transform&quot;, `translate(${width + 35},${(height / 2) - 150})`)
.call(y_axis_color);
//defs Element sind zum speichern von Opbjekten, die zu einem sp&#228;teren zeitpunkt gebraucht werden
const g_def = g_colorScale.append(&quot;defs&quot;)
.append(&quot;linearGradient&quot;)
.attr(&quot;id&quot;, &quot;grad&quot;)
.attr(&quot;x1&quot;, &quot;0%&quot;)
.attr(&quot;x2&quot;, &quot;0%&quot;)
.attr(&quot;y1&quot;, &quot;0%&quot;)
.attr(&quot;y2&quot;, &quot;100%&quot;);
g_def.selectAll(&quot;stop&quot;)
.data(GradColors)
.enter()
.append(&quot;stop&quot;)
.style(&quot;stop-color&quot;, function (d) { return d; })
.attr(&quot;offset&quot;, function (d, i) {
return 100 * (i / (GradColors.length - 1)) + &quot;%&quot;;
})
g_colorScale.append(&quot;rect&quot;)
.attr(&quot;id&quot;, &quot;hm_colorScale_legend&quot;)
.attr(&quot;x&quot;, width + 20)
.attr(&quot;y&quot;, (height / 2) - 150)
.attr(&quot;rx&quot;, &quot;4px&quot;)
.attr(&quot;width&quot;, 15)
.attr(&quot;height&quot;, height - 70)
.attr(&quot;stroke&quot;, &quot;black&quot;)
.attr(&quot;stroke-width&quot;, &quot;0.5px&quot;)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.style(&quot;fill&quot;, &quot;url(#grad)&quot;);
// Erstellen eines Tooltip
const tooltip = d3.select(&quot;#heatmap&quot;)
.append(&quot;div&quot;)
.attr(&quot;id&quot;, &quot;tooltip&quot;)
.style(&quot;position&quot;, &quot;absolute&quot;)
.style(&quot;opacity&quot;, 0)
.style(&quot;background-color&quot;, &quot;white&quot;)
.style(&quot;border&quot;, &quot;solid&quot;)
.style(&quot;border-width&quot;, &quot;2px&quot;)
.style(&quot;border-radius&quot;, &quot;5px&quot;)
.style(&quot;padding&quot;, &quot;5px&quot;);
const mouseover = function (event, d) {
tooltip
.style(&quot;opacity&quot;, 1)
d3.select(this).select(&quot;#hm_node&quot;)
.style(&quot;stroke&quot;, &quot;black&quot;)
.style(&quot;stroke-width&quot;, &quot;2px&quot;)
};
const mousemove = function (event, d) {
tooltip
.text(&quot;Wert: &quot; + d.value)
.style(&quot;left&quot;, (event.x) + 20 + &quot;px&quot;)
.style(&quot;top&quot;, (event.y) - 10 + &quot;px&quot;)
};
const mouseleave = function (d) {
tooltip.style(&quot;opacity&quot;, 0)
d3.select(this).select(&quot;#hm_node&quot;)
.style(&quot;stroke&quot;, &quot;none&quot;)
};
// Erstellen von Rechtecken 
// Wird erstellt um den node sowie sein Label in eine gruppe (g) zu packen
const g_nodes = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_nodes&quot;)
.selectAll(&quot;rect&quot;)
.data(data)
.enter()
//F&#220;r jeden Datensatz ein &quot;g&quot; Element erstellen
.append(&quot;g&quot;)
.attr(&quot;class&quot;, &quot;hm_node_grp&quot;)
.attr(&quot;group&quot;, function (d) { return d.group })
.attr(&quot;variable&quot;, function (d) { return d.variable })
.attr(&quot;value&quot;, function (d) { return d.value })
.on(&quot;mouseover&quot;, mouseover)
.on(&quot;mousemove&quot;, mousemove)
.on(&quot;mouseleave&quot;, mouseleave)
.on(&quot;click&quot;, function (d, i) { console.log(i) });
const rect_node = g_nodes
.append(&quot;rect&quot;) //Anf&#252;gen von RECT-Elementen
.attr(&quot;x&quot;, function (d) { return x_scale(d.group) })
.attr(&quot;y&quot;, function (d) { return y_scale(d.variable) })
.attr(&quot;id&quot;, &quot;hm_node&quot;)
// &quot;Bandwidth&quot; ist &#252;ber die Methode &quot;BandScale&quot; verf&#252;gbar
.attr(&quot;width&quot;, x_scale.bandwidth())
.attr(&quot;height&quot;, y_scale.bandwidth())
.style(&quot;fill&quot;, function (d) { return myColor(d.value) })
// Einf&#252;gen der Labels zu den Rechtecken
const rect_label = g_nodes
.append(&quot;text&quot;)
.attr(&quot;class&quot;, &quot;hm_label&quot;)
.attr(&quot;x&quot;, (d) =&gt; x_scale(d.group))
.attr(&quot;y&quot;, (d) =&gt; y_scale(d.variable))
.attr(&quot;dx&quot;, x_scale.bandwidth() / 2)
.attr(&quot;dy&quot;, y_scale.bandwidth() / 2)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.attr(&quot;dominant-baseline&quot;, &quot;central&quot;)
.style(&quot;font-size&quot;, &quot;12px&quot;)
.style(&quot;font-family&quot;, &quot;sans-serif&quot;)
.text((d) =&gt; d.value);

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;             
&lt;!-- Code Vorschlage mit STRG+Leertaste aktivieren--&gt;
&lt;head&gt;              
&lt;meta charset=&quot;utf-8&quot; /&gt;    &lt;!-- Welche Sonderzeichen verwendet werden k&#246;nnen --&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;    &lt;!-- Wie sollte sich die Seite auf einem Handy verhalten --&gt;
&lt;title&gt; 1.Grundger&#252;st &lt;/title&gt;      &lt;!-- title als Tag --&gt;
&lt;style&gt;
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;             
&lt;div id=&quot;heatmap&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://d3js.org/d3.v7.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/body&gt;     &lt;!-- HTML schlie&#223;ender Tag--&gt; 
&lt;/html&gt;     &lt;!-- HTML schlie&#223;ender Tag--&gt;

<!-- end snippet -->

Can anyone help me with this?

答案1

得分: 1

// Setze Dimension und Größe
var parentDiv = document.getElementById("heatmap")
var i_width = parentDiv.clientWidth;
var i_height = 450;

// Anfügen von svg Elementen zu #heatmap
const svg = d3.select("#heatmap")
                .append("svg") //Anfügen von svg an #heatmap
                    .attr("id", "hm_svg")
                    .style('min-width','700px')
                    .attr("viewBox", `0 0 ${i_width + margin.right} ${i_height + margin.top + margin.bottom}`)
英文:

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

<!-- language: lang-js -->

                const data = [
{group:&quot;A&quot;,variable:&quot;v1&quot;,value: 98},{group:&quot;A&quot;,variable:&quot;v2&quot;,value:95},
{group:&quot;A&quot;,variable:&quot;v3&quot;,value: 22},{group:&quot;A&quot;,variable:&quot;v4&quot;,value:14},
{group:&quot;A&quot;,variable:&quot;v5&quot;,value: 59},{group:&quot;A&quot;,variable:&quot;v6&quot;,value:52},
{group:&quot;A&quot;,variable:&quot;v7&quot;,value: 88},{group:&quot;A&quot;,variable:&quot;v8&quot;,value:20},
{group:&quot;A&quot;,variable:&quot;v9&quot;,value: 99},{group:&quot;A&quot;,variable:&quot;v10&quot;,value:66},
{group:&quot;B&quot;,variable:&quot;v1&quot;,value: 37},{group:&quot;B&quot;,variable:&quot;v2&quot;,value:50},
{group:&quot;B&quot;,variable:&quot;v3&quot;,value: 81},{group:&quot;B&quot;,variable:&quot;v4&quot;,value:79},
{group:&quot;B&quot;,variable:&quot;v5&quot;,value: 84},{group:&quot;B&quot;,variable:&quot;v6&quot;,value:91},
{group:&quot;B&quot;,variable:&quot;v7&quot;,value: 82},{group:&quot;B&quot;,variable:&quot;v8&quot;,value:89},
{group:&quot;B&quot;,variable:&quot;v9&quot;,value: 6},{group:&quot;B&quot;,variable:&quot;v10&quot;,value:67},
{group:&quot;C&quot;,variable:&quot;v1&quot;,value: 96},{group:&quot;C&quot;,variable:&quot;v2&quot;,value:13},
{group:&quot;C&quot;,variable:&quot;v3&quot;,value: 98},{group:&quot;C&quot;,variable:&quot;v4&quot;,value:10},
{group:&quot;C&quot;,variable:&quot;v5&quot;,value: 86},{group:&quot;C&quot;,variable:&quot;v6&quot;,value:23},
{group:&quot;C&quot;,variable:&quot;v7&quot;,value: 74},{group:&quot;C&quot;,variable:&quot;v8&quot;,value:47},
{group:&quot;C&quot;,variable:&quot;v9&quot;,value: 73},{group:&quot;C&quot;,variable:&quot;v10&quot;,value:40},
{group:&quot;D&quot;,variable:&quot;v1&quot;,value: 75},{group:&quot;D&quot;,variable:&quot;v2&quot;,value:18},
{group:&quot;D&quot;,variable:&quot;v3&quot;,value: 92},{group:&quot;D&quot;,variable:&quot;v4&quot;,value:43},
{group:&quot;D&quot;,variable:&quot;v5&quot;,value: 16},{group:&quot;D&quot;,variable:&quot;v6&quot;,value:27},
{group:&quot;D&quot;,variable:&quot;v7&quot;,value: 76},{group:&quot;D&quot;,variable:&quot;v8&quot;,value:24},
{group:&quot;D&quot;,variable:&quot;v9&quot;,value: 1},{group:&quot;D&quot;,variable:&quot;v10&quot;,value:87},
{group:&quot;E&quot;,variable:&quot;v1&quot;,value: 44},{group:&quot;E&quot;,variable:&quot;v2&quot;,value:29},
{group:&quot;E&quot;,variable:&quot;v3&quot;,value: 58},{group:&quot;E&quot;,variable:&quot;v4&quot;,value:55},
{group:&quot;E&quot;,variable:&quot;v5&quot;,value: 65},{group:&quot;E&quot;,variable:&quot;v6&quot;,value:56},
{group:&quot;E&quot;,variable:&quot;v7&quot;,value: 9},{group:&quot;E&quot;,variable:&quot;v8&quot;,value:78},
{group:&quot;E&quot;,variable:&quot;v9&quot;,value: 49},{group:&quot;E&quot;,variable:&quot;v10&quot;,value:36},
{group:&quot;F&quot;,variable:&quot;v1&quot;,value: 35},{group:&quot;F&quot;,variable:&quot;v2&quot;,value:80},
{group:&quot;F&quot;,variable:&quot;v3&quot;,value: 8},{group:&quot;F&quot;,variable:&quot;v4&quot;,value:46},
{group:&quot;F&quot;,variable:&quot;v5&quot;,value: 48},{group:&quot;F&quot;,variable:&quot;v6&quot;,value:102}
];
// Setze Dimension und Gr&#246;&#223;e
var parentDiv = document.getElementById(&quot;heatmap&quot;)
var i_width = parentDiv.clientWidth;
var i_height = 450;
//const margin = {top: 40, right: 60, bottom: 40, left: 50};
const margin = {top: 0, right: 50, bottom: 30, left: 100};
const width = i_width - margin.left - margin.right;
const height = i_height - margin.top - margin.bottom;
const extent = d3.extent(data, d =&gt; d.value); 
const max = d3.max(data, function(d){return d.value})
// Holt mir die Unique Group und Variables von der Datenquelle f&#252;r die X- und Y-Achse
// d[value] ist wenn value eine Variable ist 
// d[&#39;value&#39;] ist das gleiche wie d.value um den Key aus dem Array zu erhalten
const myGroups = data.map(function(d){return d.group})
const myVars = data.map(function(d){return d.variable})
// Anf&#252;gen von svg Elementen zu #heatmap
const svg = d3.select(&quot;#heatmap&quot;)
.append(&quot;svg&quot;) //Anf&#252;gen von svg an #heatmap
.attr(&quot;id&quot;, &quot;hm_svg&quot;)
.style(&#39;min-width&#39;,&#39;700px&#39;)
//.attr(&quot;width&quot;, width + margin.left + margin.right)
//.attr(&quot;height&quot;, height + margin.top + margin.bottom)
.attr(&quot;viewBox&quot;, `0 0 ${i_width + margin.right} ${i_height + margin.top + margin.bottom}`)
const g_area = d3.select(&quot;svg&quot;)                           
// Anf&#252;gen einer Gruppe an das SVG Element
.append(&quot;g&quot;) //Anf&#252;gen von g (Groups) an svg
.attr(&quot;id&quot;, &quot;hm_area&quot;)
.attr(&quot;transform&quot;, `translate(${margin.left},${margin.top})`);
// Color Skalierung
const myColor = d3.scaleLinear()
.range([&quot;#f9f9f9&quot;, &quot;#6e69b3&quot;, /*&quot;#69b3a2&quot;*/])
.domain([extent[0],(Math.ceil(extent[1] / 50) * 50)])
// X-Skalierung
const x_scale = d3.scaleBand()
.range([0, width])
.domain(myGroups)
.padding(0.01);
// X-Achse erstellen
const xAxis = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_x_axis&quot;)
xAxis.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_x_axis_value&quot;)
.attr(&quot;transform&quot;, `translate(0, ${height})`)
.style(&quot;color&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;16px&quot;)
.style(&quot;font-family&quot;, &quot;arial&quot;)
.call(d3.axisBottom(x_scale));     
// Einf&#252;gen xAxis Label
xAxis.append(&quot;text&quot;)
.attr(&quot;id&quot;, &quot;hm_x_axis_label&quot;)
.attr(&quot;x&quot;, width / 2)
.attr(&quot;y&quot;, height + 40)
.style(&quot;fill&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;18px&quot;)
.style(&quot;font-family&quot;, &quot;sans-serif&quot;)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.text(&quot;Label X&quot;);
// Y-Skalierung
const y_scale = d3.scaleBand()
.range([height, 0])
.domain(myVars)
.padding(0.03);
// Y-Achse erstellen
const yAxis = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_y_axis&quot;)
yAxis.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_y_axis_value&quot;)
.style(&quot;color&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;16px&quot;)
.style(&quot;font-family&quot;, &quot;arial&quot;)
.call(d3.axisLeft(y_scale));
// Einf&#252;gen yAxis Label
yAxis.append(&quot;text&quot;)
.attr(&quot;id&quot;, &quot;hm_y_axis_label&quot;)
.attr(&quot;y&quot;, - 35)         //Y ist hierbei die horizontale ausrichtung
.attr(&quot;x&quot;, - height / 2) //X ist hierbei die vertikale ausrichtung
.attr(&quot;transform&quot;, &quot;rotate(-90)&quot;)
.style(&quot;fill&quot;, &quot;rgba(0, 0, 0, 0.65)&quot;)
.style(&quot;font-size&quot;, &quot;18px&quot;)
.style(&quot;font-family&quot;, &quot;sans-serif&quot;)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.text(&quot;Label Y&quot;);
// ColorScale Legend
const GradColors = [myColor(Math.ceil(extent[1] / 50) * 50), myColor(extent[0])];
const g_colorScale = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;colorLegend&quot;)
//console.log(Math.ceil(extent[1] / 100) * 100)
// Erstelle yColorscale
var yColorscale = d3.scaleLinear()
// Mit Math.ceil zum n&#228;chsten 100er Runden
.domain([0, Math.ceil(extent[1] / 50) * 50])
.range([height - 70, 0]);
// Add scales to axis
var y_axis_color = d3.axisRight(yColorscale)
.ticks(1);
//Append group and insert axis
g_colorScale.append(&quot;g&quot;)
.attr(&quot;transform&quot;, `translate(${width + 35},${(height / 2) - 150})`)
.call(y_axis_color);
//defs Element sind zum speichern von Opbjekten, die zu einem sp&#228;teren zeitpunkt gebraucht werden
const g_def = g_colorScale.append(&quot;defs&quot;)
.append(&quot;linearGradient&quot;)
.attr(&quot;id&quot;, &quot;grad&quot;)
.attr(&quot;x1&quot;, &quot;0%&quot;)
.attr(&quot;x2&quot;, &quot;0%&quot;)
.attr(&quot;y1&quot;, &quot;0%&quot;)
.attr(&quot;y2&quot;, &quot;100%&quot;);            
g_def.selectAll(&quot;stop&quot;)
.data(GradColors)
.enter()
.append(&quot;stop&quot;)
.style(&quot;stop-color&quot;, function(d){return d;})
.attr(&quot;offset&quot;, function(d,i){
return 100 * (i / (GradColors.length - 1)) + &quot;%&quot;;
})
g_colorScale.append(&quot;rect&quot;)
.attr(&quot;id&quot;, &quot;hm_colorScale_legend&quot;)
.attr(&quot;x&quot;, width + 20)
.attr(&quot;y&quot;, (height / 2) - 150)
.attr(&quot;rx&quot;, &quot;4px&quot;)
.attr(&quot;width&quot;, 15)
.attr(&quot;height&quot;, height - 70)
.attr(&quot;stroke&quot;, &quot;black&quot;)
.attr(&quot;stroke-width&quot;, &quot;0.5px&quot;)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.style(&quot;fill&quot;, &quot;url(#grad)&quot;);
// Erstellen eines Tooltip
const tooltip = d3.select(&quot;#heatmap&quot;)
.append(&quot;div&quot;)
.attr(&quot;id&quot;, &quot;tooltip&quot;)
.style(&quot;position&quot;, &quot;absolute&quot;)
.style(&quot;opacity&quot;, 0)
.style(&quot;background-color&quot;, &quot;white&quot;)
.style(&quot;border&quot;, &quot;solid&quot;)
.style(&quot;border-width&quot;, &quot;2px&quot;)
.style(&quot;border-radius&quot;, &quot;5px&quot;)
.style(&quot;padding&quot;, &quot;5px&quot;);
const mouseover = function(event,d) {
tooltip
.style(&quot;opacity&quot;, 1)
d3.select(this).select(&quot;#hm_node&quot;)
.style(&quot;stroke&quot;, &quot;black&quot;) 
.style(&quot;stroke-width&quot;, &quot;2px&quot;) 
};
const mousemove = function(event,d) {
tooltip
.text(&quot;Wert: &quot; + d.value)
.style(&quot;left&quot;, (event.x) + 20 + &quot;px&quot;)
.style(&quot;top&quot;, (event.y) - 10 + &quot;px&quot;)
};
const mouseleave = function(d) {
tooltip.style(&quot;opacity&quot;, 0)
d3.select(this).select(&quot;#hm_node&quot;)
.style(&quot;stroke&quot;, &quot;none&quot;)
};
// Erstellen von Rechtecken 
// Wird erstellt um den node sowie sein Label in eine gruppe (g) zu packen
const g_nodes = g_area
.append(&quot;g&quot;)
.attr(&quot;id&quot;, &quot;hm_nodes&quot;)
.selectAll(&quot;rect&quot;)
.data(data)
.enter()
//F&#220;r jeden Datensatz ein &quot;g&quot; Element erstellen
.append(&quot;g&quot;)
.attr(&quot;class&quot;, &quot;hm_node_grp&quot;)
.attr(&quot;group&quot;, function(d) {return d.group})
.attr(&quot;variable&quot;,function(d) {return d.variable})
.attr(&quot;value&quot;, function(d) {return d.value})
.on(&quot;mouseover&quot;, mouseover)
.on(&quot;mousemove&quot;, mousemove)
.on(&quot;mouseleave&quot;, mouseleave)
.on(&quot;click&quot;, function(d, i){console.log(i)});
const rect_node = g_nodes
.append(&quot;rect&quot;) //Anf&#252;gen von RECT-Elementen
.attr(&quot;x&quot;, function(d) {return x_scale(d.group)})
.attr(&quot;y&quot;, function(d) {return y_scale(d.variable)})
.attr(&quot;id&quot;, &quot;hm_node&quot;)
// &quot;Bandwidth&quot; ist &#252;ber die Methode &quot;BandScale&quot; verf&#252;gbar
.attr(&quot;width&quot;, x_scale.bandwidth())
.attr(&quot;height&quot;, y_scale.bandwidth())
.style(&quot;fill&quot;, function(d) {return myColor(d.value)})
// Einf&#252;gen der Labels zu den Rechtecken
const rect_label = g_nodes
.append(&quot;text&quot;)
.attr(&quot;class&quot;, &quot;hm_label&quot;)
.attr(&quot;x&quot;, (d) =&gt; x_scale(d.group))
.attr(&quot;y&quot;, (d) =&gt; y_scale(d.variable))
.attr(&quot;dx&quot;, x_scale.bandwidth()/2)
.attr(&quot;dy&quot;, y_scale.bandwidth()/2)
.attr(&quot;text-anchor&quot;, &quot;middle&quot;)
.attr(&quot;dominant-baseline&quot;, &quot;central&quot;)
.style(&quot;font-size&quot;, &quot;12px&quot;)
.style(&quot;font-family&quot;, &quot;sans-serif&quot;)
.text((d) =&gt; d.value);

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;             
&lt;!-- Code Vorschlage mit STRG+Leertaste aktivieren--&gt;
&lt;head&gt;              
&lt;meta charset=&quot;utf-8&quot; /&gt;    &lt;!-- Welche Sonderzeichen verwendet werden k&#246;nnen --&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;    &lt;!-- Wie sollte sich die Seite auf einem Handy verhalten --&gt;
&lt;title&gt; 1.Grundger&#252;st &lt;/title&gt;      &lt;!-- title als Tag --&gt;
&lt;style&gt;
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;             
&lt;div id=&quot;heatmap&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://d3js.org/d3.v7.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/body&gt;     &lt;!-- HTML schlie&#223;ender Tag--&gt; 
&lt;/html&gt;     &lt;!-- HTML schlie&#223;ender Tag--&gt;

<!-- end snippet -->

Does css "min-width" can help you here?

const svg = d3.select(&quot;#heatmap&quot;)
.style(&#39;min-width&#39;,&#39;700px&#39;)

huangapple
  • 本文由 发表于 2023年8月5日 01:35:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838090.html
匿名

发表评论

匿名网友

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

确定