I'm trying to make a Zoom Out Function for my mschart chart. My idea is to have a click method for the chart and each time the chart is clicked with the right mouse button the zoom value should change. Therefore the chart should response and have room for more values. I’ve made 4 zoom steps: 240 values, 3600 values, 43’200 values, 86’400. The chart click methodic looks like this:
void chart_holder_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (counter < 3)
{
counter++;
}
else
{
counter = 0;
}
switch (counter)
{
default:
//zoomsize -> 240s | 4min
zoomSize = 0;
graphSizing = 240;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 0:
//zoomsize -> 240s | 4min
zoomSize = 0;
graphSizing = 240;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 1:
//zoomsize -> 3'600s | 1h
zoomSize = 1;
graphSizing = 3600;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 2:
//zoomsize -> 43'200s | 12h
zoomSize = 2;
graphSizing = 43200;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 3:
//zoomsize -> 86'400s | 24h
zoomSize = 3;
graphSizing = 86400;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
}
}
}
The AxisX.Maximum is defined with the property public static int graphSizing { get; set; }
. The chart that I’ve got is a real time chart, so each second there are new values being added etc. TempbufferValuesTOZero, FileValuesTOZero fills the whole array with 0/0.
Before I add the values to the chart I prepare them in this method: I got a switch statement that checks the zoomSize to know how to prepare the Data
public void prepareData()
{
switch (zoomSize)
{
default:
prepData1();
break;
case 0:
prepData1();
break;
case 1:
prepData2();
break;
case 2:
prepData3();
break;
case 3:
prepData4();
break;
}
}
example how its prepared
public void prepData1()
{
graphSizing = 240;
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
for (double i = unixTimestamp; unixTimestamp - graphSizing < i; i--)
{
bool exists = true;
DateTime x = UnixTODateTime(i);
if (exists != (Array.Exists(file, element => element.XValue == i)))
{
TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(x.ToOADate(), 0) }).ToArray();
}
else
{
DataPoint point = Array.Find(file, element => element.XValue == i);
TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(x.ToOADate(), point.YValues) }).ToArray();
}
}
}
After that this method is called to display the values in the chart:
public void fileRead()
{
chart_holder.Series[0].Points.Clear();
foreach (DataPoint a in TempBuffer)
{
chart_holder.Series[0].Points.Add(a);
if (a.YValues[0] == 9999)
{
a.Color = Color.Red;
}
}
}
TempBuffer is an DataPoint Array where I store the values (public DataPoint[] TempBuffer = new DataPoint[graphSizing];
).
But it’s not quiet working like expected. Only the default / 0 case are working perfectly as planned. But as soon as I want to zoom out, it’s not working out anymore. On case 2 the AxisX Values are completely wrong and AxisY Values aren’t even showing up. And on case 3 and 4 the program is crashing. Also the CPU Usage is going very high.
Any idea why the other cases aren’t working out like default / 1? And how can I improve the performance and minimize the high cpu usage?
Also for better visualization of the chart:
240 Values:
Thanks for help
Greetings C.User