1

Updated the code to give you a better idea of what's happening and to allow the data to load faster. I have still not seen anything posted online that answers this question.

I have a MS Chart control on a C# application (Visual Studio 2010 Express, if that makes a difference). Data loads at a slow realtime rate (once per second). My intent is to display 6 minutes of data at a time, with the ability to scroll to another page of data if necessary. I want the data to fill the chart from the left hand side. As the data is added to the chart, the scroll bar and X axis legends also move over so that I can only see the latest data point. I have to scroll to the left to see earlier data -- and the the scroll bar jumps back when the new data point is added. I want the scroll bar to stay in one place (left edge) unless I move it. I have a 1 second timer on my form and new data is added with every time cycle.

I hope this is sufficient. Any help?

Code to Initialize the Chart control:

DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddMinutes(6);
DateTime maxTime = startTime.AddMinutes(24);
// Bind the chart to the list. 
chartAssociateProductivity.DataSource = Globals.listKohlsPerformanceDataSource;

chartAssociateProductivity.ChartAreas["ChartArea1"].CursorX.AutoScroll = true; // enable autoscroll
chartAssociateProductivity.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = false;
chartAssociateProductivity.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = false;

chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.Minimum = startTime.ToOADate();
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.Maximum = endTime.ToOADate();
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.ScrollBar.IsPositionedInside = true;
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(startTime.ToOADate(), 6, DateTimeIntervalType.Minutes);
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.ScaleView.Position = 0;// startTime.ToOADate();
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.MinorTickMark.Enabled = true;
// disable zoom-reset button (only scrollbar's arrows are available)
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;

// set scrollbar small change to blockSize (e.g. 100)
chartAssociateProductivity.ChartAreas["ChartArea1"].AxisX.ScaleView.SmallScrollSize = 100;

Code to Add the Data (for the example, I will add constant data):

private void timer1_Tick(object sender, EventArgs e)
{
    listPerformanceDataSource.Add(new PerformanceRecord(0, 248));
}

The data structure:

public class PerformanceRecord
{
    int bagCount, goal;
    public PerformanceRecord(int bagCount, int goal)
    {
        this.bagCount = bagCount;
        this.goal = goal;
    }
    public int BagCount
    {
        get { return bagCount; }
        set { bagCount = value; }
    }
    public int Goal
    {
        get { return goal; }
        set { goal = value; }
    }
}

// Create a list. 
public static List<PerformanceRecord> listPerformanceDataSource = new List<PerformanceRecord>();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
MarkAllan
  • 11
  • 1
  • 3
  • 1
    Hm, there a few thing missing, to reproduce this. For one we don't have `RoundUp` (if it matters?) nor is the _code to add data_ comprehensible (at least to me). - My efforts to reproduce by changing and twisting things didn't succeed. Once I set the scrollbar to th left it stayed there. Another problem with reproducing the problem obviously is the time frame: Nobody can test your code if it takes hours (or even 'only' a few minutes) to see the problem.. Can you post a modified version, that is self-contained and reproducible? (Maybe the problem will dissolve while you try to do it .. ;-) – TaW Sep 22 '14 at 23:44
  • Thanks for looking. I will try to update the post today. – MarkAllan Sep 23 '14 at 13:06
  • Post is updated with more detailed information, and it runs a lot faster. Are there any things about the chart control that I need to include? I am still seeing the problem. – MarkAllan Sep 23 '14 at 19:25
  • I think (but not 100% sure) it is just how this control works and this feature cannot be turned off. Your chance will be to scroll programetically back to the previous place whenever you add the new data. But the screen might flicker then. Maybe when you play with Invalidate method you can achieve something acceptable, but I am afraid there is no really simple solution. – Vojtěch Dohnal Sep 23 '14 at 19:58
  • _chartAssociateProductivity.DataSource = Globals.listKohlsPerformanceDataSource;_ Two problems, obviuosly the Kohls is just a tyoe, but how is the databinding defined in the chart? and what charttype is it anyway? – TaW Sep 23 '14 at 20:35
  • Thank you VDohnal. I was afraid of that. – MarkAllan Sep 23 '14 at 21:13
  • Sorry TaW, I neglected to remove the Kohls tag from one line. Not sure how to answer your questions. The chart is the regular microsoft chart control and there is not charttype property, if that's what you are asking. I don't have any databinding defined in the chart that I can find. Do you have a good place for me to look? – MarkAllan Sep 23 '14 at 21:17
  • There is a group in the property tab of the designer; but maybe it is in the code; see [this post's answer](http://stackoverflow.com/questions/18744092/chart-databinding-to-datatable-chart-not-updating) for an example.. – TaW Sep 24 '14 at 12:11
  • I decided to give up on the Chart control. I downloaded ZedGraph from the internet. It works much more like I wanted it and I got it running in a day. Their "documentation" is also terrible, but I was able to find some examples that were helpful. Thanks for your help. – MarkAllan Sep 26 '14 at 16:26

1 Answers1

0
ChartArea chartArea=new ChartArea();
chartArea = chart1.ChartAreas[series.ChartArea]; 
int scrollBarVal=chartArea.AxisX.ScaleView.Position;

you have to make a chartArea instance and use its axis position