1

I cannot seem to get my individual subcharts (contained within my single chart object) to host individual legends for their respective data series. I would like to know if this is possible and, if so, what I can adjust in my code to achieve this effect.

Thanks for your help

Code is as follows:

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);
}
Kashif
  • 865
  • 2
  • 12
  • 33

1 Answers1

1

At first glance you seem to be missing the association between the series and the Legend this is from the WebSamples project that you can download here, it really helps to see a full source examples.

In the MultiLegends section, the code looks like this:

            // 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";

The only thing that I didn't see in your code is that last line of association.

CheGueVerra
  • 7,849
  • 4
  • 37
  • 49