蛙蛙推薦:在HTML裡利用OWC做圖表

來源:互聯網
上載者:User

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="蛙蛙王子">
<META NAME="Keywords" CONTENT="owc,報表,圖表">
<META NAME="Description" CONTENT="呵呵,哥們把vb的一個例子徹底轉換成vbs指令碼並移植到網頁了,就為了做一個既好看效能又好的圖表控制項,哥們找了N多資料,光看VML就看的眼畫了,本來想用VML寫一個chart控制項的,後來太麻煩了,直接用OWC算了,功能也暴強,OWC不用我介紹了吧,估計就算我費半天勁學好VML,再學好JS的物件導向開發,最後寫出一個VMLCHART估計也沒這麼強大,微軟免費提供的東西為什麼不利用一下呢,昨天和今天剛看了MS的一個sql reporting serivice的視頻教程,那個更NB,有空裝一個,什麼報表,圖表統統搞定,不過什麼東西都有缺點,我寫的這個例子必須在用戶端安裝OFFICE才行,或者得單獨分發OWC組件才行,我在對象聲明的時候沒有寫DLL的位置,也沒有引用網上的CAB包,大家測試的時候裝個OFS200就可以了">
<SCRIPT LANGUAGE="VbScript">
Private Sub Command1_Click()

    'Create arrays for the x-values and the y-values
 '為X軸資料和Y軸資料定義幾個數組
    Dim xValues, yValues1, yValues2
    'xValues = Array("Beverages", "Condiments", "Confections", _
    '               "Dairy Products", "Grains & Cereals", _
    '               "Meat & Poultry", "Produce", "Seafood")
 xValues = Array("飲料", "調味品", "糖果", _
                    "乳製品", "魚肉", _
                    "家禽肉類", "農作物", "海產食品")
   
 yValues1 = Array(104737, 50952, 78128, 117797, 52902, 80160, 47491, _
                     62435)
    yValues2 = Array(20000, 15000, 36000, 56000, 40000, 18000, 20000, _
                     33000)

    'Create a new chart
 '建立一個新的圖表對象
    Dim oChart,chConstants
    ChartSpace1.Clear '清空圖表
    ChartSpace1.Refresh '重繪圖表
    Set oChart = ChartSpace1.Charts.Add '建立一個空圖表
 Set chConstants = ChartSpace1.Constants '在 VBScript 中使用命名常量

    'Add a title to the chart
    oChart.HasTitle = True
    oChart.Title.Caption = "Sales Per Category"

    'Add a series to the chart with the x-values and y-values
    'from the arrays and set the series type to a column chart
    Dim oSeries
    Set oSeries = oChart.SeriesCollection.Add
    With oSeries
        .Caption = "1995"
        .SetData chConstants.chDimCategories, chConstants.chDataLiteral, xValues
        .SetData chConstants.chDimValues, chConstants.chDataLiteral, yValues1
        .Type = chConstants.chChartTypeColumnClustered
    End With

    'Add another series to the chart with the x-values and y-values
    'from the arrays and set the series type to a line chart
    Set oSeries = oChart.SeriesCollection.Add
    With oSeries
        .Caption = "1996"
        .SetData chConstants.chDimCategories, chConstants.chDataLiteral, xValues
        .SetData chConstants.chDimValues, chConstants.chDataLiteral, yValues2
        .Type = chConstants.chChartTypeLineMarkers
    End With

    'Add a value axis to the right of the chart for the second series
    oChart.Axes.Add oChart.Axes(chConstants.chAxisPositionLeft).Scaling,chConstants.chAxisPositionRight, chConstants.chValueAxis

    'Format the Value Axes
    oChart.Axes(chConstants.chAxisPositionLeft).NumberFormat = "$#,##0"
 oChart.Axes(chConstants.chAxisPositionRight).NumberFormat = "0"
 oChart.Axes(chConstants.chAxisPositionLeft).MajorUnit = 20000
    oChart.Axes(chConstants.chAxisPositionRight).MajorUnit = 20000

    'Show the legend at the bottom of the chart
    oChart.HasLegend = True
    oChart.Legend.Position = chLegendPositionBottom

End Sub
Private Sub Command2_Click()

    'Set up the DataSourceControl for the Chartspace
    Dim rsd, chConstants
    Set chConstants = ChartSpace1.Constants
    DataSourceControl1.ConnectionString = _
        "DRIVER={Microsoft Access Driver (*.mdb)}; " & _
        "DBQ=C:/Program Files/Microsoft Visual Studio/VB98/nwind.mdb"
    Set rsd = DataSourceControl1.RecordsetDefs.AddNew( _
             "Select * from [Category Sales for 1995]", 3)
    With ChartSpace1
        .Clear
        .Refresh
        .DataSource = DataSourceControl1
        .DataMember = rsd.Name
    End With

    'This Chartspace will contain 2 charts. Make the layout so that the
    'charts are positioned horizontally
    ChartSpace1.ChartLayout = chConstants.chChartLayoutHorizontal

    'Create a new bar chart from the query
    Dim oBarChart
    Set oBarChart = ChartSpace1.Charts.Add
    With oBarChart
        .Type = chConstants.chChartTypeBarClustered
        .SetData chConstants.chDimCategories, 0, 0  'Categories are first field
        .SetData chConstants.chDimValues, 0, 1      'Values are second field

        'Format the value axis for the bar chart so that it
        'shows values in thousands (i.e., 45000 displays as 45) and
        'in increments of 25000. Remove the gridlines
        With .Axes(chConstants.chAxisPositionBottom)
            .NumberFormat = "0,"
            .MajorUnit = 25000
            .HasMajorGridlines = False
        End With

        'Change the color of the series and the plot area
        .SeriesCollection(0).Interior.Color = RGB(150, 0, 150)
        .PlotArea.Interior.Color = RGB(240, 240, 10)
    End With

    'Create a new exploded pie chart from the query
    Dim oPieChart
    Set oPieChart = ChartSpace1.Charts.Add
    With oPieChart
        .Type = chConstants.chChartTypePie
        .SetData chConstants.chDimCategories, 0, 0  'Categories are first field
        .SetData chConstants.chDimValues, 0, 1      'Values are second field
        .SeriesCollection(0).Explosion = 20

        'Add a legend to the bottom of the pie chart
        .HasLegend = True
        .Legend.Position = chConstants.chLegendPositionBottom

        'Add a title to the chart
        .HasTitle = True
        .Title.Caption = "Sales by Category for 1995"
        .Title.Font.Bold = True
        .Title.Font.Size = 11

        'Make the chart width 50% the size of the bar chart's width
        .WidthRatio = 50

        'Show data labels on the slices as percentages
        With .SeriesCollection(0).DataLabelsCollection.Add
            .HasValue = False
            .HasPercentage = True
            .Font.Size = 8
            .Interior.Color = RGB(255, 255, 255)
        End With

    End With

End Sub
Private Sub Command3_Click()

   'Dynamically add a spreadsheet control to the form
   Dim chConstants
   Set chConstants = ChartSpace1.Constants
 
   'Fill the Sheet with data
   With oSheet
        .Range("A1:A10").Formula = "=Row()"
        .Range("B1:B10").Formula = "=A1^2"
        .Range("A12").Formula = "=Max(A1:A10)"
        .Range("B12").Formula = "=Max(B1:B10)"
   End With

   'Create an xy-scatter chart using the data in the spreadsheet
   Dim oChart
   With ChartSpace1
        .Clear
        .Refresh
        .DataSource = oSheet.object
        Set oChart = .Charts.Add
        oChart.Type = chConstants.chChartTypeScatterSmoothLineMarkers
        oChart.SetData chConstants.chDimXValues, 0, "a1:a10"
        oChart.SetData chConstants.chDimYValues, 0, "b1:b10"
   End With

   With oChart
        'Display the Axes Titles and
        'set the major units for the axes
        With .Axes(chConstants.chAxisPositionBottom)
            .HasTitle = True
            .Title.Caption = "X"
            .Title.Font.Size = 8
            .MajorUnit = 1
        End With
        With .Axes(chConstants.chAxisPositionLeft)
            .HasTitle = True
            .Title.Caption = "X Squared"
            .Title.Font.Size = 8
            .MajorUnit = 10
        End With

        'Set the maximum and minimum axis values
        .Scalings(chConstants.chDimXValues).Maximum = oSheet.Range("A12").Value
        .Scalings(chConstants.chDimXValues).Minimum = 1
        .Scalings(chConstants.chDimYValues).Maximum = oSheet.Range("B12").Value

        'Change the marker and line styles for the series
        With .SeriesCollection(0)
            .Marker.Style = chConstants.chMarkerStyleDot
            .Marker.Size = 6
            .Line.Weight = 1
            .Line.Color = RGB(255, 0, 0)
        End With
   End With
End Sub
</SCRIPT>
</HEAD>
<BODY>
<!-- 聲明一個圖表對象ChartSpace1-->
<object id=ChartSpace1 classid=CLSID:0002E500-0000-0000-C000-000000000046 codebase=”msowc.dll” style="width:100%;height:350;border:#ffffff 0px solid"></object>
<!-- 聲明一個圖表資料來源對象DataSourceControl1 -->
<object id=DataSourceControl1 classid=CLSID:0002E55B-0000-0000-C000-000000000046></object>
<!-- 聲明一個試算表對象oSheet -->
<object id=oSheet classid=CLSID:0002E559-0000-0000-C000-000000000046 style="width:0;height:0;border:#ffffff 0px solid"></object>

<INPUT TYPE="button" language="vbscript" onclick="Command1_Click()" value="使用數組">
<INPUT TYPE="button" language="vbscript" onclick="Command2_Click()" value="使用ado記錄機">
<INPUT TYPE="button" language="vbscript" onclick="Command3_Click()" value="使用試算表">
</BODY>
</HTML>
<!-- 做了半截注釋,不想做了,裡面大家不懂的參數什麼的,大家自己參考相關資料算了,我得下班了,如果你裝了OFFICE,在OFFICE的安裝目錄下面有個OWCVBA11.CHM的檔案,你開啟它,關於OWC的資料非常詳細了,可能這個協助檔案的名字會根據你安裝的OFS的版本不同而不同 -->

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.