MS图表控制两个Y轴

时间:2010-08-11 13:31:15

标签: c# controls charts

我正在构建一个图表,按类别按体积显示项目。到目前为止,我已经成功地按体积显示项目,因为它是一个简单的x / y图表,但是我想显示y2并且我知道MS Chart Controls有一个内置AxisY2然而当我尝试任何东西时,Chart得到了所有时髦。

这是我正在寻找的东西(在ascii艺术中):

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

如前所述,我可以获得物品和计数,因为它相对容易,这是我似乎无法放置的类别。

由于

5 个答案:

答案 0 :(得分:31)

以下是为我做的事情 - 在我创建图表后,我添加了以下几行:

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;

没有必要叠加两个图表或任何东西!

答案 1 :(得分:9)

它变得更好:

对于使用第二个Y轴,不需要第二个图表区域。您可以根据系列决定要使用哪个轴与Series.YAxisType属性。 请查看http://msdn.microsoft.com/en-us/library/dd489216.aspx

上的文档

马亭

答案 2 :(得分:6)

简短回答:根据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;
PS:我已经花了两个晚上搞砸MS图表控件,试图在图表区域放置两个不同的Y轴。我想放两个不同比例的系列(相同的X刻度,不同的Y刻度:一个在左边用于A系列,另一个在右边用于B系列)。 事实上,这被证明是一个真正的噩梦,当人们可以期待这是非常简单的。事实上, MS Chart Controls绝对不适合这个特定用例恕我直言。在MSCC样本示例中建议的多个Y轴样本是一个非常丑陋且非常难看的解决方法,它需要在默认的两个图表区域之上,使用可见性和透明度来实现期望的效果(这听起来像一个非常糟糕的幻觉魔法特技)。

虽然希望在未来的版本中以适当的方式丰富和修复它,但如果你真的需要一种有效的方法来管理多个Y轴,那么请ZedGraph

答案 3 :(得分:3)

您可以根据需要在Y轴上添加任意数量的系列,下面的代码是我使用的具有2个以上辅助y轴的图表的摘录,代码适用于vb.net,但我确定你可以解决这个问题:

        ChartKPI.Series.Clear()

        ChartKPI.Series.Add("Series1")
        ChartKPI.Series("Series1").XValueMember = "Date"
        ChartKPI.Series("Series1").YValueMembers = "HSDPA_Vol_MBy"
        ChartKPI.Series("Series1").Name = "HSDPA_Vol_MBy"
        ChartKPI.Series("HSDPA_Vol_MBy").ChartType = SeriesChartType.Column
        ChartKPI.Series("HSDPA_Vol_MBy").ToolTip = "HSDPA MBytes: #VAL"

        ChartKPI.Series.Add("Series2")
        ChartKPI.Series("Series2").YAxisType = AxisType.Secondary
        ChartKPI.Series("Series2").XValueMember = "Date"
        ChartKPI.Series("Series2").YValueMembers = "cs_voice_traffic"
        ChartKPI.Series("Series2").Name = "cs_voice_traffic"
        ChartKPI.Series("cs_voice_traffic").ChartType = SeriesChartType.Line
        ChartKPI.Series("cs_voice_traffic").BorderWidth = 3
        ChartKPI.Series("cs_voice_traffic").ToolTip = "CS Voice Traffic: #VAL"

        ChartKPI.Series.Add("Series3")
        ChartKPI.Series("Series3").YAxisType = AxisType.Secondary
        ChartKPI.Series("Series3").XValueMember = "Date"
        ChartKPI.Series("Series3").YValueMembers = "cs_conv_traffic"
        ChartKPI.Series("Series3").Name = "cs_conv_traffic"
        ChartKPI.Series("cs_conv_traffic").ChartType = SeriesChartType.Line
        ChartKPI.Series("cs_conv_traffic").BorderWidth = 3
        ChartKPI.Series("cs_conv_traffic").ToolTip = "CS Conv Traffic: #VAL"

        ChartKPI.Series.Add("Series4")
        ChartKPI.Series("Series4").YAxisType = AxisType.Secondary
        ChartKPI.Series("Series4").XValueMember = "Date"
        ChartKPI.Series("Series4").YValueMembers = "ps_backg_traffic_ul"
        ChartKPI.Series("Series4").Name = "ps_backg_traffic_ul"
        ChartKPI.Series("ps_backg_traffic_ul").ChartType = SeriesChartType.Line
        ChartKPI.Series("ps_backg_traffic_ul").BorderWidth = 3
        ChartKPI.Series("ps_backg_traffic_ul").ToolTip = "PS Backg Traffic UL: #VAL"

        ChartKPI.Series.Add("Series5")
        ChartKPI.Series("Series5").YAxisType = AxisType.Secondary
        ChartKPI.Series("Series5").XValueMember = "Date"
        ChartKPI.Series("Series5").YValueMembers = "ps_backg_traffic_dl"
        ChartKPI.Series("Series5").Name = "ps_backg_traffic_dl"
        ChartKPI.Series("ps_backg_traffic_dl").ChartType = SeriesChartType.Line
        ChartKPI.Series("ps_backg_traffic_dl").BorderWidth = 3
        ChartKPI.Series("ps_backg_traffic_dl").ToolTip = "PS Backg Traffic DL: #VAL"

        ChartKPI.ChartAreas("ChartArea1").AxisX.Title = "HSDPA Traffic (MB)"
        ChartKPI.ChartAreas("ChartArea1").AxisX.MajorGrid.Interval = 1
        ChartKPI.ChartAreas("ChartArea1").AxisX.LabelStyle.Interval = 1
        ChartKPI.ChartAreas("ChartArea1").AxisY.Title = "RRC Attempts"
        ChartKPI.ChartAreas("ChartArea1").AxisY2.Title = "R99 Traffic (Erlang)"

        ChartKPI.DataBind()

答案 4 :(得分:2)

解决方案:

chart1.ChartAreas[1].AlignWithChartArea = chart1.ChartAreas[0].Name;
chart1.ChartAreas[1].AlignmentOrientation = AreaAlignmentOrientations.All;