单个图表中多个ChartAreas的单独图例

时间:2015-02-04 00:55:49

标签: c# .net winforms asp.net-charts

我似乎无法获取我的单个子图表(包含在我的单个图表对象中)来托管各自数据系列的单个图例。我想知道这是否可行,如果可行,我可以在代码中调整以实现此效果。

感谢您的帮助

代码如下:

chart_MyChart.Legends.Clear();

ChartArea chartArea_MyData = new ChartArea("My Data");
ChartArea chartArea_YourData = new ChartArea("Your Data");
ChartArea chartArea_OtherData = new ChartArea("Other Data");

chart_MyChart.ChartAreas.Clear();
chart_MyChart.ChartAreas.Add(chartArea_MyData);
chart_MyChart.ChartAreas.Add(chartArea_YourData);
chart_MyChart.ChartAreas.Add(chartArea_OtherData);             

/* Chart Area: My Data */
Series series01 = this.chart_MyChart.Series.Add("My first series");
series01.ChartArea = chartArea_MyData.Name;
Series series02 = this.chart_MyChart.Series.Add("My second series");
series02.ChartArea = chartArea_MyData.Name;

Legend legend01 = new Legend(series01.Name);
Legend legend02 = new Legend(series02.Name);

legend01.DockedToChartArea = chartArea_MyData.Name;
legend02.DockedToChartArea = chartArea_MyData.Name;

chart_MyChart.Legends.Add(legend01);
chart_MyChart.Legends.Add(legend02);

/* Chart Area: Your Data */
Series series03 = this.chart_MyChart.Series.Add("Your first series");
series03.ChartArea = chartArea_YourData.Name;
Series series04 = this.chart_MyChart.Series.Add("Your second series");
series04.ChartArea = chartArea_YourData.Name;

Legend legend03 = new Legend(series03.Name);
Legend legend04 = new Legend(series04.Name);

legend03.DockedToChartArea = chartArea_YourData.Name;
legend04.DockedToChartArea = chartArea_YourData.Name;

chart_MyChart.Legends.Add(legend03);
chart_MyChart.Legends.Add(legend04);

/* Chart Area: Other Data */
Series series05 = this.chart_MyChart.Series.Add("Other series");
series05.ChartArea = chartArea_OtherData.Name;

Legend legend05 = new Legend(series05.Name);

legend05.DockedToChartArea = chartArea_OtherData.Name;

chart_MyChart.Legends.Add(legend05);

foreach(Legend legend in chart_MyChart.Legends)
{
    legend.IsDockedInsideChartArea = true;
}

foreach(widget myWidget in some.widget)
{
    series01.Points.AddXY(widget.timeStamp, widget.data1);
    series02.Points.AddXY(widget.timeStamp, widget.data2);
    series03.Points.AddXY(widget.timeStamp, widget.data3);
    series04.Points.AddXY(widget.timeStamp, widget.data4);
    series05.Points.AddXY(widget.timeStamp, widget.data5);
}

1 个答案:

答案 0 :(得分:1)

乍一看,您似乎错过了系列与图例之间的关联,这可以来自您可以下载{2}的WebSamples项目,查看完整的源示例确实很有帮助。

在MultiLegends部分中,代码如下所示:

            // Add a second legend
            Legend secondLegend = new Legend("Second");
            secondLegend.BackColor = Color.FromArgb(((System.Byte)(220)), ((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
            secondLegend.BorderColor = Color.Gray;
            secondLegend.Font = this.Chart1.Legends["Default"].Font;

            this.Chart1.Legends.Add(secondLegend);

            // Associate Series 2 with the second legend 
            this.Chart1.Series["Series 2"].Legend = "Second";

我在你的代码中唯一没有看到的是最后一行关联。