vb.netwinformschartssubsetdatapoint

VB.NET CHART- Display a subset of the total data points


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...!


Solution

  • 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.