首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WinForms实时图表图表标题

问WinForms实时图表图表标题
EN

Stack Overflow用户
提问于 2017-03-08 17:56:30
回答 1查看 3.3K关注 0票数 2

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

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

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

代码语言:javascript
复制
        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轴之上)。有什么想法吗?

​

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-09 12:51:04

这似乎起到了关键作用:

代码语言:javascript
复制
    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);
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42668102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档