2

我在 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 轴上方)。任何想法?

在此处输入图像描述

4

1 回答 1

2

这似乎可以解决问题:

    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);
    }
于 2017-03-09T04:51:03.770 回答