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
185
ColumnLine Chart legend
posted

Hello

I created a columnLine chart

and bound it to a datatable with two columns one for the column series and the other for the line series

I used the following code to draw the line serie in a different color

        private void Chart_ChartDrawItem(object sender, ChartDrawItemEventArgs e)
        {
            if (e.Primitive is Polyline )
            {
                e.Primitive.PE.Fill = Color.Firebrick;
            }
        }

and I  used the following code to bind the data 

            chart.ColumnLineChart.ColumnData.DataSource = datatable;
            chart.ColumnLineChart.ColumnData.IncludeColumn("line", false);
            chart.ColumnLineChart.ColumnData.SwapRowsAndColumns = true;
            chart.ColumnLineChart.ColumnData.DataBind();
            chart.ColumnLineChart.LineData.DataSource = datatable;
            chart.ColumnLineChart.LineData.IncludeColumn("column", false);
            chart.ColumnLineChart.LineData.SwapRowsAndColumns = true;
            chart.ColumnLineChart.LineData.DataBind();

 

My problem is that now I now only have the column series in my legend.

 

 

 

Parents
  • 28496
    Suggested Answer
    Offline posted

    try this:

    this.ultraChart1.Legend.DataAssociation = ChartTypeData.SplitData;

    that will make your legend show the line data as well as the column data.  but the legend icon for the line will be the wrong color, because you recolored the line in the chart.

    in order to get this to work properly, i had to implement a FillSceneGraph event handler.  here's the complete code:

    private void Form1_Load(object sender, EventArgs e)
            {
                this.ultraChart1.ChartType = ChartType.ColumnLineChart;
                DataTable table = new DataTable();
                table.Columns.Add("line", typeof(double));
                table.Columns.Add("column", typeof(double));
                table.Rows.Add(new object[] { 2.0, 3.0 });
                table.Rows.Add(new object[] { 1.0, 1.0 });
                table.Rows.Add(new object[] { 3.0, 2.0 });
                this.ultraChart1.ColumnLineChart.ColumnData.DataSource = table;
                this.ultraChart1.ColumnLineChart.ColumnData.IncludeColumn("line", false);
                this.ultraChart1.ColumnLineChart.ColumnData.DataBind();

                this.ultraChart1.ColumnLineChart.LineData.DataSource = table;
                this.ultraChart1.ColumnLineChart.LineData.SwapRowsAndColumns = true;
                this.ultraChart1.ColumnLineChart.LineData.IncludeColumn("column", false);
                this.ultraChart1.ColumnLineChart.LineData.DataBind();

                this.ultraChart1.Legend.DataAssociation = ChartTypeData.SplitData;
                this.ultraChart1.Legend.Visible = true;
            }

            private void ultraChart1_ChartDrawItem(object sender, ChartDrawItemEventArgs e)
            {
                if (e.Primitive is Polyline)
                {
                    e.Primitive.PE = new PaintElement(Color.Firebrick);
                }
               
            }

            private void ultraChart1_FillSceneGraph(object sender, FillSceneGraphEventArgs e)
            {
                for (int current = 0; current < e.SceneGraph.Count; current++)
                {
                    Primitive currentPrim = e.SceneGraph[current];
                    bool legendBox = currentPrim is Box && currentPrim.Path != null && currentPrim.Path.Contains("Legend") && currentPrim.Row == 0;
                    if (legendBox && current < e.SceneGraph.Count - 1)
                    {
                        Text nextLabel = e.SceneGraph[current + 1] as Text;
                        if (nextLabel != null && nextLabel.GetTextString() == "line")
                        {
                            currentPrim.PE = new PaintElement(Color.Firebrick);
                        }
                    }
                }
            }

Reply Children