Majid Hojati
Majid Hojati

Reputation: 1790

Add two charts below each other using windows chart form

I can easily create two separate charts but I want to add them in one single chart. one on top and another below it. So How can I do that?

Here is the code that I have tried so far but it only draws one chart on top.

// Chart chart1 = new Chart();
            Chart chart1 = new Chart()
            {
                Width = 500,
                Height = 1000
            };


            chart1.Legends[0].Docking = Docking.Bottom;
            ChartArea chartArea = new ChartArea() { Name = "ChartArea0" };
            //Remove X-axis grid lines
            chartArea.AxisX.MajorGrid.LineWidth = 0;
            //Remove Y-axis grid lines
            chartArea.AxisY.MajorGrid.LineWidth = 0;
            //Chart Area Back Color

            chart1.ChartAreas.Add(chartArea);

            chart1.Palette = ChartColorPalette.SeaGreen;

            // Set title.
            chart1.Titles.Add(" GIS");

            // Add series.
            for (int i = 0; i < seriesEXpArray.Length; i++)
            {
                // Add series.
                Series series = chart1.Series.Add(seriesEXpArray[i]);
                series.ChartArea= "ChartArea0";
                // Add point.
                series.Points.Add(ExppointsArray[i]);
            }

             chartArea = new ChartArea() { Name = "ChartArea1" };
            //Remove X-axis grid lines
            chartArea.AxisX.MajorGrid.LineWidth = 0;
            //Remove Y-axis grid lines
            chartArea.AxisY.MajorGrid.LineWidth = 0;
            //Chart Area Back Color

            chart1.ChartAreas.Add(chartArea);

            // Add series.
            for (int i = 0; i < seriesUserCommenArray.Length; i++)
            {
                // Add series.
                Series series = chart1.Series.Add(seriesUserCommenArray[i]);
                series.ChartArea = "ChartArea1";
                // Add point.
                series.Points.Add(UserCommentpointsArray[i]);
            }

Upvotes: 0

Views: 993

Answers (1)

TaW
TaW

Reputation: 54433

Most likely your form isn't large enough to hold the 1000 pixels you set the Height to..

Change it to 500 and observe the two Series in their ChartAreas..

The second (and third) ChartArea is by default placed below the first one. And both are sized autmatically to display all the points in all their Series.

When adding a fourth ChartArea the layout automatically changes to a 2x2 grid.

If you want to override the default arrangement of the ChartAreas you can do so by setting their Positions. See here for an example!

Btw: To get rid of the MajorGrid lines it is cleaner imo to write:

chartArea.AxisX.MajorGrid.Enabled = false;

etc..

Also you can simplify the creation of ChartAreas and also Series like this:

ChartArea chartArea1 = chart1.ChartAreas.Add("ChartArea1");
..
Series series1 = chart1.Series.Add("S1");
..

Finally: You do realize that you are adding numerous Series each with only one DataPoint? This is rather unusual..

Upvotes: 1

Related Questions