private void DataBind()
        {
            try
            {
                // Remove all series
                Chart1.Series.Clear();

                // Access database
                System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm mainForm = (System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm) this.ParentForm;
                string fileNameString = mainForm.applicationPath + "\\data\\chartdata.mdb";

                // initialize a connection string
                string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;

                // define the database query
                string mySelectQuery = "SELECT * FROM REPSALES;";

                // create a database connection object using the connection string
                OleDbConnection myConnection = new OleDbConnection(myConnectionString);

                // create a database command on the connection using query
                OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);

                // open the connection
                myCommand.Connection.Open();

                // Bind grid to the original data
                OleDbDataAdapter custDA = new OleDbDataAdapter();
                custDA.SelectCommand = myCommand;
                DataSet custDS = new DataSet();
                custDA.Fill(custDS, "Customers");
                this.dataGrid.SetDataBinding(custDS, "Customers");
                this.dataGrid.TableStyles[0].MappingName = "Customers";

                // create a database reader
                OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

                // data bind chart to a table where all rows are grouped by the "Name" column
                Chart1.DataBindCrossTable(
                    myReader,
                    (comboBoxGroupBy.SelectedIndex == 0) ? "Name" : "Year",
                    (comboBoxGroupBy.SelectedIndex == 0) ? "Year" : "Name",
                    "Sales",
                    "Label=Commissions{C0}");

                // close the reader and the connection
                myReader.Close();
                myConnection.Close();

                // Set series appearance
                MarkerStyle marker = MarkerStyle.Star4;
                foreach (Series ser in Chart1.Series)
                {
                    ser.ShadowOffset      = 1;
                    ser.BorderWidth       = 3;
                    ser.ChartType         = SeriesChartType.Line;
                    ser.MarkerSize        = 12;
                    ser.MarkerStyle       = marker;
                    ser.MarkerBorderColor = Color.FromArgb(64, 64, 64);
                    ser.Font = new Font("Trebuchet MS", 8, FontStyle.Bold);
                    marker++;
                }
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 2
0
        private MSCHART.Chart Build()
        {
            if (this.XColumn == null)
            {
                throw new System.ArgumentNullException("XColumn");
            }

            if (this.YColumn == null)
            {
                throw new System.ArgumentNullException("YColumn");
            }

            var chart = new MSCHART.Chart();

            if (this.Name != null)
            {
                chart.Name = this.Name;

                chart.Titles.Clear();
                var title = new MSCHART.Title(this.Name);
                chart.Titles.Add(title);
            }

            var area = new MSCHART.ChartArea();

            chart.ChartAreas.Add(area);

            if (this.SeriesColumn != null)
            {
                chart.DataBindCrossTable(this.DataTable.Rows, this.SeriesColumn, this.XColumn, this.YColumn, "");
                var legend = new MSCHART.Legend("LEGEND");
                chart.Legends.Add(legend);
            }
            else
            {
                chart.DataSource = this.DataTable;
                var series = new MSCHART.Series("Series1");
                series.XValueMember  = this.XColumn;
                series.YValueMembers = this.YColumn;
                chart.Series.Add(series);
            }


            var t = this.ChartType.HasValue ? this.ChartType.Value : MSChartStylesheet.ChartType.Column;

            foreach (var s in chart.Series)
            {
                if (t == MSChartStylesheet.ChartType.Column)
                {
                    s.ChartType = MSCHART.SeriesChartType.Column;
                }
                else if (t == MSChartStylesheet.ChartType.Bar)
                {
                    s.ChartType = MSCHART.SeriesChartType.Bar;
                }
                else if (t == MSChartStylesheet.ChartType.Pie)
                {
                    s.ChartType = MSCHART.SeriesChartType.Pie;
                }
                else if (t == MSChartStylesheet.ChartType.DoughNut)
                {
                    s.ChartType = MSCHART.SeriesChartType.Doughnut;
                }
                else if (t == MSChartStylesheet.ChartType.Line)
                {
                    s.ChartType = MSCHART.SeriesChartType.Line;
                }
            }



            var stylesheet = this.StyleSheet ?? new MSChartStylesheet.Stylesheet();

            stylesheet.RadialLabelStyle = MSChartStylesheet.RadialLabelStyle.Outside;
            stylesheet.Format(chart);

            chart.Width  = this.Width.HasValue ? this.Width.Value : 600;
            chart.Height = this.Height.HasValue ? this.Height.Value : 400;


            return(chart);
        }