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

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

问题描述

我正在构建一个图表,以按类别按数量显示项目.到目前为止,我已经成功地按体积显示项目,因为它是一个简单的 x/y 图表,但是我想显示 y2 并且我知道 MS Chart Controls 有一个内置的 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

就像前面提到的,我可以让项目和计数显示得很好,因为这相对容易,但我似乎无法放置类别.

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.

谢谢

推荐答案

Short Answer first : 根据 MS 示例,没有直接的方法可以做到这一点,但只是一个解决方法:在第二个完全匹配的图表区域上绘制您的系列您现有的区域位置,(通过执行系列的副本)具有不可见的主要 X/Y 轴和可见的次要 Y 轴(AxisY2).并将图表区域和复制的系列的背景色设置为透明.(这可以应用于柱状图而不是条形图的辅助 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).事实上,事实证明这是一场真正的噩梦,当时人们可以期望这非常简单.事实是MS 图表控件绝对不适用于这个特定用例恕我直言.MSCC 示例示例中建议的多 Y 轴示例是一种糟糕且非常丑陋的解决方法,它需要在默认图表区域之上有两个图表区域,以可见性和透明度播放,以达到所需的效果(这听起来像是一种非常糟糕的幻觉魔法诡计).

虽然希望在未来的版本中以适当的方式丰富和修复这一点,但如果您确实需要一种有效的方式来管理多个 Y 轴,请坐到 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天全站免登陆