simple ASP.NET 2.0 app that demonstrates the use of OWC11 to create a pie chart

Discussion in 'ASP.NET 2.0' started by shorttimer, Apr 17, 2006.

  1. Here is a simple ASP.NET 2.0 app that demonstrates the use of OWC11 to create a pie chart from a sql server 2000 database using an ObjectdataSource. </o:p>
    </o:p>
    You?ll have to create a sql query and connection that will return the fields the business object is looking for to actually run it.</o:p>
    </o:p>
    I?ve spent many hours and not an insignificant quantity of alcohol fussing with this and related syntax ? and the discountASP support folks have been very helpful - so thought I?d share this on the forum in the event anyone else might be experiencing similar struggles.</o:p>
    </o:p>
    Here we go -</o:p>
    </o:p>
    Create a new web project (file system) : owc_test</o:p>
    </o:p>
    Add new asp.net folder: bin</o:p>
    </o:p>
    Add OWC11 reference: </o:p>
    Select website from menu.</o:p>
    Select Add_Reference</o:p>
    Click the Com tab</o:p>
    Choose Microsoft Office Web Components 11.0</o:p>
    Click OK</o:p>
    This will add the Interop.OWC11.dll to the bid folder (along with three other dll?s which you can delete if you wish).</o:p>
    </o:p>
    Add new asp.net folder: app_code</o:p>
    </o:p>
    In the App_Code folder add: owc_data.vb</o:p>
    Code as follows:</o:p>
    </o:p>
    Imports Microsoft.VisualBasic</o:p>
    Imports System</o:p>
    Imports System.Configuration</o:p>
    Imports System.Data</o:p>
    Imports System.Data.SqlClient</o:p>
    Imports System.Collections.Generic</o:p>
    Imports System.Web.UI</o:p>
    Imports System.Web.UI.WebControls</o:p>
    Imports OWC11</o:p>
    </o:p>
    Namespace BusinessLogicLayer</o:p>
    </o:p>
    Public Class owc_data</o:p>
    </o:p>
    Public ChartItems As DataSet</o:p>
    Public ChartxArr() As String</o:p>
    Public ChartyArr() As String</o:p>
    Public aSeries() As String</o:p>
    Public aColors() As String</o:p>
    Public aAxis() As String</o:p>
    </o:p>
    Public Function GetChartDataplpie(ByVal id As Integer)</o:p>
    Dim dsn As String = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString</o:p>
    Dim myConnection As SqlConnection = New SqlConnection(dsn)</o:p>
    Dim myCommand As SqlCommand = New SqlCommand("your_stored_proc", myConnection)</o:p>
    Dim parameteridx As SqlParameter = New SqlParameter("@your_param", SqlDbType.Int)</o:p>
    parameteridx.Value = id</o:p>
    myCommand.Parameters.Add(parameteridx)</o:p>
    Dim sda As New SqlDataAdapter(myCommand)</o:p>
    Dim PriceList As New DataSet</o:p>
    myCommand.CommandType = CommandType.StoredProcedure</o:p>
    myConnection.Open()</o:p>
    sda.Fill(PriceList)</o:p>
    Dim rcnt As Integer = PriceList.Tables(0).Rows.Count()</o:p>
    ReDim ChartxArr(rcnt - 1)</o:p>
    ReDim ChartyArr(rcnt - 1)</o:p>
    Dim row As DataRow</o:p>
    Dim cnt As Integer = 0</o:p>
    For Each row In PriceList.Tables(0).Rows</o:p>
    ChartxArr(cnt) = row("acct").ToString()</o:p>
    ChartyArr(cnt) = row("amt").ToString()</o:p>
    cnt = cnt + 1</o:p>
    Next row</o:p>
    Return PriceList</o:p>
    End Function</o:p>
    </o:p>
    <CLSCompliantAttribute(False)> _</o:p>
    Public Function Chartplpie(ByVal chartCatArr() As String, ByVal chartVal1Arr() As String) As OWC11.ChartSpaceClass</o:p>
    </o:p>
    Dim chartCategoriesArr As String() = chartCatArr</o:p>
    Dim chartValues1Arr As String() = chartVal1Arr</o:p>
    </o:p>
    Dim chartCategoriesStr As String = String.Join(ControlChars.Tab, chartCategoriesArr)</o:p>
    Dim chartValues1Str As String = String.Join(ControlChars.Tab, chartValues1Arr)</o:p>
    </o:p>
    Dim oChartSpace As OWC11.ChartSpaceClass = New OWC11.ChartSpaceClass</o:p>
    </o:p>
    ReDim aColors(3)</o:p>
    aColors(0) = "Brown"</o:p>
    aColors(1) = "Blue"</o:p>
    aColors(2) = "red"</o:p>
    </o:p>
    oChartSpace.Border.Color = "white"</o:p>
    oChartSpace.Charts.Add(0)</o:p>
    oChartSpace.Charts(0).HasTitle = False</o:p>
    oChartSpace.Charts(0).PlotArea.Interior.Color = "cornsilk"</o:p>
    oChartSpace.Charts(0).HasLegend = False</o:p>
    oChartSpace.Charts(0).SeriesCollection.Add(0)</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection.Add()</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasCategoryName = True</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasValue = False</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).Caption = String.Empty</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Name = "verdana"</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Size = 10</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Bold = True</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Color = "red"</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).SetData(OWC11.ChartDimensionsEnum.chDimCategories, _</o:p>
    Convert.ToInt32(OWC11.ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr)</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).SetData(OWC11.ChartDimensionsEnum.chDimValues, _</o:p>
    Convert.ToInt32(OWC11.ChartSpecialDataSourcesEnum.chDataLiteral), chartValues1Str)</o:p>
    oChartSpace.Charts(0).SeriesCollection(0).Type = oChartSpace.Constants.chChartTypePieExploded3D</o:p>
    oChartSpace.Charts(0).Rotation = 160</o:p>
    </o:p>
    Return oChartSpace</o:p>
    </o:p>
    End Function</o:p>
    </o:p>
    End Class</o:p>
    </o:p>
    End Namespace</o:p>
    </o:p>
    </o:p>
    </o:p>
    Change web_config to: </o:p>
    </o:p>
    </o:p>
    <configuration></o:p>
    </o:p>
    <connectionStrings></o:p>
    <add name="ConnStr" connectionString="Data Source=localhost;Initial Catalog=your_database;Persist Security Info=True;
    User ID=your_user;Password=your_pw" providerName="System.Data.SqlClient"/></o:p>
    </connectionStrings></o:p>
    </o:p>
    <system.web></o:p>
    </o:p>
    <authentication mode="Forms"></o:p>
    </authentication></o:p>
    </o:p>
    <authorization></o:p>
    <allow users="*"/></o:p>
    </authorization></o:p>
    </o:p>
    <trace enabled="false"/></o:p>
    </o:p>
    </system.web></o:p>
    </o:p>
    </configuration></o:p>
    </o:p>
    </o:p>
    Create an new web page in the root: owc_chart.aspx</o:p>
    Code as follows:</o:p>
    </o:p>
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="owc_chart.aspx.vb" Inherits="owc_chart" %></o:p>
    </o:p>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></o:p>
    </o:p>
    <html xmlns="http://www.w3.org/1999/xhtml" ></o:p>
    <head id="Head1" runat="server"></o:p>
    <title><st1:Street w:st="on"><st1:address w:st="on">Chart PL</title></o:p>
    </head></o:p>
    </o:p>
    <form id="form1" runat="server"></o:p>
    <div></o:p>
    </o:p>
    </div></o:p>
    </form></o:p>
    </body></o:p>
    </html></o:p>
    </o:p>
    </o:p>
    Page code-behind as follows:</o:p>
    </o:p>
    Imports BusinessLogicLayer</o:p>
    Imports OWC11</o:p>
    </o:p>
    Partial Class owc_chart</o:p>
    Inherits System.Web.UI.Page</o:p>
    </o:p>
    Protected WithEvents ChartSpace1 As OWC11.ChartSpace</o:p>
    </o:p>
    Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load</o:p>
    </o:p>
    Dim pl As owc_data = New owc_data</o:p>
    Response.Buffer = True</o:p>
    Response.ContentType = "image/gif"</o:p>
    pl.GetChartDataplpie(32) '</o:p>
    ChartSpace1 = pl.Chartplpie(pl.ChartxArr, pl.ChartyArr)</o:p>
    Response.BinaryWrite(ChartSpace1.GetPicture("gif", 400, 400))</o:p>
    Response.End()</o:p>
    </o:p>
    End Sub</o:p>
    </o:p>
    End Class</o:p>
    </o:p>
    </o:p>
    That?s about it ? run the app and you should create a 3-d pie chart (2-d on discountASP - must be the com ver.).</o:p>
    </o:p>
    Create your remote database and FTP the web code to discountASP as is and it should run fine.</o:p>
    </o:p>
    Do you best to enjoy the day ? life is short. [​IMG]</o:p>
    </o:p>
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    Very cool..

    not many people have successfully used OWC with InterOp. It bombed on me a few times... so i gave up!!

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     

Share This Page