2

I have a Bar chart on Kentico reporting section. And I know Kentico uses Microsoft Chart Controls. Microsoft Chart controls have the capability of creating a trending line on Bar graph - But I do see any option how I can utilize those on Kentico Reporting. Is there any option there on reporting tool to get this trending line ? If there is no option can anybody suggest anything else ? Using custom module is the last option for me to try. If anybody has any specific suggestion regarding this custom module, please share that, too. I am using Kentico 7. enter image description here enter image description here

Got it working the way Brend suggested however the mean is not coming up

ChartArea area = graph.ChartControl.ChartAreas[chartAreas - 1];
                StripLine line = new StripLine();

                // Set threshold line so that it is only shown once
                line.Interval = 0;

                // Set the threshold line to be drawn at the calculated mean of the first series
                line.IntervalOffset = graph.ChartControl.DataManipulator.Statistics.Mean(graph.ChartControl.Series[0].Name);

            line.BackColor = System.Drawing.Color.DarkGreen;
            line.StripWidth = 0.25;

            // Set text properties for the threshold line
            //line.Text = "Mean";
            line.ForeColor = System.Drawing.Color.Black;

            // Add strip line to the chart
            area.AxisY.StripLines.Add(line);

Also, for other trend line I am using bellow code, again having no luck as it looks like the datapoints are not set at the point where my code runs :

int chartAreas = graph.ChartControl.ChartAreas.Count;
            if (chartAreas > 0)
            {
                graph.ChartControl.Series.Add("TrendLine");
                graph.ChartControl.Series["TrendLine"].ChartType = SeriesChartType.Line;
                graph.ChartControl.Series["TrendLine"].BorderWidth = 3;
                graph.ChartControl.Series["TrendLine"].Color = System.Drawing.Color.Red;
                // Line of best fit is linear
                string typeRegression = "Linear";//"Exponential";//
                                                 // The number of days for Forecasting
                string forecasting = "1";
                // Show Error as a range chart.
                string error = "false";
                // Show Forecasting Error as a range chart.
                string forecastingError = "false";
                // Formula parameters
                string parameters = typeRegression + ',' + forecasting + ',' + error + ',' + forecastingError;
                graph.ChartControl.Series[0].Sort(PointSortOrder.Ascending, "X");
                // Create Forecasting Series.
                graph.ChartControl.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, parameters, graph.ChartControl.Series[0], graph.ChartControl.Series["TrendLine"]);
            }

The actual issue, I guess, is not having the graph.ChartControl.Series[0] at the place I am running my TrendLine generation code. Any idea how can I get it ?

marifrahman
  • 681
  • 2
  • 13
  • 31

2 Answers2

2

Report charts are rendered through \CMSModules\Reporting\Controls\ReportGraph.ascx

You can modify the method GetReportGraph and add additional setup to the chart control ucChart based on some condition, e.g. the report name and chart name (you will have to hardcode that)

Note that you will need to modify Kentico code directly, so keep the changes at the lowest possible level, I recommend:

  • Put the extra setup code to an external class
  • Call it with just one extra line of code
  • Add comment to mark that extra line of code as customization

e.g.:

/* YourCompany */
MyChartExtender.ExtendChart(ucChart, ...);
/* YourCompany end */
  • Make sure you note that change for future upgrades
martinh_kentico
  • 913
  • 7
  • 17
1

I've modified the controls before and you can use this code in the GetReportGraph() method just before enabling the subscription.

// apply the trendline
if (TrendValue > 0)
{
    int chartAreas = graph.ChartControl.ChartAreas.Count;
    if (chartAreas > 0)
    {
        ChartArea area = graph.ChartControl.ChartAreas[chartAreas - 1];
        StripLine line = new StripLine();
        line.IntervalOffset = TrendValue;
        line.BorderColor = System.Drawing.ColorTranslator.FromHtml(TrendColor);
        line.BackColor = System.Drawing.ColorTranslator.FromHtml(TrendColor);
        line.StripWidth = TrendLineWidth;
        line.ToolTip = TrendToolTip;
        line.Text = TrendText;
        line.TextLineAlignment = trendLineAlignment;
        line.TextOrientation = TextOrientation.Horizontal;
        line.TextAlignment = trendTextAlignment;
        area.AxisY.StripLines.Add(line);
    }
}

Of course you'll have to add the appropriate properties and pass the values through from the rest of the pages/controls using this control.

#region Trending
/// <summary>
/// Value for the single trendline for whole chart
/// </summary>
public int TrendValue
{
    get
    {
        return mTrendValue;
    }
    set
    {
        mTrendValue = value;
    }
}

/// <summary>
/// Color of the trend line in hex format (i.e. #0000FF)
/// </summary>
public string TrendColor
{
    get
    {
        return mTrendColor;
    }
    set
    {
        mTrendColor = value;
    }
}

/// <summary>
/// Tool tip of the trend line
/// </summary>
public string TrendToolTip
{
    get
    {
        return mTrendToolTip;
    }
    set
    {
        mTrendToolTip = value;
    }
}

/// <summary>
/// Text of the trend line
/// </summary>
public string TrendText
{
    get
    {
        return mTrendText;
    }
    set
    {
        mTrendText = value;
    }
}

/// <summary>
/// Trend line width
/// </summary>
public double TrendLineWidth
{
    get
    {
        return mTrendLineWidth;
    }
    set
    {
        mTrendLineWidth = value;
    }
}

string mTrendLineAlignment;
public string TrendLineAlignment
{
    get
    {
        return mTrendLineAlignment;
    }
    set
    {
        mTrendLineAlignment = value;
    }
}
private System.Drawing.StringAlignment trendLineAlignment
{
    get
    {
        switch (TrendLineAlignment)
        {
            case "center":
                return System.Drawing.StringAlignment.Center;
            case "near":
                return System.Drawing.StringAlignment.Near;
            case "far":
                return System.Drawing.StringAlignment.Far;
            default:
                return System.Drawing.StringAlignment.Near;
        }
    }
}

string mTrendTextAlignment;
public string TrendTextAlignment
{
    get
    {
        return mTrendTextAlignment;
    }
    set
    {
        mTrendTextAlignment = value;
    }
}
private System.Drawing.StringAlignment trendTextAlignment
{
    get
    {
        switch (TrendTextAlignment)
        {
            case "center":
                return System.Drawing.StringAlignment.Center;
            case "near":
                return System.Drawing.StringAlignment.Near;
            case "far":
                return System.Drawing.StringAlignment.Far;
            default:
                return System.Drawing.StringAlignment.Near;
        }
    }
}


#endregion
Brenden Kehren
  • 5,919
  • 16
  • 27
  • Sorry, finally I have managed to make it work - but what is the trend value here ? I was using line.IntervalOffset = graph.ChartControl.DataManipulator.Statistics.Mean(graph.ChartControl.Series[0].Name); and it was throwing error for it was not getting any value for the points graph.ChartControl.Series[0].Name – marifrahman Jan 13 '16 at 02:50
  • The `IntervalOffset` property is an integer and it appears you're getting a string. So even if your chart series[0] had a name assigned to it, it would be an invalid cast. – Brenden Kehren Jan 13 '16 at 14:01
  • Din get your point ?! the IntervalOffset is od type double and the Mean returns double; Issue is at the point I run graph.ChartControl.Series[0].‌​Name is suppose to return the series - but it doesn't - looks like the datapoints are not set !! – marifrahman Jan 13 '16 at 23:48