• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

Set firsta and last data points on vb chart to zero

Thanks to Robert i can now hide a datapoint label on the x axis of a vb chart.

    Private Sub Chart1_Customize(sender As System.Object, e As System.EventArgs) Handles Chart1.Customize
        If Chart1.ChartAreas(0).AxisX.CustomLabels.Count > 1 Then
            Chart1.ChartAreas(0).AxisX.CustomLabels.RemoveAt(Chart1.ChartAreas(0).AxisX.CustomLabels.Count - 1)
            Chart1.ChartAreas(0).AxisX.CustomLabels.RemoveAt(0)
        End If
    End Sub

But for some applications i need to not only hide the label but the bar itself or set the first and last data points in aseries to zero (the same positions aat which the labels are removed).

All help most welcome.

Thsnyou for your patience.
0
SweetingA
Asked:
SweetingA
  • 5
  • 2
1 Solution
 
Robert SchuttSoftware EngineerCommented:
One important piece of information from your previous question is that you use a DataSource. http:/Q_28488496.html

That being the case, you can't directly change the Points values, which might otherwise have been the solution you need.

However, you can change the underlying data:
        For Each dr As DataRow In Sql.SQLDataSet.Tables("qry_OEE_Weekly").Select(String.Format("Year = {0}", Chart1.ChartAreas(0).AxisX.Minimum))
            dr("OEE") = 0
        Next

Open in new window

Then again, when I asked you if it was possible to filter the data up front, I forgot about a similar option that makes it easy to set the Chart DataSource to only the subset of data you need from the dataset you have now. All you need to do is move your start/end year variables before the line that sets the DataSource and then adjust that line to select only the years that you want to show in the Chart. In this situation you wouldn't need to set the x axis minimum/maximum anymore by the way.
        Dim sd As Integer
        Dim ed As Integer
        sd = Date.Today.Year
        ed = sd - 10
        Chart1.DataSource = Sql.SQLDataSet.Tables("qry_OEE_Weekly").Select(String.Format("Year >= {0} AND Year <= {1}", ed, sd))

Open in new window

0
 
SweetingAAuthor Commented:
Hmm...can;t do either of those.....

I want to keep the chart view the same always (i.e. the number of data points on the x axis) independant of whether there is data to display for those data points so the 2nd solution would not work.

The 1st one will work if i creat and temp table and do it in there rather than deleting data in main source table, i will have a little play around and see if i can get it to work.
0
 
Robert SchuttSoftware EngineerCommented:
Just thought of another option: do it in the Customize function! The Points array is initialized by then, just like the CustomLabels property.

This is probably not what you need (because your array contains more points) but for a first idea:
    Private Sub Chart1_Customize(sender As System.Object, e As System.EventArgs) Handles Chart1.Customize
        If Chart1.ChartAreas(0).AxisX.CustomLabels.Count > 1 Then
            Chart1.ChartAreas(0).AxisX.CustomLabels.RemoveAt(Chart1.ChartAreas(0).AxisX.CustomLabels.Count - 1)
            Chart1.ChartAreas(0).AxisX.CustomLabels.RemoveAt(0)
        End If
        ' add this to remove points as well
        If Chart1.Series(0).Points.Count > 1 Then
            Chart1.Series(0).Points.RemoveAt(Chart1.Series(0).Points.Count - 1)
            Chart1.Series(0).Points.RemoveAt(0)
        End If
    End Sub

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Robert SchuttSoftware EngineerCommented:
maybe this'll do:
        ' add this to remove points as well
        For p As Integer = Chart1.Series(0).Points.Count - 1 To 0 Step -1
            If Chart1.Series(0).Points(p).XValue = Chart1.ChartAreas(0).AxisX.Minimum Or Chart1.Series(0).Points(p).XValue = Chart1.ChartAreas(0).AxisX.Maximum Then Chart1.Series(0).Points.RemoveAt(p)
        Next

Open in new window

I thought there was a GetPointByXValue or something but haven't found it yet ;)
0
 
Robert SchuttSoftware EngineerCommented:
Aha, found it... This would be the 'nicer' version but probably does practically the same:
        If Chart1.Series(0).Points.Count > 0 Then
            Chart1.Series(0).Points.Remove(Chart1.Series(0).Points.FindByValue(Chart1.ChartAreas(0).AxisX.Maximum, "X"))
            Chart1.Series(0).Points.Remove(Chart1.Series(0).Points.FindByValue(Chart1.ChartAreas(0).AxisX.Minimum, "X"))
        End If

Open in new window

0
 
SweetingAAuthor Commented:
Worked perfectly, thanks - i tried playing around with the SQL statement and could similar end result but i prefer this.
0
 
Robert SchuttSoftware EngineerCommented:
After seeing your new question I wanted to share some more info and a warning.

I see you're using the Customize event for several charts. If you're always going to process the charts in exactly the same way, there is no need to define all the events separately. You can define one event handler for all charts:
    Private Sub Chart_Customize(sender As System.Object, e As System.EventArgs) Handles Chart1.Customize ', Chart2.Customize
        Dim ch As Chart = CType(sender, Chart)
        If ch.ChartAreas(0).AxisX.CustomLabels.Count > 1 Then
            ch.ChartAreas(0).AxisX.CustomLabels.RemoveAt(ch.ChartAreas(0).AxisX.CustomLabels.Count - 1)
            ch.ChartAreas(0).AxisX.CustomLabels.RemoveAt(0)
        End If
        If ch.Series(0).Points.Count > 0 Then
            ch.Series(0).Points.Remove(ch.Series(0).Points.FindByValue(ch.ChartAreas(0).AxisX.Maximum, "X"))
            ch.Series(0).Points.Remove(ch.Series(0).Points.FindByValue(ch.ChartAreas(0).AxisX.Minimum, "X"))
        End If
    End Sub

Open in new window

So the commented out bit ", Chart2.Customize" can be repeated for each chart object. The first thing this code does is determine which actual chart object it needs to handle.

And then a bit of a warning: after stepping through the code in the debugger I noticed something I hadn't realized before: the CustomLabels are initialized each time and indeed need to be removed each time the chart is regenerated (for example when resizing). But the data points that are removed are not 'refreshed' so after the first time the 2 Remove calls are actually called with an argument of Nothing. In this case maybe not relevant but when I noticed I thought I should let you know anyway.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now