Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Winforms - Creating a chart with multiple Y axis (3 or more)

I have a client that needs to use charts with more than 2 Y axes.

I am already using Component One (C1Chart) chart that has a built in Y2 axis and it's working great.

Does anyone knows of a chart control that can display 3 or more Y axes on a chart?

like image 532
user2243704 Avatar asked Apr 04 '13 07:04

user2243704


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

What is full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

Samples Environments for Microsoft Chart Controls contains example of multiple Y axis.
Some pieces of code:

    private void checkBoxUseMultipleYAxis_CheckedChanged(object sender, System.EventArgs e)
    {
        if(checkBoxUseMultipleYAxis.Checked)
        {
            // Set custom chart area position
            Chart1.ChartAreas["Default"].Position = new ElementPosition(25,10,68,85);
            Chart1.ChartAreas["Default"].InnerPlotPosition = new ElementPosition(10,0,90,90);

            // Create extra Y axis for second and third series
            CreateYAxis(Chart1, Chart1.ChartAreas["Default"], Chart1.Series["Series2"], 13, 8);
            CreateYAxis(Chart1, Chart1.ChartAreas["Default"], Chart1.Series["Series3"], 22, 8);
        }
        else
        {
            // Set default chart areas
            Chart1.Series["Series2"].ChartArea = "Default";
            Chart1.Series["Series3"].ChartArea = "Default";

            // Remove newly created series and chart areas
            while(Chart1.Series.Count > 3)
            {
                Chart1.Series.RemoveAt(3);
            }
            while(Chart1.ChartAreas.Count > 1)
            {
                Chart1.ChartAreas.RemoveAt(1);
            }

            // Set default chart are position to Auto
            Chart1.ChartAreas["Default"].Position.Auto = true;
            Chart1.ChartAreas["Default"].InnerPlotPosition.Auto = true;

        }
    }

public void CreateYAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
    {
        // Create new chart area for original series
        ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
        areaSeries.BackColor = Color.Transparent;
        areaSeries.BorderColor = Color.Transparent;
        areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
        areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
        areaSeries.AxisX.MajorGrid.Enabled = false;
        areaSeries.AxisX.MajorTickMark.Enabled = false;
        areaSeries.AxisX.LabelStyle.Enabled = false;
        areaSeries.AxisY.MajorGrid.Enabled = false;
        areaSeries.AxisY.MajorTickMark.Enabled = false;
        areaSeries.AxisY.LabelStyle.Enabled = false;
        areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;


        series.ChartArea = areaSeries.Name;

        // Create new chart area for axis
        ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
        areaAxis.BackColor = Color.Transparent;
        areaAxis.BorderColor = Color.Transparent;
        areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
        areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());

        // Create a copy of specified series
        Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
        seriesCopy.ChartType = series.ChartType;
        foreach(DataPoint point in series.Points)
        {
            seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
        }

        // Hide copied series
        seriesCopy.IsVisibleInLegend = false;
        seriesCopy.Color = Color.Transparent;
        seriesCopy.BorderColor = Color.Transparent;
        seriesCopy.ChartArea = areaAxis.Name;

        // Disable drid lines & tickmarks
        areaAxis.AxisX.LineWidth = 0;
        areaAxis.AxisX.MajorGrid.Enabled = false;
        areaAxis.AxisX.MajorTickMark.Enabled = false;
        areaAxis.AxisX.LabelStyle.Enabled = false;
        areaAxis.AxisY.MajorGrid.Enabled = false;
        areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
        areaAxis.AxisY.LabelStyle.Font = area.AxisY.LabelStyle.Font;

        // Adjust area position
        areaAxis.Position.X -= axisOffset;
        areaAxis.InnerPlotPosition.X += labelsSize;

    }

Result:
Multi Y axis offMulti Y axis off

like image 146
fat Avatar answered Oct 21 '22 14:10

fat


I think you could try multiple ChartArea's, which allows you to arrange multiple plots in the same chart object.

You can assign each Series to its own ChartArea.

like image 31
Matt L Avatar answered Oct 21 '22 14:10

Matt L


The MS Chart control has almost everything you will ever need as far as charting is concerned. Download the sample, run it and go to Chart Features > Axes > Multiple Y axis. I think you will find what you are looking for!

like image 1
Farhan Ahmed Avatar answered Oct 21 '22 14:10

Farhan Ahmed