【问题标题】:WinForms Livecharts Chart TitleWinForms Livecharts 图表标题
【发布时间】:2017-03-08 09:56:30
【问题描述】:

我在 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 轴上方)。有什么想法吗?

【问题讨论】:

    标签: c# winforms livecharts


    【解决方案1】:

    这似乎可以解决问题:

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

    【讨论】:

    • 不是我想要的,但它解决了标题外观问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多