public Graph(Canvas drawingCanvas) { _drawCanvas = drawingCanvas; _height = DesiredHeight; _width = DesiredWidth; _origin = new Vec2(); _incrementX = 1; _incrementY = 1; if (_yRange == 0) { _aspect = _width / _height; _xStart = -NumberOfIntervals; _xEnd = NumberOfIntervals; _yStart = -NumberOfIntervals / _aspect; _yEnd = NumberOfIntervals / _aspect; } _templineChunk.SetColour(Brushes.Gray); _penlineChunk.SetColour(Brushes.Black); _dataLineChunk.SetColour(Brushes.Red); _dataLineChunk.LineThickness = 2.0f; }
//Draws the background gridLightLines on the canvas. private void GridLightLines() { var lineChunk = new LineChunk(); lineChunk.SetColour(Brushes.LightGray); //Draw faint grid LightLines: for (double i = 0; i <= _xRange + _incrementX; i += _incrementX) { if (!((-_startOffSetX + i) / _incrementX * _xInterval >= 0) || !((-_startOffSetX + i) / _incrementX * _xInterval <= _width)) { continue; } var newLightLine = new LightLine { X1 = (-_startOffSetX + i) / _incrementX * _xInterval, Y1 = 0, X2 = (-_startOffSetX + i) / _incrementX * _xInterval, Y2 = _height }; lineChunk.Add(newLightLine); } for (var i = _yRange; i >= -_incrementY; i -= _incrementY) { if (!((_startOffSetY + i) / _incrementY * _yInterval >= 0) || !((_startOffSetY + i) / _incrementY * _yInterval <= _height)) { continue; } var newLightLine = new LightLine { X1 = 0, Y1 = (_startOffSetY + i) / _incrementY * _yInterval, X2 = _width, Y2 = (_startOffSetY + i) / _incrementY * _yInterval }; lineChunk.Add(newLightLine); } _chunks.Add(lineChunk); }
//Draws X and y axes. private void Axes() { //The faint grid is drawn first to minimize overlapping of canvas objects. var lineChunk = new LineChunk(); lineChunk.SetColour(Brushes.Black); //Draw increment LightLines and axes for each interval: //If the yStart and yEnd Values are both less than zero the X axis is drawn at the top of the canvas. for (var i = -_startOffSetX; i <= _xRange + _incrementX; i += _incrementX) { if (!(i / _incrementX * _xInterval >= 0) || !(i / _incrementX * _xInterval <= _width)) { continue; } var incrementLightLine = new LightLine { X1 = i / _incrementX * _xInterval, Y1 = _origin.Y + 4, X2 = i / _incrementX * _xInterval, Y2 = _origin.Y - 4 }; lineChunk.Add(incrementLightLine); } var axesLightLine = new LightLine { X1 = 0, Y1 = _origin.Y, X2 = _width, Y2 = _origin.Y }; lineChunk.Add(axesLightLine); //If the xStart and xEnd Values are both less than zero the y axis is drawn at the right of the canvas. for (var i = _yRange + _startOffSetY; i >= -_incrementY; i -= _incrementY) { if (!(i / _incrementY * _yInterval >= 0) || !(i / _incrementY * _yInterval <= _height)) { continue; } var incrementLightLine = new LightLine { X1 = _origin.X + 4, Y1 = i / _incrementY * _yInterval, X2 = _origin.X - 4, Y2 = i / _incrementY * _yInterval }; lineChunk.Add(incrementLightLine); } var axesLightLineY = new LightLine { X1 = _origin.X, Y1 = 0, X2 = _origin.X, Y2 = _height }; lineChunk.Add(axesLightLineY); _chunks.Add(lineChunk); //If the origin is present to be drawn on the canvas a ellipse will be drawn to mark its location. if (_xStart <= 0 && _xEnd >= 0 && _yStart <= 0 && _yEnd >= 0) { _chunks.Add(DrawingMethods.Circle(_origin, 10)); } }