简体   繁体   English

WinForms Livecharts图表标题

[英]WinForms Livecharts Chart Title

I'm using LiveCharts in WinForms. 我在WinForms中使用LiveCharts。 Reason why I'm not using WPF is because I don't want to rewrite the GUI in WPF, so I'm trying to see if I can make LiveCharts work in WinForms. 我不使用WPF的原因是因为我不想在WPF中重写GUI,所以我试图查看是否可以使LiveCharts在WinForms中工作。

I'm saving the LiveCharts control as an image to a PDF, so the title needs to be on the chart itself. 我将LiveCharts控件另存为PDF图像,因此标题必须在图表本身上。

I cannot find any functionality for adding a title on the chart. 我找不到在图表上添加标题的任何功能。 What I have tried is the following: 我尝试了以下内容:

        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);

The above code only adds a label on the chart itself (within the y axis range). 上面的代码仅在图表本身上添加了一个标签(在y轴范围内)。 The title needs to be independent (above the y axis). 标题必须独立(y轴上方)。 Any idea? 任何想法?

在此处输入图片说明

This seems to do the trick: 这似乎可以解决问题:

    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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM