.NET Framework Bookmark and Share   
 index > Chart Controls for .NET Framework > Chart controls - text histogram
 

Chart controls - text histogram

Hi
I'm a completly beginner with MS Chart Controls for VB, and looks like i cannot find answer for my question in examples :

I'd like to have a chart with 'histogram' of string from my database . For example : 50%of records in your database have Name : Andy, 20% Mike , and so on.


I'll be thanfull for any hints how to make it or just links to some examples ;
es111
I've tried with DataBindCrossTab but it looks like thats not what i was looking for ...

anyone, anything ?:)
es111
Have you downloaded the Chart Sample Environment from here: http://code.msdn.microsoft.com/mschart. It is a great place to start.

Alex.
http://blogs.msdn.com/alexgor
Alex Gorev
Yes I have, is there any direct example to what I'm looking for? , couse i cannot see so far any ;/
es111

I could only find a direct example with the names Andy, Pete and Bob.. No Mike, sorry.

Let's say you've managed to install chart controls and you're using some fairly new version of visual studio.

Create a new (Visual Basic) Windows Froms Application.

There should be a Form1 ready for you in the project.

Open that form and add a Chart from the toolbox to the form in the design view.

Open the code of the form the menu View -> Code (or press F7) and just paste the following on top everything that's already there.

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		'Init chart
		'You don't have to do this if you use the default values.
		Chart1.Legends.Clear()
		Chart1.ChartAreas.Clear()
		Chart1.Series.Clear()

		'Create a chartarea
		Dim area As New ChartArea("AREA")
		'To have a percentage on the y-axis
		area.AxisY.Minimum = 0
		area.AxisY.Maximum = 100
		Chart1.ChartAreas.Add(area)

		'Create a series
		Dim series As New Series("SERIES")
		series.ChartArea = "AREA"
		'It won't be a real histogram, since it doesn't group any x-values, there won't be 
		'any empty points on the x-axis and the y-axis won't indicate the amount of names.
		'You most likely don't need a histogram here anyway.
		'So, we just use the column type.
		series.ChartType = SeriesChartType.Column
		'To show the label on top of the column.
		series.IsValueShownAsLabel = True
		series.LabelFormat = "{0:0}%"
		Chart1.Series.Add(series)

		'You probably have a query looking something like this.
		Dim sql As String = _
		"SELECT name, COUNT(name) AS amount FROM sometable GROUP BY name ORDER BY name"

		'getDataToDataTable is just some function (that you have to implemet) 
		'that returns a datatable with the selected data.
		'Use some other datatype if you don't want to use a datatable.
		Dim dTable As DataTable = getDataToDataTable(sql)

		'Find the total amount of names in your data
		Dim total As Integer = 0
		For Each row As DataRow In dTable.Rows
			total += CInt(row("amount"))
		Next

		Dim percentOfTotal As Double
		For Each row As DataRow In dTable.Rows
			'You could also bind the data to the chart, but this way you can manipulate the data 
			'in a straightforward way and might get some sense of what's happening.
			percentOfTotal = row("amount") / total * 100
			series.Points.AddXY(row("name"), percentOfTotal)
		Next

	End Sub

	Private Function getDataToDataTable(ByVal sql As String) As DataTable
		'This is just to get example data.
		'Normally you'd connect to your database and fetch the data from there.

		Dim dtable As DataTable = New DataTable

		dtable.Columns.Add("name")
		dtable.Columns.Add("amount")

		Dim row As DataRow

		row = dtable.NewRow
		row("name") = "Andy"
		row("amount") = 25
		dtable.Rows.Add(row)

		row = dtable.NewRow
		row("name") = "Pete"
		row("amount") = 10
		dtable.Rows.Add(row)

		row = dtable.NewRow
		row("name") = "Bob"
		row("amount") = 15
		dtable.Rows.Add(row)

		Return dtable

	End Function

End Class

Now press play on tape.

Someone please make a sticky FAQ-thread to this forum.

sipla

You can use google to search for other answers

Custom Search

More Threads

• In the Crystal Report, change the data displayed at runtime
• Pie Chart ToolTip Not Working Need Help
• Retain palette/custom palette when using PieCollectedDataHelper
• need to create a very simple bar chart
• Integers grouping
• X Axis zoom
• Does the install touch the .Net framework?
• Cross-Table Data Binding
• DataManipulator.InsertEmptyPoints Does it work?
• chart control will not dispay in a multiview view when not in opening view