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.
SweetingAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.