2

VB 2008 .NET FRAMEWORK 3.5 MSCHART - FASTLINE CHART TYPE

Is it possible to have an MS Chart control contain 20,000 data points, but only show the last 100?

I know that I can select the last 100 from my datatable and use it as a datasource.

Chart1.DataSource = cMs2.dsData.Tables("readings").Select(wFilter, wSort).Take(100)

That's not what I want.

I know that I can populate an array or collection with the last 100 data points and use it as a data source.

Chart1.Series("readings").Points.DataBindXY(colCtr, colReadings)

That's not what I want.

I need to do 1 of 2 things:

  1. Manually add data points and be able to show only the last 100 or last 1000 of them that just came in. This must be done without re-populating the chart. Just show a portion of the complete set of data points.

    wSample = wSample + 1
    
    Chart1.Series("readings").Points.AddXY(wSample, wReading)
    
    Chart1.Series("readings").SHOWONLYTHELAST100DATAPOINTSWITHOUTCLEARING
    
  2. Initialize a chart with a certain number of the total readings with a databind, then manually add new data points one at a time while removing the oldest data point. For example, initialize the chart with 100 data points, then add a new data point, remove the first data point, getting us back to 100. (I'm successfully doing this one, except the chart doesn't behave like I expect. The chart grows, remaining blank/empty where the 'removed' data points were. I do chart.update but it doesn't refresh it.) Note that I am allowed to take more time to initialize the chart (clear/populate), I don't have that time to do it as each new data point comes in.

    wSample = wSample + 1
    
    Chart1.Series("readings").Points.AddXY(wSample, wReading)
    
    If Chart1.Series("readings").Points.Count > 100 Then
    
        Chart1.Series("readings").Points.RemoveAt(0)
    
        Chart1.Update()
    
    End If
    

NOTE: Doing a process that causes me to have to clear and rebind the data to handle the addition of a single data point causes me problems because it takes too long. I'm just looking for the quickest, most efficient way to handle this. Thank you for taking the time to read this...!

Mazdak
  • 105,000
  • 18
  • 159
  • 188
user3670107
  • 23
  • 1
  • 3
  • 1
    Welcome to the control of dirty hacks. Try setting the Minimum/Maximum of the XAxis with a Linq query (method 2) instead of `Chart1.Update`, e.g. (same with `.Max` for `Maximum`): `Chart1.ChartAreas(0).AxisX.Minimum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Min` – Jens Aug 30 '14 at 19:46
  • Thanks, that is very helpful. I didn't do the linq query yet, but I set the .Minimum to (Total Data Points minus 10) which worked perfectly. I was making it too complicated thinking that the number of data points needed to equal the number that I see in the chart. Thanks for the good solution. - Jeff – user3670107 Sep 01 '14 at 18:03
  • Good to hear. I have added my comment as an answer. – Jens Sep 01 '14 at 20:08

1 Answers1

1

You can manually set the Minimum and Maximum values of each axis by modifying the .Minimum and .Maximum values of the axes like

Chart1.ChartAreas(0).AxisX.Minimum = 100 'Example value
Chart1.ChartAreas(0).AxisX.Minimum = 200 'Example value

As discussed in the comments to your question you can either use your method #2 (add datapoint and delete datapoint index 0 and then select the new minimum and maximum X-Value from the series with LinQ:

Chart1.ChartAreas(0).AxisX.Minimum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Min
Chart1.ChartAreas(0).AxisX.Maximum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Max

or you can, as you have now done, just set the minimum value to a X-value some points before the last point.

Jens
  • 6,275
  • 2
  • 25
  • 51
  • Note that my X value is simply a counter which corresponds to a distance (1 foot at a time.) So, I am able to simply subtract 100 from the number of data points and get my last 100. Am not understanding your linq query... Can you provide a query that would correctly set the min to show the last 100? Note also that I've changed my logic to not remove any data points, because getting the correct min value solved my problem. (Will be reading up on linq, but seeing a working example is most helpful.) - Jeff – user3670107 Sep 02 '14 at 15:18