利用ComponentOne生成柱状图和折线图相结合,ComponentOne这个我就不用介绍了,大家搜一下就知道了。
可以直接在窗体上面把两个相结合起来。也可以完全通过代码来实现。以下就是通过代码来实现的:
先拖放一个C1Chart到窗体上面。然后删除上面的折线图:
删除后效果如下:
Designer.cs文件:
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form4));
this.c1Chart1 = new C1.Win.C1Chart.C1Chart();
((System.ComponentModel.ISupportInitialize)(this.c1Chart1)).BeginInit();
this.SuspendLayout();
//
// c1Chart1
//
this.c1Chart1.Location = new System.Drawing.Point(28, 33);
this.c1Chart1.Name = "c1Chart1";
this.c1Chart1.PropBag = resources.GetString("c1Chart1.PropBag");
this.c1Chart1.Size = new System.Drawing.Size(427, 303);
this.c1Chart1.TabIndex = 0;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(467, 434);
this.Controls.Add(this.c1Chart1);
this.MinimumSize = new System.Drawing.Size(475, 462);
this.Name = "Form2";
this.Text = "Separate Group Visual Effects";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.c1Chart1)).EndInit();
this.ResumeLayout(false);
}
private C1.Win.C1Chart.C1Chart c1Chart1;
后台代码:
private void Form1_Load(object sender, EventArgs e)
{
//Y,Y2设置
Area a = c1Chart1.ChartArea;
a.AxisX.UnitMajor = 10;
a.AxisX.UnitMinor = 10;
//设置轴线的最大值和最小值(当AutoMin和AutoMax被设置为False时).关于此的更多信息,请参见轴线边界(209页).
a.AxisY.Max = 50;
a.AxisY.Min = 0;
//设置主要和次要刻度线之间的间距(当AutoMajor和AutoMinor属性被设置为False时). 66page
a.AxisY.UnitMajor = 10;
a.AxisY.UnitMinor = 10;
// Clear 轴线标签的名
a.AxisY.Text = "";
a.AxisY2.Min = 0;
a.AxisY2.Max = 100;
a.AxisY2.UnitMajor = 10;
a.AxisY2.UnitMinor = 10;
// Clear 轴线标签的名
a.AxisY2.Text = "";
ChartGroup cgroup = c1Chart1.ChartGroups.Group0;
cgroup.ChartType = Chart2DTypeEnum.XYPlot;
//input the data through the series collection
ChartDataSeriesCollection cdsc = cgroup.ChartData.SeriesList;
cdsc.Clear(); //remove default data
//create the series object from the collection and add data
ChartDataSeries cds = cdsc.AddNewSeries();
// Add Data for ChartGroup0, Bar chart
string[] MonthNames = { "10月", "11月", "12月", "13月", "14月" };
double[] AvgPrecip = { 16, 19, 19, 19, 26 };
//create a label for the Bar chart data series
cds.Label = "左軸:月降水量(㎜/h)";
cds.FillStyle.Color1 = Color.Red;
cds.SymbolStyle.Shape = SymbolShapeEnum.None;
////Use the CopyDataIn method of the ChartDataArray object to copy the X and Y value data into the data series
cds.X.CopyDataIn(MonthNames);
cds.Y.CopyDataIn(AvgPrecip);
////create and add the data for the XY chart in Group1
ChartGroup cgroup2 = c1Chart1.ChartGroups.Group1;
cgroup2.ChartType = Chart2DTypeEnum.Bar;
//input the bar chart data of group1 through the series collection
ChartDataSeriesCollection cdsc2 = cgroup2.ChartData.SeriesList;
//create the series object from the second collection and add data
ChartDataSeries cds2 = cdsc2.AddNewSeries();
cds2.X.CopyDataIn(MonthNames);
cds2.Y.CopyDataIn(new double[] {41, 10, 0, 0, 27 });
cds2.Label = "右軸:月積算降水量(㎜)";
cds2.FillStyle.Color1 = Color.Blue;
//取得或设定轴是通常的或者反转的(升序或者降序).198page
c1Chart1.Legend.Reversed = true;
}
效果如下:
上面的写法会造成左右轴数据在条形状和折线图中显示的数据不对。改成下面的方法。
private void Form1_Load(object sender, EventArgs e)
{
string[] MonthNames = { "10时", "11时", "12时", "13时", "14时" };
ChartGroup cgroup0 = c1Chart1.ChartGroups.Group0;
cgroup0.ChartType = Chart2DTypeEnum.XYPlot;
ChartDataSeriesCollection cdsc = cgroup0.ChartData.SeriesList;
cdsc.Clear();
ChartDataSeries cds = cdsc.AddNewSeries();
cds.X.CopyDataIn(MonthNames);
cds.Y.CopyDataIn(new double[] { 50, 30, 10, 40, 20 });
ChartGroup cgroup2 = c1Chart1.ChartGroups.Group1;
cgroup2.ChartType = Chart2DTypeEnum.Bar;
ChartDataSeriesCollection cdsc2 = cgroup2.ChartData.SeriesList;
cdsc2.Clear();
ChartDataSeries cds2 = cdsc2.AddNewSeries();
cds2.X.CopyDataIn(MonthNames);
cds2.Y.CopyDataIn(new double[] { 40, 42, 40, 45, 10 });
this.c1Chart1.ChartArea.AxisY.Min = 0;
this.c1Chart1.ChartArea.AxisY.Max = 100;
this.c1Chart1.ChartArea.AxisY2.Min = 0;
this.c1Chart1.ChartArea.AxisY2.Max = 50;
c1Chart1.ChartArea.AxisY.Compass = C1.Win.C1Chart.CompassEnum.East;
c1Chart1.ChartArea.AxisY2.Compass = C1.Win.C1Chart.CompassEnum.West;
}