Bumblebee/Classes/ExChart.cs
Flow diagram
Description
- 类定义 / Class Definition
public class ExChart
{
// ...
}
这个类定义了整个图表的结构,对应流程图中的所有步骤。
- 构造函数 / Constructor
public ExChart(string name, ExRange range, bool flip, Rg.Rectangle3d boundary)
{
// ...
}
这对应流程图中的"创建 ExChart 对象"和"初始化图表"步骤(B和C)。
- 设置图表属性 / Set Chart Properties
以下方法对应流程图中的D到N步骤:
- SetFillColors (设置填充颜色 / Set Fill Color)
- SetStrokeColors (设置轮廓颜色 / Set Outline Color)
- SetStrokeWeights (设置轮廓粗细 / Set Outline Weight)
- SetTitle (设置标题 / Set Title)
- SetLabel (设置标签 / Set Labels)
- SetLegend (设置图例 / Set Legend)
- SetAxisX 和 SetAxisY (设置X轴和Y轴 / Set X Axis and Y Axis)
- SetGridX 和 SetGridY (设置X轴和Y轴网格 / Set X Grid and Y Grid)
例如:
public void SetTitle(string title)
{
this.ComObj.Chart.HasTitle = true;
this.ComObj.Chart.ChartTitle.Text = title;
if (title == "") this.ComObj.Chart.HasTitle = false;
}
- 设置图表类型 / Set Chart Type
以下方法对应流程图中的O到U步骤:
- SetBarChart (设置条形图 / Set Bar Chart)
- SetColumnChart (设置柱状图 / Set Column Chart)
- SetRadialChart (设置径向图 / Set Radial Chart)
- SetScatterChart (设置散点图 / Set Scatter Chart)
- SetSurfaceChart (设置表面图 / Set Surface Chart)
- SetLineChart (设置折线图 / Set Line Chart)
例如:
public void SetBarChart(BarChartType chartType, ChartFill fillType)
{
// ...
this.ComObj.Chart.ChartType = (XL.XlChartType)type;
}
这些方法实现了流程图中描述的各种图表类型的设置。
- 其他辅助方法 / Other Helper Methods
代码中还包含了一些辅助方法,如ToString()重写,这些并不直接对应流程图中的步骤,但是为整个类的功能提供了支持。
总的来说,这个ExChart类提供了一个全面的接口来创建和自定义Excel图表。它实现了流程图中描述的所有主要步骤,从创建图表对象,到设置各种属性,再到选择特定的图表类型。代码的结构和方法名称清晰地反映了流程图中的各个步骤,使得使用者可以方便地按照流程图的逻辑来使用这个类创建所需的图表。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sd = System.Drawing;
using Rg = Rhino.Geometry;
using XL = Microsoft.Office.Interop.Excel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
namespace Bumblebee
{
// ExChart类:用于创建和管理Excel图表
public class ExChart
{
#region 成员变量
// Excel图表对象
public XL.ChartObject ComObj = null;
// 图表数据范围
protected ExRange range = null;
#endregion
#region 构造函数
/// <summary>
/// 创建一个新的ExChart对象
/// </summary>
/// <param name="name">图表名称</param>
/// <param name="range">数据范围</param>
/// <param name="flip">是否翻转行列</param>
/// <param name="boundary">图表边界</param>
public ExChart(string name, ExRange range, bool flip, Rg.Rectangle3d boundary)
{
this.range = new ExRange(range);
bool isNew = true;
// 检查是否已存在同名图表
foreach(XL.ChartObject obj in range.ComObj.Worksheet.ChartObjects())
{
if(name == obj.Name)
{
this.ComObj = obj;
isNew = false;
break;
}
}
// 如果是新图表,则创建
if(isNew)this.ComObj = range.ComObj.Worksheet.ChartObjects().Add(boundary.Corner(0).X, boundary.Corner(0).Y, boundary.Width, boundary.Height);
// 设置图表数据源
this.ComObj.Chart.SetSourceData(range.ComObj);
// 设置图表方向(按行或按列)
if (flip)
{
this.ComObj.Chart.PlotBy = XL.XlRowCol.xlColumns;
}
else
{
this.ComObj.Chart.PlotBy = XL.XlRowCol.xlRows;
}
// 默认不显示图例
this.ComObj.Chart.HasLegend = false;
// 设置所有系列的标记样式为点
foreach(XL.Series series in this.ComObj.Chart.SeriesCollection())
{
series.MarkerStyle = XL.XlMarkerStyle.xlMarkerStyleDot;
}
// 设置图表名称
this.ComObj.Name = name;
}
/// <summary>
/// 复制构造函数
/// </summary>
public ExChart(ExChart chart)
{
this.ComObj = chart.ComObj;
this.range = new ExRange(chart.range);
}
#endregion
#region 属性
// 这里可以添加属性
#endregion
#region 方法
/// <summary>
/// 设置填充颜色
/// </summary>
/// <param name="bySeries">是否按系列设置</param>
/// <param name="values">颜色列表</param>
public void SetFillColors(bool bySeries, List<Sd.Color> values)
{
int i = 0;
int c = values.Count - 1;
range.Worksheet.Freeze();
if (bySeries)
{
// 按系列设置颜色
foreach (XL.Series series in this.ComObj.Chart.SeriesCollection())
{
int j = i > c ? c : i;
series.Interior.Color = values[j];
i++;
}
}
else
{
// 按点设置颜色
foreach (XL.Series series in this.ComObj.Chart.SeriesCollection())
{
foreach (XL.Point point in series.Points())
{
int j = i > c ? c : i;
point.Interior.Color = values[j];
i++;
}
}
}
range.Worksheet.UnFreeze();
}
/// <summary>
/// 设置轮廓颜色
/// </summary>
/// <param name="bySeries">是否按系列设置</param>
/// <param name="values">颜色列表</param>
public void SetStrokeColors(bool bySeries, List<Sd.Color> values)
{
// 实现类似于SetFillColors
}
/// <summary>
/// 设置轮廓粗细
/// </summary>
/// <param name="bySeries">是否按系列设置</param>
/// <param name="values">粗细值列表</param>
public void SetStrokeWeights(bool bySeries, List<int> values)
{
// 实现类似于SetFillColors,但设置边框粗细
}
/// <summary>
/// 设置字体名称
/// </summary>
/// <param name="bySeries">是否按系列设置</param>
/// <param name="values">字体名称列表</param>
public void SetFontNames(bool bySeries, List<double> values)
{
// 实现类似于SetFillColors,但设置字体名称
}
/// <summary>
/// 设置图表标题
/// </summary>
/// <param name="title">标题文本</param>
public void SetTitle(string title)
{
this.ComObj.Chart.HasTitle = true;
this.ComObj.Chart.ChartTitle.Text = title;
if (title == "") this.ComObj.Chart.HasTitle = false;
}
/// <summary>
/// 设置数据标签
/// </summary>
/// <param name="label">标签类型</param>
public void SetLabel(LabelType label)
{
switch (label)
{
default:
this.ComObj.Chart.ApplyDataLabels(XL.XlDataLabelsType.xlDataLabelsShowNone);
break;
case LabelType.Category:
this.ComObj.Chart.ApplyDataLabels(XL.XlDataLabelsType.xlDataLabelsShowLabel);
break;
case LabelType.Value:
this.ComObj.Chart.ApplyDataLabels(XL.XlDataLabelsType.xlDataLabelsShowValue);
break;
}
}
/// <summary>
/// 设置图例位置
/// </summary>
/// <param name="legend">图例位置</param>
public void SetLegend(LegendLocations legend)
{
switch (legend)
{
case LegendLocations.None:
this.ComObj.Chart.HasLegend = false;
break;
case LegendLocations.Bottom:
this.ComObj.Chart.HasLegend = true;
this.ComObj.Chart.Legend.Position = XL.XlLegendPosition.xlLegendPositionBottom;
break;
case LegendLocations.Left:
this.ComObj.Chart.HasLegend = true;
this.ComObj.Chart.Legend.Position = XL.XlLegendPosition.xlLegendPositionLeft;
break;
case LegendLocations.Right:
this.ComObj.Chart.HasLegend = true;
this.ComObj.Chart.Legend.Position = XL.XlLegendPosition.xlLegendPositionRight;
break;
case LegendLocations.Top:
this.ComObj.Chart.HasLegend = true;
this.ComObj.Chart.Legend.Position = XL.XlLegendPosition.xlLegendPositionTop;
break;
}
}
/// <summary>
/// 设置X轴标题
/// </summary>
/// <param name="name">X轴标题文本</param>
public void SetAxisX(string name)
{
XL.Axis axis = this.ComObj.Chart.Axes(XL.XlAxisType.xlValue, XL.XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = name;
if (name == "") axis.HasTitle = false;
}
/// <summary>
/// 设置Y轴标题
/// </summary>
/// <param name="name">Y轴标题文本</param>
public void SetAxisY(string name)
{
// 实现类似于SetAxisX
}
/// <summary>
/// 设置X轴网格线
/// </summary>
/// <param name="type">网格线类型</param>
public void SetGridX(GridType type)
{
XL.Axis axis = this.ComObj.Chart.Axes(XL.XlAxisType.xlValue, XL.XlAxisGroup.xlPrimary);
switch (type)
{
case GridType.None:
axis.HasMajorGridlines = false;
axis.HasMinorGridlines = false;
break;
case GridType.Primary:
axis.HasMajorGridlines = true;
axis.HasMinorGridlines = false;
break;
case GridType.All:
axis.HasMajorGridlines = true;
axis.HasMinorGridlines = true;
break;
}
}
/// <summary>
/// 设置Y轴网格线
/// </summary>
/// <param name="type">网格线类型</param>
public void SetGridY(GridType type)
{
// 实现类似于SetGridX
}
/// <summary>
/// 设置条形图类型
/// </summary>
/// <param name="chartType">条形图类型</param>
/// <param name="fillType">填充类型</param>
public void SetBarChart(BarChartType chartType, ChartFill fillType)
{
int type = 57;
switch (chartType)
{
case BarChartType.Basic:
type = 57;
break;
case BarChartType.Box:
type = 60;
break;
case BarChartType.Pyramid:
type = 109;
break;
case BarChartType.Cylinder:
type = 95;
break;
case BarChartType.Cone:
type = 102;
break;
}
type += (int)fillType;
this.ComObj.Chart.ChartType = (XL.XlChartType)type;
}
/// <summary>
/// 设置柱状图类型
/// </summary>
/// <param name="chartType">柱状图类型</param>
/// <param name="fillType">填充类型</param>
public void SetColumnChart(BarChartType chartType, ChartFill fillType)
{
// 实现类似于SetBarChart
}
/// <summary>
/// 设置径向图类型
/// </summary>
/// <param name="chartType">径向图类型</param>
public void SetRadialChart(RadialChartType chartType)
{
XL.XlChartType type = XL.XlChartType.xlPie;
switch (chartType)
{
case RadialChartType.Pie:
type = XL.XlChartType.xlPie;
break;
case RadialChartType.Donut:
type = XL.XlChartType.xlDoughnut;
break;
case RadialChartType.Radar:
type = XL.XlChartType.xlRadar;
break;
case RadialChartType.Pie3D:
type = XL.XlChartType.xl3DPie;
break;
case RadialChartType.RadarFilled:
type = XL.XlChartType.xlRadarFilled;
break;
}
this.ComObj.Chart.ChartType = type;
}
/// <summary>
/// 设置散点图类型
/// </summary>
/// <param name="chartType">散点图类型</param>
public void SetScatterChart(ScatterChartType chartType)
{
// 实现类似于SetRadialChart
}
/// <summary>
/// 设置表面图类型
/// </summary>
/// <param name="chartType">表面图类型</param>
public void SetSurfaceChart(SurfaceChartType chartType)
{
// 实现类似于SetRadialChart
}
/// <summary>
/// 设置线图类型
/// </summary>
/// <param name="chartType">线图类型</param>
/// <param name="fillType">填充类型</param>
public void SetLineChart(LineChartType chartType, ChartFill fillType)
{
XL.XlChartType type = XL.XlChartType.xlLine;
switch (chartType)
{
case LineChartType.Line:
switch (fillType)
{
case ChartFill.Stack:
type = XL.XlChartType.xlLineStacked;
break;
case ChartFill.Fill:
type = XL.XlChartType.xlLineStacked100;
break;
default:
type = XL.XlChartType.xlLine;
break;
}
break;
// ... 其他类型的设置 ...
}
this.ComObj.Chart.ChartType = type;
}
#endregion
#region 重写方法
/// <summary>
/// 重写ToString方法,返回图表名称
/// </summary>
public override string ToString()
{
return "Chart | " + this.ComObj.Name;
}
#endregion
}
}