我的VB不是很好,所以我将开始用C#发布一个例子(如果你真的需要我可以尝试翻译它)。
以下是可用于将项目绑定到 mschart 并获取柱形图的三个方法示例:
示例 1:单个区域和并排列
private void FillChartSingleArea()
{
// this set the datasource
this.chart1.DataSource = GetItems();
// clear all the (possible) existing series
this.chart1.Series.Clear();
// add the hours series
var hoursSeries = this.chart1.Series.Add("Hours");
hoursSeries.XValueMember = "Name";
hoursSeries.YValueMembers = "Hours";
hoursSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
// add the percentages series
var percSeries = this.chart1.Series.Add("Percentages");
percSeries.XValueMember = "Name";
percSeries.YValueMembers = "Percent";
percSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
}
示例 2:一张一张的两张图表
private void FillChartDoubleArea()
{
// this set the datasource
this.chart1.DataSource = GetItems();
// clear all the (possible) existing series
this.chart1.Series.Clear();
// clear all the existing areas and add 2 new areas
this.chart1.ChartAreas.Clear();
this.chart1.ChartAreas.Add("Area1");
this.chart1.ChartAreas.Add("Area2");
// add the hours series
var hoursSeries = this.chart1.Series.Add("Hours");
hoursSeries.ChartArea = "Area1";
hoursSeries.XValueMember = "Name";
hoursSeries.YValueMembers = "Hours";
hoursSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
// add the percentages series
var percSeries = this.chart1.Series.Add("Percentages");
hoursSeries.ChartArea = "Area2";
percSeries.XValueMember = "Name";
percSeries.YValueMembers = "Percent";
percSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
}
示例 3:单个区域和堆叠列
private void FillStackedChartSingleArea()
{
// this set the datasource
this.chart1.DataSource = GetItems();
// clear all the (possible) existing series
this.chart1.Series.Clear();
// add the hours series
var hoursSeries = this.chart1.Series.Add("Hours");
hoursSeries.XValueMember = "Name";
hoursSeries.YValueMembers = "Hours";
hoursSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
// add the percentages series
var percSeries = this.chart1.Series.Add("Percentages");
percSeries.XValueMember = "Name";
percSeries.YValueMembers = "Percent";
percSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
}
其中GetItems
方法定义如下(对于所有示例):
private List<DOHoursChartItem> GetItems()
{
var items = new List<DOHoursChartItem>()
{
new DOHoursChartItem("John", 120),
new DOHoursChartItem("Amanda", 40),
new DOHoursChartItem("David", 70),
new DOHoursChartItem("Rachel", 10),
};
// compute the percentages
var totalHours = items.Sum(x => x.Hours);
foreach (var item in items)
item.Percent = (item.Hours * 100.0) / totalHours;
return items;
}
并DOHoursChartItem
作为:
class DOHoursChartItem
{
public String Name { get; set; }
public double Hours { get; set; }
public double Percent { get; set; }
public DOHoursChartItem(string name, double hours)
{
this.Name = name;
this.Hours = hours;
}
}
注意
这些实际上是柱形图;通过将 ChartType 设置为Bar
(或StackedBar
),您将获得相同的结果,但条形图将具有水平方向。