Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
130
Multiple Legends in Composite Chart
posted

I have a composite chart made of four layers: two line charts and two scatter charts.  I want to have two legends on the chart (one for each chart type).

To accomplish this, I've created one legend and added the two line chart layers to its ChartLayers collection, then created another legend and added the two scatter chart layers to its ChartLayers collection.  I then added the two legends to my UltraChart.CompositeChart.Legends collection.  Instead of drawing the line chart items in one legend and the scatter chart items in the other, they both just display the line chart items.  The following code snippets illustrate my approach:

// Line Chart Layer 1
ChartLayerAppearance layerAppearance1 = new ChartLayerAppearance();
layerAppearance1.ChartType = ChartType.LineChart;
layerAppearance1.Series.Add(series1);
layerAppearance1.Series.Add(series2);
layerAppearance1.Series.Add(series3);
layerAppearance1.ChartTypeAppearance = chartAppearance1;
inChart.CompositeChart.ChartLayers.Add(layerAppearance1);

// Line Chart Layer 2
ChartLayerAppearance layerAppearance2 = new ChartLayerAppearance();
layerAppearance2.ChartType = ChartType.LineChart;
layerAppearance2.Series.Add(series4);
layerAppearance2.ChartTypeAppearance = chartAppearance3;
inChart.CompositeChart.ChartLayers.Add(layerAppearance2);

// Line Chart Legend
CompositeLegend legend = new CompositeLegend();
legend.ChartLayers.Add(layerAppearance1);
legend.ChartLayers.Add(layerAppearance2);
inChart.CompositeChart.Legends.Add(legend);

// Scatter Chart Layer 1
ChartLayerAppearance layerAppearance3 = new ChartLayerAppearance();
layerAppearance3.ChartType = ChartType.ScatterChart;
// Series added later
layerAppearance3.ChartTypeAppearance = scatterAppearance1;
inChart.CompositeChart.ChartLayers.Add(layerAppearance3);

// Scatter Chart Layer 2
ChartLayerAppearance layerAppearance4 = new ChartLayerAppearance();
layerAppearance4.ChartType = ChartType.ScatterChart;
// Series added later
layerAppearance4.ChartTypeAppearance = scatterAppearance2;
inChart.CompositeChart.ChartLayers.Add(layerAppearance4);

// Scatter Chart Legend
CompositeLegend scatterLegend = new CompositeLegend();
scatterLegend.ChartLayers.Add(layerAppearance3);
scatterLegend.ChartLayers.Add(layerAppearance4);
inChart.CompositeChart.Legends.Add(scatterLegend);

Is this the correct approach or is there something I'm missing?

In the attached image, the two legends are located on the bottom of the chart (the top one is scatterLegend).

  • 28496
    Offline posted

    everything looks right to me.  i tried to reproduce the problem, but i didn't have any trouble getting both legends showing.  here's the complete code i used:

     

                UltraChart inChart = new UltraChart();
                this.Controls.Add(inChart);
                inChart.Dock = DockStyle.Fill;
                inChart.ChartType = ChartType.Composite;

                // Line Chart Layer 1
                ChartLayerAppearance layerAppearance1 = new ChartLayerAppearance();
                layerAppearance1.ChartType = ChartType.LineChart;
                inChart.CompositeChart.ChartLayers.Add(layerAppearance1);

                // Line Chart Layer 2
                ChartLayerAppearance layerAppearance2 = new ChartLayerAppearance();
                layerAppearance2.ChartType = ChartType.LineChart;
                inChart.CompositeChart.ChartLayers.Add(layerAppearance2);

                // Line Chart Legend
                CompositeLegend legend = new CompositeLegend();
                legend.ChartLayers.Add(layerAppearance1);
                legend.ChartLayers.Add(layerAppearance2);
                inChart.CompositeChart.Legends.Add(legend);

                // Scatter Chart Layer 1
                ChartLayerAppearance layerAppearance3 = new ChartLayerAppearance();
                layerAppearance3.ChartType = ChartType.ScatterChart;
                inChart.CompositeChart.ChartLayers.Add(layerAppearance3);

                // Scatter Chart Layer 2
                ChartLayerAppearance layerAppearance4 = new ChartLayerAppearance();
                layerAppearance4.ChartType = ChartType.ScatterChart;
                inChart.CompositeChart.ChartLayers.Add(layerAppearance4);

                // Scatter Chart Legend
                CompositeLegend scatterLegend = new CompositeLegend();
                scatterLegend.ChartLayers.Add(layerAppearance3);
                scatterLegend.ChartLayers.Add(layerAppearance4);
                inChart.CompositeChart.Legends.Add(scatterLegend);


                NumericSeries numeric1 = new NumericSeries() { Label = "numeric1" };
                numeric1.Points.Add(new NumericDataPoint(1.0, "A", false));
                numeric1.Points.Add(new NumericDataPoint(2.0, "B", false));
                numeric1.Points.Add(new NumericDataPoint(3.0, "C", false));

                NumericSeries numeric2 = new NumericSeries() { Label = "numeric2" };
                numeric2.Points.Add(new NumericDataPoint(4.0, "D", false));
                numeric2.Points.Add(new NumericDataPoint(5.0, "E", false));
                numeric2.Points.Add(new NumericDataPoint(6.0, "F", false));

                XYSeries xy1 = new XYSeries() { Label = "xy1" };
                xy1.Points.Add(new XYDataPoint(1.0, 1.0, "G", false));
                xy1.Points.Add(new XYDataPoint(2.0, 2.0, "H", false));
                xy1.Points.Add(new XYDataPoint(3.0, 3.0, "I", false));
               
                XYSeries xy2 = new XYSeries() { Label = "xy2" };
                xy2.Points.Add(new XYDataPoint(4.0, 4.0, "J", false));
                xy2.Points.Add(new XYDataPoint(5.0, 5.0, "K", false));
                xy2.Points.Add(new XYDataPoint(6.0, 6.0, "L", false));

                layerAppearance1.Series.Add(numeric1);
                layerAppearance2.Series.Add(numeric2);
                layerAppearance3.Series.Add(xy1);
                layerAppearance4.Series.Add(xy2);

                inChart.CompositeChart.ChartAreas.Add(new ChartArea());
                AxisItem axisX1 = new AxisItem();
                axisX1.DataType = AxisDataType.String;
                axisX1.SetLabelAxisType = SetLabelAxisType.ContinuousData;
                AxisItem axisY1 = new AxisItem();
                axisY1.OrientationType = AxisNumber.Y_Axis;
                AxisItem axisX2 = new AxisItem();
                inChart.CompositeChart.ChartAreas[0].Axes.Add(axisX1);
                inChart.CompositeChart.ChartAreas[0].Axes.Add(axisY1);
                inChart.CompositeChart.ChartAreas[0].Axes.Add(axisX2);

                layerAppearance1.ChartArea =
                    layerAppearance2.ChartArea =
                    layerAppearance3.ChartArea =
                    layerAppearance4.ChartArea = inChart.CompositeChart.ChartAreas[0];

                layerAppearance1.AxisX = layerAppearance2.AxisX = axisX1;
                layerAppearance1.AxisY = layerAppearance2.AxisY = axisY1;

                layerAppearance3.AxisX = layerAppearance4.AxisX = axisX2;
                layerAppearance3.AxisY = layerAppearance4.AxisY = axisY1;

                legend.Bounds = new Rectangle(0, 0, 100, 100);
                scatterLegend.Bounds = new Rectangle(0, 100, 100, 100);

                inChart.InvalidateLayers();

  • 130
    posted

    Sorry, I seem to have posted this in the wrong place.  It should be under Windows Forms, not ASP.net.  Anybody have any idea how I can move it?