WinForms Livecharts图表标题 [英] WinForms Livecharts Chart Title

查看:575
本文介绍了WinForms Livecharts图表标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using LiveCharts in WinForms. 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.

我将LiveCharts控件另存为PDF图像,因此标题必须在图表本身上.

I'm saving the LiveCharts control as an image to a PDF, so the title needs to be on the chart itself.

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

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

以上代码仅在图表本身上添加了一个标签(在y轴范围内).标题必须独立(y轴上方).有什么主意吗?

The above code only adds a label on the chart itself (within the y axis range). The title needs to be independent (above the y axis). Any idea?

推荐答案

这似乎可以解决问题:

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

这篇关于WinForms Livecharts图表标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆