/// <summary> /// Pass params to build the chart on start up, set null (is default) for nothing display /// </summary> /// <param name="sensor"></param> /// <param name="group"></param> /// <param name="pictureView"></param> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="chartName"></param> /// <param name="xLabel"></param> /// <param name="yLabel"></param> /// <param name="showCalcValue"></param> public ChartViewForm(Sensor sensor = null, Group group = null, PictureView pictureView = null, DateTime?startDate = null, DateTime?endDate = null, string chartName = "Chart View", string xLabel = "Value", string yLabel = "Datetime", bool showCalcValue = true) { // set value to private field _sensor = sensor; _group = group; // ... // continue here InitializeComponent(); GraphPane myPane; LineItem mycurve; Random randomGen = new Random(); myPane = zedChartMain.GraphPane; myPane.XAxis.Type = AxisType.Date; myPane.XAxis.Scale.Format = "HH:mm:ss\ndd-MM-yyyy"; myPane.Title.Text = chartName; myPane.XAxis.Title.Text = xLabel; myPane.YAxis.Title.Text = yLabel; myPane.XAxis.MajorGrid.IsVisible = true; myPane.YAxis.MajorGrid.IsVisible = true; if (sensor != null) { List <SensorValue> listvalue = entityConntext.SensorValues.Where(ent => ent.SensorID == sensor.SensorID).ToList(); DataSourcePointList dsp = new DataSourcePointList(); dsp.DataSource = listvalue; if (!showCalcValue) { dsp.YDataMember = "RawValue"; } if (showCalcValue) { dsp.YDataMember = "CalcValue"; } dsp.XDataMember = "MeaTime"; mycurve = myPane.AddCurve(sensor.Name, dsp, Color.FromArgb(randomGen.Next(255), randomGen.Next(255), randomGen.Next(255))); mycurve.Line.Width = 1; } if (group != null) { List <Sensor> sensorsInGroup = group.Sensors.ToList(); for (int i = 0; i < sensorsInGroup.Count; i++) { int idsen = Convert.ToInt16(sensorsInGroup[i].SensorID); List <SensorValue> listvalue = entityConntext.SensorValues.Where(ent => ent.SensorID == idsen).ToList(); DataSourcePointList dsp = new DataSourcePointList(); dsp.DataSource = listvalue; if (!showCalcValue) { dsp.YDataMember = "RawValue"; } if (showCalcValue) { dsp.YDataMember = "CalcValue"; } dsp.XDataMember = "MeaTime"; mycurve = myPane.AddCurve(sensorsInGroup[i].Name, dsp, Color.FromArgb(randomGen.Next(255), randomGen.Next(255), randomGen.Next(255))); mycurve.Line.Width = 1; } } //lenh hien thi chart zedChartMain.IsShowPointValues = true; zedChartMain.AxisChange(); zedChartMain.Invalidate(); zedChartMain.Refresh(); }
private void DrawCharts(DataTable data) { GraphPane pane = zedGraph.GraphPane; pane.CurveList.Clear(); pane.YAxisList.Clear(); pane.GraphObjList.Clear(); dataGridView.DataSource = null; if (data.Rows.Count > 1) { ushort colorIdx = 0; // this will auto increment with each new curve in list #region Populate dataGridView dataGridView.DataSource = data; dataGridView.Columns[0].DefaultCellStyle.Format = "HH:mm:ss"; foreach (DataGridViewColumn column in dataGridView.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } #endregion foreach (DataColumn col in data.Columns) { if (col.ColumnName != "Datetime") { Color axisColor = ColorTranslator.FromHtml(Utils.colors[colorIdx]); DataSourcePointList dsp = new DataSourcePointList(); dsp.DataSource = data; dsp.XDataMember = data.Columns[0].ToString(); // "dt"; dsp.YDataMember = col.ToString(); // "val"; YAxis yaxis = new YAxis(col.ColumnName); yaxis.Type = AxisType.Linear; yaxis.Color = axisColor; yaxis.Scale.FontSpec.Size = 11f; yaxis.Scale.FontSpec.FontColor = axisColor; // yaxis.IsVisible = true; yaxis.Scale.MaxAuto = true; yaxis.Scale.MinAuto = true; yaxis.MajorGrid.IsZeroLine = false; yaxis.MajorGrid.Color = axisColor; yaxis.MajorGrid.IsVisible = true; yaxis.MajorTic.IsInside = false; yaxis.MajorTic.IsOpposite = false; yaxis.MajorTic.Color = axisColor; yaxis.MinorTic.IsInside = false; yaxis.MinorTic.IsOpposite = false; yaxis.MinorTic.Color = axisColor; yaxis.Scale.Align = AlignP.Inside; yaxis.Title.FontSpec.Size = 12f; yaxis.Title.FontSpec.FontColor = axisColor; pane.YAxisList.Add(yaxis); int curveIdx = zedGraph.GraphPane.YAxisList.IndexOf(yaxis); LineItem curve = pane.AddCurve(col.ColumnName, dsp, axisColor, SymbolType.None); curve.Line.IsSmooth = true; curve.Line.Width = 1f; curve.YAxisIndex = curveIdx; dataGridView.Columns[colorIdx + 1].DefaultCellStyle.ForeColor = axisColor; colorIdx++; } } zedGraph.AxisChange(); } //If zedGraph IsEnabled and there is no data for selected range, it will crash while zooming! if (pane.CurveList.Count == 0) { zedGraph.Enabled = false; pane.Title.Text = "There is NO DATA available for selected period"; pane.Title.IsVisible = true; pane.Title.FontSpec.FontColor = Color.White; } else { pane.Title.IsVisible = false; zedGraph.Enabled = true; } zedGraph.IsShowPointValues = true; zedGraph.Invalidate(); GC.Collect(); }
private void CreateGraph_DataSource(ZedGraphControl z1) { GraphPane myPane = z1.GraphPane; // Set the titles myPane.Title.Text = titulo; myPane.XAxis.Title.Text = xAxisSource; myPane.YAxis.Title.Text = yAxisSource; // Create a new DataSourcePointList to handle the database connection var dspl = new DataSourcePointList { DataSource = data.Tables[0], XDataMember = xAxisSource, YDataMember = yAxisSource, ZDataMember = null }; // Specify the table as the data source // The X data will come from the "OrderDate" column // The Y data will come from the "Freight" column // The Z data are not used if (tipoGrafico == TipoGrafico.Torta) { // dibujar torta var values = new List <double>(); var labels = new List <string>(); foreach (DataRow dr in data.Tables[0].Rows) { if (dr[xAxisSource] != DBNull.Value) { values.Add(Convert.ToDouble(dr[xAxisSource])); } else { values.Add(0); } if (dr[yAxisSource] != DBNull.Value) { labels.Add(Convert.ToString(dr[yAxisSource])); } else { labels.Add(string.Empty); } } myPane.AddPieSlices(values.ToArray(), labels.ToArray()); } else { // formatear strings if (data.Tables[0].Columns[xAxisSource].DataType == typeof(string)) { myPane.XAxis.Scale.FontSpec.Angle = 90; myPane.XAxis.Scale.FontSpec.Size = myPane.XAxis.Scale.FontSpec.Size / 2; // agregar datos como textlabels var labels = new List <string>(); foreach (DataRow dr in data.Tables[0].Rows) { if (dr[xAxisSource] != DBNull.Value) { labels.Add((string)dr[xAxisSource]); } else { labels.Add(""); } } myPane.XAxis.Scale.TextLabels = labels.ToArray(); myPane.XAxis.Type = AxisType.Text; } if (data.Tables[0].Columns[yAxisSource].DataType == typeof(string)) { myPane.YAxis.Scale.FontSpec.Size = myPane.YAxis.Scale.FontSpec.Size / 2; // agregar datos como textlabels var labels = new List <string>(); foreach (DataRow dr in data.Tables[0].Rows) { if (dr[yAxisSource] != DBNull.Value) { labels.Add((string)dr[yAxisSource]); } else { labels.Add(""); } } myPane.YAxis.Scale.TextLabels = labels.ToArray(); myPane.YAxis.Type = AxisType.Text; } // formatear fechas if (data.Tables[0].Columns[xAxisSource].DataType == typeof(DateTime)) { myPane.XAxis.Type = AxisType.Date; } if (data.Tables[0].Columns[yAxisSource].DataType == typeof(DateTime)) { myPane.YAxis.Type = AxisType.Date; } switch (tipoGrafico) { case TipoGrafico.Puntos: myPane.AddCurve(yAxisSource, dspl, Color.Blue).Line.IsVisible = false; break; case TipoGrafico.Lineas: myPane.AddCurve(yAxisSource, dspl, Color.Blue); break; case TipoGrafico.Barras: myPane.AddBar(yAxisSource, dspl, Color.Blue); break; default: break; } myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); myPane.XAxis.MajorGrid.IsVisible = true; myPane.YAxis.MajorGrid.IsVisible = true; } // Auto set the scale ranges z1.AxisChange(); }
public void chartview(DataTable tablechart, List <Color> colorchart) { // Edit by binhpro 19/10/2012 _startDate = fromdateTextBox.Text.ToDateTimeTryParse(null); _endDate = todateTextBox.Text.ToDateTimeTryParse(null); // fix datetime if (_startDate != null) { _startDate = new DateTime(_startDate.Value.Year, _startDate.Value.Month, _startDate.Value.Day, 0, 0, 0); } if (_endDate != null) { _endDate = new DateTime(_endDate.Value.Year, _endDate.Value.Month, _endDate.Value.Day, 23, 59, 59); } for (int i = 0; i < sensorinchart; i++) { int idsen = Convert.ToInt16(tablechart.Rows[i]["SensorID"]); List <SensorValue> listvalue = entityConntext.SensorValues.Where(ent => ent.SensorID == idsen && (_startDate == null || ent.MeaTime >= _startDate) && (_endDate == null || ent.MeaTime <= _endDate) ).OrderBy(ent => ent.MeaTime).ToList(); DataSourcePointList dsp = new DataSourcePointList(); dsp.DataSource = listvalue; if (Convert.ToInt16(tablechart.Rows[i]["TypeofValue"]) == 0) { dsp.YDataMember = "RawValue"; } if (Convert.ToInt16(tablechart.Rows[i]["TypeofValue"]) == 1) { dsp.YDataMember = "CalcValue"; } dsp.XDataMember = "MeaTime"; LineItem mycurve = myPane.AddCurve(Convert.ToString(tablechart.Rows[i]["SensorName"]), dsp, colorchart[i], SymbolType.Circle); mycurve.Line.Width = tablechart.Rows[i]["BoldLine"].ToInt32TryParse(); switch (tablechart.Rows[i]["LineStyle"].ToInt32TryParse()) { case 0: mycurve.Line.Style = DashStyle.Solid; break; case 1: mycurve.Line.Style = DashStyle.Dot; break; case 2: mycurve.Line.Style = DashStyle.Dash; break; case 3: mycurve.Line.Style = DashStyle.DashDot; break; case 4: mycurve.Line.Style = DashStyle.DashDotDot; break; } //mycurve.Line.Style = (DashStyle)tablechart.Rows[i]["LineStyle"]; } //myPane. zedGraphChart.IsShowPointValues = true; //lenh hien thi chart zedGraphChart.AxisChange(); zedGraphChart.Invalidate(); zedGraphChart.Refresh(); }