private void SetupHistogramChart(C1Chart chartRaw, C1Chart chartHisto) { chartHisto.Reset(); chartHisto.Left = 0; Form1_Resize(null, null); // Setup the Histogram chart appearance chartHisto.Style.BackColor = Color.AliceBlue; chartHisto.Style.Border.BorderStyle = BorderStyleEnum.Solid; chartHisto.Style.Border.Thickness = 2; chartHisto.Style.Border.Color = Color.Black; chartHisto.Legend.Visible = true; // Create a histogram type chart using a bar chart type rather an actual // histogram type. Histograms reflect single series of data well, but do // not show multiple series as well as a bar chart. // // Use the chart static method to generate histogram data from the raw data. ChartGroup cgHisto = chartHisto.ChartGroups[0]; ChartDataSeriesCollection cdscHisto = cgHisto.ChartData.SeriesList; cgHisto.ChartType = Chart2DTypeEnum.Bar; // obtain the data from chartRaw. ChartData cdRaw = chartRaw.ChartGroups[0].ChartData; ChartDataSeriesCollection cdscRaw = cdRaw.SeriesList; double dataMax = 0, dataMin = 0; // determine the extremes from the series means foreach (ChartDataSeries cdsRaw0 in cdscRaw) { double mean = cdsRaw0.Y.Statistics.Mean; double mn = cdsRaw0.MinY - mean; double mx = cdsRaw0.MaxY - mean; if (dataMax < mx) { dataMax = mx; } if (dataMin > mn) { dataMin = mn; } } dataMax = Math.Ceiling(dataMax); dataMin = Math.Floor(dataMin); // Assume that the interval width is one degree. int intervalCount = (int)(dataMax - dataMin) + 1; foreach (ChartDataSeries cdsRaw in cdscRaw) { double [] rawData = (double[])cdsRaw.Y.CopyDataOut(typeof(double)); // determine the difference of each point from the mean. double mean = cdsRaw.Y.Statistics.Mean; double [] differencesFromMean = (double[])rawData.Clone(); for (int d = 0; d < differencesFromMean.Length; d++) { differencesFromMean[d] -= mean; } double [] xbounds = null; double [] ycounts = null; double [] bis = null; // generate histogram data of the difference from the means. Start // using a half-width less than the dataMin. This will provide // intervals that center on each integral temperature difference. C1Chart.GenerateHistogramData(differencesFromMean, dataMin - 0.5, 1.0, intervalCount, -1.0, ref xbounds, ref ycounts, ref bis); // add a series of histogramdata ChartDataSeries cdsHisto = cdscHisto.AddNewSeries(); cdsHisto.Label = cdsRaw.Label; cdsHisto.LineStyle.Color = Color.FromArgb(128, cdsRaw.LineStyle.Color); bis = (double[])Array.CreateInstance(typeof(double), intervalCount); for (int p = 0; p < intervalCount; p++) { bis[p] = (xbounds[p] + xbounds[p + 1]) / 2; } cdsHisto.X.CopyDataIn(bis); cdsHisto.Y.CopyDataIn(ycounts); } // Add description and instructions to the header, footer and axes. chartHisto.Header.Text = "Bar Chart Histogram of Differences from Seasonal Mean Kansas City Temperatures"; chartHisto.Header.Style.Font = chartRaw.Header.Style.Font; chartHisto.Footer.Text = "Double-Click to Toggle Bar Stacking"; chartHisto.ChartArea.AxisX.Text = "Difference From Mean Seasonal Mean Temperature (F)"; chartHisto.ChartArea.AxisY.Text = "Count of Seasonal Mean Temperatures"; }
/// <summary> /// 设置chart的基本信息 /// </summary> public void SetXyChart() { c1Chart1.ChartGroups[0].ChartData.SeriesList.Clear(); Point pLoc = new Point(); pLoc.X = -1; pLoc.Y = -1; c1Chart1.Reset(); c1Chart1.ChartArea.ResetSizeDefault(); c1Chart1.ChartArea.ResetStyle(); // c1Chart1.Style.sho c1Chart1.ChartArea.AxisX.GridMajor.Visible = true; c1Chart1.ChartArea.AxisY.GridMajor.Visible = true; c1Chart1.ToolTip.Enabled = true; c1Chart1.BackColor = Color.White; c1Chart1.Legend.Compass = CompassEnum.East; c1Chart1.Legend.Location = pLoc; c1Chart1.Legend.Visible = true; c1Chart1.ToolTip.Enabled = true; c1Chart1.ToolTip.PlotElement = PlotElementEnum.Series; c1Chart1.ToolTip.SelectAction = SelectActionEnum.MouseOver; c1Chart1.ChartGroups[0].ChartData.SeriesList.Clear(); c1Chart1.ChartGroups[0].ChartType = C1.Win.C1Chart.Chart2DTypeEnum.XYPlot; c1Chart1.ChartArea.AxisX.ScrollBar.Visible = false; //设置图片的显示方式 C1Chart chart = c1Chart1; Area area = chart.ChartArea; Axis ax = area.AxisX; ax.AnnotationRotation = -30; C1.Win.C1Chart.ValueLabel vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); //C1FlexGrid c1Flex = (C1FlexGrid)_tab.TabPages[_tab.SelectedIndex].Controls[0]; var lines = CurrentSection.lines; string sDate = ""; string eDate = ""; foreach (var line in lines) { if (line.dataTable != null && line.dataTable.Rows.Count > 0) { var tsDate = line.dataTable.Rows[0][0].ToString(); var teDate = line.dataTable.Rows[line.dataTable.Rows.Count - 1][0].ToString(); var ti = line.dataTable.Rows.Count - 1; while (string.IsNullOrEmpty(teDate)) { ti--; teDate = line.dataTable.Rows[ti][0].ToString(); } if (sDate == "" || eDate == "") { sDate = tsDate; eDate = teDate; } else { if (DateTime.Parse(sDate) > DateTime.Parse(tsDate)) { sDate = tsDate; } if (DateTime.Parse(teDate) > DateTime.Parse(eDate)) { sDate = tsDate; } } } } string strPreDate = ""; string fmt = "yyyy.MM.dd"; if (sDate == "" || eDate == "") { return; } string strStartDate = sDate; string strEndDate = eDate; //第一天和最后一天都应该标注 vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); vlbl.NumericValue = 0; vlbl.Text = DateTime.Parse(strStartDate).ToString(fmt); int iDays = Convert.ToDateTime(DateTime.Parse(strEndDate).ToString("yyyy-MM-dd")).Subtract(Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd"))).Days; if (iDays < 32) //一个月以内,每天都标注 { for (int i = 1; i < iDays; i++) { vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); vlbl.NumericValue = i * 1; //修改前为50 vlbl.Text = Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd")).AddDays(i).ToString(fmt); } } else if (iDays < 93) ////三个月以内,10天标注一次 { for (int i = 10; i < iDays; i += 10) { vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); vlbl.NumericValue = i * 1; //修改前为50 vlbl.Text = Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd")).AddDays(i).ToString(fmt); } } else if (iDays < 186) ////六个月以内,15天标注一次 { for (int i = 15; i < iDays; i += 15) { vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); vlbl.NumericValue = i * 1; //修改前为50 vlbl.Text = Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd")).AddDays(i).ToString(fmt); } } else if (iDays < 731) ////两年以内,一个月标注一次 { for (int i = 30; i < iDays; i += 30) { vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); vlbl.NumericValue = i * 1; //修改前为50 vlbl.Text = Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd")).AddDays(i).ToString(fmt); } } else //两年以上,2个月标注一次 { for (int i = 60; i < iDays; i += 60) { vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); vlbl.NumericValue = i * 1; //修改前为50 vlbl.Text = Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd")).AddDays(i).ToString(fmt); } } ////第一天和最后一天都应该标注 //vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); //vlbl.NumericValue = iDays * 1; //修改前为50 //vlbl.Text = Convert.ToDateTime(DateTime.Parse(strStartDate).ToString("yyyy-MM-dd")).AddDays(iDays).ToString(fmt); ////首先得到开始时间这个月还剩多少天 //int iFirstLeft = DateTime.DaysInMonth(Convert.ToDateTime(strStartDate.Substring(0, 10)).Year, Convert.ToDateTime(strStartDate.Substring(0, 10)).Month) - Convert.ToDateTime(strStartDate.Substring(0, 10)).Day; ////下个月第一天 //string strFirstDay = Convert.ToDateTime(strStartDate.Substring(0, 10)).AddMonths(1).ToString(fmt).Substring(0, 7) + ".01"; ////判定有没有到最后一个月 //int iMonthCount = DateTime.Compare(Convert.ToDateTime(strFirstDay), Convert.ToDateTime(strEndDate.Substring(0, 10))); //vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); //vlbl.NumericValue = iFirstLeft * 50; //vlbl.Text = strFirstDay; //while (iMonthCount < 0) //{ // strPreDate = strFirstDay; // strFirstDay = Convert.ToDateTime(strPreDate.Substring(0, 10)).AddMonths(1).ToString(fmt).Substring(0, 7) + ".01"; // iFirstLeft = Convert.ToDateTime(strFirstDay).Subtract(Convert.ToDateTime(strStartDate.Substring(0, 10))).Days; // vlbl = chart.ChartArea.AxisX.ValueLabels.AddNewLabel(); // vlbl.NumericValue = iFirstLeft * 50; // vlbl.Text = strFirstDay; // iMonthCount = DateTime.Compare(Convert.ToDateTime(strFirstDay), Convert.ToDateTime(strEndDate.Substring(0, 10))); //} chart.ChartArea.AxisX.AnnoMethod = C1.Win.C1Chart.AnnotationMethodEnum.ValueLabels; }
private void SetupRawTempChart(C1Chart chart) { // show the average mean temperature data and resize // the chart with the form. chart.Reset(); chart.Width = this.ClientSize.Width - chart.Left; chart.Height = this.ClientSize.Height / 2; // setup the chart appearance chart.Style.BackColor = Color.White; chart.Style.Border.BorderStyle = BorderStyleEnum.Inset; chart.Style.Border.Thickness = 2; chart.Style.Border.Color = Color.Black; // setup the axis limits and titles Axis ax = chart.ChartArea.AxisX; ax.Min = 1880; ax.Max = 2020; ax = chart.ChartArea.AxisY; ax.Text = "Temperature F"; ax.Min = 0; ax.Max = 100; ax = chart.ChartArea.AxisY2; ax.Text = "Temperature C"; ax.Min = (0.0 - 32.0) * 5.0 / 9.0; ax.Max = (100.0 - 32.0) * 5.0 / 9.0; // Legend and header chart.Legend.Visible = true; chart.Header.Text = "Yearly Average Kansas City Mean Temperatures by Season"; chart.Header.Style.Font = new Font("Arial Narrow", 12, FontStyle.Bold); // get and add the data double [] years = KansasCityTempData(SeasonEnum.Years); ChartDataSeriesCollection cdsc = chart.ChartGroups[0].ChartData.SeriesList; // use a thicker line for the plots so the lines are very visible. const int lineThickness = 2; // Winter ChartDataSeries cds = cdsc.AddNewSeries(); cds.X.CopyDataIn(years); cds.Y.CopyDataIn(KansasCityTempData(SeasonEnum.Winter)); cds.SymbolStyle.Shape = SymbolShapeEnum.None; cds.Label = "Winter"; cds.LineStyle.Color = Color.Blue; cds.LineStyle.Thickness = lineThickness; // Spring cds = cdsc.AddNewSeries(); cds.X.CopyDataIn(years); cds.Y.CopyDataIn(KansasCityTempData(SeasonEnum.Spring)); cds.SymbolStyle.Shape = SymbolShapeEnum.None; cds.Label = "Spring"; cds.LineStyle.Color = Color.Green; cds.LineStyle.Thickness = lineThickness; // Summer cds = cdsc.AddNewSeries(); cds.X.CopyDataIn(years); cds.Y.CopyDataIn(KansasCityTempData(SeasonEnum.Summer)); cds.SymbolStyle.Shape = SymbolShapeEnum.None; cds.Label = "Summer"; cds.LineStyle.Color = Color.Red; cds.LineStyle.Thickness = lineThickness; // Fall cds = cdsc.AddNewSeries(); cds.X.CopyDataIn(years); cds.Y.CopyDataIn(KansasCityTempData(SeasonEnum.Fall)); cds.SymbolStyle.Shape = SymbolShapeEnum.None; cds.Label = "Fall"; cds.LineStyle.Color = Color.Brown; cds.LineStyle.Thickness = lineThickness; // add a series to Group1 so the Y2 axis is shown. At the same time // hide the series in the legend, since it is a dummy series. chart.ChartGroups[1].ChartData.SeriesList.AddNewSeries().LegendEntry = false; }