This is a migrated thread and some comments may be shown as answers.

Access textbox from chart needdatasource method

5 Answers 209 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bader
Top achievements
Rank 1
Bader asked on 20 Jul 2011, 04:28 PM
Hello,

How can I access the a textbox value from chart needdatasource method?
My code:
private void Pie_NeedDataSource(object sender, EventArgs e)
       {
           Telerik.Reporting.Processing.Chart chart = sender as Telerik.Reporting.Processing.Chart;
           //Telerik.Reporting.Chart defChart = (Telerik.Reporting.Chart)chart.ItemDefinition;
           //Processing.TextBox textBox21 = (defChart.Items["textBox21"] as Processing.TextBox);
           Telerik.Reporting.Chart chartDef = (Telerik.Reporting.Chart)chart.ItemDefinition;
           Telerik.Reporting.Charting.ChartSeries series = new Telerik.Reporting.Charting.ChartSeries();
           series.Type = ChartSeriesType.Pie;
           Telerik.Reporting.Charting.ChartLegend legend = new Telerik.Reporting.Charting.ChartLegend();
 
           string CoverDescription;
           int itemValue;
          
           string commandText = string.Empty;
           SqlConnection connection = new SqlConnection(global::QTaskReporting.Properties.Settings.Default.QTDbConnString);
           commandText = "SELECT COUNT(*) AS ItemCount, SDesc FROM vwSubTasksAndStatuses where TaskID=" + textBox21.Value + " GROUP BY SDesc";
           SqlCommand cmd = new SqlCommand(commandText, connection);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
            
           foreach (DataRow rowView in ds.Tables[0].Rows)
           {
               Telerik.Reporting.Charting.ChartSeriesItem seriesItem = new Telerik.Reporting.Charting.ChartSeriesItem();
 
               //The chart Y value will be based on the Value amount
               seriesItem.YValue = Convert.ToInt32(rowView["ItemCount"]);
               seriesItem.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
 
               //The chart item label should be set to the Value amount
               itemValue = Convert.ToInt32(rowView["ItemCount"]);
               seriesItem.Label.TextBlock.Text = itemValue.ToString("##");
               seriesItem.Label.Appearance.LabelLocation = Telerik.Reporting.Charting.Styles.StyleSeriesItemLabel.ItemLabelLocation.Inside;
               seriesItem.Label.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(58)))), ((int)(((byte)(112)))));
 
               //Assign the chart item description
               CoverDescription = rowView["SDesc"].ToString();
               seriesItem.Name = CoverDescription.Trim();
 
               //Give the "Shortfall in Cover" chart item an exploded appearance
               if (seriesItem.Name == "New")
               {
                   seriesItem.Appearance.Exploded = true;
               }
 
               series.AddItem(seriesItem);
 
           }
 
           //Display the legend
           series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels;
           series.Appearance.Border.Color = System.Drawing.Color.White;
           series.Appearance.Border.Visible = false;
           series.Appearance.DiameterScale = .90;
 
           series.Appearance.ExplodePercent = 10;
 
           chartDef.Series.Clear();
           chartDef.Series.Add(series);
 
           chartDef.Legend.Appearance.Position.AlignedPosition = Telerik.Reporting.Charting.Styles.AlignedPositions.TopRight;
       }

In the above code I need to use the textbox value in the sql syntax.
Both chart and textbox are placed within a table (Please view the attached screen-shot).

Please, I need your help,
It is appreciated to send me the modified code.

Regards,
Bader

5 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 21 Jul 2011, 05:26 PM
Hi Bader,

Check out the following code snippet that illustrates how get the processing textbox from the chart's NeedDatasource:

private void chart1_NeedDataSource(object sender, System.EventArgs e)
{
    var procChart = (Telerik.Reporting.Processing.Chart)sender;
    var procTextBox = (Telerik.Reporting.Processing.TextBox)
           procChart.Report.ChildElements.Find("textBox21", true)[index];

However for your scenario our suggestion is to set the chart datasource with binding to =ReportItem.DataObject as shown in the following example binding:
 Property path Expression 
 DataSource  =ReportItem.DataObject

Move the chart generation code to Charts.ItemDataBinding, because NeedDataSource is called only when data item have no datasource is set .

Access the desired field as shown in the following code snippet:
private void chart1_ItemDataBinding(object sender, System.EventArgs e)
{
    var procChart = (Telerik.Reporting.Processing.Chart)sender;
    var p = procChart.DataObject["Sku"];

You may find useful Access items from report, calling application and Table help article.

Greetings,
Peter
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Bader
Top achievements
Rank 1
answered on 24 Jul 2011, 10:12 AM
Hello,

Thank you for your reply,I still have a problem.
Note: I found another thread which handles the same problem, but unfortunatilty there is no sample code which is intended to solve the problem (http://www.telerik.com/community/forums/reporting/telerik-reporting/chart-repeating-for-each-record.aspx).

Here is my new code:
private void PHIChart_ItemDataBinding(object sender, EventArgs e)
        {
            Telerik.Reporting.Processing.Chart chart = sender as Telerik.Reporting.Processing.Chart;
            Telerik.Reporting.Processing.Table section = (Telerik.Reporting.Processing.Table)chart.Parent.Parent;
            Telerik.Reporting.Processing.IDataObject dataObject = (Telerik.Reporting.Processing.IDataObject)section.DataObject;
            object rowdata = (object)section.DataObject.RawData;
            Telerik.Reporting.Processing.TextBox procTextbox = (Telerik.Reporting.Processing.TextBox)section.ChildElements.Find("taskIDTextBox", true)[0];
 
            Telerik.Reporting.Chart chartDef = (Telerik.Reporting.Chart)chart.ItemDefinition;
            Telerik.Reporting.Charting.ChartSeries series = new Telerik.Reporting.Charting.ChartSeries();
            series.Type = ChartSeriesType.Pie;
            Telerik.Reporting.Charting.ChartLegend legend = new Telerik.Reporting.Charting.ChartLegend();
 
            string CoverDescription;
            int itemValue;
 
            string commandText = string.Empty;
            SqlConnection connection = new SqlConnection(global::QTaskReporting.Properties.Settings.Default.QTDbConnString);
            commandText = "SELECT COUNT(*) AS ItemCount, SDesc FROM vwSubTasksAndStatuses where TaskID=" + procTextbox.Value + " GROUP BY SDesc";
            SqlCommand cmd = new SqlCommand(commandText, connection);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
 
            foreach (DataRow rowView in ds.Tables[0].Rows)
            {
                Telerik.Reporting.Charting.ChartSeriesItem seriesItem = new Telerik.Reporting.Charting.ChartSeriesItem();
 
                //The chart Y value will be based on the Value amount
                seriesItem.YValue = Convert.ToInt32(rowView["ItemCount"]);
                seriesItem.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
 
                //The chart item label should be set to the Value amount
                itemValue = Convert.ToInt32(rowView["ItemCount"]);
                seriesItem.Label.TextBlock.Text = itemValue.ToString("##");
                seriesItem.Label.Appearance.LabelLocation = Telerik.Reporting.Charting.Styles.StyleSeriesItemLabel.ItemLabelLocation.Inside;
                seriesItem.Label.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(58)))), ((int)(((byte)(112)))));
 
                //Assign the chart item description
                CoverDescription = rowView["SDesc"].ToString();
                seriesItem.Name = CoverDescription.Trim();
 
                //Give the "Shortfall in Cover" chart item an exploded appearance
                if (seriesItem.Name == "New")
                {
                    seriesItem.Appearance.Exploded = true;
                }
 
                series.AddItem(seriesItem);
 
            }
 
            //Display the legend
            series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels;
            series.Appearance.Border.Color = System.Drawing.Color.White;
            series.Appearance.Border.Visible = false;
            series.Appearance.DiameterScale = .90;
 
            series.Appearance.ExplodePercent = 10;
 
            chartDef.Series.Clear();
            chartDef.Series.Add(series);
 
            chartDef.Legend.Appearance.Position.AlignedPosition = Telerik.Reporting.Charting.Styles.AlignedPositions.TopRight;
        }

But the problem that the chart is repeating it self in each record (View the attached screen-shot)

Please, I need your help,
It is very appreciated to send me a modified code.

Regards,
Bader
0
Steve
Telerik team
answered on 25 Jul 2011, 09:44 AM
Hello Bader,

The answer is indeed contained within the forum thread you have found. Although in your case you use a Table item, most likely the chart item is placed in a detail group. Detail groups have a single empty grouping, ex. group1.Grouping.Add(new Grouping()). Usually you have only one row or column detail group. For a detail group the corresponding row/column is repeated for every row of the Table's data source. You may think of the detail TableGroup as the Report's Detail section.

So the solution is to place the chart in a static or dynamic group depending on what you are after. Here is more info:
  • dynamic groups - have any Grouping expression, ex. group1.Grouping.Add(new Grouping("=Fields.Country")). For the dynamic groups, the corresponding row/column will repeat for every record of the grouped data.
  • static groups - no grouping criteria at all. For the static groups the corresponding row/column is rendered only once.

Best wishes,
Steve
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Bader
Top achievements
Rank 1
answered on 25 Jul 2011, 06:58 PM
Hello,

Thank you very much,
But I still can't do it.

It is very appreciated to send me a step by step guide which explains how to do that.

Regards,
Bader
0
Peter
Telerik team
answered on 28 Jul 2011, 12:05 PM
Hello Bader,

The chart is placed in a detail table group so you will get a chart for every detail. In order to avoid this behavior you have to place the chart in the static group. For example the table's header. For more information check out the Understanding table cells, rows and columns help article. Give it a try and if you need additional assistance we will appreciate if you elaborate further on the desired report layout.

Best wishes,
Peter
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
General Discussions
Asked by
Bader
Top achievements
Rank 1
Answers by
Peter
Telerik team
Bader
Top achievements
Rank 1
Steve
Telerik team
Share this question
or