经过一段时间的学习掌握了调整chart控件的相关代码
public class ChartHelper
{
///
/// Name:添加序列
///
/// 图表对象
/// 序列名称
/// 图表类型
/// 颜色
/// 标记点颜色
/// 是否显示数值
public static void AddSeries(Chart chart, string seriesName, SeriesChartType chartType, Color color, Color markColor, bool showValue = false)
{
chart.Series.Add(seriesName);
chart.Series[seriesName].ChartType = chartType;
chart.Series[seriesName].Color = color;
if (showValue)
{
chart.Series[seriesName].IsValueShownAsLabel = true;
chart.Series[seriesName].MarkerStyle = MarkerStyle.Circle;
chart.Series[seriesName].MarkerColor = markColor;
chart.Series[seriesName].LabelForeColor = color;
chart.Series[seriesName].LabelAngle = -90;
}
}
///
/// Name:设置标题
/// Author:by boxuming 2019-04-28 14:25
///
/// 图表对象
/// 图表名称
public static void SetTitle(Chart chart, string chartName, Font font, Docking docking, Color foreColor)
{
chart.Titles.Add(chartName);
chart.Titles[0].Font = font;
chart.Titles[0].Docking = docking;
chart.Titles[0].ForeColor = foreColor;
}
///
/// Name:设置样式
/// Author:by boxuming 2019-04-23 14:04
///
/// 图表对象
/// 背景颜色
/// 字体颜色
public static void SetStyle(Chart chart, Color backColor, Color foreColor)
{
chart.BackColor = backColor;
chart.ChartAreas[0].BackColor = backColor;
chart.ForeColor = Color.Red;
}
///
/// Name:设置图例
/// Author:by boxuming 2019-04-23 14:30
///
/// 图表对象
/// 停靠位置
/// 对齐方式
/// 背景颜色
/// 字体颜色
public static void SetLegend(Chart chart, Docking docking, StringAlignment align, Color backColor, Color foreColor)
{
chart.Legends[0].Docking = docking;
chart.Legends[0].Alignment = align;
chart.Legends[0].BackColor = backColor;
chart.Legends[0].ForeColor = foreColor;
}
///
/// Name:设置XY轴
/// Author:by boxuming 2019-04-23 14:35
///
/// 图表对象
/// X轴标题
/// Y轴标题
/// 坐标轴标题对齐方式
/// 坐标轴字体颜色
/// 坐标轴颜色
/// 坐标轴箭头样式
/// X轴的间距
/// Y轴的间距
public static void SetXY(Chart chart, string xTitle, string yTitle, StringAlignment align, Color foreColor, Color lineColor, AxisArrowStyle arrowStyle, double xInterval, double yInterval)
{
chart.ChartAreas[0].AxisX.Title = xTitle;
chart.ChartAreas[0].AxisY.Title = yTitle;
chart.ChartAreas[0].AxisX.TitleAlignment = align;
chart.ChartAreas[0].AxisY.TitleAlignment = align;
chart.ChartAreas[0].AxisX.TitleForeColor = foreColor;
chart.ChartAreas[0].AxisY.TitleForeColor = foreColor;
chart.ChartAreas[0].AxisX.LabelStyle = new LabelStyle() { ForeColor = foreColor };
chart.ChartAreas[0].AxisY.LabelStyle = new LabelStyle() { ForeColor = foreColor };
chart.ChartAreas[0].AxisX.LineColor = lineColor;
chart.ChartAreas[0].AxisY.LineColor = lineColor;
chart.ChartAreas[0].AxisX.ArrowStyle = arrowStyle;
chart.ChartAreas[0].AxisY.ArrowStyle = arrowStyle;
chart.ChartAreas[0].AxisX.Interval = xInterval;
chart.ChartAreas[0].AxisY.Interval = yInterval;
}
///
/// Name:设置网格
/// Author:by boxuming 2019-04-23 14:55
///
/// 图表对象
/// 网格线颜色
/// X轴网格的间距
/// Y轴网格的间距
public static void SetMajorGrid(Chart chart, Color lineColor, double xInterval, double yInterval)
{
chart.ChartAreas[0].AxisX.MajorGrid.LineColor = lineColor;
chart.ChartAreas[0].AxisY.MajorGrid.LineColor = lineColor;
chart.ChartAreas[0].AxisX.MajorGrid.Interval = xInterval;
chart.ChartAreas[0].AxisY.MajorGrid.Interval = yInterval;
}
}
private void button4_Click(object sender, EventArgs e)
{
chart1.Series.Clear();
chart1.Titles.Clear();
chart1.ChartAreas[0].AxisX.Maximum = 10;//设定x轴的最大值为10
chart1.ChartAreas[0].AxisY.Maximum = 40;//设定y轴的最大值为40
chart1.ChartAreas[0].AxisX.Minimum = 1;//设定x轴的最小值1
chart1.ChartAreas[0].AxisY.Minimum = 10;//设定y轴的最小值10
ChartHelper.AddSeries(chart1, "曲线图", SeriesChartType.SplineRange, Color.FromArgb(100,46, 199, 201), Color.Red, true);
ChartHelper.SetTitle(chart1, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
ChartHelper.SetStyle(chart1, Color.Transparent, Color.Black);
ChartHelper.SetLegend(chart1, Docking.Top, StringAlignment.Center, Color.Transparent, Color.Black);
ChartHelper.SetXY(chart1, "序号", "数值", StringAlignment.Far, Color.Black, Color.Black, AxisArrowStyle.SharpTriangle, 1, 2);
ChartHelper.SetMajorGrid(chart1, Color.Black, 20, 2);
RefreshData();
}
末尾的RefreshData();是我自己设计的不通过数据库把数组添加到chart控件的方式,与题目没有太大关联,有兴趣的可以私信问我要源码。