Search code examples
c#winformslivecharts

WinForms Livecharts Chart Title


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.

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

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?

enter image description here


Solution

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