The Wayback Machine - https://web.archive.org/web/20090130081407/http://www.codeproject.com:80/KB/web-image/Column-Chart.aspx
Click here to Skip to main content
5,845,357 members and growing! (19,884 online)
Web Development » Charts, Graphs and Images » Images and multimedia     Beginner License: The Code Project Open License (CPOL)

Create Column Charts Using OWC11

By Rupesh Kumar Swami

A column chart (simple,stacked, or 100% stacked column) representation using Office Web Components.
C# (C# 2.0, C#), VB (VB 8.0, VB), .NET (.NET, .NET 2.0), WinForms

Posted: 25 Jun 2008
Updated: 5 Jul 2008
Views: 20,754
Bookmarked: 46 times
Announcements
Loading...



Advanced Search
Sitemap
Prize winner in Competition "Best VB.NET article of June 2008"
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
37 votes for this Article.
Popularity: 6.69 Rating: 4.26 out of 5
3 votes, 8.1%
1
2 votes, 5.4%
2
1 vote, 2.7%
3

4
31 votes, 83.8%
5

Introduction

This is my very first article to any open source site. Since I've gotten a lot of useful articles from this site, I'll be glad if I can to contribute to this great site. This is a simple application to get some knowledge of using the OWC11 in a .NET application for chart representation.

Office Web Components (OWC) are a group of OLE classes implemented as ActiveX controls in Microsoft Office 2000, Office XP, and Office 2003. The Office Web Components have been discontinued in Office 2007, and are not included, except as a part of the Office Project Server 2007. OWC can be used by any COM-compliant Component Object Model programming language.

Background

Any application that uses the Office Web Components 11 requires that the OWC 11 package be installed on the system. OWC 11 is a separate download available on the Microsoft Website or in the Microsoft Office 2003 Installation Kit. Office Web Components 11 does not require support from Microsoft Office.

Office 2003 Web Components do not provide event handling by default. We need to do some alterations. So, please refer this link first: HOW TO: Handle Events for the Office 2003 Web Components in Visual Studio.NET. Using this link, we can get AxOWC11.dll. Add this newly-compiled AxOWC11.dll to the Toolbox. Now, the Toolbox contains the AxChartSpace control, the AxPivotTable control, and the AxSpreadsheet control.

Column Chart

A column chart displays values and series groups as sets of vertical columns that are grouped by category. Values are represented by the height of the columns as measured by the y-axis. Category labels are displayed on the x-axis. Column charts are typically used to compare values between categories. There are three types of column charts:

  • Column
  • Stacked Column
  • 100% Stacked Column

A column chart displays series as individual columns, grouped by category. The height of each column is determined by the series value.

A stacked column chart displays all series stacked in a single column for each category. The height of each column is determined by the total of all the series values for the category.

A 100% stacked column chart displays all series stacked in a single column for each category. The height of each column is always the full height of the chart. The series values are displayed as percentages of each column.

The abbove definition of the column chart is picked from the MSDN. For more details, please refer: Column Chart.

Steps for Creating the Chart

First of all, from the Toolbox property window, select the AxChartSpace control and drag it on to the design surface. After this step, our form looks like the following:

MainScreen.jpg

Figure-1

Now, in the code window, write the following statement:

Imports owc11 = Microsoft.Office.Interop.Owc11

Since the code only shows how can we create the Column Chart, the code is not efficient. Here, we are skipping some statements which fill dummy data in the DataGridView. Now, we declare two variables for holding data for the categories (related to the x-axis) and the values (related to the y-axis):

Dim categories(3)
For i As Integer = 0 To 3
    categories(i) = DataGridView1.Columns(i + 1).HeaderText.ToString
Next
Dim values(3)
Dim chConstants

For clearing the contents of the chart workspace, use the following statement. The following statement removes any old charts that may already exist and adds a chart to the chart space.

AxChartSpace1.Clear()
AxChartSpace1.Charts.Add()
chConstants = AxChartSpace1.Constants

Now, add series to the existing chart. Using the following statement, you can add as much series as you want.

AxChartSpace1.Charts(0).SeriesCollection.Add()

Now, we supply the data to each series as follows:

Dim MaxTotal As Integer = 0
For j As Integer = 0 To DataGridView1.Rows.Count - 2
AxChartSpace1.Charts(0).SeriesCollection(j).SetData(chConstants.chDimCategories, _
              chConstants.chDataLiteral, categories)
    For i As Integer = 1 To DataGridView1.Columns.Count - 1
        values(i - 1) = Val(DataGridView1.Rows(j).Cells(i).Value)
    Next
    AxChartSpace1.Charts(0).SeriesCollection(j).SetData(chConstants.chDimValues, _
                            chConstants.chDataLiteral, values)
    AxChartSpace1.Charts(0).SeriesCollection(j).Caption = _
                  DataGridView1.Rows(j).Cells(0).Value
    MaxTotal = MaxTotal + Val(DataGridView1.Rows(j).Cells(4).Value)
Next

We can also set the color for each series as follows:

AxChartSpace1.Charts(0).SeriesCollection(0).Interior.Color = "DarkOrange"
AxChartSpace1.Charts(0).SeriesCollection(1).Interior.Color = "Cyan"
AxChartSpace1.Charts(0).SeriesCollection(2).Interior.Color = "Yellow"
AxChartSpace1.Charts(0).SeriesCollection(3).Interior.Color = "Red"
AxChartSpace1.Charts(0).SeriesCollection(4).Interior.Color = "Black"

Notice one thing in the above statements. I supply the color for a series as a string. It does not support the Color enum. At this point, we decide which type of chart we want (simple, stacked, or 100 % stacked column). Also, we can draw charts with 3D effect. Use the following statement for a simple and a 3D stacked column.

If cboChartType.Text = "3D" Then
    AxChartSpace1.Charts(0).Type = owc11.ChartChartTypeEnum.chChartTypeColumnStacked3D
    '(See Figure-2)
Else
    AxChartSpace1.Charts(0).Type = owc11.ChartChartTypeEnum.chChartTypeColumnStacked
    (See Figure-3)
End If

StackedColumn3D.JPG

Figure-2

StackedColumn.JPG

Figure-3

And for a simple and 3D 100% stacked column, use the following statement:

If cboChartType.Text = "3D" Then
    AxChartSpace1.Charts(0).Type = owc11.ChartChartTypeEnum.chChartTypeColumnStacked1003D
    (see Figure-4)
Else
    AxChartSpace1.Charts(0).Type = owc11.ChartChartTypeEnum.chChartTypeColumnStacked100
    (see Figure-5)
End If

100PercentStackedColumn3D.JPG

Figure-4

100PercentStackedColumn.JPG

Figure-5

And finally, for a simple and 3D column, use the following statement:

If cboChartType.Text = "3D" Then
    AxChartSpace1.Charts(0).Type = owc11.ChartChartTypeEnum.chChartTypeColumn3D
    (see Figure-6)
End If

SimpleColumn3D.JPG

Figure-6

SimpleColumn.JPG

Figure-7

Along with the above statements, the way we provide the data to all the charts is also responsible for getting the correct results. In all the charts, the variable MaxTotal also plays a role which decides the scaling of the chart.

AxChartSpace1.Charts(0).Axes(1).Scaling.Maximum = MaxTotal
AxChartSpace1.Charts(0).Axes(1).MajorUnit = MaxTotal / 4
AxChartSpace1.Charts(0).Axes(1).Scaling.Minimum = 0

Now, in the final step, we assign proper title and font to the chart axes, as follows:

AxChartSpace1.Charts(0).Axes(0).HasTitle = True
AxChartSpace1.Charts(0).Axes(0).Title.Caption = "Time Range"
AxChartSpace1.Charts(0).Axes(0).Title.Font.Name = "Arial"
AxChartSpace1.Charts(0).Axes(0).Title.Font.Size = 9

AxChartSpace1.Charts(0).Axes(1).HasTitle = True
AxChartSpace1.Charts(0).Axes(1).Title.Caption = "Number of Members"
AxChartSpace1.Charts(0).Axes(1).Title.Font.Name = "Arial"
AxChartSpace1.Charts(0).Axes(1).Title.Font.Size = 9

Points of Interest

When I use the OWC 11 chart component in real applications, I get some strange results. If we declare the variable categories as follows:

Dim categories(3) As String

then the chart object does not display any series. However, the legend shows all the series names. One more thing, if we change the series color before supplying the data, then the series does not display the proper color.

I hope you've enjoyed this little tutorial. In future, I will provide you a lot more stuff. And, I am expecting your valuable suggestions.

Finally, please excuse me for my English.

 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
Generalvb.netmemberMember 21001981:55 20 Dec '08  
Dear Guru ,

How to add components for AXCHARTSPACE through owc11 installing.i got AxoWc11.dll but i cannot able to install in component.
is there any other way to redraggin axchsrtspace ,please advice me

Regards
Deepak Mathur
system -Manager,Jodhpur
GeneralRe: vb.netmemberRupesh Kumar Swami19:13 20 Dec '08  
I think yor are unable to show the controls in Tool Box.if yes then follow steps
1. open the Tool Box window
2. right click and select "Choose Item"
3 .A new window named "Choose ToolBox Item" appear. select .Net framework components Tab and click on Browse button in lower section
4. Now pick "AxOWC11.dll"
5. now notice there are three components(AxChartSpace, AxPivotTable and AxSpreadSheet) is added in components list. Now click on Ok Button
6. Notice that these three item is added in your toolbox

if you have other problem then let me know

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

GeneralRe: vb.netmemberMember 210019818:32 30 Dec '08  
Dear Rupesh,
I got it ,now i am getting erorr that "not in range " & iind i wanted to open desgine form in vb.net ,i am not able to do it

Please let me know how to do it

Regards
Deepak Mathur
Sr.Mgr-systems
Videocon
GeneralRe: vb.netmemberRupesh Kumar Swami23:18 30 Dec '08  
hi deepak,
can you explain your application detail or send application to me ?

if yes, then mail me at rup28aug@yahoo.co.in.

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

Generalcan we do in vb6memberdeepakmathur1:38 12 Dec '08  
Dear Guru,
Can we develop this same thing in VB 6.0 ,if yes then how we can do it ,please let me know.

Regards!
Deepak Mathur
Sr.Mgr.-systems
Videocon
GeneralRe: can we do in vb6memberRupesh Kumar Swami3:20 12 Dec '08  
hi deepak,
i have little knowledge of vb6 so i am not sure
however i think you can use OWC in vb6. following link help you
MSDN article[^]


one more thing-
why not you switch on .NET(vb.net)? since vb6 is dead language(out dated).

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

Generalin vb.netmemberMember 210019818:34 18 Dec '08  
Dear Guru,
I AM GOING TO ACCORDING TO YOUR SUGGESTION TO DO IN .NET BUT HOW TO ADD CONTROL OF AXCHARTSPACE IN COMPONENTS

PLEASE ADVICE ME

REGARDS
dEEPAK mTAHUR
Generalhow to add components of owc in vb.net,please let me knowmemberMember 21001980:30 24 Dec '08  
how to add components of owc in vb.net,please let me know
GeneralRe: how to add components of owc in vb.net,please let me knowmemberMember 21001980:59 24 Dec '08  
i did according to your suggestion but i got message that " axowcc11.dll is not a .NET module .

Now how to proceed,i tried by several times,even that installing owc11.exe by three times


Please Let me know, as early as possible.

Regards
Deepak Mathur
Generaldatagrid1memberMember 210019818:36 24 Dec '08  
Dear Guru,
I got Axchartspace component,but i wanted to know how you have defined "datagridview1"

Please Let me know so that i can try on my pc

Regards!

Deepak Mathur
GeneralRe: can we do in vb6memberMember 21001981:26 24 Dec '08  
i rectified my problem ,
thanks
deepak mathur
Generaldatagridview1memberMember 210019819:35 24 Dec '08  
Dear Guru,

I solve my query which i gave you yesterday

Thanks
Deepak Mathur
GeneralRe: datagridview1memberRupesh Kumar Swami23:40 24 Dec '08  
hi deepak,
because of my busy schedule,i can not check my mail and this article message from last 2-3 days. so i can not reply you. I think you have two account on code project.

these days you post your question multiple time and finally you solve the problem.
so i want to ask whether you have any more question?

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
Award: Best VB.NET article of June 2008: Create Column Charts Using OWC11

GeneralRe: datagridview1memberMember 210019820:57 26 Dec '08  
i rectified that problem ,i am new for vb.net that's y i copied your source code & now i am compiling & getting error that "Public member 'HeaderText' on type 'Range' not found." how to rectified ??

IInd thing i wanted to know how i will save this source code becuase after saving i am not getting vb.desgine ,only i am getting source code ,how to come out from this problem

Best Regards
Deepak Mathur
Sr.Manager-Systems
Videocon Industries Limited
GeneralHi rupeshmemberMember 40829960:00 25 Sep '08  
Can u please explain, how can i use this in web applicatins.
Thanks in advance.
GeneralRe: Hi rupeshmemberRupesh Kumar Swami5:33 25 Sep '08  
hi,
I have no idea about Web programming . Search On code Project and Search on Google may help you.

I will discuss with my subordinate(Which works on ASP.net) and try to give positive result ASAP

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company

GeneralRe: Hi rupeshmemberMember 408299620:33 25 Sep '08  
Thanks for your quick response.

--
Kranthi
GeneralThanksmemberRafael Linares12:24 4 Sep '08  
My english is bad, but many thanks for this article. In Spanish (Muchas gracias amigo)
GeneralRupeshmemberSAMir Nigam19:36 23 Jul '08  
Congratulation fd for winning the competition.

SAMir Nigam

GeneralRe: RupeshmemberRupesh Kumar Swami4:33 24 Jul '08  
Thanks Samir. Same for you .

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
My Article

GeneralRe: RupeshmemberSAMir Nigam19:07 24 Jul '08  
Your Welcome.

SAMir Nigam

GeneralCongrats !!!memberashu fouzdar20:21 21 Jul '08  
Please keep up good work Smile

dnpro
"Very bad programmer"

GeneralRe: Congrats !!!memberRupesh Kumar Swami0:04 22 Jul '08  
Thanks a lot.well i will try my best

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
My Article

GeneralHow to generate .pdf (Crystal Report) from AxMicrosoft.Office.Interop.Owc11.AxChartSpace in C#memberjanpalmerv23:42 13 Jul '08  
I am able to generate a line chart. The problem is how to generate in a long pdf width assuming that the line chart is very big..

can the graph be stretched horizontally?

You may also email me janverge@gmail.com


Thanks
Jan
GeneralRe: How to generate .pdf (Crystal Report) from AxMicrosoft.Office.Interop.Owc11.AxChartSpace in C#memberRupesh Kumar Swami3:58 19 Jul '08  
hi jan,
Presently i am very busy with current application. So whenever i am free, i will try to give positive reply.

Rupesh Kumar Swami
Software Developer,
Integrated Solution,
Bikaner (India)

My Company
My Article

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jul 2008
Editor: Smitha Vijayan
Copyright 2008 by Rupesh Kumar Swami
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project