c# – MS图表控制两个Y轴
发布时间:2020-12-15 06:32:43 所属栏目:百科 来源:网络整理
导读:我正在构建一个图表,按类别显示项目.到目前为止,我已经成功地显示项目,因为它是一个简单的x / y图表,但是我想显示y2,我知道MS Chart Controls有一个内置的AxisY2,但是当我尝试任何东西,Chart得到所有时髦 这是我正在寻找(在ascii艺术): item1 |[][][][][]..
我正在构建一个图表,按类别显示项目.到目前为止,我已经成功地显示项目,因为它是一个简单的x / y图表,但是我想显示y2,我知道MS Chart Controls有一个内置的AxisY2,但是当我尝试任何东西,Chart得到所有时髦
这是我正在寻找(在ascii艺术): item1 |[][][][][].............| cat1 item2 |[][]...................| cat2 item3 |[][....................| cat1 item4 |[][][][][][][][........| cat1 |_______________________| 0 1 2 3 4 5 像以前提到的,我可以得到项目和计数显示罚款,因为这是比较容易的,这是我似乎不能放置的类别. 谢谢 解决方法
简短回答首先:根据MS示例,没有直接的方法来做,但只是一个解决办法:将您的系列放在第二个图表上,与您现有的区域位置匹配(通过执行您的系列的副本)具有不可见的主要X / Y轴和可见辅助Y轴(AxisY2).并将chartArea和复制的系列的背景色设置为透明. (这可以应用于辅助X轴,如果是列图而不是条)
//Suppose you already have a ChartArea with the series plotted and the left Y Axis //Add a fake Area where the only appearent thing is your secondary Y Axis ChartArea area1 = chart.ChartAreas.Add("ChartAreaCopy_" + series.Name); area1.BackColor = Color.Transparent; area1.BorderColor = Color.Transparent; area1.Position.FromRectangleF(area.Position.ToRectangleF()); area1.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF()); area1.AxisX.MajorGrid.Enabled = false; area1.AxisX.MajorTickMark.Enabled = false; area1.AxisX.LabelStyle.Enabled = false; area1.AxisY.MajorGrid.Enabled = false; area1.AxisY.MajorTickMark.Enabled = false; area1.AxisY.LabelStyle.Enabled = false; area1.AxisY2.Enabled = AxisEnabled.True; area1.AxisY2.LabelStyle.Enabled = true; // Create a copy of specified series,and change Y Values to categories Series seriesCopy = chart.Series.Add(series.Name + "_Copy"); seriesCopy.ChartType = series.ChartType; foreach(DataPoint point in series.Points) { double category = getYourItemCategory(point.XValue); seriesCopy.Points.AddXY(point.XValue,category); } // Hide copied series seriesCopy.IsVisibleInLegend = false; seriesCopy.Color = Color.Transparent; seriesCopy.BorderColor = Color.Transparent; //Drop it in the chart to make the area show (only the AxisY2 should appear) seriesCopy.ChartArea = area1.Name; PS:我花了两个晚上清醒的MS图表控件,试图把两个不同的Y轴在图表区域.我想放两个不同比例的系列(相同的X尺度,不同的Y刻度:左边是A系列,另一个在B系列的右侧). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |