Spire.Presentation组件的使用(2)--制作标题
先附上官网的使用教程:
Spire.Presentation for .NET 中文教程
结合实际应用,我将一步一步构建图形:
1.定义变量
首先,先定义全局使用的变量,比如X,Y是否多轴,是否为立体图,字体大小,文本字体等等
bool isMultiaxis = false;//X轴是否为多轴
bool isDoubleaxis = false;//Y轴是否含主轴和次轴
bool is3dPie = false;//是否为3D饼图
bool is3dBar = false;//是否为3D柱图
bool isAreaStyle = false;//是否为面积图
string title_fontFamily = "微软雅黑";//默认的标题字体
float legendFontSize = 12;//图例的大小
int pptHeight = 540;//图形画布高度
int pptWidth = 700;//图形画布宽度
int titleTop = 50;//标题的位置/// <summary>
/// 当前chart使用的字体
/// </summary>
public TextFont ChartTextFont
{get{TextFont textFont = new TextFont(title_fontFamily);//约定标题的字体就是其他样式的字体return textFont;}
}
/// <summary>
/// X是否多轴-类目轴
/// </summary>
public bool IsMultiAxis
{get { return isMultiaxis; }
}
/// <summary>
/// Y是否双轴-Value轴
/// </summary>
public bool IsDoubleAxis
{get { return isDoubleaxis; }
}
/// <summary>
/// 是否是立体饼子图
/// </summary>
public bool Is3DPie
{get { return is3dPie; }
}
/// <summary>
/// 是否是立体柱图
/// </summary>
public bool Is3DBar
{get { return is3dBar; }
}
/// <summary>
/// 图中是否有面积图
/// </summary>
public bool IsAreaStyle
{get { return isAreaStyle; }
}
2. 构建标题
构建标题有两种方式:
1)chart自带的ChartTitle
public void SetChartTitle(IChart chart, string title = "", string titleColor = "#000")
{chart.HasTitle = true;chart.ChartTitle.TextProperties.Text = title;chart.ChartTitle.TextProperties.IsCentered = true;chart.ChartTitle.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = ChartTextFont;chart.ChartTitle.TextProperties.Paragraphs[0].DefaultCharacterProperties.EastAsianFont = ChartTextFont;chart.ChartTitle.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;//文本填充类型
chart.ChartTitle.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = ColorTranslator.FromHtml(titleColor);//标题颜色}
}
LatinFont属性:配置汉字的字体;
EastAsianFont属性:配置英文和数字的字体。
2)自定义标题
public static void SetChartTitle(Presentation presentation, string title, string titleColor = "#000", float titleSize = 18)
{//添加标题IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(10, titleTop, pptWidth, 20));//因为chart从70开始,所以这里是20shape.Fill.FillType = FillFormatType.None;//标题的边框shape.ShapeStyle.LineColor.Color = Color.White;//标题的背景TextRange range = new TextRange(title);//标题名称range.LatinFont = ChartTextFont;range.EastAsianFont = ChartTextFont;range.Fill.FillType = FillFormatType.Solid;range.Fill.SolidColor.Color = ColorTranslator.FromHtml(titleColor);//标题颜色range.FontHeight = titleSize;//标题大小range.IsBold = TriState.True;//文本加粗shape.TextFrame.Paragraphs[0].TextRanges.Append(range);
}
每一个属性后面都加有注释,就不用再详细介绍了