/// ---------------------------------------------------------------- /// 文 件 名:ChartControl.cs /// 功能描述:图表管理 /// /// 创建标识:Jdragon 2011-11-22 /// ---------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms.DataVisualization.Charting; using System.IO; using System.Drawing; using System.Drawing.Imaging; using SunVoteARSPPT; namespace GeneralLib { /// /// 枚举类型(自定义枚举类型) /// public enum ChartTypes { /// /// 柱形图 /// ctColumn, /// /// 条形图 /// ctBar, /// /// 垂直方块图 杨斌 2014-04-22 /// ctColumnBox, /// /// 水平方块图 杨斌 2014-04-22 /// ctBarBox, /// /// 饼图 /// ctPie, /// /// 文本 /// ctText, } /// /// 标签类型 /// public enum LabelTypes { // 无标签 ltNone, // 数值 ltNumberValue, // 百分比 ltPercent, // 数值加百分比 ltNumberValueAndPercent, } /// /// 图例样式 /// public enum LegendStyles { /// /// 列方式 /// lsColumn = 0, /// /// 行方式 /// lsRow = 1, /// /// 表格方式 /// 杨斌 2014-02-25 /// lstTable = 2 } /// /// 图例对齐方式 /// public enum LegentAlignments { /// /// 靠近布局对齐。在左到右布局中,近端位置是左。在右到左布局中,近端位置是右。 /// laNear = 0, /// /// 居中对齐 /// laCenter = 1, /// /// 指定文本远离布局矩形的原点位置对齐。在左到右布局中,远端位置是右。在右到左布局中,远端位置是左。 /// laFar = 2 } /// /// 图例位置 /// public enum LegentDockings { /// /// 上 /// ldTop = 0, /// /// 右 /// ldRight = 1, /// /// 下 /// ldBottom = 2, /// /// 左 /// ldLeft = 3 } public class ManageChart { /// /// 微软控件属性 /// public Chart MsChart { get; set; } /// /// 图表类型(默认值:柱形图) /// private ChartTypes chartType = ChartTypes.ctColumn; /// /// 图表类型 /// public ChartTypes ChartType { get { return chartType; } set { chartType = value; } } /// /// 文本模式图表数据按行排列 /// public bool TextChartTypeByLine = false; /// /// 只显示文本的图表形式,数字和选项之间的空格(水平)或空行(垂直)数。杨斌 2019-04-03 /// public int ChartTypeTextSpaceOrLine = 0; /// /// 图表宽度,默认为320 /// private int width = 320; public int Width { get { return width <= 0 ? 320 : width; } set { if (value <= 0) throw (new ApplicationException("Abnormal width of the chart, the width must be greater than 0")); width = (value <= 0) ? 320 : value; } } /// /// 图表高度,默认为240 /// private int height = 240; public int Height { get { return height <= 0 ? 240 : height; } set { if (value <= 0) throw (new ApplicationException("Degree of abnormal length of the chart, the width must be greater than 0")); height = (value <= 0) ? 240 : value; } } /// /// X轴值(选择项) /// public string[] XValue { get; set; } /// /// Y轴值集合 /// public List YValues { get; set; } /// /// Y轴值集合。Angage定制不按标准如权重,按投票人数,与百分比分离。杨斌 2019-11-26 /// public List YValuesNew { get; set; } /// /// 数据标签(默认值:数值) /// private LabelTypes labelType = LabelTypes.ltNumberValue; public LabelTypes LabelType { get { return labelType; } set { labelType = value; } } /// /// 2014-03-17 增加标签小数位数 /// public int NumberDec = 0; public int PercentDec = 1; private double labelDenominator = 0; /// /// 百分比标签的分母,为0时表示以总数量计算,默认为0 /// public double LabelDenominator { get { return labelDenominator; } set { if (value < 0) throw (new ApplicationException("Percentage of the label must be greater than or equal to 0 denominator")); labelDenominator = value; } } /// /// 分母计算选项限制,如三键表决按赞成反对。默认0为所有选项。 /// 杨斌 2015-06-18 /// public int LabelDenominatorOptionLimit = 0; /// /// 计算系列总数值 /// /// 系列 /// 总数值 private int SumPointsValue(Series series) { int value = 0; //杨斌 2015-06-18 int count = series.Points.Count; if ((this.LabelDenominatorOptionLimit > 0) && (this.LabelDenominatorOptionLimit < series.Points.Count)) { count = this.LabelDenominatorOptionLimit; } for (int i = 0; i < count; i++)//杨斌 2015-06-18 { value = value + (int)series.Points[i].GetValueByName("Y"); } return value; } /// /// 杨斌 2012-06-29 /// public Color[] SeriesColorsInit = { Color.FromArgb(65, 140, 240), Color.FromArgb(224, 64, 10), Color.FromArgb(252, 180, 65), Color.FromArgb(5, 100, 146), Color.FromArgb(191, 191, 191), Color.FromArgb(26, 59, 105), Color.FromArgb(255, 227, 130), Color.FromArgb(18, 156, 221), Color.FromArgb(202, 107, 75), Color.FromArgb(0, 92, 219) }; /// /// 系列(Series)颜色 /// 修改:杨斌 2012-06-28 /// public Color[] SeriesColors { get { //return seriesColors; Color[] aryColors = new Color[9999]; int count = SeriesColorsInit.Length; for (int i = 0; i < aryColors.Length; i++) { int n = i % count; aryColors[i] = SeriesColorsInit[n]; } return aryColors; } set { SeriesColorsInit = value; if (value == null || value.Length < 0) throw (new ApplicationException("Series (Series) colors set at least one color")); } } /// /// Point颜色 /// public Color[] PointColors { get; set; } //杨斌 2014-06-04。默认改为0 private double pointWidth = 0;//必须设置,因为默认为0。图表控件应默认设置为1,否则默认为0时图表柱状图画不出来 2012-02-02 /// /// 系列各项的宽度,必须0-1之间,0表示默认 /// public double PointWidth { get { return pointWidth; } set { if (value < 0 || value > 1) throw (new ApplicationException("The width of the series, must be between 0-1")); pointWidth = (value < 0 || value > 1) ? 1 : value; } } /// /// 是否显示图例 /// private bool legendEnable = true; public bool LegendEnable { get { return legendEnable; } set { legendEnable = value; } } private LegendStyles legendStyle = LegendStyles.lsColumn; /// /// 图例样式 /// private LegendStyles LegendStyle { get { return legendStyle; } set { legendStyle = value; } } private Font legendFont = new Font("Arial", 8); /// /// 图例文本格式,如字体、字号、字形属性 /// private Font LegendFont { get { return legendFont; } set { legendFont = value; } } private LegentAlignments legendAlignment = LegentAlignments.laCenter; /// /// 图例对齐方式,默认居中对齐 /// public LegentAlignments LegendAlignment { get { return legendAlignment; } set { legendAlignment = value; } } private LegentDockings legentDocking = LegentDockings.ldBottom; /// /// 图例位置 /// public LegentDockings LegentDocking { get { return legentDocking; } set { legentDocking = value; } } private Color legentForeColor = Color.Black; /// /// 图例文本颜色 /// public Color LegentForeColor { get { return legentForeColor; } set { legentForeColor = value; } } /// /// 图例文本 /// public string[] LegentText { get; set; } /// /// 是否3D图 /// private bool is3D = false; public bool Is3D { get { return is3D; } set { is3D = value; } } private bool axisYEnable = false; /// /// 是否显示Y轴,默认显示 /// private bool AxisYEnabled { get { return axisYEnable; } set { axisYEnable = value; } } private bool axisXEnable = true; private int iChangeColor = 0; //0:初始值,1:Bar图,2:Other图 20120204 private int iChiceCount = 0; //记录Bar图选项选项个数(Bar图时记录) 20120204 /// /// 是否显示X轴,默认显示 /// public bool AxisXEnabled { get { return axisXEnable; } set { axisXEnable = value; } } /// /// 柱状图的最大值 /// private int maxValue = 0; public int MaxValue { get { return maxValue; } set { maxValue = value; } } private Font dataLabelFont = new Font("Arial", 14); /// /// 数据标签字体 /// 创建:杨斌 2012-06-26 /// public Font DataLabelFont { get { return dataLabelFont; } set { dataLabelFont = value; } } /// /// 是否使用系统字体 /// 杨斌 2015-05-29 /// public bool UseSystemFont = true; private Color dataLabelColor = Color.Black; /// /// 数据标签颜色 /// 创建:杨斌 2012-06-26 /// public Color DataLabelColor { get { return dataLabelColor; } set { dataLabelColor = value; } } private Font itemLabelFont = new Font("Arial", 16); /// /// 选项标签字体 /// 创建:杨斌 2012-06-26 /// public Font ItemLabelFont { get { return itemLabelFont; } set { itemLabelFont = value; } } private Color itemLabelColor = Color.Black; /// /// 选项标签颜色 /// 创建:杨斌 2012-06-26 /// public Color ItemLabelColor { get { return itemLabelColor; } set { itemLabelColor = value; } } /// /// 图表最大数据值,用于柱状图或条形图最大长度设置 /// 若为0则长度自动适应 /// 创建:杨斌 2013-02-21 /// public double MaxDataValue = 0; /// /// 是否强制显示选项文本 /// 杨斌 2015-01-08 /// //public bool ShowItemText = false; /// /// 使用堆积图 /// 杨斌 2014-11-04 /// public bool UseStacked = false; /// /// 在使用UseStacked堆积图基础上,使用100堆积图 /// 杨斌 2017-06-13 /// public bool UseStacked100 = false; /// /// 图表标题。杨斌 2017-03-21 /// public string ChartTitle = ""; public bool UpdateChart2(Dictionary optionTextFull = null) { string titleSlide = ""; if (MsChart.Titles.Count > 0) { titleSlide = MsChart.Titles[0].Text; } //清除图表(区域) MsChart.ChartAreas.Clear(); MsChart.Series.Clear(); MsChart.Titles.Clear(); MsChart.Legends.Clear(); //图表、字体清晰度(锯齿) MsChart.AntiAliasing = AntiAliasingStyles.Graphics; //图表背景色 MsChart.BackColor = Color.Transparent; //图表位置 MsChart.Location = new System.Drawing.Point(0, 0); //图表大小 MsChart.Size = new System.Drawing.Size(this.width, this.height); if (titleSlide.Length < 1) titleSlide = "Group Comparison"; Title ti = new Title(titleSlide, Docking.Top); ti.Name = "Title"; ti.Alignment = ContentAlignment.MiddleCenter; ti.ForeColor = Color.Black; ti.Font = new Font(ItemLabelFont.Name, ItemLabelFont.Size, ItemLabelFont.Style); MsChart.Titles.Add(ti); string foot = ""; if ((optionTextFull != null) && (optionTextFull.Count > 0)) { int nItem = 0; foreach (var v in optionTextFull) { nItem++; foot += v.Key + "." + v.Value + "; "; } } ti = new Title(foot, Docking.Bottom); ti.Name = "Foot"; ti.Alignment = ContentAlignment.MiddleLeft; ti.ForeColor = Color.Black; ti.Font = new Font(ItemLabelFont.Name, ItemLabelFont.Size, ItemLabelFont.Style); MsChart.Titles.Add(ti); int groupCount = this.YValues.Count; int yStart = 10; int hStep = 80; int xStart = 5; int wStep = (100 - xStart) / groupCount; ChartArea area = null; Series ser = null; for (int i = 1; i <= groupCount; i++) { area = new ChartArea("Area" + i); area.Position.Auto = false; area.Position = new ElementPosition(xStart, yStart, wStep, hStep); area.BackColor = Color.Transparent; MsChart.ChartAreas.Add(area); xStart += wStep; ser = new Series("Series" + i); ser.ChartArea = area.Name; ser.IsValueShownAsLabel = true; ser.ChartType = SeriesChartType.Bar; MsChart.Series.Add(ser); string sGroup = LegentText[i - 1]; ti = new Title(sGroup, Docking.Top); ti.Name = "Title" + i; ti.Alignment = ContentAlignment.TopLeft; ti.DockedToChartArea = area.Name; //ti.DockingOffset = 7; //ti.IsDockedInsideChartArea = false; ti.ForeColor = Color.Black; ti.Font = new Font(LegendFont.Name, 12, LegendFont.Style); MsChart.Titles.Add(ti); } area = new ChartArea("AreaRank"); area.Position.Auto = false; area.Position = new ElementPosition(0, yStart, 50, hStep); area.BackColor = Color.Transparent; MsChart.ChartAreas.Add(area); ser = new Series("SeriesRank"); ser.ChartArea = "AreaRank"; ser.IsValueShownAsLabel = false; ser.ChartType = SeriesChartType.Bar; MsChart.Series.Add(ser); string rankTi = GlobalInfo.SysLanguage.LPT.ReadString("Other", "OrderGroupChartRank", "Avg.\r\nRanking"); ti = new Title(rankTi, Docking.Top); ti.Name = "Rank"; ti.Alignment = ContentAlignment.TopLeft; ti.DockedToChartArea = "AreaRank"; ti.DockingOffset = 4; ti.IsDockedInsideChartArea = false; ti.ForeColor = Color.Black; MsChart.Titles.Add(ti); //如果是条形图,反转数据,颜色翻转 杨斌 2014-03-21 if ((this.chartType == ChartTypes.ctBar) || (this.chartType == ChartTypes.ctBarBox) || (this.chartType == ChartTypes.ctText))//杨斌 2015-04-20 { //杨斌 2014-12-09 string tempXVal; double tempYVal; int count = XValue.Length; for (int j = 0; j < count / 2; j++) { tempXVal = XValue[j]; XValue[j] = XValue[count - 1 - j]; XValue[count - 1 - j] = tempXVal; } for (int i = 0; i < YValues.Count; i++) { count = YValues[i].Length; for (int j = 0; j < count / 2; j++) { //tempXVal = XValue[j]; //XValue[j] = XValue[count - 1 - j]; //XValue[count - 1 - j] = tempXVal; tempYVal = YValues[i][j]; YValues[i][j] = YValues[i][count - 1 - j]; YValues[i][count - 1 - j] = tempYVal; } } } groupCount++; for (int y = 0; y < groupCount; y++) { //创建图表区域 ChartArea chartArea = MsChart.ChartAreas[y]; chartArea.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;//是否显示x轴 chartArea.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;//是否显示y轴 //X/Y轴网格线 chartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; chartArea.AxisX.MajorGrid.Enabled = false; chartArea.AxisX.MajorTickMark.Enabled = false; chartArea.AxisX.LineColor = Color.Transparent; chartArea.AxisX.LabelStyle.Interval = 1;//解决图表显示13 5 7 9 11问题 //数据列是否聚合 chartArea.Area3DStyle.IsClustered = false;// this.ChartParam.IsClustered; //图表区域背景色 chartArea.BackColor = Color.Transparent; //X/Y轴标签文本颜色 chartArea.AxisY.LabelStyle.ForeColor = Color.Black; chartArea.AxisX.LabelStyle.ForeColor = ItemLabelColor;//Color.Black;//杨斌 2012-06-26 //系列各项深度,3D时有效,取值0-1000之间,默认60 chartArea.Area3DStyle.PointDepth = 60;// this.ChartParam.PointDepth; //系列各项前后空白的深度,3D时有效,取值0-1000之间,默认100 chartArea.Area3DStyle.PointGapDepth = 100;// this.ChartParam.PointGapDepth; //杨斌 2016-05-13 //chartArea.BorderDashStyle = ChartDashStyle.Solid; //chartArea.BorderColor = Color.Red; chartArea.AlignmentOrientation = AreaAlignmentOrientations.Vertical; if (chartArea.Name == "AreaRank") { chartArea.BorderColor = Color.Transparent; chartArea.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.NotSet; //cha.Series[i].Points.DataBindXY(aryItemR, aryValueR); } else { chartArea.BorderColor = Color.Black; chartArea.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid; } if (iChangeColor == 1 && iChiceCount > 1) // 颜色翻转-颜色还原 --解决由5个选项变成7个选项颜色会变的bug jdragon -2012-02-06 { Color[] tempColors = this.SeriesColors; Color tempColor; for (int j = 0; j < iChiceCount / 2; j++) { tempColor = tempColors[iChiceCount - 1 - j]; tempColors[iChiceCount - 1 - j] = tempColors[j]; tempColors[j] = tempColor; } this.SeriesColors = tempColors; iChangeColor = 2; } //组内排序。杨斌 2016-05-17 ////如果是条形图,反转数据,颜色翻转 杨斌 2014-03-21 //if ((this.chartType == ChartTypes.ctBar) || // (this.chartType == ChartTypes.ctBarBox) || // (this.chartType == ChartTypes.ctText))//杨斌 2015-04-20 //{ // //杨斌 2014-12-09 // string tempXVal; // double tempYVal; // int count = XValue.Length; // for (int j = 0; j < count / 2; j++) // { // tempXVal = XValue[j]; // XValue[j] = XValue[count - 1 - j]; // XValue[count - 1 - j] = tempXVal; // } // for (int i = 0; i < YValues.Count; i++) // { // count = YValues[i].Length; // for (int j = 0; j < count / 2; j++) // { // //tempXVal = XValue[j]; // //XValue[j] = XValue[count - 1 - j]; // //XValue[count - 1 - j] = tempXVal; // tempYVal = YValues[i][j]; // YValues[i][j] = YValues[i][count - 1 - j]; // YValues[i][count - 1 - j] = tempYVal; // } // } //} //找到最大数据值,方便设置柱状图最大高度。杨斌 2012-04-11 double yValueMax = maxValue; //for (int i = 0; i < this.YValues.Count; i++) //{ // double[] YVals = this.YValues[i]; // for (int j = 0; j < YVals.Length; j++) // { // if (YVals[j] > yValueMax) // yValueMax = YVals[j]; // } //} if (this.YValues.Count > 1)//杨斌 2014-11-04 { if (UseStacked) { for (int j = 0; j < this.YValues[0].Length; j++) { double count = 0; for (int i = 0; i < this.YValues.Count; i++) { count += this.YValues[i][j]; } if (count > yValueMax) yValueMax = count; } } else { for (int i = 0; i < this.YValues.Count; i++) { double[] YVals = this.YValues[i]; for (int j = 0; j < YVals.Length; j++) { if (YVals[j] > yValueMax) yValueMax = YVals[j]; } } } } else { for (int i = 0; i < this.YValues.Count; i++) { double[] YVals = this.YValues[i]; for (int j = 0; j < YVals.Length; j++) { if (YVals[j] > yValueMax) yValueMax = YVals[j]; } } } //创建数据系列 //for (int i = 0; i < this.YValues.Count; i++) //{ Series series = MsChart.Series[y]; //DrawingStyle取值范围:Cylinder 、Emboss、LightToDark、Wedge、Default。杨斌 2014-04-18 //series.ChartArea = this.MsChart.ChartAreas[y].Name; this.LegendStyle = LegendStyles.lsColumn; ////图表类型 //switch (this.chartType) //{ // case ChartTypes.ctColumn: // case ChartTypes.ctColumnBox: // //series.ChartType = SeriesChartType.Column; // if ((this.YValues.Count > 1) && UseStacked)//杨斌 2014-11-04 // { // series.ChartType = SeriesChartType.StackedColumn; // //this.LegendStyle = LegendStyles.lstTable; // this.LegendStyle = LegendStyles.lsColumn; // } // else // { // series.ChartType = SeriesChartType.Column; // this.LegendStyle = LegendStyles.lsColumn; // } // //系列效果 杨斌 2014-11-04 // if (this.chartType == ChartTypes.ctColumn) // series["DrawingStyle"] = "Cylinder"; // 圆柱 // else // series["DrawingStyle"] = "Default"; // //if (iChangeColor == 1 && iChiceCount > 1) // 颜色翻转(上次是Bar时颜色时翻转) -jdragon 20120204 // //{ // // Color[] tempColors = this.SeriesColors; // // Color tempColor; // // for (int j = 0; j < iChiceCount / 2; j++) // // { // // tempColor = tempColors[iChiceCount - 1 - j]; // // tempColors[iChiceCount - 1 - j] = tempColors[j]; // // tempColors[j] = tempColor; // // } // // this.SeriesColors = tempColors; // //} // //iChangeColor = 2; // break; // case ChartTypes.ctBar: // case ChartTypes.ctBarBox: // case ChartTypes.ctText://杨斌 2015-04-20 // //series.ChartType = SeriesChartType.Bar; // if ((this.YValues.Count > 1) && UseStacked)//杨斌 2014-11-04 // { // series.ChartType = SeriesChartType.StackedBar; // //this.LegendStyle = LegendStyles.lstTable; // this.LegendStyle = LegendStyles.lsColumn; // } // else // { // series.ChartType = SeriesChartType.Bar; // this.LegendStyle = LegendStyles.lsColumn; // } // //系列效果 杨斌 2014-11-04 // if (this.chartType == ChartTypes.ctBar) // series["DrawingStyle"] = "Cylinder"; // 圆柱 // else // series["DrawingStyle"] = "Default"; // ////颜色翻转--20120202 // //string[] XValsTemp = this.XValue; // //if (iChangeColor != 1) //颜色翻转 // //{ // // Color[] tempColors = this.SeriesColors; // // int count = XValsTemp.Length; // // iChiceCount = XValsTemp.Length; // // Color tempColor; // // for (int j = 0; j < count / 2; j++) // // { // // tempColor = tempColors[count - 1 - j]; // // tempColors[count - 1 - j] = tempColors[j]; // // tempColors[j] = tempColor; // // } // // this.SeriesColors = tempColors; // // iChangeColor = 1; // //} // break; // //case ChartTypes.ctColumnBox://杨斌 2014-04-22 // // series.ChartType = SeriesChartType.Column; // // //系列效果 // // series["DrawingStyle"] = "Default"; // // break; // //case ChartTypes.ctBarBox://杨斌 2014-04-22 // // series.ChartType = SeriesChartType.Bar; // // //系列效果 // // series["DrawingStyle"] = "Default"; // // break; // case ChartTypes.ctPie://PieDrawingStyle范围:Default、SoftEdge、Concave // series.ChartType = SeriesChartType.Pie; // //系列效果 // series["PieDrawingStyle"] = "Default";//默认 // //if (iChangeColor == 1 && iChiceCount > 1) // 颜色翻转(上次是Bar时颜色时翻转) -jdragon 20120204 // //{ // // Color[] tempColors = this.SeriesColors; // // Color tempColor; // // for (int j = 0; j < iChiceCount / 2; j++) // // { // // tempColor = tempColors[iChiceCount - 1 - j]; // // tempColors[iChiceCount - 1 - j] = tempColors[j]; // // tempColors[j] = tempColor; // // } // // this.SeriesColors = tempColors; // //} // //iChangeColor = 2; // break; //} //不显示柱状图。杨斌 2015-04-20 ////series.Points.DataBindXY(this.XValue, this.YValues[i]); double[] zeroVal = new double[this.YValues[0].Length];//杨斌 2016-05-17 if (chartArea.Name == "AreaRank")//杨斌 2016-05-17 { int itemCount = zeroVal.Length; string[] aryItemR = new string[itemCount]; double[] aryValueR = new double[itemCount]; for (int n = 0; n < itemCount; n++) { aryItemR[n] = (aryItemR.Length - n).ToString(); } series.Points.DataBindXY(aryItemR, aryValueR); } else { //排序处理。杨斌 2016-05-17 List lsOrder = new List(); for (int j = 0; j < this.YValues[y].Length; j++) { lsOrder.Add(new object[] { j, this.YValues[y][j] }); } lsOrder = lsOrder.OrderBy(o => o[1]).ToList(); List lsOn = new List(); string[] newXValue = null; double[] newYValue = null; if (lsOrder.Count > 0) { foreach (var v in lsOrder) { lsOn.Add((int)v[0]); } newXValue = new string[lsOrder.Count]; newYValue = new double[lsOrder.Count]; for (int j = 0; j < this.YValues[y].Length; j++) { newXValue[j] = this.XValue[lsOn[j]]; newYValue[j] = this.YValues[y][lsOn[j]]; } for (int j = 0; j < this.YValues[y].Length; j++) { this.YValues[y][j] = newYValue[j]; } } bool showBar = !(this.chartType == ChartTypes.ctText); if (showBar) { series.Points.DataBindXY(newXValue, newYValue); } else { chartArea.AxisX.LineColor = Color.Transparent; chartArea.AxisX.MajorTickMark.Enabled = false; chartArea.AxisX.MajorGrid.Enabled = false; series.Points.DataBindXY(this.XValue, zeroVal); } //设置标签 series.IsValueShownAsLabel = true; switch (this.labelType) { case LabelTypes.ltNone://不显示标签 series.IsValueShownAsLabel = false; break; case LabelTypes.ltNumberValue://显示数值 for (int j = 0; j < series.Points.Count; j++) { //if (NumberDec < 0)//杨斌 2014-08-07 // series.Points[j].Label = series.Points[j].GetValueByName("Y").ToString(); //else // series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")); if (NumberDec < 0)//杨斌 2014-08-07 series.Points[j].Label = this.YValues[y][j].ToString(); else series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[y][j]); } break; case LabelTypes.ltPercent://显示百分比 for (int j = 0; j < series.Points.Count; j++) { if (this.LabelDenominator > 0)//设置了分母 { //series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[y][j] / this.LabelDenominator * 100) + "%"; } else if (this.LabelDenominator == 0)//没有设置分母 { double count = this.SumPointsValue(series); if (this.UseStacked)//杨斌 2014-11-10 { count = 0; for (int n = 0; n < YValues.Count; n++) count += YValues[n][j]; } //杨斌 2015-06-18 if ((this.LabelDenominatorOptionLimit == 0) || (j < this.LabelDenominatorOptionLimit)) //series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", (count > 0 ? series.Points[j].GetValueByName("Y") / count : 0) * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", (count > 0 ? this.YValues[y][j] / count : 0) * 100) + "%"; else series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[y][j]); } } break; case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 for (int j = 0; j < series.Points.Count; j++) { if (this.LabelDenominator > 0)//设置了分母 { if (series.ChartType == SeriesChartType.Bar) { //series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")) + "; " + // String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[y][j]) + "; " + String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[y][j] / this.LabelDenominator * 100) + "%"; } else { //series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")) + "\n" + // String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[y][j]) + "\n" + String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[y][j] / this.LabelDenominator * 100) + "%"; } } else if (this.LabelDenominator == 0)//没有设置分母 { double count = this.SumPointsValue(series); if (this.UseStacked)//杨斌 2014-11-10 { count = 0; for (int n = 0; n < YValues.Count; n++) count += YValues[n][j]; } //杨斌 2015-06-18 string spl = "; "; if (series.ChartType == SeriesChartType.Bar) { spl = "; "; } else { spl = "\n"; } series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[y][j]); if ((this.LabelDenominatorOptionLimit == 0) || (j < this.LabelDenominatorOptionLimit)) series.Points[j].Label += spl + String.Format("{0:F" + PercentDec.ToString() + "}", (count > 0 ? this.YValues[y][j] / count : 0) * 100) + "%"; } } break; } } ////杨斌 2014-09-01 3D条形图数据标签右移 //if (this.labelType != LabelTypes.ltNone) //{ // if (chartArea.Area3DStyle.Enable3D) // { // switch (series.ChartType) // { // case SeriesChartType.Bar: // for (int j = 0; j < series.Points.Count; j++) // { // series.Points[j].Label = " " + series.Points[j].Label; // } // break; // } // } //} //if (this.labelType != LabelTypes.ltNone) //{ // //堆积图表数据为0不显示。杨斌 2014-02-20 // for (int j = 0; j < series.Points.Count; j++) // { // switch (series.ChartType) // { // case SeriesChartType.StackedColumn: // case SeriesChartType.StackedBar: // if (series.Points[j].GetValueByName("Y") == 0) // series.Points[j].Label = " "; // //series.Label = "AA"; // break; // } // } //} if ((this.YValues.Count > 1) && UseStacked)//杨斌 2014-01-27 { } else { //控制百分比显示在柱状图的外面,杨斌 2012-04-11 //chartArea.AxisY.Maximum = yValueMax * 1.16;// Double.NaN;//默认自动 //if (hchuw < 0.9) hchuw = 0.9; //switch (this.labelType) //{ // case LabelTypes.ltNone://不显示标签 // break; // case LabelTypes.ltNumberValue://显示数值 // break; // case LabelTypes.ltPercent://显示百分比 // if (this.ChartType == ChartTypes.ctBar) // chartArea.AxisY.Maximum = yValueMax * 1.4; // break; // case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 // if (this.ChartType == ChartTypes.ctBar) // chartArea.AxisY.Maximum = yValueMax * 1.7; // break; //} //杨斌 2012-06-06 chartArea.AxisY.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap; double wchuh = (double)MsChart.Width / MsChart.Height; if (wchuh > 2) chartArea.AxisY.Maximum = yValueMax + 25; else chartArea.AxisY.Maximum = yValueMax * 2 + 30; //杨斌 2013-02-22 if (MaxDataValue > yValueMax) yValueMax = MaxDataValue; double cC = 0;//字宽/高 double cL = 0;//柱宽/高 switch (this.chartType) { case ChartTypes.ctBar: case ChartTypes.ctBarBox://杨斌 2014-04-22 cC = 15 * dataLabelFont.Size / 14;//字宽。字号14,16=15,根据字体算 cL = (double)MsChart.Width * 0.9;//柱宽 switch (this.labelType) { case LabelTypes.ltNone://不显示标签 break; case LabelTypes.ltNumberValue://显示数值 cC *= (((int)yValueMax).ToString().Length + 1); break; case LabelTypes.ltPercent://显示百分比 cC *= ("100.0%".Length); //cC *= 0.95;//杨斌 2013-03-14 break; case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 cC *= (((int)yValueMax).ToString().Length + "; 100.0%".Length); //cC *= 0.95;//杨斌 2013-03-14 break; } if (cL > cC) { double cL2 = cL - cC;//去掉字宽的条形图柱宽 yValueMax = yValueMax * cL / cL2; } else { yValueMax = yValueMax * 100; } break; case ChartTypes.ctColumn: case ChartTypes.ctColumnBox://杨斌 2014-04-22 cC = 30 * dataLabelFont.Size / 14;//字高。字号14,16=30,根据字体算 cL = (double)MsChart.Height * 0.8;//柱高 //杨斌 2015-03-31 if ((chartArea.AxisX.Enabled == AxisEnabled.True) && (XValue.Length > 0)) { foreach (string s in XValue) { if (Encoding.Default.GetByteCount(s) > 10) { cL = cL * 0.5; break; } } } switch (this.labelType) { case LabelTypes.ltNone://不显示标签 break; case LabelTypes.ltNumberValue://显示数值 break; case LabelTypes.ltPercent://显示百分比 break; case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 cC *= 2;//两行显示 break; } if (cL > cC) { double cL2 = cL - cC;//去掉字宽的条形图柱宽 yValueMax = yValueMax * cL / cL2; } else { yValueMax = yValueMax * 100; } break; } } chartArea.AxisY.Maximum = yValueMax; //系列颜色 series.Color = this.SeriesColors[y % this.SeriesColors.Length]; MsChart.ChartAreas[series.ChartArea].BorderColor = series.Color; //杨斌 2012-06-26 ////系列标签文本格式 ////series.Font = new Font("Arial", 16); //if (series.Points.Count > 16) //{ // series.Font = new Font("Arial", 4); // chartArea.AxisX.LabelStyle.Font = new Font("Arial", 4); // chartArea.AxisY.LabelStyle.Font = new Font("Arial", 4); //} //else //{ // float size = 16;//原来是20 杨斌 2012-06-06 // series.Font = new Font("Arial", size - series.Points.Count); // //X/Y轴标签文本格式,如字体、字号、字形属性(必须放在Add之后,否则字号无效) // chartArea.AxisX.LabelStyle.Font = new Font("Arial", size - series.Points.Count); // chartArea.AxisY.LabelStyle.Font = new Font("Arial", size - series.Points.Count); //} //杨斌 2015-05-29 if (this.UseSystemFont) { chartArea.AxisX.LabelStyle.Font = new Font(GlobalInfo.SysFontName, ItemLabelFont.Size);// new Font("Arial", 16);//杨斌 2012-06-26 chartArea.AxisY.LabelStyle.Font = new Font(GlobalInfo.SysFontName, 16); series.Font = new Font(GlobalInfo.SysFontName, DataLabelFont.Size);// new Font("Arial", 16);//杨斌 2012-06-26 } else { chartArea.AxisX.LabelStyle.Font = ItemLabelFont;// new Font("Arial", 16);//杨斌 2012-06-26 chartArea.AxisY.LabelStyle.Font = new Font("Arial", 16); series.Font = DataLabelFont;// new Font("Arial", 16);//杨斌 2012-06-26 } //系列标签文本颜色 series.LabelForeColor = DataLabelColor;// Color.Black;//杨斌 2012-06-26 //添加数据系列 //this.MsChart.Series.Add(series); ////如果是饼图设置Point边框 //if (this.is3D == false && this.chartType == ChartTypes.ctPie) //{ // for (int j = 0; j < series.Points.Count; j++) // { // series.Points[j].BorderColor = Color.Silver; // series.Points[j].BorderDashStyle = ChartDashStyle.Solid; // series.Points[j].BorderWidth = 1; // } //} //} //设置图表区域大小,尽量大。杨斌 2012-04-11 //for (int i = 0; i < MsChart.ChartAreas.Count; i++) //{ // MsChart.ChartAreas[i].Position.Width = 100; // MsChart.ChartAreas[i].Position.Height = 100; //} //设置Point颜色,只支持一个系列时设置 if (MsChart.Series.Count == 2)//杨斌 2016-05-17 { if ((this.chartType == ChartTypes.ctBar) || (this.chartType == ChartTypes.ctBarBox))//颜色翻转 杨斌 2014-04-22 { if (this.PointColors != null && this.PointColors.Length > 0) { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) MsChart.Series[0].Points[j].Color = this.PointColors[(this.PointColors.Length - 1 - j) % this.PointColors.Length]; } else { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) MsChart.Series[0].Points[j].Color = this.SeriesColors[(MsChart.Series[0].Points.Count - 1 - j) % MsChart.Series[0].Points.Count]; } } else { if (this.PointColors != null && this.PointColors.Length > 0) { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) MsChart.Series[0].Points[j].Color = this.PointColors[j % this.PointColors.Length]; } else { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) { MsChart.Series[0].Points[j].Color = this.SeriesColors[j % MsChart.Series[0].Points.Count]; } } } //if (this.PointColors != null && this.PointColors.Length > 0) //{ // for (int j = 0; j < MsChart.Series[0].Points.Count; j++) // MsChart.Series[0].Points[j].Color = this.PointColors[j % this.PointColors.Length]; //} //else //{ // for (int j = 0; j < MsChart.Series[0].Points.Count; j++) // MsChart.Series[0].Points[j].Color = this.SeriesColors[j % MsChart.Series[0].Points.Count]; //} } //系列各项的宽度,必须0-1之间,0表示默认 for (int i = 0; i < MsChart.Series.Count; i++) { string pointWidth = ""; if (this.PointWidth == 0)//杨斌 2014-05-14 pointWidth = (Double.Parse(MsChart.Series.Count.ToString()) / (MsChart.Series.Count + 1)).ToString(); else pointWidth = this.PointWidth.ToString(); pointWidth = pointWidth.Replace(',', '.'); MsChart.Series[i]["PointWidth"] = pointWidth; //MsChart.Series[i]["PointWidth"] = "0.1"; MsChart.Series[i]["PieStartAngle"] = "270";//饼图起始角度 杨斌 2012-06-08 //饼图变形。杨斌 2015-07-07 //MsChart.Series[i].Points[0].BorderWidth = 3; //MsChart.Series[i].Points[1].BorderWidth = 100; } } return true; } /// /// 图表更新。杨斌 2016-06-16 /// /// /// public bool UpdateChart(bool multiChartAreas = false, string numUnit = "") { return UpdateChart(MsChart, multiChartAreas, numUnit); } /// /// 图表更新 /// /// 初始化成功:True,初始化失败:False public bool UpdateChart(Chart MsChart, bool multiChartAreas = false, string numUnit = "") { //图表、字体清晰度(锯齿) MsChart.AntiAliasing = AntiAliasingStyles.Graphics; //图表背景色 MsChart.BackColor = Color.Transparent; //图表位置 //MsChart.Location = new System.Drawing.Point(5, 10); MsChart.Location = new System.Drawing.Point(0, 0); //图表大小 MsChart.Size = new System.Drawing.Size(this.width, this.height); //清除图表(区域) MsChart.ChartAreas.Clear(); //创建图表区域 ChartArea chartArea = new ChartArea(); //ChartAreas:增加多个绘图区域,每个绘图区域包含独立的图表组、数据源,用于多个图表类型在一个绘图区不兼容时。 //添加图表区域到图表 MsChart.ChartAreas.Add(chartArea); MsChart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//解决图表显示13 5 7 9 11问题 //是否3D chartArea.Area3DStyle.Enable3D = this.is3D;//.ChartParam.Is3D; //设置图片旋转角度、倾斜角度 if (this.chartType == ChartTypes.ctPie) { //旋转角度 chartArea.Area3DStyle.Rotation = 0; //倾斜角度 chartArea.Area3DStyle.Inclination = 25; //20111226修改 } else { //旋转角度 chartArea.Area3DStyle.Rotation = 10; //倾斜角度 chartArea.Area3DStyle.Inclination = 20; } //数据列是否聚合 chartArea.Area3DStyle.IsClustered = false;// this.ChartParam.IsClustered; //图表区域背景色 chartArea.BackColor = Color.Transparent; //是否显示Y轴 chartArea.AxisY.Enabled = this.AxisYEnabled ? AxisEnabled.True : AxisEnabled.False; //是否显示x轴 chartArea.AxisX.Enabled = axisXEnable ? AxisEnabled.True : AxisEnabled.False; //X/Y轴网格线 chartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //X/Y轴标签文本格式,如字体、字号、字形属性(必须放在Add之后,否则字号无效) // chartArea.AxisX.LabelStyle.Font = new Font("Arial", 16); ; // chartArea.AxisY.LabelStyle.Font = new Font("Arial", 16); ; //X/Y轴标签文本颜色 chartArea.AxisY.LabelStyle.ForeColor = Color.Black; chartArea.AxisX.LabelStyle.ForeColor = ItemLabelColor;//Color.Black;//杨斌 2012-06-26 //系列各项深度,3D时有效,取值0-1000之间,默认60 chartArea.Area3DStyle.PointDepth = 60;// this.ChartParam.PointDepth; //系列各项前后空白的深度,3D时有效,取值0-1000之间,默认100 chartArea.Area3DStyle.PointGapDepth = 100;// this.ChartParam.PointGapDepth; //图表标题。杨斌 2017-03-21 MsChart.Titles.Clear(); if (!string.IsNullOrEmpty(ChartTitle)) { Title ti = new Title(ChartTitle, Docking.Top); ti.Name = "Title"; ti.Alignment = ContentAlignment.MiddleCenter; ti.ForeColor = Color.Black; ti.Font = new Font(ItemLabelFont.Name, ItemLabelFont.Size, ItemLabelFont.Style); MsChart.Titles.Add(ti); } MsChart.Series.Clear(); if (iChangeColor == 1 && iChiceCount > 1) // 颜色翻转-颜色还原 --解决由5个选项变成7个选项颜色会变的bug jdragon -2012-02-06 { Color[] tempColors = this.SeriesColors; Color tempColor; for (int j = 0; j < iChiceCount / 2; j++) { tempColor = tempColors[iChiceCount - 1 - j]; tempColors[iChiceCount - 1 - j] = tempColors[j]; tempColors[j] = tempColor; } this.SeriesColors = tempColors; iChangeColor = 2; } //如果是条形图,反转数据,颜色翻转 杨斌 2014-03-21 if ((this.chartType == ChartTypes.ctBar) || (this.chartType == ChartTypes.ctBarBox) || ((this.chartType == ChartTypes.ctText) && (!TextChartTypeByLine)))//杨斌 2015-04-20 { //杨斌 2014-12-09 string tempXVal; double tempYVal; int count = XValue.Length; for (int j = 0; j < count / 2; j++) { tempXVal = XValue[j]; XValue[j] = XValue[count - 1 - j]; XValue[count - 1 - j] = tempXVal; } for (int i = 0; i < YValues.Count; i++) { count = YValues[i].Length; for (int j = 0; j < count / 2; j++) { //tempXVal = XValue[j]; //XValue[j] = XValue[count - 1 - j]; //XValue[count - 1 - j] = tempXVal; tempYVal = YValues[i][j]; YValues[i][j] = YValues[i][count - 1 - j]; YValues[i][count - 1 - j] = tempYVal; } } } //找到最大数据值,方便设置柱状图最大高度。杨斌 2012-04-11 double yValueMax = maxValue; //for (int i = 0; i < this.YValues.Count; i++) //{ // double[] YVals = this.YValues[i]; // for (int j = 0; j < YVals.Length; j++) // { // if (YVals[j] > yValueMax) // yValueMax = YVals[j]; // } //} if (this.YValues.Count > 1)//杨斌 2014-11-04 { if (UseStacked) { for (int j = 0; j < this.YValues[0].Length; j++) { double count = 0; for (int i = 0; i < this.YValues.Count; i++) { count += this.YValues[i][j]; } if (count > yValueMax) yValueMax = count; } } else { for (int i = 0; i < this.YValues.Count; i++) { double[] YVals = this.YValues[i]; for (int j = 0; j < YVals.Length; j++) { if (YVals[j] > yValueMax) yValueMax = YVals[j]; } } } } else { for (int i = 0; i < this.YValues.Count; i++) { double[] YVals = this.YValues[i]; for (int j = 0; j < YVals.Length; j++) { if (YVals[j] > yValueMax) yValueMax = YVals[j]; } } } //创建数据系列 for (int i = 0; i < this.YValues.Count; i++) { Series series = new Series(); //DrawingStyle取值范围:Cylinder 、Emboss、LightToDark、Wedge、Default。杨斌 2014-04-18 //图表类型 switch (this.chartType) { case ChartTypes.ctColumn: case ChartTypes.ctColumnBox: //series.ChartType = SeriesChartType.Column; if ((this.YValues.Count > 1) && UseStacked)//杨斌 2014-11-04 { series.ChartType = UseStacked100 ? SeriesChartType.StackedColumn100 : SeriesChartType.StackedColumn;//杨斌 2017-06-13 //this.LegendStyle = LegendStyles.lstTable; this.LegendStyle = LegendStyles.lsColumn; } else { series.ChartType = SeriesChartType.Column; this.LegendStyle = LegendStyles.lsColumn; } //系列效果 杨斌 2014-11-04 if (this.chartType == ChartTypes.ctColumn) series["DrawingStyle"] = "Cylinder"; // 圆柱 else series["DrawingStyle"] = "Default"; //if (iChangeColor == 1 && iChiceCount > 1) // 颜色翻转(上次是Bar时颜色时翻转) -jdragon 20120204 //{ // Color[] tempColors = this.SeriesColors; // Color tempColor; // for (int j = 0; j < iChiceCount / 2; j++) // { // tempColor = tempColors[iChiceCount - 1 - j]; // tempColors[iChiceCount - 1 - j] = tempColors[j]; // tempColors[j] = tempColor; // } // this.SeriesColors = tempColors; //} //iChangeColor = 2; break; case ChartTypes.ctBar: case ChartTypes.ctBarBox: case ChartTypes.ctText://杨斌 2015-04-20 //series.ChartType = SeriesChartType.Bar; if ((this.YValues.Count > 1) && UseStacked)//杨斌 2014-11-04 { series.ChartType = UseStacked100 ? SeriesChartType.StackedBar100 : SeriesChartType.StackedBar;//杨斌 2017-06-13 //this.LegendStyle = LegendStyles.lstTable; this.LegendStyle = LegendStyles.lsColumn; } else { //杨斌 2019-04-03 if (TextChartTypeByLine) series.ChartType = SeriesChartType.Column; else series.ChartType = SeriesChartType.Bar; this.LegendStyle = LegendStyles.lsColumn; } //系列效果 杨斌 2014-11-04 if (this.chartType == ChartTypes.ctBar) series["DrawingStyle"] = "Cylinder"; // 圆柱 else series["DrawingStyle"] = "Default"; ////颜色翻转--20120202 //string[] XValsTemp = this.XValue; //if (iChangeColor != 1) //颜色翻转 //{ // Color[] tempColors = this.SeriesColors; // int count = XValsTemp.Length; // iChiceCount = XValsTemp.Length; // Color tempColor; // for (int j = 0; j < count / 2; j++) // { // tempColor = tempColors[count - 1 - j]; // tempColors[count - 1 - j] = tempColors[j]; // tempColors[j] = tempColor; // } // this.SeriesColors = tempColors; // iChangeColor = 1; //} break; //case ChartTypes.ctColumnBox://杨斌 2014-04-22 // series.ChartType = SeriesChartType.Column; // //系列效果 // series["DrawingStyle"] = "Default"; // break; //case ChartTypes.ctBarBox://杨斌 2014-04-22 // series.ChartType = SeriesChartType.Bar; // //系列效果 // series["DrawingStyle"] = "Default"; // break; case ChartTypes.ctPie://PieDrawingStyle范围:Default、SoftEdge、Concave series.ChartType = SeriesChartType.Pie; //系列效果 series["PieDrawingStyle"] = "Default";//默认 //if (iChangeColor == 1 && iChiceCount > 1) // 颜色翻转(上次是Bar时颜色时翻转) -jdragon 20120204 //{ // Color[] tempColors = this.SeriesColors; // Color tempColor; // for (int j = 0; j < iChiceCount / 2; j++) // { // tempColor = tempColors[iChiceCount - 1 - j]; // tempColors[iChiceCount - 1 - j] = tempColors[j]; // tempColors[j] = tempColor; // } // this.SeriesColors = tempColors; //} //iChangeColor = 2; break; } //屏蔽下面代码,反转数据放到前面。杨斌 2014-11-10 ////绑定数据 //string[] XVals = this.XValue; //double[] YVals = this.YValues[i]; //if ((series.ChartType == SeriesChartType.Bar) || //(series.ChartType == SeriesChartType.StackedBar) || //(series.ChartType == SeriesChartType.StackedBar100))//如果是条形图,反转数据,颜色翻转 杨斌 2014-03-21 //{ // string tempXVal; // double tempYVal; // int count = XVals.Length; // for (int j = 0; j < count / 2; j++) // { // tempXVal = XVals[count - 1 - j]; // tempYVal = YVals[count - 1 - j]; // XVals[count - 1 - j] = XVals[j]; // YVals[count - 1 - j] = YVals[j]; // XVals[j] = tempXVal; // YVals[j] = tempYVal; // } // //绑定数据 // series.Points.DataBindXY(XVals, YVals); //} //else //绑定数据 //不显示柱状图。杨斌 2015-04-20 ////series.Points.DataBindXY(this.XValue, this.YValues[i]); double[] zeroVal = new double[this.YValues[i].Length]; //杨斌 2019-04-03 bool showBar = (this.chartType != ChartTypes.ctText); if (showBar) { series.Points.DataBindXY(this.XValue, this.YValues[i]); } else { chartArea.AxisX.LineColor = Color.Transparent; chartArea.AxisX.MajorTickMark.Enabled = false; chartArea.AxisX.MajorGrid.Enabled = false; //杨斌 2019-04-03 chartArea.AxisY.LineColor = Color.Transparent; chartArea.AxisY.MajorTickMark.Enabled = false; chartArea.AxisY.MajorGrid.Enabled = false; series.Points.DataBindXY(this.XValue, zeroVal); } //设置标签 series.IsValueShownAsLabel = true; switch (this.labelType) { case LabelTypes.ltNone://不显示标签 series.IsValueShownAsLabel = false; break; case LabelTypes.ltNumberValue://显示数值 for (int j = 0; j < series.Points.Count; j++) { //if (NumberDec < 0)//杨斌 2014-08-07 // series.Points[j].Label = series.Points[j].GetValueByName("Y").ToString(); //else // series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")); if ((YValuesNew != null) && (YValuesNew.Count > 0))//杨斌 2019-11-26 { if (NumberDec < 0)//杨斌 2014-08-07 series.Points[j].Label = this.YValuesNew[i][j].ToString(); else series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValuesNew[i][j]); } else { if (NumberDec < 0)//杨斌 2014-08-07 series.Points[j].Label = this.YValues[i][j].ToString(); else series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[i][j]); } series.Points[j].Label += numUnit;//杨斌 2018-05-09 } break; case LabelTypes.ltPercent://显示百分比 for (int j = 0; j < series.Points.Count; j++) { if (this.LabelDenominator > 0)//设置了分母 { //series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[i][j] / this.LabelDenominator * 100) + "%"; } else if (this.LabelDenominator == 0)//没有设置分母 { double count = this.SumPointsValue(series); if (this.UseStacked)//杨斌 2014-11-10 { count = 0; for (int n = 0; n < YValues.Count; n++) count += YValues[n][j]; } //杨斌 2015-06-18 if ((this.LabelDenominatorOptionLimit == 0) || (j < this.LabelDenominatorOptionLimit)) //series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", (count > 0 ? series.Points[j].GetValueByName("Y") / count : 0) * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + PercentDec.ToString() + "}", (count > 0 ? this.YValues[i][j] / count : 0) * 100) + "%"; else series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[i][j]); } } break; case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 for (int j = 0; j < series.Points.Count; j++) { if (this.LabelDenominator > 0)//设置了分母 { if (series.ChartType == SeriesChartType.Bar) { if ((YValuesNew != null) && (YValuesNew.Count > 0))//杨斌 2019-11-26 { //series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")) + "; " + // String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValuesNew[i][j]) + numUnit/*杨斌 2018-05-09*/ + "; " + String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[i][j] / this.LabelDenominator * 100) + "%"; } else { //series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")) + "; " + // String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[i][j]) + numUnit/*杨斌 2018-05-09*/ + "; " + String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[i][j] / this.LabelDenominator * 100) + "%"; } } else { if ((YValuesNew != null) && (YValuesNew.Count > 0))//杨斌 2019-11-26 { //series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")) + "\n" + // String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValuesNew[i][j]) + numUnit/*杨斌 2018-05-09*/ + "\n" + String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[i][j] / this.LabelDenominator * 100) + "%"; } else { //series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", series.Points[j].GetValueByName("Y")) + "\n" + // String.Format("{0:F" + PercentDec.ToString() + "}", series.Points[j].GetValueByName("Y") / this.LabelDenominator * 100) + "%"; series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[i][j]) + numUnit/*杨斌 2018-05-09*/ + "\n" + String.Format("{0:F" + PercentDec.ToString() + "}", this.YValues[i][j] / this.LabelDenominator * 100) + "%"; } } } else if (this.LabelDenominator == 0)//没有设置分母 { double count = this.SumPointsValue(series); if (this.UseStacked)//杨斌 2014-11-10 { count = 0; for (int n = 0; n < YValues.Count; n++) count += YValues[n][j]; } //杨斌 2015-06-18 string spl = "; "; if (series.ChartType == SeriesChartType.Bar) { spl = "; "; } else { spl = "\n"; } if ((YValuesNew != null) && (YValuesNew.Count > 0))//杨斌 2019-11-26 series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValuesNew[i][j]); else series.Points[j].Label = String.Format("{0:F" + NumberDec.ToString() + "}", this.YValues[i][j]); if ((this.LabelDenominatorOptionLimit == 0) || (j < this.LabelDenominatorOptionLimit)) series.Points[j].Label += spl + String.Format("{0:F" + PercentDec.ToString() + "}", (count > 0 ? this.YValues[i][j] / count : 0) * 100) + "%"; } } break; } if (this.labelType != LabelTypes.ltNone) { switch (series.ChartType)//杨斌 2019-04-03 { case SeriesChartType.Bar: string addSpace = ""; addSpace = new string(' ', ChartTypeTextSpaceOrLine); for (int j = 0; j < series.Points.Count; j++) { series.Points[j].Label = addSpace + series.Points[j].Label; } break; case SeriesChartType.Column: string addLine = ""; for (int n = 1; n <= ChartTypeTextSpaceOrLine; n++) { addLine += "\r\n"; } for (int j = 0; j < series.Points.Count; j++) { series.Points[j].Label = series.Points[j].Label + addLine; } break; } if (chartArea.Area3DStyle.Enable3D)//杨斌 2014-09-01 3D条形图数据标签右移 { switch (series.ChartType) { case SeriesChartType.Bar: for (int j = 0; j < series.Points.Count; j++) { series.Points[j].Label = " " + series.Points[j].Label; } break; } } } if (this.labelType != LabelTypes.ltNone) { //堆积图表数据为0不显示。杨斌 2014-02-20 for (int j = 0; j < series.Points.Count; j++) { switch (series.ChartType) { case SeriesChartType.StackedColumn: case SeriesChartType.StackedBar: //case SeriesChartType.StackedColumn100: //case SeriesChartType.StackedBar100: if (series.Points[j].GetValueByName("Y") == 0) series.Points[j].Label = " "; //series.Label = "AA"; break; } } } if ((this.YValues.Count > 1) && UseStacked)//杨斌 2014-01-27 { } else { //控制百分比显示在柱状图的外面,杨斌 2012-04-11 //chartArea.AxisY.Maximum = yValueMax * 1.16;// Double.NaN;//默认自动 //if (hchuw < 0.9) hchuw = 0.9; //switch (this.labelType) //{ // case LabelTypes.ltNone://不显示标签 // break; // case LabelTypes.ltNumberValue://显示数值 // break; // case LabelTypes.ltPercent://显示百分比 // if (this.ChartType == ChartTypes.ctBar) // chartArea.AxisY.Maximum = yValueMax * 1.4; // break; // case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 // if (this.ChartType == ChartTypes.ctBar) // chartArea.AxisY.Maximum = yValueMax * 1.7; // break; //} //杨斌 2012-06-06 chartArea.AxisY.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap; double wchuh = (double)MsChart.Width / MsChart.Height; if (wchuh > 2) chartArea.AxisY.Maximum = yValueMax + 25; else chartArea.AxisY.Maximum = yValueMax * 2 + 30; //杨斌 2013-02-22 if (MaxDataValue > yValueMax) yValueMax = MaxDataValue; double cC = 0;//字宽/高 double cL = 0;//柱宽/高 switch (this.chartType) { case ChartTypes.ctBar: case ChartTypes.ctBarBox://杨斌 2014-04-22 cC = 15 * dataLabelFont.Size / 14;//字宽。字号14,16=15,根据字体算 cL = (double)MsChart.Width * 0.9;//柱宽 switch (this.labelType) { case LabelTypes.ltNone://不显示标签 break; case LabelTypes.ltNumberValue://显示数值 cC *= (((int)yValueMax).ToString().Length + 1 + numUnit.Length);//杨斌 2018-05-09 break; case LabelTypes.ltPercent://显示百分比 cC *= (" 100.0%".Length); //cC *= 0.95;//杨斌 2013-03-14 break; case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 cC *= (((int)yValueMax).ToString().Length + " ; 100.0%".Length + numUnit.Length);//杨斌 2018-05-09 //cC *= 0.95;//杨斌 2013-03-14 break; } if (cL > cC) { double cL2 = cL - cC;//去掉字宽的条形图柱宽 yValueMax = yValueMax * cL / cL2; } else { yValueMax = yValueMax * 100; } break; case ChartTypes.ctColumn: case ChartTypes.ctColumnBox://杨斌 2014-04-22 cC = 30 * dataLabelFont.Size / 14;//字高。字号14,16=30,根据字体算 cL = (double)MsChart.Height * 0.8;//柱高 //杨斌 2015-03-31 if ((chartArea.AxisX.Enabled == AxisEnabled.True) && (XValue.Length > 0)) { foreach (string s in XValue) { if (Encoding.Default.GetByteCount(s) > 10) { cL = cL * 0.5; break; } } } switch (this.labelType) { case LabelTypes.ltNone://不显示标签 break; case LabelTypes.ltNumberValue://显示数值 break; case LabelTypes.ltPercent://显示百分比 break; case LabelTypes.ltNumberValueAndPercent://显示数值加百分比 cC *= 2;//两行显示 break; } if (cL > cC) { double cL2 = cL - cC;//去掉字宽的条形图柱宽 yValueMax = yValueMax * cL / cL2; } else { yValueMax = yValueMax * 100; } break; } } //杨斌 2017-06-13 switch (series.ChartType) { //case SeriesChartType.StackedColumn: case SeriesChartType.StackedColumn100: //case SeriesChartType.StackedBar: case SeriesChartType.StackedBar100: break; default: chartArea.AxisY.Maximum = yValueMax; break; } //系列颜色 series.Color = this.SeriesColors[i % this.SeriesColors.Length]; //杨斌 2012-06-26 ////系列标签文本格式 ////series.Font = new Font("Arial", 16); //if (series.Points.Count > 16) //{ // series.Font = new Font("Arial", 4); // chartArea.AxisX.LabelStyle.Font = new Font("Arial", 4); // chartArea.AxisY.LabelStyle.Font = new Font("Arial", 4); //} //else //{ // float size = 16;//原来是20 杨斌 2012-06-06 // series.Font = new Font("Arial", size - series.Points.Count); // //X/Y轴标签文本格式,如字体、字号、字形属性(必须放在Add之后,否则字号无效) // chartArea.AxisX.LabelStyle.Font = new Font("Arial", size - series.Points.Count); // chartArea.AxisY.LabelStyle.Font = new Font("Arial", size - series.Points.Count); //} //杨斌 2015-05-29 if (this.UseSystemFont) { chartArea.AxisX.LabelStyle.Font = new Font(GlobalInfo.SysFontName, ItemLabelFont.Size);// new Font("Arial", 16);//杨斌 2012-06-26 chartArea.AxisY.LabelStyle.Font = new Font(GlobalInfo.SysFontName, 16); series.Font = new Font(GlobalInfo.SysFontName, DataLabelFont.Size);// new Font("Arial", 16);//杨斌 2012-06-26 } else { chartArea.AxisX.LabelStyle.Font = ItemLabelFont;// new Font("Arial", 16);//杨斌 2012-06-26 chartArea.AxisY.LabelStyle.Font = new Font("Arial", 16); series.Font = DataLabelFont;// new Font("Arial", 16);//杨斌 2012-06-26 } //系列标签文本颜色 series.LabelForeColor = DataLabelColor;// Color.Black;//杨斌 2012-06-26 //添加数据系列 MsChart.Series.Add(series); //如果是饼图设置Point边框 if (this.is3D == false && this.chartType == ChartTypes.ctPie) { for (int j = 0; j < series.Points.Count; j++) { series.Points[j].BorderColor = Color.Silver; series.Points[j].BorderDashStyle = ChartDashStyle.Solid; series.Points[j].BorderWidth = 1; } } } //设置图表区域大小,尽量大。杨斌 2012-04-11 //for (int i = 0; i < MsChart.ChartAreas.Count; i++) //{ // MsChart.ChartAreas[i].Position.Width = 100; // MsChart.ChartAreas[i].Position.Height = 100; //} //设置Point颜色,只支持一个系列时设置 if (MsChart.Series.Count == 1) { if ((this.chartType == ChartTypes.ctBar) || (this.chartType == ChartTypes.ctBarBox))//颜色翻转 杨斌 2014-04-22 { if (this.PointColors != null && this.PointColors.Length > 0) { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) MsChart.Series[0].Points[j].Color = this.PointColors[(this.PointColors.Length - 1 - j) % this.PointColors.Length]; } else { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) MsChart.Series[0].Points[j].Color = this.SeriesColors[(MsChart.Series[0].Points.Count - 1 - j) % MsChart.Series[0].Points.Count]; } } else { if (this.PointColors != null && this.PointColors.Length > 0) { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) MsChart.Series[0].Points[j].Color = this.PointColors[j % this.PointColors.Length]; } else { for (int j = 0; j < MsChart.Series[0].Points.Count; j++) { MsChart.Series[0].Points[j].Color = this.SeriesColors[j % MsChart.Series[0].Points.Count]; } } } //if (this.PointColors != null && this.PointColors.Length > 0) //{ // for (int j = 0; j < MsChart.Series[0].Points.Count; j++) // MsChart.Series[0].Points[j].Color = this.PointColors[j % this.PointColors.Length]; //} //else //{ // for (int j = 0; j < MsChart.Series[0].Points.Count; j++) // MsChart.Series[0].Points[j].Color = this.SeriesColors[j % MsChart.Series[0].Points.Count]; //} } //系列各项的宽度,必须0-1之间,0表示默认 for (int i = 0; i < MsChart.Series.Count; i++) { string pointWidth = ""; if (this.PointWidth == 0)//杨斌 2014-05-14 pointWidth = (Double.Parse(MsChart.Series.Count.ToString()) / (MsChart.Series.Count + 1)).ToString(); else pointWidth = this.PointWidth.ToString(); pointWidth = pointWidth.Replace(',', '.'); MsChart.Series[i]["PointWidth"] = pointWidth; //MsChart.Series[i]["PointWidth"] = "0.1"; MsChart.Series[i]["PieStartAngle"] = "270";//饼图起始角度 杨斌 2012-06-08 //饼图变形。杨斌 2015-07-07 //MsChart.Series[i].Points[0].BorderWidth = 3; //MsChart.Series[i].Points[1].BorderWidth = 100; } //杨斌 2015-02-10 chartArea.AxisX.IsLabelAutoFit = true; chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30; //chartArea.AxisX.LabelStyle.Angle = 45;//-90到90之间 MsChart.Legends.Clear(); //图例设置 && (this.chartType ==ChartTypes.ctPie) if (this.legendEnable) { MsChart.Legends.Add(""); //背景透明 MsChart.Legends[0].BackColor = Color.Transparent; //图例位置 MsChart.Legends[0].Docking = (Docking)((int)this.legentDocking); //图例样式 MsChart.Legends[0].LegendStyle = (LegendStyle)((int)this.LegendStyle); //图例对齐方式 MsChart.Legends[0].Alignment = (StringAlignment)((int)this.LegendAlignment); //图例标签文本格式。系统字体。杨斌 2015-05-29 if (this.UseSystemFont) MsChart.Legends[0].Font = new Font(GlobalInfo.SysFontName, chartArea.AxisX.LabelStyle.Font.Size);// this.legendFont;//杨斌 2012-06-26 else MsChart.Legends[0].Font = chartArea.AxisX.LabelStyle.Font;// this.legendFont;//杨斌 2012-06-26 //图例文本颜色 MsChart.Legends[0].ForeColor = chartArea.AxisX.LabelStyle.ForeColor;// this.LegentForeColor;//杨斌 2012-06-26 //图例文本 if (this.LegentText != null) { if (this.ChartType == ChartTypes.ctPie)//如果是饼图 { for (int i = 0; i < MsChart.Series.Count; i++) { if (this.LegentText.Length >= MsChart.Series[i].Points.Count) { for (int j = 0; j < MsChart.Series[i].Points.Count; j++) { MsChart.Series[i].Points[j].LegendText = this.LegentText[j]; } } else { throw (new ApplicationException("Legend text's number is not less than the number")); } } } else//如果是条形图或柱状图 { if (this.LegentText.Length >= MsChart.Series.Count) { for (int i = 0; i < MsChart.Series.Count; i++) MsChart.Series[i].Name = this.LegentText[i]; } else { throw (new ApplicationException("Legend text's number is not less than the number")); } } } } ////杨斌 2016-05-10 //MsChart.Titles.Add(new Title("1 2 3", Docking.Left)); //MsChart.Titles[MsChart.Titles.Count -1].TextOrientation = TextOrientation.Stacked; //MsChart.Titles[MsChart.Titles.Count - 1].Font = DataLabelFont; return true; } /// /// 保存图片 /// /// 保存图片路径 /// public bool SaveAsPicture(string strPath, string strPicName) { if (!System.IO.Directory.Exists(strPath)) { // 目录不存在,建立目录 System.IO.Directory.CreateDirectory(strPath); } try { this.MsChart.SaveImage(strPath + strPicName, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png); return true; } catch (Exception ex) { string sErr = ex + ""; return false; } } /// /// 删除文件 /// /// /// public bool DeletePicture(string strPath) { File.Delete(strPath); return true; } /// /// 获取图表的图像 /// 创建:杨斌 2012-02-07 /// /// public Bitmap GetBitmap() { int w = MsChart.Size.Width; int h = MsChart.Size.Height; Bitmap bm = new Bitmap(w, h); MsChart.DrawToBitmap(bm, new Rectangle(0, 0, w, h)); return bm; } } }