- 积分
- 83
- 在线时间
- 7 小时
- 主题
- 0
- 注册时间
- 2022-2-18
- 帖子
- 9
- 最后登录
- 2023-11-27
- 帖子
- 9
- 软币
- 189
- 在线时间
- 7 小时
- 注册时间
- 2022-2-18
|
发表于 2022-3-13 22:43:41
|
显示全部楼层
#region 曲线图
private void ClearSeries(ChartControl chart)
{
chart.Series.Clear();//清除Series
}
/// <summary>
/// 创建折线图
/// </summary>
private void CreateChart(ChartControl chart, DataTable dt)
{
#region Series 创建几个图形的对象
ClearSeries(chart);
List<Series> list = new List<Series>();
for (int i = 1; i < dt.Columns.Count; i++)
{
list.Add(CreateSeries(dt.Columns.ColumnName, ViewType.Line, dt, i));
}
#endregion
chart.Series.AddRange(list.ToArray());
chart.Legend.Visible = false;
chart.SeriesTemplate.LabelsVisibility = DefaultBoolean.True;
SetX(chart);
List<Color> colorList = new List<Color> { Color.Red, Color.Yellow, Color.Tomato, Color.Blue, Color.Green,Color.BlueViolet };
if (((XYDiagram)chart.Diagram).SecondaryAxesY != null)//清空y轴
((XYDiagram)chart.Diagram).SecondaryAxesY.Clear();
for (int i = 0; i < list.Count; i++)
{
list.View.Color = colorList;
CreateAxisY(chart, list);
}
}
/// <summary>
/// 设置x轴可缩放
/// </summary>
/// <param name="chart1"></param>
private void SetX(ChartControl chart)
{
XYDiagram diagram = (XYDiagram)chart.Diagram;
diagram.EnableAxisXScrolling = true;
diagram.EnableAxisXZooming = true;
}
/// <summary>
/// 根据数据创建一个图形展现
/// </summary>
/// <param name="caption">图形标题</param>
/// <param name="viewType">图形类型</param>
/// <param name="dt">数据DataTable</param>
/// <param name="rowIndex">图形数据的行序号</param>
private Series CreateSeries(string caption, ViewType viewType, DataTable dt, int columnsIndex)
{
Series series = new Series(caption, viewType);
for (int i = 0; i < dt.Rows.Count; i++)
{
string argument = dt.Rows[0].ToString();//参数名称
decimal value = (decimal)dt.Rows[columnsIndex];//参数值
series.Points.Add(new SeriesPoint(argument, value));
}
//必须设置ArgumentScaleType的类型,否则显示会转换为日期格式,导致不是希望的格式显示
//也就是说,显示字符串的参数,必须设置类型为ScaleType.Qualitative
series.ArgumentScaleType = ScaleType.Qualitative;
series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//显示标注标签
return series;
}
/// <summary>
/// 创建图表的第二坐标系
/// </summary>
/// <param name="series">Series对象</param>
private SecondaryAxisY CreateAxisY(ChartControl chart, Series series)
{
SecondaryAxisY myAxis = new SecondaryAxisY(series.Name);
((XYDiagram)chart.Diagram).SecondaryAxesY.Add(myAxis);
((LineSeriesView)series.View).AxisY = myAxis;
myAxis.Title.Text = series.Name;
myAxis.Title.Alignment = StringAlignment.Far; //顶部对齐
myAxis.Title.Visible = true; //显示标题
myAxis.Title.Font = new Font("宋体", 9.0f);
Color color = series.View.Color;//设置坐标的颜色和图表线条颜色一致
myAxis.Title.TextColor = color;
myAxis.Label.TextColor = color;
myAxis.Color = color;
return myAxis;
}
#endregion |
|