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
1060
Adding an average line to a stack chart
posted

I have a stack chart.... and i want to add a line going parallel to the X-Axis that would represent an average.

Right now My datasource is a datatable.  the last column is the average (same number for everyrow).

I could modify the source if this is not the best way to get the average.

 

I manyally added the red line using paint to show what i need. 

 

Can anyone help me ?

  • 26458
    Offline posted

    I think the best approach for this scenario is to have a composite chart with a stacked column layer and a line layer. Here's an example:

    private void Form1_Load(object sender, EventArgs e)
    {
        //data
        DataTable dt = new DataTable();
        dt.Columns.Add("col1", typeof(string));
        dt.Columns.Add("col2", typeof(double));
        dt.Columns.Add("col3", typeof(double));
        dt.Rows.Add(new object[] { "item1", 5, 4 });
        dt.Rows.Add(new object[] { "item2", 2, 6 });
        dt.Rows.Add(new object[] { "item3", 4, 7 });

        ultraChart1.ChartType = ChartType.Composite;
        ChartArea area = new ChartArea();
        ultraChart1.CompositeChart.ChartAreas.Add(area);

        //create 2 stacked series
        NumericSeries series1 = new NumericSeries();
        series1.Data.DataSource = dt;
        series1.Data.LabelColumn = "col1";
        series1.Data.ValueColumn = "col2";
        series1.Label = "series1";
        series1.DataBind();
        ultraChart1.CompositeChart.Series.Add(series1);

        NumericSeries series2 = new NumericSeries();
        series2.Data.DataSource = dt;
        series2.Data.LabelColumn = "col1";
        series2.Data.ValueColumn = "col3";
        series2.Label = "series2";
        series2.DataBind();
        ultraChart1.CompositeChart.Series.Add(series2);

        //create the axes for stacked chart
        AxisItem xAxis = new AxisItem(ultraChart1, AxisNumber.X_Axis);
        xAxis.DataType = AxisDataType.String;
        xAxis.SetLabelAxisType = SetLabelAxisType.GroupBySeries;
        xAxis.Labels.ItemFormat = AxisItemLabelFormat.ItemLabel;
        area.Axes.Add(xAxis);

        AxisItem yAxis = new AxisItem(ultraChart1, AxisNumber.Y_Axis);
        yAxis.DataType = AxisDataType.Numeric;
        yAxis.Labels.ItemFormat = AxisItemLabelFormat.DataValue;
        area.Axes.Add(yAxis);

        //stacked layer
        ChartLayerAppearance stackLayer = new ChartLayerAppearance();
        stackLayer.ChartType = ChartType.StackColumnChart;
        stackLayer.AxisX = xAxis;
        stackLayer.AxisY = yAxis;
        stackLayer.Series.Add(series1);
        stackLayer.Series.Add(series2);
        stackLayer.ChartArea = area;
        ultraChart1.CompositeChart.ChartLayers.Add(stackLayer);

        //create an axis for the line chart as it's different from the column axis
        AxisItem xAxisLine = new AxisItem(ultraChart1, AxisNumber.X_Axis);
        xAxisLine.DataType = AxisDataType.String;
        xAxisLine.SetLabelAxisType = SetLabelAxisType.ContinuousData;
        area.Axes.Add(xAxisLine);

        //you can calculate the average here.
        double average = 12;

        //create a line series
        NumericSeries lineSeries = new NumericSeries();
        for (int i = 0; i < series1.Points.Count; i++)
        {
            lineSeries.Points.Add(new NumericDataPoint(average, "", false));
        }
        lineSeries.Label = "average";
        lineSeries.PEs.Add(new PaintElement(Color.Red));
        ultraChart1.CompositeChart.Series.Add(lineSeries);

        //create a line layer
        ChartLayerAppearance lineLayer = new ChartLayerAppearance();
        lineLayer.ChartType = ChartType.LineChart;
        lineLayer.AxisX = xAxisLine;
        lineLayer.AxisY = yAxis;
        lineLayer.Series.Add(lineSeries);
        lineLayer.ChartArea = area;
        ultraChart1.CompositeChart.ChartLayers.Add(lineLayer);
    }