Creating Bar Charts With NPlot

You may have seen the article on ASP.NET Charting with NPlot on the 4 Guys From Rolla.com web site. Unfortunately, all of the sample code provided only shows you how to create line charts. If you visit the wiki for NPlot you will only find a plea for a BarPlot tutorial. I sent an email to one of the NPlot developers asking for some sample code for creating bar charts and he provided enough code for me to figure it out. I contributed several examples of NPlot bar charts in VB.NET and C# using the Northwind database and even one using XML as the data source but these examples have not been added to the wiki. I will provide you with enough information here to create bar charts in case someone is still looking for a solution.

First copy all of the sample code from the 4 Guys From Rolla.com web site article because you only need to change a few lines. The first step is just to change the type of plot object you are creating, obvious enough:

'One Plot for each timeseries:
Dim npPlot1 As New NPlot.BarPlot
Dim npPlot2 As New NPlot.BarPlot

The tricky part that is not documented and which you cannot figure out without a clue is how to assign the data to each axis. It is slightly different from the Line Chart example:

<>'Timeseries 1 to Plot 1:
npPlot1.AbscissaData = X1
npPlot1.OrdinateDataTop = OT1
npPlot1.OrdinateDataBottom = OB1

Here OT1 and OB1 are arrays of double values and X1 is whatever data your X axis uses:

OT1 = New Double() {2, 4, 6, 4, 12, 5, 8, 1, 3, 9}
OB1 =
New Double() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

If you use a table for your datasouce then use the columns for the axis data:

<>npPlot1.DataSource = objDataSet.Tables(0)
'Column names in the dataset were established in the SQL statement
npPlot1.AbscissaData = "AbscissaData"
npPlot1.OrdinateDataTop = "OrdinateDataTop"
npPlot1.OrdinateDataBottom = "OrdinateDataBottom"

Free Image Hosting at www.ImageShack.us

6 Comments

Comments have been disabled for this content.