下面给出了转载链接,这里先贴一个参考链接,在本文章内没引用
MsChart图报表控件用法
MsChart可让您的项目及报表,轻松套用各种功能强大的 2D、3D、实时变化的动态图表;且透过 AJAX,可让图表及里面的数据,每秒钟都持续更新;使用者透过浏览器,可和图表做各种互动设定
要想利用这个功能强大的控件,首先必须引用以下DLL和相关文件(从VS2010开始已经集成了该控件,所以直接饮用下面的dll就可以了,控件在工具箱的数据栏中。VS2010之前需要安装):
-
在WinForm应用程序中要想使用该图表控件,需引用如下DLL(这些dll在C盘安装的.NET框架文件夹内):
System.Windows.Forms.DataVisualization.Design.dll
System.Windows.Forms.DataVisualization.dll
System.Windows.Forms.DataVisualization.xml (这个我没用到好像?不知道啥时要用) -
在Web应用程序中要想使用该图表控件,需引用如下DLL:
System.Web.DataVisualization.dll
System.Web.DataVisualization.Design.dll
System.Web.DataVisualization.xml
下面介绍该控件的几个重要属性
转载链接关于微软C#中的CHART图表控件的简单使用
1.ChartAreas属性
ChartAreas属性指绘图区,一个控件可以有多个绘图区,比如我要在同一个控件内显示饼图和柱状图,肯定不能放在同一个ChartAreas区域内,应该在同一个Chart控件里增加两个ChartAreas并分别绑定Series对象。所以ChartAreas属性对应的是一个集合。
2.Series属性
Series属性就是各种图表的图形啦,比如我们要显示某月的天气变化,那么应该有这样两组数据,一组是天数,一组是每天对应的温度值,同时绑定到Series对象中,再将Series对象Add()到Chart控件的Series属性里即可。为了横向比较,例如我要看本月与上月的天气曲线变化图,并同时显示在同一个ChartAreas中,那该怎么办呢?很简单,再实例一个Series对象,将上月的天数数组与温度值数组绑定到一个新的Series2实例中,再将Series2实例Add()到Chart控件的Series属性里,此时,Chart控件的第一个绘图区ChartArea里就会有两条曲线。
3.Legends属性
Legend就是指一个图标的图例,当一个Series属性有多个Series时,或是一个Series有几组数据时,为了区分各自的颜色,通常每个Serie对象一种颜色,这就需要用到图例来指明哪个颜色的代表的是什么数据。如下图所示:
4.关于数据的绑定
将要显示的X轴和Y轴的数据分别放到两个数组里在, 然后绑定即可。
cht1.Series[0].Points.DataBindXY(x, y);
5.一些关键字的使用
/*
#VALX 显示当前图例的X轴的对应文本(或数据)
#VAL, #VALY, 显示当前图例的Y轴的对应文本(或数据)
#VALY2, #VALY3, 显示当前图例的辅助Y轴的对应文本(或数据)
#SER: 显示当前图例的名称
#LABEL 显示当前图例的标签文本
#INDEX 显示当前图例的索引
#PERCENT 显示当前图例的所占的百分比
#TOTAL 总数量
#LEGENDTEXT 图例文本
*/
代码如下
首先第一个图片代码和效果如下:
下面代码也解决了,X轴文本显示不完全的问题。
/// <summary>
/// 另一个柱状图,直接绑定数组数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton3_Click(object sender, EventArgs e)
{
string[] x = new string[] { "南山大队", "福田大队", "罗湖大队", "宝安大队", "指挥处", "大帝科技",
"南山大队", "福田大队", "罗湖大队", "宝安大队", "指挥处", "大帝科技" };
double[] y = new double[] { 541, 574, 345, 854, 684, 257, 541, 574, 345, 854, 684, 257 };
#region 柱状图
chart1.Series.Clear();
Series series1 = new Series();
//将系列添加到图表
chart1.Series.Add(series1);
//设置图表Chart标题
chart1.Titles.Add("柱状图数据分析");
chart1.Titles[0].ForeColor = Color.White;
chart1.Titles[0].Font = new Font("微软雅黑", 12f, FontStyle.Regular) ;
chart1.Titles[0].Alignment = ContentAlignment.TopCenter;
chart1.Titles.Add("合计:25414宗");
//设置标题文本颜色
chart1.Titles[1].ForeColor = Color.White;
chart1.Titles[1].Font = new Font("微软雅黑",8f,FontStyle.Regular);
chart1.Titles[1].Alignment = ContentAlignment.TopRight;
//控件背景
chart1.BackColor = Color.Black;
//图表区背景
chart1.ChartAreas[0].BackColor = Color.Transparent;
chart1.ChartAreas[0].BorderColor = Color.Transparent;
//X轴标签间距 !!!!!下面四句的设置能保证X轴标签完全显示出来!!!!
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45; //标签显示角度反向45°,也可设置为90°
chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
//X轴间隔大小随变量数量变化
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
//设置Y轴间隔
chart1.ChartAreas[0].AxisY.Interval = 200;
//确定标签显示是否带有偏移量
chart1.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑",14f,FontStyle.Regular);
chart1.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//X坐标轴颜色
chart1.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a");
chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑",10f,FontStyle.Regular);
//X坐标轴标题
//chart1.ChartAreas[0].AxisX.Title = "数量(宗)";
//chart1.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑",10f,FontStyle.Regular);
//chart1.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
//chart1.ChartAreas[0].AxisX.ToolTip = "数量(宗)";
//X轴网格线条
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
//Y坐标轴颜色
chart1.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑",10f,FontStyle.Regular);
//Y坐标轴标题
chart1.ChartAreas[0].AxisY.Title = "数量(宗)";
chart1.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑",10f,FontStyle.Regular);
chart1.ChartAreas[0].AxisY.TitleForeColor = Color.White;
chart1.ChartAreas[0].AxisY.TextOrientation = System.Windows.Forms.DataVisualization.Charting.TextOrientation.Rotated270;
chart1.ChartAreas[0].AxisY.ToolTip = "数量(宗)";
//Y轴网格线条
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
//辅助Y轴的线
chart1.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
chart1.ChartAreas[0].BackGradientStyle = GradientStyle.TopBottom;
chart1.Series[0].XValueType = ChartValueType.String; //设置X轴上的值类型
chart1.Series[0].Label = "#VAL"; //设置显示X Y的值
chart1.Series[0].LabelForeColor = Color.White;
chart1.Series[0].ToolTip = "#VALX:#VAL"; //鼠标移动到对应点显示数值
chart1.Series[0].ChartType = SeriesChartType.Column; //图类型(柱状图)
//设置数据点的颜色
chart1.Series[0].Color = Color.Lime;
chart1.Series[0].IsValueShownAsLabel = true;
chart1.Series[0].LabelForeColor = Color.White;
//设置数据点自定义属性
chart1.Series[0].CustomProperties = "DrawingStyle = Cylinder";
//绑定数据
chart1.Series[0].Points.DataBindXY(x, y);
chart1.Series[0].Points[0].Color = Color.White;
//设置Series对象的调色板
chart1.Series[0].Palette = ChartColorPalette.Bright;
#endregion
}
其他图片的实现代码如下:
饼图
#region 饼图
string[] x = new string[] { "南山大队", "福田大队", "罗湖大队", "宝安大队", "指挥处", "大帝科技",
"南山大队", "福田大队", "罗湖大队", "宝安大队", "指挥处", "大帝科技" };
double[] y = new double[] { 541, 574, 345, 854, 684, 257, 541, 574, 345, 854, 684, 257 };
//标题
cht2.Titles.Add("饼图数据分析");
cht2.Titles[0].ForeColor = Color.White;
cht2.Titles[0].Font = new Font("微软雅黑", 12f, FontStyle.Regular);
cht2.Titles[0].Alignment = ContentAlignment.TopCenter;
cht2.Titles.Add("合计:25412 宗");
cht2.Titles[1].ForeColor = Color.White;
cht2.Titles[1].Font = new Font("微软雅黑", 8f, FontStyle.Regular);
cht2.Titles[1].Alignment = ContentAlignment.TopRight;
//控件背景
cht2.BackColor = Color.Transparent;
//图表区背景
cht2.ChartAreas[0].BackColor = Color.Transparent;
cht2.ChartAreas[0].BorderColor = Color.Transparent;
//X轴标签间距
cht2.ChartAreas[0].AxisX.Interval = 1;
cht2.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht2.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht2.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 14f, FontStyle.Regular);
cht2.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//X坐标轴颜色
cht2.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht2.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht2.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
//X坐标轴标题
cht2.ChartAreas[0].AxisX.Title = "数量(宗)";
cht2.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
cht2.ChartAreas[0].AxisX.TitleForeColor = Color.White;
cht2.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
cht2.ChartAreas[0].AxisX.ToolTip = "数量(宗)";
//X轴网络线条
cht2.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht2.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
//Y坐标轴颜色
cht2.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht2.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht2.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
//Y坐标轴标题
cht2.ChartAreas[0].AxisY.Title = "数量(宗)";
cht2.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
cht2.ChartAreas[0].AxisY.TitleForeColor = Color.White;
cht2.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
cht2.ChartAreas[0].AxisY.ToolTip = "数量(宗)";
//Y轴网格线条
cht2.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht2.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
cht2.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
//背景渐变
cht2.ChartAreas[0].BackGradientStyle = GradientStyle.None;
//图例样式
Legend legend2 = new Legend("#VALX");
legend2.Title = "图例";
legend2.TitleBackColor = Color.Transparent;
legend2.BackColor = Color.Transparent;
legend2.TitleForeColor = Color.White;
legend2.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
legend2.Font = new Font("微软雅黑", 8f, FontStyle.Regular);
legend2.ForeColor = Color.White;
cht2.Series[0].XValueType = ChartValueType.String; //设置X轴上的值类型
cht2.Series[0].Label = "#VAL"; //设置显示X Y的值
cht2.Series[0].LabelForeColor = Color.White;
cht2.Series[0].ToolTip = "#VALX:#VAL(宗)"; //鼠标移动到对应点显示数值
cht2.Series[0].ChartType = SeriesChartType.Pie; //图类型(折线)
cht2.Series[0].Color = Color.Lime;
cht2.Series[0].LegendText = legend2.Name;
cht2.Series[0].IsValueShownAsLabel = true;
cht2.Series[0].LabelForeColor = Color.White;
cht2.Series[0].CustomProperties = "DrawingStyle = Cylinder";
cht2.Series[0].CustomProperties = "PieLabelStyle = Outside";
cht2.Legends.Add(legend2);
cht2.Legends[0].Position.Auto = true;
cht2.Series[0].IsValueShownAsLabel = true;
//是否显示图例
cht2.Series[0].IsVisibleInLegend = true;
cht2.Series[0].ShadowOffset = 0;
//饼图折线
cht2.Series[0]["PieLineColor"] = "White";
//绑定数据
cht2.Series[0].Points.DataBindXY(x, y);
cht2.Series[0].Points[0].Color = Color.White;
//绑定颜色
cht2.Series[0].Palette = ChartColorPalette.BrightPastel;
#endregion
Bar图
#region Bar图
string[] a = new string[] { "南山大队", "福田大队", "罗湖大队", "宝安大队", "指挥处", };
double[] b = new double[] { 541, 574, 345, 854, 257 };
//标题
cht3.Titles.Add("交通违法行为TOP5");
cht3.Titles[0].ForeColor = Color.White;
cht3.Titles[0].Font = new Font("微软雅黑", 12f, FontStyle.Regular);
cht3.Titles[0].Alignment = ContentAlignment.TopCenter;
cht3.Titles.Add("合计:25412 宗 ");
cht3.Titles[1].ForeColor = Color.White;
cht3.Titles[1].Font = new Font("微软雅黑", 8f, FontStyle.Regular);
cht3.Titles[1].Alignment = ContentAlignment.TopRight;
//控件背景
cht3.BackColor = Color.Transparent;
//图表区背景
cht3.ChartAreas[0].BackColor = Color.Transparent;
cht3.ChartAreas[0].BorderColor = Color.Transparent;
//X轴标签间距
cht3.ChartAreas[0].AxisX.Interval = 1;
cht3.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht3.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht3.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 14f, FontStyle.Regular);
cht3.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//X坐标轴颜色
cht3.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht3.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht3.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
//X坐标轴标题
//cht3.ChartAreas[0].AxisX.Title = "数量(宗)";
//cht3.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
//cht3.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//cht3.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Auto;
//cht3.ChartAreas[0].AxisX.ToolTip = "数量(宗)";
//X轴网络线条
cht3.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht3.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
//Y坐标轴颜色
cht3.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht3.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht3.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
//Y坐标轴标题
//cht3.ChartAreas[0].AxisY.Title = "数量(宗)";
//cht3.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
//cht3.ChartAreas[0].AxisY.TitleForeColor = Color.White;
//cht3.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Auto;
//cht3.ChartAreas[0].AxisY.ToolTip = "数量(宗)";
//Y轴网格线条
cht3.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht3.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
cht3.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
cht3.ChartAreas[0].AxisX.IsMarginVisible = false;
cht3.ChartAreas[0].Area3DStyle.Enable3D = true;
//背景渐变
cht2.ChartAreas[0].BackGradientStyle = GradientStyle.None;
//图例样式
Legend legend3 = new Legend("#VALX");
legend3.Title = "图例";
legend3.TitleBackColor = Color.Transparent;
legend3.BackColor = Color.Transparent;
legend3.TitleForeColor = Color.White;
legend3.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
legend3.Font = new Font("微软雅黑", 8f, FontStyle.Regular);
legend3.ForeColor = Color.White;
cht3.Series[0].XValueType = ChartValueType.String; //设置X轴上的值类型
cht3.Series[0].Label = "#VAL"; //设置显示X Y的值
cht3.Series[0].LabelForeColor = Color.White;
cht3.Series[0].ToolTip = "#VALX:#VAL(宗)"; //鼠标移动到对应点显示数值
cht3.Series[0].ChartType = SeriesChartType.Bar; //图类型(折线)
cht3.Series[0].Color = Color.Lime;
//cht3.Series[0].LegendText = legend3.Name;
cht3.Series[0].IsValueShownAsLabel = true;
cht3.Series[0].LabelForeColor = Color.White;
cht3.Series[0].CustomProperties = "DrawingStyle = Cylinder";
cht3.Series[0].CustomProperties = "PieLabelStyle = Outside";
//cht3.Legends.Add(legend3);
//cht3.Legends[0].Position.Auto = true;
//是否显示图例
cht3.Series[0].IsVisibleInLegend = true;
cht3.Series[0].ShadowOffset = 0;
//饼图折线
cht3.Series[0]["PieLineColor"] = "White";
//绑定数据
cht3.Series[0].Points.DataBindXY(a, b);
//cht3.Series[0].Points[0].Color = Color.White;
//绑定颜色
cht3.Series[0].Palette = ChartColorPalette.BrightPastel;
//for (int n = 0; n < x.Length; n++)
//{
// int ptIdx = cht3.Series[0].Points.AddY(Convert.ToDouble(y[n]));
// DataPoint pt = this.cht3.Series[0].Points[ptIdx];
// pt.LegendText = x[n] + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 次" + " ]";//右边标签列显示的文字
// pt.Label = x[n] + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 次" + " ]"; //圆饼外显示的信息
// // pt.LabelToolTip = "#PERCENT{P2}";
// //pt.LabelBorderColor = Color.Red;//文字背景色
//}
#endregion
雷达图
#region 雷达图
// //标题
cht4.Titles.Add("交通违法行为TOP5");
cht4.Titles[0].ForeColor = Color.White;
cht4.Titles[0].Font = new Font("微软雅黑", 12f, FontStyle.Regular);
cht4.Titles[0].Alignment = ContentAlignment.TopCenter;
cht4.Titles.Add("合计:25412 宗 ");
cht4.Titles[1].ForeColor = Color.White;
cht4.Titles[1].Font = new Font("微软雅黑", 8f, FontStyle.Regular);
cht4.Titles[1].Alignment = ContentAlignment.TopRight;
//控件背景
cht4.BackColor = Color.Transparent;
cht4.ChartAreas[0].BackColor = Color.Transparent;
cht4.ChartAreas[0].BorderColor = Color.Transparent;
//X轴标签间距
cht4.ChartAreas[0].AxisX.Interval = 1;
cht4.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht4.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht4.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 14f, FontStyle.Regular);
cht4.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//X坐标轴颜色
cht4.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht4.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht4.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
//X坐标轴标题
//cht4.ChartAreas[0].AxisX.Title = "数量(宗)";
//cht4.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
//cht4.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//cht4.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Auto;
//cht4.ChartAreas[0].AxisX.ToolTip = "数量(宗)";
//X轴网络线条
cht4.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht4.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
//Y坐标轴颜色
cht4.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht4.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht4.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
//Y坐标轴标题
//cht4.ChartAreas[0].AxisY.Title = "数量(宗)";
//cht4.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
//cht4.ChartAreas[0].AxisY.TitleForeColor = Color.White;
//cht4.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Auto;
//cht4.ChartAreas[0].AxisY.ToolTip = "数量(宗)";
//Y轴网格线条
cht4.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht4.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
cht4.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
cht4.ChartAreas[0].AxisX.IsMarginVisible = false;
cht4.ChartAreas[0].Area3DStyle.Enable3D = true;
cht4.ChartAreas[0].AxisX.IsInterlaced = false;
cht4.ChartAreas[0].AxisX.IsMarginVisible = false;
//刻度线
cht4.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
//cht4.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
//cht4.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
//cht4.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
cht4.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
//背景渐变
cht4.ChartAreas[0].BackGradientStyle = GradientStyle.None;
//cht4.ChartAreas[0].AxisX2.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].BorderWidth = 0;
//cht4.ChartAreas[0].BackSecondaryColor = Color.Red;
//cht4.ChartAreas[0].BackImageTransparentColor = Color.Red;
//cht4.ChartAreas[0].AxisX.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisX.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.MajorGrid.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.MajorTickMark.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.MinorTickMark.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisY.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.MajorGrid.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.MajorTickMark.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.MinorTickMark.LineColor = Color.Red;
//图例样式
Legend legend4 = new Legend();
legend4.Title = "图例";
legend4.TitleBackColor = Color.Transparent;
legend4.BackColor = Color.Transparent;
legend4.TitleForeColor = Color.White;
legend4.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
legend4.Font = new Font("微软雅黑", 8f, FontStyle.Regular);
legend4.ForeColor = Color.White;
cht4.Legends.Add(legend4);
cht4.Legends[0].Position.Auto = true;
//Series1
cht4.Series[0].XValueType = ChartValueType.String;
cht4.Series[0].Label = "#VAL";
cht4.Series[0].LabelForeColor = Color.White;
cht4.Series[0].ToolTip = "#LEGENDTEXT:#VAL(宗)";
cht4.Series[0].ChartType = SeriesChartType.Radar;
cht4.Series[0]["RadarDrawingStyle"] = "Line";
cht4.Series[0].LegendText = "2015年";
cht4.Series[0].IsValueShownAsLabel = true;
//Series2
cht4.Series.Add(new Series("Series2"));
cht4.Series[1].Label = "#VAL";
cht4.Series[1].LabelForeColor = Color.White;
cht4.Series[1].ToolTip = "#LEGENDTEXT:#VAL(宗)";
cht4.Series[1].ChartType = SeriesChartType.Radar;
cht4.Series[1]["RadarDrawingStyle"] = "Line";
cht4.Series[1].LegendText = "2016年";
cht4.Series[1].IsValueShownAsLabel = true;
//Series3
cht4.Series.Add(new Series("Series3"));
cht4.Series[2].Label = "#VAL";
cht4.Series[2].LabelForeColor = Color.White;
cht4.Series[2].ToolTip = "#LEGENDTEXT:#VAL(宗)";
cht4.Series[2].ChartType = SeriesChartType.Radar;
cht4.Series[2]["RadarDrawingStyle"] = "Line";
cht4.Series[2].LegendText = "2017年";
cht4.Series[2].IsValueShownAsLabel = true;
double[] yValues = { 65.62, 75.54, 60.45, 34.73, 85.42, 55.9, 63.6, 55.2, 77.1 };
string[] xValues = { "France", "Canada", "Germany", "USA", "Italy", "Spain", "Russia", "Sweden", "Japan" };
//Seris2
double[] y2 = { 45.62, 65.54, 70.45, 84.73, 35.42, 55.9, 63.6 };
double[] y3 = { 88.62, 35.54, 52.45, 45.73, 88.42, 14.9, 33.6 };
this.cht4.Series[0].Points.DataBindXY(xValues, yValues);
this.cht4.Series[1].Points.DataBindY(y2);
this.cht4.Series[2].Points.DataBindY(y3);
//设置X轴显示间隔为1,X轴数据比较多的时候比较有用
cht4.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
//设置XY轴标题的名称所在位置位远
cht4.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
for (int i = 0; i < cht4.Series[2].Points.Count; i++)
{
cht4.Series[2].Points[i].MarkerStyle = MarkerStyle.Circle;//设置折点的风格
cht4.Series[2].Points[i].MarkerColor = Color.Red;//设置seires中折点的颜色
// cht4.Series[1].Points[i].MarkerStyle = MarkerStyle.Square;//设置折点的风格
// cht4.Series[1].Points[i].MarkerColor = Color.Blue;//设置seires中折点的颜色
// cht4.Series[2].Points[i].MarkerStyle = MarkerStyle.Square;//设置折点的风格
// cht4.Series[2].Points[i].MarkerColor = Color.Green;//设置seires中折点的颜色
}
for (int i = 0; i < cht4.Series.Count; i++)
{
for (int j = 0; j < cht4.Series[i].Points.Count; j++)
{
cht4.Series[i].Points[j].Label = " ";
//cht4.Series[i].Points[j].LabelToolTip = "string.Empty";
}
}
//cht4.ImageType = ChartImageType.Jpeg;
//反锯齿
cht4.AntiAliasing = AntiAliasingStyles.All;
//调色板 磨沙:SemiTransparent
cht4.Palette = ChartColorPalette.BrightPastel;
cht4.Series[0].ChartType = SeriesChartType.Radar;
cht4.Series[1].ChartType = SeriesChartType.Radar;
cht4.Series[2].ChartType = SeriesChartType.Radar;
cht4.Width = 500;
cht4.Height = 350;
#endregion
另外,柱状图的Legend(图例)可通过设置Series的Name属性来显示指定的文本,如下面的示例
代码如下(窗体上放了工具箱数据栏下的Chart控件,name为chart1,也拉了一个工具栏toolStrip控件,设置了一个显示上面柱状图的按钮。下面代码是窗体的load事件和柱状图按钮事件处理函数)
private void Form1_Load(object sender, EventArgs e)
{
#region 构造数据表
dt = new DataTable("chart");
DataColumn col = new DataColumn();
col.ColumnName = "name";
col.DataType = typeof(System.String);
dt.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "quantity";
col.DataType = typeof(System.Int32);
dt.Columns.Add(col);
Random rand = new Random();
DataRow row = dt.NewRow();
row["name"] = "青岛";
row["quantity"] = rand.Next(1,100);
dt.Rows.Add(row);
row = dt.NewRow();
row["name"] = "武汉";
row["quantity"] = rand.Next(1,100);
dt.Rows.Add(row);
row = dt.NewRow();
row["name"] = "北京";
row["quantity"] = rand.Next(1,100);
dt.Rows.Add(row);
row = dt.NewRow();
row["name"] = "深圳";
row["quantity"] = rand.Next(1,100);
dt.Rows.Add(row);
row = dt.NewRow();
row["name"] = "上海";
row["quantity"] = rand.Next(1,100);
dt.Rows.Add(row);
dt2 = dt.Clone();
row = dt2.NewRow();
row["name"] = "青岛";
row["quantity"] = rand.Next(1,100);
dt2.Rows.Add(row);
row = dt2.NewRow();
row["name"] = "武汉";
row["quantity"] = rand.Next(1,100);
dt2.Rows.Add(row);
row = dt2.NewRow();
row["name"] = "北京";
row["quantity"] = rand.Next(1,100);
dt2.Rows.Add(row);
row = dt2.NewRow();
row["name"] = "深圳";
row["quantity"] = rand.Next(1,50);
dt2.Rows.Add(row);
row = dt2.NewRow();
row["name"] = "上海";
row["quantity"] = rand.Next(1,50);
dt2.Rows.Add(row);
#endregion
}
再加上下面的代码
/// <summary>
/// 柱状图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton2_Click(object sender, EventArgs e)
{
//清除全部Series系列
chart1.Series.Clear();
//创建series1系列对象
System.Windows.Forms.DataVisualization.Charting.Series series1 =
new System.Windows.Forms.DataVisualization.Charting.Series();
//设置系列图标类型,设置为柱形图
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
//将系列添加到图表
chart1.Series.Add(series1);
//设置图标系列1的图表类型,前面已经对Series[0]对应的series1设置过了
//chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
//设置Y轴坐标的间隔为1
chart1.ChartAreas[0].AxisY.Interval = 20;
//!!!!!!!!!下面五句设置能保证X轴文本显示完全。!!!!!!!!!
//设置X轴坐标的间隔为1
chart1.ChartAreas[0].AxisX.Interval = 1;
//设置X轴坐标偏移为1
chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
//设置是否交错显示
chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45; //标签显示角度反向45°
//====================================================================
chart1.Series[0]["ColumnLabelStyle"] = "Outside"; //将文字移到外侧
chart1.Series[0]["ColumnLineColor"] = "Black";//绘制黑色的连线
//指示是否在图例中显示指示项 //是否显示图列
chart1.Series[0].IsVisibleInLegend = true;
//清除系列上所有点信息
chart1.Series[0].Points.Clear();
int i = 0;
int quantity;
string name;
foreach (DataRow row in dt.Rows)
{
name = row["name"].ToString();
quantity = int.Parse(row["quantity"].ToString());
//对系列0添加一个坐标点。
chart1.Series[0].Points.AddXY(name,quantity);
chart1.Series[0].Points[i].LabelToolTip = quantity.ToString("###,###,###.##");
chart1.Series[0].Points[i].ToolTip = quantity.ToString("###,###,###.##");
chart1.Series[0].Points[i].Label = "#AXISLABEL(#PERCENT)";
//没作用,不知道干啥的
//chart1.Series[0].Points[i].AxisLabel = name + " " + quantity.ToString("###,###,###.##");
i++;
}
i = 0;
//创建series1系列对象
Series series2 = new Series();
//设置系列图标类型,设置为柱形图
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
//将系列添加到图表
chart1.Series.Add(series2);
//设置图标系列1的图表类型,前面已经对Series[1]对应的series2设置过了
//chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[1]["ColumnLabelStyle"] = "Outside"; //将文字移到外侧
chart1.Series[1]["ColumnLineColor"] = "Black";//绘制黑色的连线
//是否显示图列
chart1.Series[1].IsVisibleInLegend = true;
chart1.Series[0].Name = "上月";
chart1.Series[1].Name = "本月";
//清除系列上所有点信息
chart1.Series[1].Points.Clear();
foreach (DataRow row in dt2.Rows)
{
name = row["name"].ToString();
quantity = int.Parse(row["quantity"].ToString());
//对系列0添加一个坐标点。
chart1.Series[1].Points.AddXY(name, quantity);
chart1.Series[1].Points[i].LabelToolTip = quantity.ToString("###,###,###.##");
chart1.Series[1].Points[i].ToolTip = quantity.ToString("###,###,###.##");
chart1.Series[1].Points[i].Label = "#PERCENT";
i++;
}
}