gpt4 book ai didi

c# - WinForms Livecharts 图表标题

转载 作者:行者123 更新时间:2023-11-30 23:15:21 29 4
gpt4 key购买 nike

我在 WinForms 中使用 LiveCharts。我不使用 WPF 的原因是因为我不想在 WPF 中重写 GUI,所以我想看看我是否可以让 LiveCharts 在 WinForms 中工作。

我将 LiveCharts 控件作为图像保存为 PDF,因此标题需要在图表本身上。

我找不到在图表上添加标题的任何功能。我尝试过的是以下内容:

        VisualElement title = new VisualElement();
title.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
title.VerticalAlignment = System.Windows.VerticalAlignment.Top;
title.X = 0.5;
title.Y = maxYVal;

TextBlock titleText = new TextBlock();
titleText.Text = chartName;
var newTitleFont = HelperFunctions.NewTypeFaceFromFont(titleFont);
titleText.FontFamily = newTitleFont.FontFamily;
titleText.FontStyle = newTitleFont.Style;
titleText.FontSize = titleFont.Size;
title.UIElement = titleText;

cartChart.VisualElements.Add(title);

上面的代码只是在图表本身上添加了一个标签(在y轴范围内)。标题需要独立(在 y 轴上方)。有什么想法吗?

enter image description here

最佳答案

这似乎可以解决问题:

    public static TableLayoutPanel AddTitleToChart(Control chart,string title, System.Drawing.Font titleFont)
{

Label label = new Label();
label.AutoSize = true;
label.Dock = System.Windows.Forms.DockStyle.Fill;
label.Font = titleFont;
label.Location = new System.Drawing.Point(3, 0);
label.Name = "label1";
label.Size = new System.Drawing.Size(1063, 55);
label.TabIndex = 0;
label.Text = title;
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label.BackColor = chart.BackColor;

chart.Dock = System.Windows.Forms.DockStyle.Fill;

TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.AutoSize = true;
tableLayoutPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
tableLayoutPanel.BackColor = System.Drawing.Color.White;
tableLayoutPanel.ColumnCount = 1;
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1069F));
tableLayoutPanel.Controls.Add(label, 0, 0);
tableLayoutPanel.Controls.Add(chart, 0, 1);
tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
tableLayoutPanel.Location = new System.Drawing.Point(0, 0);
tableLayoutPanel.Name = "tableLayoutPanel1";
tableLayoutPanel.RowCount = 2;
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel.Size = new System.Drawing.Size(1069, 662);
tableLayoutPanel.TabIndex = 2;

return (tableLayoutPanel);
}

关于c# - WinForms Livecharts 图表标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668102/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com