Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.Net 2010 Chart How to display date and time separately?

Status
Not open for further replies.

Macka1147

Technical User
Jun 16, 2009
10
AU
Hi All
I am embracing VB.Net 2010.
A far cry from Microsoft Access that I have learned to love, however the new .net Chart tool is fantastic; so many options to choose from.

An existing database has the following fields. It is a stock price chart.
Date, (Formatted as short date) Time (Formatted as time), Price (formatted as currency) in the linked table.

I would like to show time as one x axis legend above the date as it changes from day to day.

The attached codes shows the date correctly retrieved form the data file an Access MDB file. Together with the price charted above so far so good.

The problem lies with the time. I have not found a way to successfully display it as a secondary x axis.

If anyone has an answer to this question it would be most appreciated.

Thank You

Code:
    Private Sub cmdLoadChart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadChart.Click
        ' The Access database
        Dim fileNameString As String
        fileNameString = "C:\Program Files (x86)\Deplenthion\data\ExchangeASX\ASL.mdb"

        ' Initialize a connection string    
        Dim myConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString

        ' Define the database query    
        Dim mySelectQuery As String = "SELECT tblCotPulseData.Date, tblCotPulseData.Price FROM tblCotPulseData ORDER BY tblCotPulseData.Date, tblCotPulseData.[Seq No];"

        ' Create a database connection object using the connection string    
        Dim myConnection As New OleDbConnection(myConnectionString)

        ' Create a database command on the connection using query    
        Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

        ' Open the connection    
        myCommand.Connection.Open()

        ' Create a database reader    
        Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

        ' Since the reader implements IEnumerable, pass the reader directly into
        '   the DataBind method with the name of the Column selected in the query    
        Chart1.Series("Series1").Points.DataBindXY(myReader, "date", myReader, "Price")

        ' Close the reader and the connection
        myReader.Close()
        myConnection.Close()

        ' Zoom into the X axis
        Chart1.ChartAreas("ChartArea1").AxisX.ScaleView.Zoom(10, 10)

        ' Enable range selection and zooming end user interface

        'Chart1.ChartAreas("ChartArea1").AxisX2.Enabled = AxisEnabled.True
        'FIXME how do you male time show separately? 

        'Cursor allows you to select a range
        Chart1.ChartAreas("ChartArea1").CursorX.IsUserEnabled = True
        Chart1.ChartAreas("ChartArea1").CursorX.IsUserSelectionEnabled = True

        'Zoom
        Chart1.ChartAreas("ChartArea1").AxisX.ScaleView.Zoomable = True
        Chart1.ChartAreas("ChartArea1").AxisX.ScrollBar.IsPositionedInside = False

        Chart1.Series(0).XValueType = DataVisualization.Charting.ChartValueType.DateTime

    End Sub
 



Hi,

Although Date & Time are stored separately, in the chart they cannot be decoupled. In realily, THE VALUE that you need to use, is (Date + Time).

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi SkipVoight

The code snip below does actually show a second axis legend The FIXME in the code example.
As is it defaults to the top of the chart however you can reposition it.

However I could not find a way to feed it with data. There is no problem retrieving the data with the SQL criteria I tested the string with another program; however that throws an error with the chart program.

I am making an assumption here; that it assumes it has found another series.
What we need is a way to tell it it is not a series and to feed the second axis.

I searched here:

A very useful resource, alas I could not find the answer to this specific question although it answered most others.

Thank You

Code:
'Chart1.ChartAreas("ChartArea1").AxisX2.Enabled = AxisEnabled.True
 



Nothing stopping you from having the same date/time values on the secondary axis, formatted to display time units. All I'm saying is that the date/time value is the value to use, whether you format as DATE, TIME, Date & Time.

For instance, if you are plotting more than one day's worth of values, how will the secondary, TIME axis know which day a 13:00 time value goes with, if the values are ONLY values between 0:00 and 23:59:59 (0 to <1)?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top