如何创建甘特图

什么是甘特图

甘特图是一种显示项目进度的条形图。它显示项目各个任务或活动的开始和完成日期。每个任务或活动由一条条形表示,其长度与其持续时间对应。甘特图还指示任务之间的依赖关系,允许项目经理可视化任务需要完成的顺序。它们被广泛应用于项目管理中,以有效地规划、安排和跟踪项目。

如何在Excel中创建甘特图

您可以按照以下步骤在Excel中创建甘特图:

  1. 为甘特图添加一些数据。

  2. 选择数据,然后转到插入 –> 图表 –> 插入柱状图或条形图 –> 堆积条形图。 在我们的示例中,这是 B1:B7,然后插入堆积条形图

  3. 选择图表,选择数据->添加,设置系列名称系列值如下。

  4. 选择图表,编辑水平(类别)轴标签

  5. 格式化轴Y轴,选择反向类别

  6. 选择蓝色系列并设置填充->无填充

  7. 格式化轴X轴,设置最小值和最大值(1/5/2019:43470,1/30/2019:43494)。

  8. 为图表添加数据标签,现在你将获得一个甘特图。

如何在Aspose.Cells中添加甘特图

请参阅以下示例代码。 它加载包含一些示例数据的示例Excel文件。 然后根据初始数据创建堆积条形图并设置相关属性。 最后,将工作簿保存到输出XLSX格式。 以下屏幕截图显示了Aspose.Cells在输出Excel文件中创建的甘特图。

示例代码

// Create an instance of Workbook
Workbook workbook = new Workbook("sample.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Create BarStacked Chart
int i = worksheet.Charts.Add(ChartType.BarStacked, 5, 6, 20, 15);
// Retrieve the Chart object
Chart chart = worksheet.Charts[i];
// Set the chart title name
chart.Title.Text = "Gantt Chart";
// Set the chart title is Visible
chart.Title.IsVisible = true;
// Set data range
chart.SetChartDataRange("B1:B6", true);
// Add series data range
chart.NSeries.Add("C2:C6", true);
// No fill for one serie
chart.NSeries[0].Area.FillFormat.FillType = FillType.None;
// Set the Horizontal(Category) Axis
chart.NSeries.CategoryData = "A2:A6";
// Reverse the Horizontal(Category) Axis
chart.CategoryAxis.IsPlotOrderReversed = true;
//Set the value axis's MinValue and MaxValue
chart.ValueAxis.MinValue = worksheet.Cells["B2"].Value;
chart.ValueAxis.MaxValue = worksheet.Cells["D6"].Value;
chart.PlotArea.Area.FillFormat.FillType = FillType.None;
//Show the DataLabels
chart.NSeries[1].DataLabels.ShowValue = true;
//Disable the Legend
chart.ShowLegend = false;
//Save the result
workbook.Save("result.xlsx");