[D3] Add label text

本文介绍如何使用D3.js在SVG中为节点添加图像和文本。通过创建图像容器,并利用数据绑定技术将图像和文本添加到指定节点上。文中详细展示了如何设置图像的位置、大小及文本的对齐方式等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If we want to add text to a node or a image

 

            // Create container for the images
            const svgNodes = svg
                .append('g')
                .attr('class', 'nodes')
                .selectAll('circle')
                .data(d3.values(nodes))
                .enter().append('g');

            // Add image to the nodes
            svgNodes
                .append('image')
                .attr('xlink:href', d => `/static/media/${d.name.toLowerCase()}.png`)
                .attr('x', -25)
                .attr('y', -25)
                .attr('height', 50)
                .attr('width', 50);

// Add text svgNodes .append(
"text") .attr('text-anchor', 'middle') .attr('dy', '.35em') .attr('y', -30) .attr('class', 'label') .text(d => d.name);
.label {
  pointer-events: none;
  font: 8px sans-serif;
  text-transform: uppercase;
  color: black;
}

 

转载于:https://www.cnblogs.com/Answer1215/p/7492269.html

### 回答1: 您可以使用`font`属性来设置标签文本的字体样式,包括字体名称、字号和字体样式。下面是示例代码: ``` for i, sheet_name in enumerate(self.sheet_names): button = tk.Button(self.panel_left, text=sheet_name, command=lambda name=sheet_name: self.show_sheet(name), bg=&#39;#D3D3D3&#39;, fg=&#39;blue&#39;, font=(&#39;Arial&#39;, 10, &#39;bold&#39;)) button.used = False button.grid(row=i, column=0, sticky="ew", padx=2, pady=2) button.bind("<Enter>", lambda event, button=button: button.config(relief="sunken")) button.bind("<Leave>", lambda event, button=button: button.config(relief="raised")) button.bind("<Button-1>", lambda event, button=button: mark_used(button)) button.config(font=(&#39;Arial&#39;, 12)) ``` 在这段代码中,我们使用`config`方法,将按钮的字体样式设置为Arial字体,字号为12。这样就可以将按钮的标签文本字体样式设置为Arial字体,字号为12了。 ### 回答2: 要修改filemenu.add_command中的標簽文本的字體,我們可以使用Tkinter中的Font模塊。首先,我們需要在代碼的開頭導入Font模塊。然後,我們可以創建一個新的字體對象,並將其應用於標簽文本。 以下是修改代碼中標簽文本字體的示例代碼: ```python from tkinter import Tk, Menu, Label, Frame, messagebox from tkinter.font import Font # 創建主窗口 root = Tk() # 創建菜單 menubar = Menu(root) root.config(menu=menubar) filemenu = Menu(menubar) menubar.add_cascade(label="File", menu=filemenu) # 創建字體對象 font = Font(family="Times New Roman", size=12, weight="bold") # 將字體應用於標簽文本 filemenu.add_command(label="PA綫", command=lambda: self.load_excel("D:\點檢系統存放資料夾\點檢明細\點檢内容明細.xlsx", "PA綫"), font=font) # 啟動主窗口的事件循環 root.mainloop() ``` 在這個示例代碼中,我們創建了一個新的字體對象,並將其應用於標簽文本。你可以根據需要調整字體的相關屬性,比如字體名稱(family),大小(size),粗細(weight)等。 需要注意的是,這段代碼中的self.load_excel()可能是在一個類的內部,因此需要確保代碼中的self鍵字正確指向相應的對象。如果不在類的內部,請刪除self並直接調用load_excel()函數。 ### 回答3: 要修改这段代码中标签文本的字体,可以使用`font`参数来设置字体属性。代码可以修改为: ``` filemenu.add_command(label="PA綫", command=lambda: self.load_excel("D:\點檢系統存放資料夾\點檢明細\點檢内容明細.xlsx", "PA綫"), font=("微软雅黑", 12)) ``` 在上面的代码中,`font=("微软雅黑", 12)`表示将字体设置为微软雅黑,字号为12。您可以根据需要修改字体的名称和大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值