Results 1 to 9 of 9

Thread: [RESOLVED] Chart Control Scale VB 2012

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Resolved [RESOLVED] Chart Control Scale VB 2012

    I'm using the stock chart control in vb 2012 and getting the data from an MS Access dB.

    Code:
    SQL = "SELECT Fuel.DatePurchased as [Date], Fuel.PPG FROM Fuel " &
            "WHERE (Fuel.DatePurchased BETWEEN #" & frmMain.dtFuelStart.Value & "# AND #" &
            frmMain.dtFuelEnd.Value & "#) AND (Fuel.VehicleID = " & VehicleID &
            ") ORDER BY Fuel.DatePurchased"
            GetData()
    
            Chart1.Series.Clear()
            Chart1.Titles.Add("Fuel PPG")
            Dim s As New Series
            s.Name = "PPG"
            
            s.ChartType = SeriesChartType.Line
            For x = 0 To TABLE.Rows.Count - 1
                s.Points.AddXY(TABLE.Rows(x).Item("Date"), TABLE.Rows(0).Item("PPG"))
            Next
            'Add the series to the Chart1 control.
            Chart1.Series.Add(s)
    The getdata statement fills a datatable using the sql and works fine. I'm getting two fields - the date fuel was purchased and the price per gallon at the time. This yields the following graph:

    Name:  graph.jpg
Views: 1879
Size:  28.0 KB

    There are only 4 points for testing - 3.329, 3.359, 3.179 and 3.319. Obviously all are very close in value. I want the range of the y axis on the chart to be between say 3 and 3.5 with the scale much finer to show a more detailed line graph of the data. I've searched on the net but nothing works ... this has to be really easy to do if I only knew how.

    TIA
    Ken

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Chart Control Scale VB 2012

    To auto scale the y axis, set the min and max value to Double.NaN. That will scale the axis based on you data which in you case is 3.179 to 3.359. If you want to scale the axis using 2 nice values (3 and 3.5) you will need to manually set the the min and max values using some rounding algorithm you create.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Chart Control Scale VB 2012

    In addition to Kevin's info on auto-scaling I would also suggest that you set the axis property "IsStartedFromZero" to false. If not, the minimum value will default to zero for your data.

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Chart Control Scale VB 2012

    Quote Originally Posted by TnTinMN View Post
    In addition to Kevin's info on auto-scaling I would also suggest that you set the axis property "IsStartedFromZero" to false. If not, the minimum value will default to zero for your data.
    From the Axis.Minimum MSDN we find this,
    Quote Originally Posted by msdn
    Also, if the Minimum value is explicitly set, the IsStartedFromZero property will be ignored.
    Whether setting it to Double.Nan is deemed to be explicit, I don't know.

    You can also chart the data without looping the rows by binding your datatable to the chart...

    Code:
    s.XValueMember = "Date"
    s.YValueMember = "PPG"
    Chart1.DataSource = TABLE
    Chart1.Databind
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Chart Control Scale VB 2012

    Quote Originally Posted by kebo View Post
    From the Axis.Minimum MSDN we find this,
    Whether setting it to Double.Nan is deemed to be explicit, I don't know.
    You did not read far enough in your link. The default value is NaN, so as far as it could discern, it is not set.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: Chart Control Scale VB 2012

    Thanks for the information guys. What I ended up doing was this:

    Code:
    Dim s As New Series
            s.Name = "PPG"
            s.ChartType = SeriesChartType.Line
    
            Dim mn As Double = 1000
            Dim mx As Double = 0
            Dim ppg As Double
            Dim dt As Date
            For x = 0 To TABLE.Rows.Count - 1
                ppg = TABLE.Rows(x).Item("PPG")
                dt = TABLE.Rows(x).Item("Date")
                If ppg < mn Then mn = ppg
                If ppg > mx Then mx = ppg
                s.Points.AddXY(dt, ppg)
            Next
    
            mn = Format(mn - 0.2, "#.###")
            mx = Format(mx + 0.2, "#.###")
            Chart1.ChartAreas(0).AxisY.Minimum = mn
            Chart1.ChartAreas(0).AxisY.Maximum = mx
    
            'Add the series to the Chart1 control.
            Chart1.Series.Add(s)
    Name:  image.jpg
Views: 1913
Size:  31.5 KB

    I was fiddling with changing the type of chart with a combo but never got that figured out so ignore it.

    Basically iterated through the data, found the low and high then set min and max for the chart to slightly more/less and formatted the double to 3 decimal places. I can adjust the .2 offset to less to get a more graphlike line as the data is so close. I stumbled on this early this morning so I haven't fiddled with the autoscaling however it appears that it must be ignored when min and max is set.

    When trying to bind the data as kebo suggested...
    Code:
    Dim s As New Series
            s.XValueMember = "Date"
            s.YValueMember = "PPG"
            Chart1.DataSource = TABLE
            Chart1.DataBind()
    Results in an error on the YValueMember being unable to resolve the symbol. YValueMember is not an option but YValueMembers (plural) is .. however choosing it results in a blank chart. I fiddled with binding the table to the chart earlier and never could get anything but a blank chart.

    Next I want the dates to appear vertically but nothing I've tried works for that yet either - just a blank chart. I would also like the chart to show only the actual dates of purchase rather than adding one before and one after .. another mystery so far. Sometimes MSDN helps and other times it's quite useless.

    Thank you for all your suggestions and interest. This is a great site when you get bamboozled by something!
    Ken

  7. #7
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Chart Control Scale VB 2012

    Quote Originally Posted by SparrowHawk7 View Post
    When trying to bind the data as kebo suggested...
    Code:
    Dim s As New Series
            s.XValueMember = "Date"
            s.YValueMember = "PPG"
            Chart1.DataSource = TABLE
            Chart1.DataBind()
    Results in an error on the YValueMember being unable to resolve the symbol. YValueMember is not an option but YValueMembers (plural) is .. however choosing it results in a blank chart. I fiddled with binding the table to the chart earlier and never could get anything but a blank chart.
    You need to specify the field name from your DataTable. Based on your select statement, that would be "DatePurchased" not "Date".

    To set the angle of X-Axis label you can do something like this
    Code:
    Chart1.ChartAreas(s.ChartArea).AxisX.LabelStyle.Angle = -45

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: [RESOLVED] Chart Control Scale VB 2012

    The angle works great .. thank you. Sometimes the syntax on things is not exactly intuitive.

    The binding is still the same problem. I used an alias in the sql statement for Date .. but the problem lies with the PPG (YValueMember) not being available. I've been using this basic method for a long time with my combos but things just don't seem to work as I would expect with the chart control.

    That's a big help with the angles!!

  9. #9
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: [RESOLVED] Chart Control Scale VB 2012

    I glazed right over that issue, it is YValueMembers. Also make sure that "Table" is filled with data before you assign it to the DataSource. Chart databinding is different from that of binding to say a DataGridView in that changes are not automatically propagated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width