SSRS Dynamic Chart Type Selection?
-
Wednesday, April 16, 2008 4:43 AM
Does anyone know if there is a way to dynamically set the chart type (i.e. pie, line, column, etc.) for your report in SSRS? I want the customer to be able to chose which type of chart they'd like to see.
I've looked all over the Internet and can't find anything.
Any help would be appreciated.
Alan
Clarification:
I guess I should clarify a little. I am using an ASP.NET 2.0 page written in C# and using a ReportViewer control.
More Clarification:
The ReportViewer displays an rdlc.
Answers
-
Thursday, May 15, 2008 6:19 AM
Hello, In further exploration, I found a totally new way to get the same effect. Turns out, the code I put above is for Windows Forms not Web Forms. This method is much better.
First, here is some code to call a function I wrote to handle this routine. RDLCXml2005Namespace is a constant set to the value of the namespace that Microsoft puts at the top of your RDLC. You can open the RDLC in an XML editor to find what the namespace is.
Code SnippetFileStream streamingReport = new FileStream(Server.MapPath("TransactionsByCoverageType.rdlc"), FileMode.Open);
//The .rdlc file is actually an XML document that structures how the report is to be built.
XmlDocument rdlcXML = new XmlDocument();
try
{
rdlcXML.Load(streamingReport);
}
finally
{
streamingReport.Close();
}
SSRSLib.UpdateReportChartType(rdlcXML, uxChartType.Items[uxChartType.SelectedIndex].Text, SSRSLib.RDLCXml2005Namespace, null, "chart1", ReportViewer1);
Here is the UpdateReportChartType function, one of the biggest problems was that you need to be sure to reset ALL of your reportviewer information, including datasource. DefaultChartType is a constant "Line".
Code Snippet/// <summary>
/// Replaces the chart type in the report with the chart type passed to this method.
/// </summary>
/// <param name="documentToUpdate">The report.rdlc (xml document) you want to change the chart type for.</param>
/// <param name="desiredChartType">The desired chart type, don't worry about blank strings, it is set to a default in the function.</param>
/// <param name="reportNamespace">The namespace of the report. You can find this by opening your .rdlc file in an xml browser.</param>
/// <param name="listName">The name of the list object that you included in your report. If your components are not using a list, pass null.</param>
/// <param name="chartName">The name of the chart you want to adjust the type for.</param>
/// <param name="viewerToUpdate">The ReportViewer object you are going to add this report to.</param>
public static void UpdateReportChartType(XmlDocument documentToUpdate, string desiredChartType, string reportNamespace, string listName, string chartName,
ReportViewer viewerToUpdate)
{
XmlElement root = documentToUpdate.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager(documentToUpdate.NameTable);
nsManager.AddNamespace("ns", reportNamespace);
XmlNode workingNode;
//If component does not use a list, leave out the list item by making the list string blank.
string listToUse;
if ((listName == null) || (listName == ""))
listToUse = "";
else
listToUse = "ns:ReportItems/ns:List[@Name='" + listName + "']/";
//Set the chart type.
workingNode = root.SelectSingleNode("ns:Body/" + listToUse + "ns:ReportItems/ns:Chart[@Name='" + chartName + "']/ns:Type", nsManager);
if (desiredChartType == "")
workingNode.InnerText = DefaultChartType;
else
workingNode.InnerText = desiredChartType;
//Save original settings to restore after resetting.
ProcessingMode originalProcessingMode = viewerToUpdate.ProcessingMode;
ReportDataSource originalDataSource = viewerToUpdate.LocalReport.DataSources[0];
//Reset the basic report information.
viewerToUpdate.Reset();
viewerToUpdate.ProcessingMode = originalProcessingMode;
viewerToUpdate.LocalReport.DataSources.Add(originalDataSource);
viewerToUpdate.LocalReport.ReportPath = string.Empty;
viewerToUpdate.LocalReport.LoadReportDefinition(new StringReader(documentToUpdate.OuterXml));
viewerToUpdate.LocalReport.Refresh();
}
All Replies
-
Wednesday, April 16, 2008 8:21 AMmoved to ssrs forum
-
Thursday, April 24, 2008 7:31 PM
Hi again,
Here's what I have so far, it's almost working. The .rdlc file the report is based off of is actually an xml document. Yes I know this isn't error and foolproof yet, but I'm trying to get it working first. My problem with what I have so far is this:
When I click the btnViewReport button the first time, it runs the appropriate type of chart (pie, line, bar, etc.) but every time I change the report type and click the btnViewReport button again, it sticks with whatever type of chart I ran the first time I clicked the button. I'm sure there's something I need to clear or refresh but I can't figure out what.
Code Snippetprotected void btnViewReport_Click(object sender, EventArgs e){
Stream streamingReport; //The .rdlc file is actually an XML document that structures how the report is to be built. XmlDocument rdlcXML = new XmlDocument();streamingReport = System.Reflection.
Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsumerRaterReports.TransactionsByHour.rdlc");rdlcXML.Load(streamingReport);
XmlNodeList typeNodeList = rdlcXML.GetElementsByTagName("Type"); XmlNode typeNode = typeNodeList[0];if (uxChartType.Items[uxChartType.SelectedIndex].Text == "")
typeNode.InnerText = "Line";
else
typeNode.InnerText = uxChartType.Items[uxChartType.SelectedIndex].Text;
ReportViewer1.LocalReport.ReportPath =
string.Empty;ReportViewer1.LocalReport.LoadReportDefinition(
new StringReader(rdlcXML.OuterXml));ReportViewer1.LocalReport.Refresh();
streamingReport.Close();
-
Friday, April 25, 2008 10:03 PM
Hooray, I figured it out and I'm posting it here in case anyone else ever needs it.
What I was doing wrong before was not "resetting" my reportviewer. I didn't do it because it kept throwing my datasource off later. So I decided to recreate the datasource and voila.
In this example, uxChartType is a drop down list that contains chart types such as Pie, Column, Bar, Line, Area.
Code Snippetprotected void btnViewReport_Click(object sender, EventArgs e)
{
Stream streamingReport;
//The .rdlc file is actually an XML document that structures how the report is to be built.
XmlDocument rdlcXML = new XmlDocument(); try{
streamingReport = System.Reflection.
Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsumerRaterReports.TransactionsByHour.rdlc");rdlcXML.Load(streamingReport);
//Change out the Type of the chart in the XML document XmlNodeList typeNodeList = rdlcXML.GetElementsByTagName("Type"); XmlNode typeNode = typeNodeList[0];if (uxChartType.Items[uxChartType.SelectedIndex].Text == "")
typeNode.InnerText = "Line";
else
typeNode.InnerText = uxChartType.Items[uxChartType.SelectedIndex].Text;
//Reset the basic report information
ReportViewer1.Reset();
ReportViewer1.ProcessingMode =
ProcessingMode.Local; ReportDataSource newDataSource = new ReportDataSource();newDataSource.DataSourceId =
"ObjectDataSource1";newDataSource.Name =
"TransactionsByHour_vwGetConsumerStatsByHour";ReportViewer1.LocalReport.DataSources.Add(newDataSource);
ReportViewer1.LocalReport.ReportPath =
string.Empty;ReportViewer1.LocalReport.LoadReportDefinition(
new StringReader(rdlcXML.OuterXml));ReportViewer1.LocalReport.Refresh();
}
finally{
streamingReport.Close();
}
... (more code to set datasource and set report to visible)
-
Friday, May 09, 2008 10:42 AMHi ,
While executing the below statement
streamingReport = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyWebAppName.ReportName.rdlc");
I am getting null in the streamingReport .
I am unable to proceed because of this null value..
Please help.
I tried using reflection to get the type of report , using the System.Reflection.Assembly.GetExecutingAssembly().getTypes()
there is no report type. -
Thursday, May 15, 2008 6:19 AM
Hello, In further exploration, I found a totally new way to get the same effect. Turns out, the code I put above is for Windows Forms not Web Forms. This method is much better.
First, here is some code to call a function I wrote to handle this routine. RDLCXml2005Namespace is a constant set to the value of the namespace that Microsoft puts at the top of your RDLC. You can open the RDLC in an XML editor to find what the namespace is.
Code SnippetFileStream streamingReport = new FileStream(Server.MapPath("TransactionsByCoverageType.rdlc"), FileMode.Open);
//The .rdlc file is actually an XML document that structures how the report is to be built.
XmlDocument rdlcXML = new XmlDocument();
try
{
rdlcXML.Load(streamingReport);
}
finally
{
streamingReport.Close();
}
SSRSLib.UpdateReportChartType(rdlcXML, uxChartType.Items[uxChartType.SelectedIndex].Text, SSRSLib.RDLCXml2005Namespace, null, "chart1", ReportViewer1);
Here is the UpdateReportChartType function, one of the biggest problems was that you need to be sure to reset ALL of your reportviewer information, including datasource. DefaultChartType is a constant "Line".
Code Snippet/// <summary>
/// Replaces the chart type in the report with the chart type passed to this method.
/// </summary>
/// <param name="documentToUpdate">The report.rdlc (xml document) you want to change the chart type for.</param>
/// <param name="desiredChartType">The desired chart type, don't worry about blank strings, it is set to a default in the function.</param>
/// <param name="reportNamespace">The namespace of the report. You can find this by opening your .rdlc file in an xml browser.</param>
/// <param name="listName">The name of the list object that you included in your report. If your components are not using a list, pass null.</param>
/// <param name="chartName">The name of the chart you want to adjust the type for.</param>
/// <param name="viewerToUpdate">The ReportViewer object you are going to add this report to.</param>
public static void UpdateReportChartType(XmlDocument documentToUpdate, string desiredChartType, string reportNamespace, string listName, string chartName,
ReportViewer viewerToUpdate)
{
XmlElement root = documentToUpdate.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager(documentToUpdate.NameTable);
nsManager.AddNamespace("ns", reportNamespace);
XmlNode workingNode;
//If component does not use a list, leave out the list item by making the list string blank.
string listToUse;
if ((listName == null) || (listName == ""))
listToUse = "";
else
listToUse = "ns:ReportItems/ns:List[@Name='" + listName + "']/";
//Set the chart type.
workingNode = root.SelectSingleNode("ns:Body/" + listToUse + "ns:ReportItems/ns:Chart[@Name='" + chartName + "']/ns:Type", nsManager);
if (desiredChartType == "")
workingNode.InnerText = DefaultChartType;
else
workingNode.InnerText = desiredChartType;
//Save original settings to restore after resetting.
ProcessingMode originalProcessingMode = viewerToUpdate.ProcessingMode;
ReportDataSource originalDataSource = viewerToUpdate.LocalReport.DataSources[0];
//Reset the basic report information.
viewerToUpdate.Reset();
viewerToUpdate.ProcessingMode = originalProcessingMode;
viewerToUpdate.LocalReport.DataSources.Add(originalDataSource);
viewerToUpdate.LocalReport.ReportPath = string.Empty;
viewerToUpdate.LocalReport.LoadReportDefinition(new StringReader(documentToUpdate.OuterXml));
viewerToUpdate.LocalReport.Refresh();
}
-
Thursday, May 15, 2008 9:53 AM
Hi,
Thanks for the reply..
I am trying to render the report dynamically in the web form , is there any way to accomplish the above task
-
Thursday, December 02, 2010 6:28 AMcan u pls provide a code for web?????
-
Friday, March 23, 2012 3:22 PMCan this be done with report builder/BIDS?

