Exemplo n.º 1
0
        private void AddSeriesForParallelLoop(IDictionary <int, double> values)
        {
            var    xValues            = values.Keys;
            var    yValues            = values.Values;
            Series parallelLoopSeries = new Series();

            parallelLoopSeries.ChartType     = SeriesChartType.Line;
            parallelLoopSeries.Color         = Color.FromArgb(112, 255, 200);
            parallelLoopSeries.BorderColor   = Color.FromArgb(164, 164, 164);
            parallelLoopSeries.Name          = "Parallel loop CPU usage";
            parallelLoopSeries.XValueMember  = "Exercies";
            parallelLoopSeries.YValueMembers = "Miliseconds";
            parallelLoopSeries.Points.DataBindXY(xValues, yValues);
            chart1.Series.Add(parallelLoopSeries);
            chart1.DataBind();
        }
        private void ArrayBinding_Load(object sender, System.EventArgs e)
        {
            // resolve the address to the 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 REPS;";

            // 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);

            myConnection.Open();

            // set chart data source - the data source must implement IEnumerable
            Chart1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            // set series members names for the X and Y values
            Chart1.Series["Series 1"].XValueMember  = "Name";
            Chart1.Series["Series 1"].YValueMembers = "Sales";

            // data bind to the selected data source
            Chart1.DataBind();

            myCommand.Dispose();
            myConnection.Close();
        }
Exemplo n.º 3
0
        private static void CreateChart(string outputFileName, DataSet dataSet, List <Series> series, string axisYtitle)
        {
            //prepare chart control...
            var chart = new System.Windows.Forms.DataVisualization.Charting.Chart
            {
                DataSource = dataSet.Tables[0],
                Width      = 1280,
                Height     = 720
            };

            foreach (var serie in series)
            {
                chart.Series.Add(serie);
            }

            //create chartareas...
            chart.ChartAreas.Add(new ChartArea
            {
                Name            = "ChartArea1",
                BackColor       = Color.White,
                BorderColor     = Color.FromArgb(26, 59, 105),
                BorderWidth     = 0,
                BorderDashStyle = ChartDashStyle.Solid,
                AxisX           = new Axis
                {
                    Title     = "Vg [Nm3/h]",
                    TitleFont = new Font("Tahoma", 12.0f),
                    Minimum   = 3,
                    Maximum   = 9
                },
                AxisY = new Axis
                {
                    Title     = axisYtitle,
                    TitleFont = new Font("Tahoma", 12.0f),
                }
            });

            chart.Legends.Add(new Legend("Legend")
            {
                Font = new Font("Tahoma", 12.0f)
            });

            //databind...
            chart.DataBind();

            //save result...
            chart.SaveImage(outputFileName, ChartImageFormat.Png);

            //open result...
            ProcessStartInfo psi = new ProcessStartInfo(outputFileName)
            {
                UseShellExecute = true
            };

            Process.Start(psi);
        }
Exemplo n.º 4
0
        private static void SaveChart(List<double> requestTimes, TimeSpan totalTime, double minTime, double maxTime, double avgTime)
        {
            DataSet dataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("Seconds", typeof(int));
            dt.Columns.Add("RequestCount", typeof(int));

            Dictionary<int, int> timeTable = new Dictionary<int, int>();

            List<DataRow> rows = new List<DataRow>();
            List<int> intTimes = requestTimes.OrderBy(t => t).Select(t => (int)Math.Round(t/1000)).ToList();

            foreach (int time in intTimes)
            {
                DataRow row = rows.FirstOrDefault(r => ((int)r[0]) == time);

                if (row == null)
                {
                    row = dt.NewRow();
                    row[0] = time;
                    row[1] = 0;
                    rows.Add(row);
                }

                row[1] = ((int)row[1]) + 1;
            }

            rows.ForEach(r => dt.Rows.Add(r));

            dataSet.Tables.Add(dt);

            Chart chart = new Chart();
            chart.DataSource = dataSet.Tables[0];
            chart.Width = 900;
            chart.Height = 500;

            Series series = new Series();
            series.Name = "Serie1";
            series.Color = Color.FromArgb(220, 0, 27);
            series.ChartType = SeriesChartType.Column;
            series.ShadowOffset = 0;
            series.IsValueShownAsLabel = true;
            series.XValueMember = "Seconds";
            series.YValueMembers = "RequestCount";
            series.Font = new Font(series.Font.FontFamily, 10);
            chart.Series.Add(series);

            ChartArea ca = new ChartArea();
            ca.Name = "ChartArea1";
            ca.BackColor = Color.White;
            ca.BorderWidth = 0;
            ca.AxisX = new Axis();
            ca.AxisY = new Axis();
            ca.AxisX.Title = "Time (seconds)";
            Font f = ca.AxisX.TitleFont;
            ca.AxisX.TitleFont = new Font(f.FontFamily, 12, f.Style);
            ca.AxisY.Title = "Request count";
            ca.AxisY.TitleFont = ca.AxisX.TitleFont;
            ca.AxisX.MajorGrid.LineColor = Color.LightGray;
            ca.AxisY.MajorGrid.LineColor = ca.AxisX.MajorGrid.LineColor;
            chart.ChartAreas.Add(ca);

            chart.Titles.Add("Requests times");
            chart.Titles[0].Font = ca.AxisX.TitleFont;
            chart.Titles.Add(GetChartDescriptionString(totalTime, minTime, maxTime, avgTime));
            chart.Titles[1].Font = new Font(chart.Titles[1].Font.FontFamily, 10);

            chart.DataBind();

            int i = 0;
            string fileName = "";

            //loop until you find a free file name (in case multiple instances are running at the same time)
            do
            {
                fileName = string.Format("chart-{0}.png", i++);
            }
            while(File.Exists(fileName));

            chart.SaveImage(fileName, ChartImageFormat.Png);
        }
        private void chartdatatable(DataTable dt, Chart Chart1)
        {
            Chart1.Series.Clear();
            Chart1.DataSource = dt;
            string serieName = "";
            int amountofrows = Convert.ToInt32(dt.Rows.Count);
            List<string> xvals = new List<string>();
            List<double> yvals = new List<double>();

            foreach (DataColumn column in dt.Columns)
            {
                xvals.Add(column.ColumnName);

            }
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                serieName = "Simulation" + i;
                Chart1.Series.Add(serieName);
                Chart1.Series[serieName].ChartType = SeriesChartType.Line;
                //       List<double> yvals = new List<double>();
                foreach (DataColumn column in dt.Columns)
                {
                    yvals.Add(Convert.ToDouble(row[column]));
                }
                try//add the newly created sereies...
                {
                    Chart1.Series[serieName].XValueType = ChartValueType.String;
                    Chart1.Series[serieName].YValueType = ChartValueType.Auto;
                    Chart1.Series[serieName].Points.DataBindXY(xvals.ToArray(), yvals.ToArray());
                    yvals.Clear();
                }
                catch (Exception)
                {

                    throw new InvalidOperationException("Kunde inte bind punkterna till Diagrammet");
                }

                i++;
            }

            Chart1.DataBind();
            Chart1.Visible = true;
        }
Exemplo n.º 6
0
        public void BuildChart(string fileName, DataTable data, LineChartContext context)
        {
            if (data == null || data.Columns.Count == 0)
                throw new System.FormatException("There is not data to plot.");

            System.Drawing.Color[] colors = ChartHelper.GetColors();
            if (data.Columns.Count - 1 > colors.Length)
                throw new IndexOutOfRangeException("We don't have enough colors to render this chart.");

            using (var targetChart = new Chart()) {

                ChartArea mainChartArea = new ChartArea("MainArea");
                targetChart.ChartAreas.Add(mainChartArea);

                ChartHelper.InitializeChart(targetChart, context.ImageSize);

                if (!string.IsNullOrWhiteSpace(context.Title)) {

                    targetChart.Titles.Add(new Title(context.Title, Docking.Top,
                                                     new System.Drawing.Font(context.TitleFontName,
                                                                             context.TitleFontSize,
                                                                             System.Drawing.FontStyle.Bold),
                                                                             context.TitleFontColor));
                }

                targetChart.DataSource = data;

                string xval = data.Columns[0].ToString();

                for (int i = 1; i < data.Columns.Count; i++) {

                    string columnName = data.Columns[i].ColumnName;

                    targetChart.Series.Add(columnName);

                    // Using column name as the legend
                    targetChart.Series[i - 1].Name = columnName;
                    targetChart.Series[i - 1].LegendText = columnName;

                    targetChart.Series[i - 1].XValueMember = xval;
                    targetChart.Series[i - 1].XValueType = ChartHelper.ParseCharValueType(context.XValueType);
                    targetChart.Series[i - 1].YValueMembers = data.Columns[i].ToString();
                    targetChart.Series[i - 1].ChartType = SeriesChartType.Line;

                    targetChart.Series[i - 1].Color = colors[i - 1];
                }

                System.Drawing.Color lineColor = context.LineColor;

                targetChart.ChartAreas[0].AxisX.LineColor =
                    targetChart.ChartAreas[0].AxisY.LineColor =
                        targetChart.ChartAreas[0].AxisX.InterlacedColor =
                            targetChart.ChartAreas[0].AxisY.InterlacedColor =
                                targetChart.ChartAreas[0].AxisX.MajorGrid.LineColor =
                targetChart.ChartAreas[0].AxisY.MajorGrid.LineColor =
                    targetChart.ChartAreas[0].AxisX.TitleForeColor =
                        targetChart.ChartAreas[0].AxisY.TitleForeColor =
                            targetChart.ChartAreas[0].AxisX.MajorTickMark.LineColor =
                                targetChart.ChartAreas[0].AxisY.MajorTickMark.LineColor = lineColor;

                ChartHelper.AddLegend(targetChart, ChartHelper.ParseLegendStyle(context.LegendStyle));

                targetChart.DataBind();

                targetChart.SaveImage(fileName, ChartImageFormat.Png);
            }
        }
Exemplo n.º 7
0
        public void BuildChart(string fileName, IEnumerable<PieChartItem> data, PieChartContext context)
        {
            if (data == null || data.Count() == 0)
                throw new System.FormatException("There is not data to plot.");

            using (var targetChart = new Chart()) {

                ChartArea mainChartArea = new ChartArea("MainArea");
                targetChart.ChartAreas.Add(mainChartArea);
                targetChart.ChartAreas[0].Area3DStyle.Enable3D = context.Show3DStyle;

                ChartHelper.InitializeChart(targetChart, context.ImageSize);

                if (!string.IsNullOrWhiteSpace(context.Title)) {

                    targetChart.Titles.Add(new Title(context.Title, Docking.Top,
                                                     new System.Drawing.Font(context.TitleFontName,
                                                                             context.TitleFontSize,
                                                                             System.Drawing.FontStyle.Bold),
                                                                             context.TitleFontColor));
                }

                targetChart.Series.Add(context.Title);
                targetChart.Series[0].XValueMember = "Label";
                targetChart.Series[0].YValueMembers = "Value";
                targetChart.DataSource = data;

                targetChart.Series[0].IsVisibleInLegend = true;
                targetChart.Series[0].ChartType = SeriesChartType.Pie;

                targetChart.Series[0].LegendText = "#VALX";

                // https://technet.microsoft.com/en-us/library/dd239373.aspx
                switch (context.LabelStyle) {
                    case PieChartLabelStyle.None:
                        targetChart.Series[0].Label = string.Empty;
                        targetChart.Series[0].XValueMember = string.Empty;
                        break;
                    case PieChartLabelStyle.Value:
                        targetChart.Series[0].Label = "#VALY";
                        break;
                    case PieChartLabelStyle.Percent:
                        targetChart.Series[0].Label = "#PERCENT{P2}";
                        break;
                    case PieChartLabelStyle.ValuePercent:
                        targetChart.Series[0].Label = "#VALY / #PERCENT{P2}";
                        break;
                    case PieChartLabelStyle.Label:
                        targetChart.Series[0].Label = "#VALX";
                        break;
                    case PieChartLabelStyle.LabelValue:
                        targetChart.Series[0].Label = "#VALX (#VALY)";
                        break;
                    case PieChartLabelStyle.LabelPercent:
                        targetChart.Series[0].Label = "#VALX (#PERCENT{P2})";
                        break;
                    case PieChartLabelStyle.LabelValuePercent:
                        targetChart.Series[0].Label = "#VALX (#VALY / #PERCENT{P2})";
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("LabelStyle");
                }

                targetChart.Series[0].IsValueShownAsLabel = context.IsValueShownAsLabel;

                if (context.IsLabelOutside)
                    targetChart.Series[0]["PieLabelStyle"] = "Outside";

                System.Drawing.Color lineColor = context.LineColor;

                targetChart.ChartAreas[0].AxisX.LineColor =
                    targetChart.ChartAreas[0].AxisY.LineColor =
                        targetChart.ChartAreas[0].AxisX.InterlacedColor =
                            targetChart.ChartAreas[0].AxisY.InterlacedColor =
                                targetChart.ChartAreas[0].AxisX.MajorGrid.LineColor =
                targetChart.ChartAreas[0].AxisY.MajorGrid.LineColor =
                    targetChart.ChartAreas[0].AxisX.TitleForeColor =
                        targetChart.ChartAreas[0].AxisY.TitleForeColor =
                            targetChart.ChartAreas[0].AxisX.MajorTickMark.LineColor =
                                targetChart.ChartAreas[0].AxisY.MajorTickMark.LineColor = lineColor;

                ChartHelper.AddLegend(targetChart, ChartHelper.ParseLegendStyle(context.LegendStyle));

                targetChart.DataBind();

                targetChart.SaveImage(fileName, ChartImageFormat.Png);
            }
        }
Exemplo n.º 8
0
        private void buttonGenerateChart_Click(object sender, EventArgs e)
        {
            if (tabControl == null)
                return;

            if (tabControl.TabPages.Count == 0)
                return;

            TabPage tabPage = tabControl.SelectedTab;
            if (tabPage == null)
                return;

            TableLayoutPanel tableLayoutInformation = (TableLayoutPanel)tabPage.Controls[0];
            if (tableLayoutInformation == null)
                return;

            DataGridView dataGridViewInformation = (DataGridView)tableLayoutInformation.Controls[0];
            if (dataGridViewInformation == null)
                return;

            DataTable dataTable = (DataTable)dataGridViewInformation.DataSource;
            if (dataTable == null)
                return;

            if (string.IsNullOrEmpty(comboBoxChartColumn.Text) || string.IsNullOrEmpty(comboBoxChartType.Text))
                return;

            string columnName = "";
            foreach (DataColumn dataColumn in dataTable.Columns) {
                if (dataColumn.DataType == typeof(DateTime))
                    columnName = dataColumn.ColumnName;
            }

            //Titles
            Title titleAts = new Title();
            titleAts.Alignment = ContentAlignment.TopCenter;
            titleAts.Name = "chartTitleAts";
            titleAts.Text = "Advanced Technologies and Solutions";

            Title titleChart = new Title();
            titleChart.Alignment = ContentAlignment.TopCenter;
            titleChart.Name = "chartTitle";
            titleChart.Text = tabControl.TabPages[0].Name + " (" + comboBoxChartType.Text + " Chart)";
            titleChart.TextStyle = TextStyle.Shadow;
            titleChart.Font = new Font("Calibri", 16, FontStyle.Bold);

            //Chart
            Chart chart = new Chart();
            chart.Titles.Add(titleAts);
            chart.Titles.Add(titleChart);
            chart.Dock = DockStyle.Fill;
            chart.BackColor = Color.FromName(comboBoxBackground.Text);
            chart.DataSource = dataTable;
            chart.AntiAliasing = AntiAliasingStyles.All;
            chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
            chart.Text = dataGridViewInformation.Name;

            //ContextMenuStrip
            ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
            contextMenuStrip.Name = "Chart";
            ToolStripItem toolStripItemChartSaveAsImage = contextMenuStrip.Items.Add("Save as image");
            toolStripItemChartSaveAsImage.Click += toolStripItemChartSaveAsImage_Click;
            chart.ContextMenuStrip = contextMenuStrip;

            //ChartLegends
            chart.Legends.Clear();
            chart.Legends.Add("Legend");
            chart.Legends["Legend"].Name = comboBoxChartColumn.Text;

            //ChartArea
            ChartArea chartArea = new ChartArea();
            chartArea.Name = columnName;
            chartArea.BackGradientStyle = GradientStyle.VerticalCenter;
            chartArea.BackColor = Color.FromName(comboBoxBackground.Text);

            //ChartAreaX
            chartArea.AxisX.Title = columnName;
            chartArea.AxisX.TitleFont = new Font("calibri", 16, FontStyle.Bold);
            chartArea.AxisX.LabelStyle.Format = "yyyy/MM/dd HH:mm:ss";
            chartArea.AxisX.TitleAlignment = StringAlignment.Center;
            chartArea.AxisX.MajorGrid.LineColor = Color.Transparent;
            chartArea.AxisX.Name = columnName;
            chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep90;
            chartArea.AxisX.ScaleView.Zoomable = true;

            //ChartAreaY
            chartArea.AxisY.Title = comboBoxChartColumn.Text;
            chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
            chartArea.AxisY.TitleFont = new Font("calibri", 16, FontStyle.Bold);
            chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.DashDotDot;
            chartArea.AxisY.Name = comboBoxChartColumn.Text;
            chartArea.AxisY.ScaleView.Zoomable = true;

            chart.ChartAreas.Add(chartArea);
            chart.MouseWheel += chart_MouseWheel;

            Series series = new Series(comboBoxChartColumn.Text);
            series.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), comboBoxChartType.Text, true);
            series.XValueMember = columnName;
            series.YValueMembers = comboBoxChartColumn.Text;
            series.Color = Color.FromName(comboBoxSeries.Text);
            series.BorderWidth = 5;

            chart.Series.Add(series);

            chart.DataBind();

            FormChart formChart = new FormChart();
            formChart.Controls.Add(chart);
            formChart.Text = tabControl.TabPages[0].Name;
            formChart.StartPosition = FormStartPosition.CenterParent;
            formChart.Show();
        }