column chart and line chart combination...

The following code creates a column chart for the data in:

double[] yValues = { 15, 60, 12, 13 };  
string[] xValues = { "September", "October", "November", "December" };

Question: How can I add a 3rd series as line on the same column chart for data in: double[] yValues2 = { 30, 32, 30, 35 };

I suppose I have to change SeriesChartType.Column;  to something like SeriesChartType.Stacked??;  and somehow make the data in yValues2 avaialable to it.

I appreciate your time and effort in advance for participating in this question.

Thank you.
        double[] yValues = { 15, 60, 12, 13 };   //for column chart
        //double[] yValues2 = { 30, 32, 30, 35 };   //for line
        string[] xValues = { "September", "October", "November", "December" };

        Chart chart = new Chart();
        Series series = new Series("Default");
        series.ChartType = SeriesChartType.Column;
        series["PieLabelStyle"] = "Disabled";
        chart.Series.Add(series);

        ChartArea chartArea = new ChartArea();
        Axis yAxis = new Axis(chartArea, AxisName.Y);
        Axis xAxis = new Axis(chartArea, AxisName.X);

        chart.Series["Default"].Points.DataBindXY(xValues, yValues);
        chart.ChartAreas.Add(chartArea);

        chart.Width = new Unit(300, UnitType.Pixel);
        chart.Height = new Unit(200, UnitType.Pixel);
        string filename = "C:\\image\\Chart.png";
        chart.SaveImage(filename, ChartImageFormat.Png);

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
Robert SchuttSoftware EngineerCommented:
No, with stacked I think you can have multiple Y values but not different types of charts.

The thing is, if you look at the statement "series.ChartType = ..." (holding a single value), you start to suspect that you need to add a second series to the chart if you want another chart type for that data, using the same X values and the new Y values.

I think this is what you need:
            double[] yValues = { 15, 60, 12, 13 };   //for column chart
            double[] yValues2 = { 30, 32, 30, 35 };   //for line
            string[] xValues = { "September", "October", "November", "December" };

            Chart chart = new Chart();
            Series series = new Series("Default");
            series.ChartType = SeriesChartType.Column;
            series["PieLabelStyle"] = "Disabled";
            chart.Series.Add(series);

            Series series2 = new Series("Default2");
            series2.ChartType = SeriesChartType.Line;
            series2.Color = System.Drawing.Color.Red;
            series2.BorderWidth = 5; // this is actually the line width...
            chart.Series.Add(series2);

            ChartArea chartArea = new ChartArea();
            Axis yAxis = new Axis(chartArea, AxisName.Y);
            Axis xAxis = new Axis(chartArea, AxisName.X);

            chart.Series["Default"].Points.DataBindXY(xValues, yValues);
            chart.Series["Default2"].Points.DataBindXY(xValues, yValues2);
            chart.ChartAreas.Add(chartArea);

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.