WinForms图表控件的分组条形图

时间:2017-06-23 05:30:19

标签: c# .net winforms microsoft-chart-controls

我可以用这样的MS Excel Grouped条形图创建。 enter image description here

我需要通过WinForms图表控件创建相同的图表。

我尝试使用此代码,但似乎我需要更复杂的解决方案来构建正确的图表,就像MS Excel一样。

请帮我修正代码,以便建立分组条形图。 (这个伟大的微软示例没有帮助https://code.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=4418

数据源

DataTable dtExc = new DataTable();

// Create columns
dtExc.Columns.Add((new DataColumn("StoodType", typeof(int))).Caption = "Stood");
dtExc.Columns.Add((new DataColumn("TotalMinutes", typeof(double))).Caption = "Time");

foreach (var exDataitem in allDataExc)
{
     DataRow drToAdd = dtExc.NewRow();

     drToAdd[0] = exDataitem.StoodTypeFullName;
     drToAdd[1] = exDataitem.TotalMinutes;

     dtExc.Rows.Add(drToAdd);   
}

图表

public Chart GenerateExcChart(DataTable dtChartDataSource, int width, int height, string bgColor, SeriesChartType seriesChartType,
            string axisXTitle, string axisYTitle)
        {
            Chart chart = new Chart()
            {
                Width = width,
                Height = height
            };


            chart.Legends.Add(new Legend() { Name = "Legend" });
            chart.Legends[0].Docking = Docking.Bottom;
            ChartArea chartArea = new ChartArea() { Name = "ChartArea" };

            //Remove X-axis grid lines
            chartArea.AxisX.MajorGrid.LineWidth = 0;
            chartArea.AxisX.Title = axisXTitle;

            chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
            chartArea.AxisX.IntervalType = DateTimeIntervalType.Number;

            //Remove Y-axis grid lines
            chartArea.AxisY.MajorGrid.LineWidth = 0;
            chartArea.AxisY.Interval = 1;
            chartArea.AxisY.Title = axisYTitle;

            //Chart Area Back Color
            chartArea.BackColor = Color.FromName(bgColor);
            chart.ChartAreas.Add(chartArea);
            chart.Palette = ChartColorPalette.BrightPastel;
            string series = string.Empty;

            //create series and add data points to the series
            if (dtChartDataSource != null)
            {
                foreach (DataColumn dc in dtChartDataSource.Columns)
                {
                    //a series to the chart
                    if (chart.Series.FindByName(dc.ColumnName) == null)
                    {
                        series = dc.ColumnName;
                        chart.Series.Add(series);
                        chart.Series[series].ChartType = seriesChartType;
                    }

                    var rowIndex = 0;
                    //Add data points to the series
                    foreach (DataRow dr in dtChartDataSource.Rows)
                    {
                        double dataPoint = 0;
                        double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint);

                        var objDataPoint = new DataPoint();
                        objDataPoint.YValues = new double[] { dataPoint };
                        objDataPoint.XValue = rowIndex + 1;
                        objDataPoint.AxisLabel = rowIndex.ToString();

                        if (dataPoint == 0)
                        {
                            objDataPoint.IsEmpty = true;
                        }

                        chart.Series[series].Points.Add(objDataPoint);
                        rowIndex++;
                    }
                }
            }

            return chart;
        }

1 个答案:

答案 0 :(得分:2)

通过使用Google和一些实验,我更正了代码,因此它可以满足我的需求。

public Chart GenerateExcChart(DataTable dtChartDataSource, int width, int height, string bgColor, SeriesChartType seriesChartType,
            string axisXTitle, string axisYTitle)
        {
            Chart chart = new Chart()
            {
                Width = width,
                Height = height
            };

            chart.Legends.Add(new Legend() { Name = "Legend" });
            chart.Legends[0].Docking = Docking.Bottom;
            ChartArea chartArea = new ChartArea() { Name = "ChartArea" };

            chartArea.AxisX.MajorGrid.LineWidth = 0;//Remove X-axis grid lines
            chartArea.AxisY.MajorGrid.LineWidth = 0;//Remove Y-axis grid lines

            chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
            chartArea.AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
            chartArea.AxisX.IntervalType = DateTimeIntervalType.Number;
            chartArea.AxisY.IntervalType = DateTimeIntervalType.Number;

            chartArea.AxisX.LabelStyle.Angle = -45;

            chartArea.AxisX.Title = axisYTitle;
            chartArea.AxisY.Title = axisXTitle;

            //Chart Area Back Color
            chartArea.BackColor = Color.FromName(bgColor);
            chart.ChartAreas.Add(chartArea);
            chart.Palette = ChartColorPalette.BrightPastel;
            string series = string.Empty;

            //create series and add data points to the series
            if (dtChartDataSource != null)
            {
                series = "Series1";
                chart.Series.Add(series);
                chart.Series[series].ChartType = SeriesChartType.Column;
                chart.Series[series].XValueType = ChartValueType.Auto;
                chart.Series[series].YValuesPerPoint = 1;
                chart.Series[series].YValueType = ChartValueType.Auto;

                var rowIndex = 0;

                //Add data points to the series
                foreach (DataRow dr in dtChartDataSource.Rows)
                {
                    double dataPoint;
                    DataPoint objDataPoint = new DataPoint();

                    objDataPoint.AxisLabel = dr[dtChartDataSource.Columns[0].ColumnName].ToString();

                    double.TryParse(dr[dtChartDataSource.Columns[1].ColumnName].ToString(), out dataPoint);


                    objDataPoint.XValue = rowIndex;
                    objDataPoint.YValues = new double[] { dataPoint };

                    chart.Series[series].Points.Add(objDataPoint);

                    rowIndex++;
                }
            }

            chart.Series[0].IsVisibleInLegend = false;

            return chart;
        }