/// <summary>
/// 基于.NET 3.5的Chart工具类;对应的DevExpress版本:12.1.7;
/// </summary>
public static class ChartUtilsV3
{
/// <summary>
/// 添加基本的Series
/// </summary>
/// <param name="chat">ChartControl</param>
/// <param name="seriesName">Series的名称</param>
/// <param name="seriesType">Series的类型</param>
/// <param name="dataSource">Series的绑定数据源</param>
/// <param name="argumentDataMember">ArgumentDataMember绑定字段名称</param>
/// <param name="valueDataMembers">ValueDataMembers的绑定字段数组</param>
/// <param name="visible">Series是否可见</param>
/// <returns>Series</returns>
public static Series AddBaseSeries(this ChartControl chat, string seriesName, ViewType seriesType, object dataSource, string argumentDataMember, string[] valueDataMembers, bool visible)
{
Series _baseSeries = new Series(seriesName, seriesType);
_baseSeries.ArgumentDataMember = argumentDataMember;
_baseSeries.ValueDataMembers.AddRange(valueDataMembers);
_baseSeries.DataSource = dataSource;
_baseSeries.Visible = visible;
chat.Series.Add(_baseSeries);
return _baseSeries;
}
/// <summary>
/// 设置SeriesTemplate参数
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="dataSource">SeriesTemplate的绑定数据源</param>
/// <param name="argumentDataMember">ArgumentDataMember绑定字段名称</param>
/// <param name="valueDataMembers">ValueDataMembers的绑定字段数组</param>
/// <param name="visible">SeriesTemplate是否可见</param>
/// <returns>SeriesBase</returns>
public static SeriesBase SetSeriesTemplate(this ChartControl chart, object dataSource, string argumentDataMember, string[] valueDataMembers, bool visible)
{
chart.SeriesTemplate.ValueDataMembers.AddRange(valueDataMembers);
chart.SeriesTemplate.ArgumentDataMember = argumentDataMember;
chart.SeriesTemplate.Visible = visible;
chart.DataSource = dataSource;
return chart.SeriesTemplate;
}
/// <summary>
/// 增加数据筛选
/// </summary>
/// <param name="series">SeriesBase</param>
/// <param name="columnName">列名称</param>
/// <param name="value">列名称对应的筛选数值</param>
/// <param name="dataFilterCondition">DataFilterCondition枚举</param>
public static void AddDataFilter(this SeriesBase series, string columnName, object value, DataFilterCondition dataFilterCondition)
{
series.DataFilters.Add(new DataFilter(columnName, value.GetType().FullName, dataFilterCondition, value));
}
/// <summary>
/// 设置X轴Lable角度
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="angle">角度</param>
public static void SetXLableAngle(this ChartControl chart, int angle)
{
if (chart.Diagram is XYDiagram)
{
XYDiagram _xyDiagram = (XYDiagram)chart.Diagram;
if (_xyDiagram != null)
_xyDiagram.AxisX.Label.Angle = angle;
}
}
/// <summary>
/// 设置Y轴Lable角度
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="angle">角度</param>
public static void SetYLableAngle(this ChartControl chart, int angle)
{
if (chart.Diagram is XYDiagram)
{
XYDiagram _xyDiagram = (XYDiagram)chart.Diagram;
_xyDiagram.AxisY.Label.Angle = angle;
}
}
/// <summary>
/// 设置ColorEach
/// </summary>
/// <param name="series">SeriesBase</param>
/// <param name="colorEach">是否设置成ColorEach</param>
public static void SetColorEach(this SeriesBase series, bool colorEach)
{
SeriesViewColorEachSupportBase colorEachView = (SeriesViewColorEachSupportBase)series.View;
if (colorEachView != null)
{
colorEachView.ColorEach = colorEach;
}
}
/// <summary>
/// 设置是否显示十字标线
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="crosshair">是否显示十字标线</param>
public static void SetCrosshair(this ChartControl chart, bool crosshair)
{
chart.CrosshairEnabled = crosshair ? DefaultBoolean.True : DefaultBoolean.False;
chart.CrosshairOptions.ShowArgumentLabels = crosshair;
chart.CrosshairOptions.ShowArgumentLine = crosshair;
chart.CrosshairOptions.ShowValueLabels = crosshair;
chart.CrosshairOptions.ShowValueLine = crosshair;
}
/// <summary>
/// 新增ChartControl的Title文字
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="title">Title文字</param>
/// <param name="titlePosition">Title位置</param>
public static void AddTitle(this ChartControl chart, string title, ChartTitleDockStyle titlePosition)
{
ChartTitle _chartTitle = new ChartTitle();
_chartTitle.Text = title;
_chartTitle.Dock = titlePosition;
chart.Titles.Add(_chartTitle);
}
/// <summary>
/// 新增ChartControl的Title文字
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="title">Title文字</param>
/// <param name="visible">是否显示</param>
/// <param name="titlePosition">Title位置</param>
public static void AddTitle(this ChartControl chart, string title, bool visible, ChartTitleDockStyle titlePosition)
{
ChartTitle _chartTitle = new ChartTitle();
_chartTitle.Text = title;
_chartTitle.Visible = visible;
_chartTitle.Dock = titlePosition;
chart.Titles.Add(_chartTitle);
}
/// <summary>
/// 先删除Chart的Title,然后添加新的Title
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="title">Title文字</param>
/// <param name="titlePosition">Title位置</param>
public static void ClearThenAddTitle(this ChartControl chart, string title, ChartTitleDockStyle titlePosition)
{
chart.Titles.Clear();
ChartTitle _chartTitle = new ChartTitle();
_chartTitle.Text = title;
_chartTitle.Dock = titlePosition;
chart.Titles.Add(_chartTitle);
}
/// <summary>
/// 创建Drill-Down样式的Title
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="title">title文字</param>
/// <param name="visible">是否可见</param>
public static void AddDrillDownTitle(this ChartControl chart, string title, bool visible)
{
ChartTitle _chartTitle = new ChartTitle();
_chartTitle.Alignment = StringAlignment.Near;
_chartTitle.Antialiasing = false;
_chartTitle.Font = new Font("Tahoma", 10F, FontStyle.Underline);
_chartTitle.Indent = 20;
_chartTitle.Text = title;
_chartTitle.TextColor = Color.RoyalBlue;
_chartTitle.Visible = false;
chart.Titles.Add(_chartTitle);
}
/// <summary>
/// 饼状Series设置成百分比显示
/// </summary>
/// <param name="series">SeriesBase</param>
/// <param name="valueLegendType">Series对应Lengend显示类型</param>
/// <param name="lengendPointView">Series对应Lengend PointView类型</param>
public static void SetPiePercentage(this SeriesBase series, NumericFormat valueLegendType, PointView lengendPointView)
{
if (series.View is PieSeriesView || series.View is Pie3DSeriesView)
{
PiePointOptions _piePointOptions = (PiePointOptions)series.Label.PointOptions;
if (_piePointOptions != null)
{
_piePointOptions.PercentOptions.ValueAsPercent = true;
_piePointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
_piePointOptions.ValueNumericOptions.Precision = 0;
series.LegendPointOptions.ValueNumericOptions.Format = valueLegendType;
series.LegendPointOptions.PointView = lengendPointView;
}
}
}
/// <summary>
/// Lable格式化设置
/// 【{A} Use it to display a series point arguments 】
/// 【{V} Use it to display a series point values】
/// 【{S} Use it to display the name of the series】
/// </summary>
/// <param name="series">SeriesBase</param>
/// <param name="formatString">Lable格式化设置;【{A}{V}{S}】</param>
public static void CustomLable(this SeriesBase series, string formatString)
{
if (series.LabelsVisibility != DefaultBoolean.True)
series.LabelsVisibility = DefaultBoolean.True;
series.Label.PointOptions.Pattern = formatString;
}
/// <summary>
/// 十字标线的Lable格式化设置
/// 【{A} Use it to display a series point arguments 】
/// 【{V} Use it to display a series point values】
/// 【{S} Use it to display the name of the series】
/// </summary>
/// <param name="series">SeriesBase</param>
/// <param name="formatString">CrosshairLabel格式化设置;【{A}{V}{S}】</param>
public static void CustomCrosshairLabel(this SeriesBase series, string formatString)
{
if (series.CrosshairEnabled != DefaultBoolean.True)
series.CrosshairEnabled = DefaultBoolean.True;
series.CrosshairLabelPattern = formatString;
}
/// <summary>
/// 将X轴格式化成时间轴
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="dateTimeMeasureUnit">X轴刻度单位</param>
/// <param name="dateTimeGridAlignment">X轴刻度间距的单位</param>
public static void SetAxisXTime(this ChartControl chart, DateTimeMeasurementUnit dateTimeMeasureUnit, DateTimeMeasurementUnit dateTimeGridAlignment)
{
if (chart.Diagram is XYDiagram)
{
XYDiagram _diagram = (XYDiagram)chart.Diagram;
if (_diagram != null)
{
_diagram.AxisX.DateTimeMeasureUnit = dateTimeMeasureUnit;//X轴刻度单位
_diagram.AxisX.DateTimeGridAlignment = dateTimeGridAlignment;//X轴刻度间距
}
}
}
/// <summary>
/// 将X轴格式化成时间轴
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="dateTimeMeasureUnit">X轴刻度单位</param>
/// <param name="dateTimeGridAlignment">X轴刻度间距的单位</param>
/// <param name="formatString">时间格式;eg:yyyy-MM</param>
public static void SetAxisXTime(this ChartControl chart, DateTimeMeasurementUnit dateTimeMeasureUnit, DateTimeMeasurementUnit dateTimeGridAlignment, string formatString)
{
if (chart.Diagram is XYDiagram)
{
XYDiagram _diagram = (XYDiagram)chart.Diagram;
if (_diagram != null)
{
_diagram.AxisX.DateTimeMeasureUnit = dateTimeMeasureUnit;//X轴刻度单位
_diagram.AxisX.DateTimeGridAlignment = dateTimeGridAlignment;//X轴刻度间距
_diagram.AxisX.DateTimeOptions.Format = DateTimeFormat.Custom;
_diagram.AxisX.DateTimeOptions.FormatString = formatString;
}
}
}
/// <summary>
/// 设置ChartControl滚动条【默认X,Y轴都出现】
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="backColor">滚动条背景颜色</param>