MS图表控件两个Y轴 [英] MS Chart Control Two Y Axis

查看:119
本文介绍了MS图表控件两个Y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个图表,按类别显示体积的物品。到目前为止,我已经succcessful在展示项目的体积,因为它是一个简单的X / Y图表,但是我想显示Y2,我知道MS图表控件有一个内置的AxisY2但是当我尝试任何事情与它图表得到的所有时髦的。

I'm building a chart to show items by volume by category. So far I've been succcessful in showing items by volume as it's a simple x/y chart, however I'd like to show y2 and I know MS Chart Controls has a built in AxisY2 however when I try anything with it the Chart get's all funky.

这就是我正在寻找(ASCII艺术):

Here's what I'm looking for (in ascii art):

item1 |[][][][][].............| cat1
item2 |[][]...................| cat2
item3 |[][....................| cat1
item4 |[][][][][][][][........| cat1
      |_______________________|
        0   1   2   3   4   5

就像pviously提到,我可以得到项目和计数显示细腻因为这是比较容易的$ P $,那就是我似乎无法放置分类。

Like previously mentioned I can get Items and counts to show fine as that's relatively easy, it's the Categories that I can't seem to place.

感谢

推荐答案

简答第一:根据MS的例子,有没有直接的方法来做到这一点,而只是一种变通方法技巧:绘制你的系列的第二chartArea完全匹配其现有的区域位置,(通过将你的系列副本)无形的主要X / Y轴,可见辅助y轴(AxisY2)。并设置chartArea和复制系列的backcolors透明。 (这可应用于二次X轴在塔的情况下的图,而该棒)

Short Answer first : According to MS Examples, there is no straight way to do that, but just a workaround trick : Plot your series on a second chartArea matching exactly your existing area position, (by performing a copy of your Series) having invisible primary X/Y Axis and a visible secondary Y Axis (AxisY2). And set the chartArea and the copied series's backcolors to transparent. (This can be applied to secondary X axis in case of column graphs rather that bars)

//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系列右侧)。
事实上,这被证明是一个真正的噩梦后,当人们可以期望这是pretty简单。事实是, MS图表控件绝对不适合这一特定使用案例恕我直言。在MSCC样品示例提出的多个Y轴样品是一个可怕的,非常难看的解决方法,这需要在一个默认的前两名chartareas,具有可视性和透明度发挥,以达到预期的效果(这听起来像一个非常坏的幻觉魔术招)。
搜索结果虽然希望这在未来的版本中丰富和固定在一个适当的方式,如果你真的需要管理多个Y轴,sitck到的 ZedGraph

PS : I've spent two nights awake messing with MS chart controls, trying to put two different Y axis on a Chart Area. I wanted to put two differently scaled series (same X scale, different Y Scales : one on the left for Series A , the other on the right for Series B). In fact, this proved to be a real nightmare, when one could expect this to be pretty straightforward. The truth is that MS Chart Controls are definitely NOT adapted for this particular use case IMHO. The multiple Y axis sample suggested in the MSCC sample examples is an awful and very ugly workaround, which requires two chartareas on top of the default one, playing with visibility and transparency, to achieve the desired effect (which sounds like a very bad illusion magic trick).

While hoping for this to be enriched and fixed in a proper way in future versions, if you really need an efficient way to manage multiple Y-Axis, sitck to ZedGraph

这篇关于MS图表控件两个Y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆