Bri*_*ian 12 c# charts controls
我正在构建一个图表,按类别按体积显示项目.到目前为止,我已经成功地按体积显示项目,因为它是一个简单的x/y图表,但是我想显示y2并且我知道MS Chart Controls有一个内置AxisY2然而当我尝试任何东西时,Chart得到了一切都很时髦.
这是我正在寻找的(在ascii艺术中):
item1 |[][][][][].............| cat1
item2 |[][]...................| cat2
item3 |[][....................| cat1
item4 |[][][][][][][][........| cat1
|_______________________|
0 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
就像之前提到的那样,我可以获得物品和计数,因为这是相对容易的,这是我似乎无法放置的类别.
谢谢
KTF*_*KTF 33
这是为我做的 - 在我创建图表后,我添加了以下几行:
chrtMain.Series[0].YAxisType = AxisType.Primary;
chrtMain.Series[1].YAxisType = AxisType.Secondary;
chrtMain.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
chrtMain.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
chrtMain.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chrtMain.ChartAreas[0].AxisY2.IsStartedFromZero = chrtMain.ChartAreas[0].AxisY.IsStartedFromZero;
Run Code Online (Sandbox Code Playgroud)
没有必要叠加两个图表或任何东西!
小智 9
它变得更好:
对于使用第二个Y轴,不需要第二个图表区域.您可以根据系列决定要使用哪个轴与Series.YAxisType属性.请查看http://msdn.microsoft.com/en-us/library/dd489216.aspx上的文档.
马亭
简短回答:根据MS示例,没有直接的方法可以做到这一点,但只是一个解决方法技巧:在第二个chartArea上绘制您的系列,完全匹配您现有的区域位置(通过执行您的系列的副本)具有隐形主要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;
Run Code Online (Sandbox Code Playgroud)
PS:我花了两个晚上清醒搞乱MS图表控件,试图在图表区域放置两个不同的Y轴.我想放两个不同比例的系列(相同的X刻度,不同的Y刻度:一个在左边是A系列,另一个在右边是B系列).事实上,这被证明是一个真正的噩梦,当人们可以期待这是非常简单的.事实上,MS Chart Controls绝对不适合这个特定用例恕我直言.在MSCC样本示例中建议的多个Y轴样本是一个非常丑陋且非常难看的解决方法,它需要在默认的两个图表区域之上,使用可见性和透明度,以实现期望的效果(这听起来像一个非常糟糕的幻觉魔法特技).
虽然希望在未来的版本中以适当的方式丰富和修复它,但如果你真的需要一种有效的方法来管理多个Y轴,请坐到ZedGraph