none
Some help with scaling a Line chart. RRS feed

  • Question

  • Hi all,
    I'm creating a line chart in a small application and i'm having issues with the scaling side.

    The problem is that the values on the Y-axis don't match the position of the points on the chart. For example a point that is supposed to represent 43 is at 47 according to the Y-axis Label.  I don't know if the scaling is wrong, Yaxis labelling is wrong or what.

    Here is the code for the chart. The first bit of code is using a scaling system I have seem common in a few charting tutorials.

    // ~~  Variables for margins and outlines, etc ~~ 
            // # of pixels Y-Axis is inset from PicBox Left 
            const double LeftMargin = 71; 
            // # of Pixels left unused at right side of PicBox 
            const double RightMargin = 15; 
            // # of pixels above base of picturebox the X-Axis is placed 
            const double BaseMargin = 65; 
            // Margin at Top 
            const double TopMargin = 32; 
     
            // Base Line Length 
            double BaseLine; 
            // spaceing of the points 
            double spacing; 
            // vertical Line Length 
            double VertLine; 
            // Yaxis Scale 
            double YScale; 
            // Y Value 
            double YValue;

     List<Double> ChartData = new List<Double>{0,36.57,37.62,17.41,0,0,38.44,37.63,43.15,33.3,0,0,35.9,36.53,36.25,34.91,0,0,37.04,40,36.61,36.76,0,0,38.52,40.7,36.19,39.03,37.44,0,0,36.95,36.37,39.24,37.16,35.65,0,35.62,41.05,0,0,0,0};
     
    private void DrawGraph(double _Height, double _Width) 
            { 
                // Clear the Chart 
                ChartGrid.Children.Clear(); 
                // Draw the Axis and stuff 
                DrawOutLine(_Height,_Width);                         
                // Get the starting position for the X-Axis 
                // for drawing points 
                double StartX = LeftMargin + (spacing / 2); 
     
                Polyline ChartLine = new Polyline(); 
                PointCollection pc = new PointCollection();              
     
                for (int No = 0; No < ChartData.Count; No++) 
                {                 
                    // Calculate point value. 
                    double yVal = ((ChartData[No] * 100 / YValue)) * 
                            (VertLine) / 100; 
     
                    pc.Add(new Point(StartX, yVal)); 
     
                    StartX += spacing; 
                } 
                 
                // Flip the lines so the chart is not upside down 
                RotateTransform Rot = new RotateTransform(180); 
                ScaleTransform Scal = new ScaleTransform(-1, 1); 
                TranslateTransform Trans = new TranslateTransform(0, VertLine + TopMargin); 
                TransformGroup transGrp = new TransformGroup(); 
                transGrp.Children.Add(Rot); 
                transGrp.Children.Add(Scal); 
                transGrp.Children.Add(Trans); 
     
                ChartLine.RenderTransform = transGrp; 
                 
                ChartLine.Points = pc; 
                ChartLine.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                ChartGrid.Children.Add(ChartLine); 
            } 
     
            private void DrawOutLine(double _Height, double _Width) 
            { 
                try 
                { 
                    // Get the BaseLineLength 
                    BaseLine = _Width - (LeftMargin + RightMargin); 
                    // Get the spaceing of the points 
                    spacing = (BaseLine / 42); 
                    // Get the length of the vertical Line 
                    VertLine = _Height - (BaseMargin + TopMargin); 
     
                    YValue = ChartData.Max() + (ChartData.Max() / 2);    
     
                    // Set the scale factor for y-axis marker. 
                    double scaleFactor = 11; 
     
                    // this value is used to increment the y-axis marker value. 
                    double yMarkerValue = Math.Ceiling(YValue / scaleFactor); 
     
                    // This value is used to increment the y-axis marker location. 
                    double scale = 5;  // default value 5. 
     
                    // get the scale based on the current max y value and other chart element area adjustments. 
                    scale = (((float)yMarkerValue * 100 / YValue)) * 
                        (VertLine) / 100; 
     
                    // Draw the Y-Axis Line 
                    Line YAxis = new Line(); 
                    YAxis.X1 = LeftMargin; 
                    YAxis.Y1 = TopMargin; 
                    YAxis.X2 = LeftMargin; 
                    YAxis.Y2 = TopMargin + VertLine; 
                    YAxis.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                    ChartGrid.Children.Add(YAxis); 
     
                    // Draw the X-Axis 
                    Line XAxis = new Line(); 
                    XAxis.X1 = LeftMargin; 
                    XAxis.Y1 = YAxis.Y2; 
                    XAxis.X2 = LeftMargin + BaseLine; 
                    XAxis.Y2 = YAxis.Y2; 
                    XAxis.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                    ChartGrid.Children.Add(XAxis); 
     
                    // Now draw the ticks for the yaxis and 
                    // the labels 
                    double YGap = VertLine / 10; 
     
                    double YPos = YAxis.Y2; 
     
                    for (int i = 1; i <= 11; i++) 
                    { 
                        // Draw Ticks 
                        Line YTick = new Line(); 
                        YTick.X1 = LeftMargin - 4; 
                        YTick.Y1 = YPos; 
                        YTick.X2 = LeftMargin; 
                        YTick.Y2 = YPos; 
                        YTick.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                        ChartGrid.Children.Add(YTick); 
     
                        // Draw label with value 
                        Label YValueText = new Label(); 
                        YValueText.Content = Math.Round((yMarkerValue * i),2).ToString(); 
                        YValueText.FontSize = 10; 
                        ChartGrid.Children.Add(YValueText); 
     
                        Canvas.SetTop(YValueText, YPos - 10); 
                        Canvas.SetLeft(YValueText, LeftMargin - 35); 
     
                        YPos -= YGap; 
                    } 
     
                    // Now draw the ticks on the Xaxis 
                    double XPos = LeftMargin; 
     
                    for (int i = 0; i <= 42; i++) 
                    { 
                        Line XTick = new Line(); 
                        XTick.X1 = XPos; 
                        XTick.Y1 = YAxis.Y2; 
                        XTick.X2 = XPos; 
     
                        if (i % 6 != 0) 
                            XTick.Y2 = YAxis.Y2 + 4; 
                        else 
                            XTick.Y2 = YAxis.Y2 + 10; 
     
                        XTick.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                        ChartGrid.Children.Add(XTick); 
     
                        XPos += spacing; 
                    } 
                } 
                catch (Exception ex) 
                { 
                    MessageBox.Show("Error drawing graph outline.\n" + ex.Message); 
                } 
            } 

    The next bit of code is that same as above but with my scaling.

    // ~~  Variables for margins and outlines, etc ~~ 
    // # of pixels Y-Axis is inset from PicBox Left 
    const double LeftMargin = 71; 
    // # of Pixels left unused at right side of PicBox 
    const double RightMargin = 15; 
    // # of pixels above base of picturebox the X-Axis is placed 
    const double BaseMargin = 65; 
    // Margin at Top 
    const double TopMargin = 32; 
     
    // Base Line Length 
    double BaseLine; 
    // spaceing of the points 
    double spacing; 
    // vertical Line Length 
    double VertLine; 
    // Yaxis Scale 
    double YScale; 
    // Y Value 
    double YValue; 

     List<Double> ChartData = new List<Double>{0,36.57,37.62,17.41,0,0,38.44,37.63,43.15,33.3,0,0,35.9,36.53,36.25,34.91,0,0,37.04,40,36.61,36.76,0,0,38.52,40.7,36.19,39.03,37.44,0,0,36.95,36.37,39.24,37.16,35.65,0,35.62,41.05,0,0,0,0};
     

    private void DrawGraph(double _Height, double _Width) 
            { 
                ChartGrid.Children.Clear(); 
     
                DrawOutLine(_Height,_Width);                         
                 
                double StartX = LeftMargin + (spacing / 2); 
     
                Polyline ChartLine = new Polyline(); 
                PointCollection pc = new PointCollection();              
     
                for (int No = 0; No < ChartData.Count; No++) 
                { 
                    //double yVal = ChartData[No] * YScale;                 
     
                    pc.Add(new Point(StartX, yVal)); 
     
                    StartX += spacing; 
                } 
     
                RotateTransform Rot = new RotateTransform(180); 
                ScaleTransform Scal = new ScaleTransform(-1, 1); 
                TranslateTransform Trans = new TranslateTransform(0, VertLine + TopMargin); 
                 
                TransformGroup transGrp = new TransformGroup(); 
                transGrp.Children.Add(Rot); 
                transGrp.Children.Add(Scal); 
                transGrp.Children.Add(Trans); 
     
                ChartLine.RenderTransform = transGrp; 
                 
                ChartLine.Points = pc; 
                ChartLine.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                ChartGrid.Children.Add(ChartLine); 
            } 
     
            private void DrawOutLine(double _Height, double _Width) 
            { 
                try 
                { 
                    // Get the BaseLineLength 
                    BaseLine = _Width - (LeftMargin + RightMargin); 
                    // Get the spaceing of the points 
                    spacing = (BaseLine / 42); 
                    // Get the length of the vertical Line 
                    VertLine = _Height - (BaseMargin + TopMargin); 
     
                    YValue = ChartData.Max() +(ChartData.Max() / 2);                
     
                    double YMarkerValue = YValue / 11; 
     
                    double Scale = VertLine / YValue; 
     
                    // Draw the Y-Axis Line 
                    Line YAxis = new Line(); 
                    YAxis.X1 = LeftMargin; 
                    YAxis.Y1 = TopMargin; 
                    YAxis.X2 = LeftMargin; 
                    YAxis.Y2 = TopMargin + VertLine; 
                    YAxis.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                    ChartGrid.Children.Add(YAxis); 
     
                    // Draw the X-Axis 
                    Line XAxis = new Line(); 
                    XAxis.X1 = LeftMargin; 
                    XAxis.Y1 = YAxis.Y2; 
                    XAxis.X2 = LeftMargin + BaseLine; 
                    XAxis.Y2 = YAxis.Y2; 
                    XAxis.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                    ChartGrid.Children.Add(XAxis); 
     
                    // Now draw the ticks for the yaxis and 
                    // the labels 
                    double YGap = VertLine / 10; 
     
                    double YPos = YAxis.Y2; 
     
                    for (int i = 1; i <= 11; i++) 
                    { 
                        // Draw Ticks 
                        Line YTick = new Line(); 
                        YTick.X1 = LeftMargin - 4; 
                        YTick.Y1 = YPos; 
                        YTick.X2 = LeftMargin; 
                        YTick.Y2 = YPos; 
                        YTick.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                        ChartGrid.Children.Add(YTick); 
     
                        // Draw label with value 
                        Label YValueText = new Label(); 
                        YValueText.Content = Math.Round((yMarkerValue * i),2).ToString(); 
                        YValueText.FontSize = 10; 
                        ChartGrid.Children.Add(YValueText); 
     
                        Canvas.SetTop(YValueText, YPos - 10); 
                        Canvas.SetLeft(YValueText, LeftMargin - 35); 
     
                        YPos -= YGap; 
                    } 
     
                    // Now draw the day ticks on the Xaxis 
                    double XPos = LeftMargin; 
     
                    for (int i = 0; i <= 42; i++) 
                    { 
                        Line XTick = new Line(); 
                        XTick.X1 = XPos; 
                        XTick.Y1 = YAxis.Y2; 
                        XTick.X2 = XPos; 
     
                        if (i % 6 != 0) 
                            XTick.Y2 = YAxis.Y2 + 4; 
                        else 
                            XTick.Y2 = YAxis.Y2 + 10; 
     
                        XTick.Stroke = new SolidColorBrush(Colors.DarkBlue); 
                        ChartGrid.Children.Add(XTick); 
     
                        XPos += spacing; 
                    } 
                     
                } 
                catch (Exception ex) 
                { 
                    MessageBox.Show("Error drawing graph outline.\n" + ex.Message); 
                } 
            } 

    Thursday, October 16, 2008 2:23 PM

Answers

  • Okay, finally I make the code working as you'd expect, I have to say I need to refine your code, since it's hard to read:

    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Shapes;
    using System.Collections.Generic;
    using System.Windows.Media;

    namespace Line_Chart
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            // ~~  Variables for margins and outlines, etc ~~
            // # of pixels Y-Axis is inset from PicBox Left
            const double LeftMargin = 71;
            // # of Pixels left unused at right side of PicBox
            const double RightMargin = 15;
            // # of pixels above base of picturebox the X-Axis is placed
            const double BaseMargin = 65;
            // Margin at Top
            const double TopMargin = 32;

            // Base Line Length
            double BaseLine;
            // spaceing of the points
            double spacing;
            // vertical Line Length
            double VertLine;
            // Yaxis Scale
            double YScale;
            // Y Value
            double YValue;

            List<Double> ChartData = new List<Double> { 0, 36.57, 37.62, 17.41, 0, 0, 38.44, 37.63, 43.15, 33.3, 0, 0, 35.9, 36.53, 36.25, 34.91, 0, 0, 37.04, 40, 36.61, 36.76, 0, 0, 38.52, 40.7, 36.19, 39.03, 37.44, 0, 0, 36.95, 36.37, 39.24, 37.16, 35.65, 0, 35.62, 41.19, 0 };

            public Window1()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(Window1_Loaded);
            }

            void Window1_Loaded(object sender, RoutedEventArgs e)
            {
                DrawGraph(400, 600);
            }

            /// <summary>
            /// Draw the chart
            /// </summary>
            /// <param name="_Height">Height of chart</param>
            /// <param name="_Width">Width of chart</param>
            private void DrawGraph(double _Height, double _Width)
            {
                // Clear the canvas to redraw the chart
                ChartCanvas.Children.Clear();
                // Draw the outline of the chart
                DrawOutLine(_Height, _Width);
                // Set the start postion of the X cord for drawing
                // the line chart
                double StartX = LeftMargin + (spacing / 2);

                // Create the line chart using poly line
                Polyline ChartLine = new Polyline();
                PointCollection pc = new PointCollection();

                for (int No = 0; No < ChartData.Count; No++)
                {
                    double yVal = ChartData[No] * YScale; // My scaling
                    pc.Add(new Point(StartX, yVal));

                    StartX += spacing;
                }

                // Flip and move the line data so its 0,0 is bottom not top
                ScaleTransform Scal = new ScaleTransform(1, -1);
                TranslateTransform Trans = new TranslateTransform(0, VertLine + TopMargin);

                TransformGroup transGrp = new TransformGroup();
                transGrp.Children.Add(Scal);
                transGrp.Children.Add(Trans);

                ChartLine.RenderTransform = transGrp;

                // Add point to polyline and add to canvas
                ChartLine.Points = pc;
                ChartLine.Stroke = new SolidColorBrush(Colors.Red);
                ChartCanvas.Children.Add(ChartLine);
            }

            /// <summary>
            /// Draw the Axis, ticks etc...
            /// </summary>
            /// <param name="_Height">Height of chart</param>
            /// <param name="_Width">Width of Chart</param>
            private void DrawOutLine(double _Height, double _Width)
            {
                try
                {
                    // Get the BaseLineLength
                    BaseLine = _Width - (LeftMargin + RightMargin);
                    // Get the spaceing of the points
                    spacing = (BaseLine / 42);
                    // Get the length of the vertical Line
                    VertLine = _Height - (BaseMargin + TopMargin);
                    // Get the max value and then add half to have a gap
                    // between the highest point and top of chart
                    YValue = ChartData.Max() * 3 / 2;

                    double yMarkerValue = YValue / 11; // Values going up Y Axis

                    YScale = VertLine / YValue; // Scale for the yaxis

                    // Draw the Y-Axis Line
                    Line YAxis = new Line();
                    YAxis.X1 = LeftMargin;
                    YAxis.Y1 = TopMargin;
                    YAxis.X2 = LeftMargin;
                    YAxis.Y2 = TopMargin + VertLine;
                    YAxis.Stroke = new SolidColorBrush(Colors.DarkBlue);
                    ChartCanvas.Children.Add(YAxis);

                    // Draw the X-Axis
                    Line XAxis = new Line();
                    XAxis.X1 = LeftMargin;
                    XAxis.Y1 = YAxis.Y2;
                    XAxis.X2 = LeftMargin + BaseLine;
                    XAxis.Y2 = YAxis.Y2;
                    XAxis.Stroke = new SolidColorBrush(Colors.DarkBlue);
                    ChartCanvas.Children.Add(XAxis);

                    double YGap = VertLine / 11; // distance between each tick
                    double YPos = YAxis.Y2; // Starting position for the y-axis ticks

                    // Now draw the ticks for the yaxis and
                    // the labels
                    for (int i = 0; i <= 11; i++)
                    {
                        // Draw Yaxis Ticks
                        Line YTick = new Line();
                        YTick.X1 = LeftMargin - 4;
                        YTick.Y1 = YPos;
                        YTick.X2 = LeftMargin;
                        YTick.Y2 = YPos;
                        YTick.Stroke = new SolidColorBrush(Colors.DarkBlue);
                        ChartCanvas.Children.Add(YTick);

                        // Draw Y-Axis label with value
                        Label YValueText = new Label();
                        YValueText.Content = Math.Round((yMarkerValue * i), 2).ToString();
                        YValueText.FontSize = 10;
                        ChartCanvas.Children.Add(YValueText);

                        Canvas.SetTop(YValueText, YPos - 10);
                        Canvas.SetLeft(YValueText, LeftMargin - 35);

                        YPos -= YGap;
                    }

                    // Now draw the ticks for the Xaxis
                    double XPos = LeftMargin;

                    for (int i = 0; i <= 42; i++)
                    {
                        Line XTick = new Line();
                        XTick.X1 = XPos;
                        XTick.Y1 = YAxis.Y2;
                        XTick.X2 = XPos;

                        if (i % 6 != 0)
                            XTick.Y2 = YAxis.Y2 + 4;
                        else
                            XTick.Y2 = YAxis.Y2 + 10;

                        XTick.Stroke = new SolidColorBrush(Colors.DarkBlue);
                        ChartCanvas.Children.Add(XTick);

                        XPos += spacing;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error drawing graph outline.\n" + ex.Message);
                }
            }
        }
    }

    Thanks
    • Marked as answer by GaztheLegend Wednesday, October 22, 2008 8:17 AM
    Wednesday, October 22, 2008 6:19 AM

All replies

  • Could you please provide a small, complete and ready-to-run example to demonstrate the issue you are encountering?

     

    Thanks

    Monday, October 20, 2008 9:27 AM
  • Here is a link to a small program I made. All it does is display the chart.

    Line Chart.Zip
    Monday, October 20, 2008 2:52 PM
  • I am not clear which issue you have with the charting code you linked above, but it seems that you want to properly scale the chart, then the simplest method is using Viewbox:

    <Grid>
      <Viewbox Margin="10">
        <Canvas Width="600" Height="400" Name="ChartCanvas" />
      </Viewbox>
    </Grid>

    Thanks
    Tuesday, October 21, 2008 3:23 AM
  • OK they say a picture can paint a thousand words or what ever so I have taken a screen shot of the chart and added notes to illustrate my problem

    Chart Pic

    As you can see from the picture, I have drawn lines from 2 points across to the Y-axis. You can clearly see that there values do not match up to the Y-axis values.


    Tuesday, October 21, 2008 9:36 AM
  • Okay, finally I make the code working as you'd expect, I have to say I need to refine your code, since it's hard to read:

    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Shapes;
    using System.Collections.Generic;
    using System.Windows.Media;

    namespace Line_Chart
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            // ~~  Variables for margins and outlines, etc ~~
            // # of pixels Y-Axis is inset from PicBox Left
            const double LeftMargin = 71;
            // # of Pixels left unused at right side of PicBox
            const double RightMargin = 15;
            // # of pixels above base of picturebox the X-Axis is placed
            const double BaseMargin = 65;
            // Margin at Top
            const double TopMargin = 32;

            // Base Line Length
            double BaseLine;
            // spaceing of the points
            double spacing;
            // vertical Line Length
            double VertLine;
            // Yaxis Scale
            double YScale;
            // Y Value
            double YValue;

            List<Double> ChartData = new List<Double> { 0, 36.57, 37.62, 17.41, 0, 0, 38.44, 37.63, 43.15, 33.3, 0, 0, 35.9, 36.53, 36.25, 34.91, 0, 0, 37.04, 40, 36.61, 36.76, 0, 0, 38.52, 40.7, 36.19, 39.03, 37.44, 0, 0, 36.95, 36.37, 39.24, 37.16, 35.65, 0, 35.62, 41.19, 0 };

            public Window1()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(Window1_Loaded);
            }

            void Window1_Loaded(object sender, RoutedEventArgs e)
            {
                DrawGraph(400, 600);
            }

            /// <summary>
            /// Draw the chart
            /// </summary>
            /// <param name="_Height">Height of chart</param>
            /// <param name="_Width">Width of chart</param>
            private void DrawGraph(double _Height, double _Width)
            {
                // Clear the canvas to redraw the chart
                ChartCanvas.Children.Clear();
                // Draw the outline of the chart
                DrawOutLine(_Height, _Width);
                // Set the start postion of the X cord for drawing
                // the line chart
                double StartX = LeftMargin + (spacing / 2);

                // Create the line chart using poly line
                Polyline ChartLine = new Polyline();
                PointCollection pc = new PointCollection();

                for (int No = 0; No < ChartData.Count; No++)
                {
                    double yVal = ChartData[No] * YScale; // My scaling
                    pc.Add(new Point(StartX, yVal));

                    StartX += spacing;
                }

                // Flip and move the line data so its 0,0 is bottom not top
                ScaleTransform Scal = new ScaleTransform(1, -1);
                TranslateTransform Trans = new TranslateTransform(0, VertLine + TopMargin);

                TransformGroup transGrp = new TransformGroup();
                transGrp.Children.Add(Scal);
                transGrp.Children.Add(Trans);

                ChartLine.RenderTransform = transGrp;

                // Add point to polyline and add to canvas
                ChartLine.Points = pc;
                ChartLine.Stroke = new SolidColorBrush(Colors.Red);
                ChartCanvas.Children.Add(ChartLine);
            }

            /// <summary>
            /// Draw the Axis, ticks etc...
            /// </summary>
            /// <param name="_Height">Height of chart</param>
            /// <param name="_Width">Width of Chart</param>
            private void DrawOutLine(double _Height, double _Width)
            {
                try
                {
                    // Get the BaseLineLength
                    BaseLine = _Width - (LeftMargin + RightMargin);
                    // Get the spaceing of the points
                    spacing = (BaseLine / 42);
                    // Get the length of the vertical Line
                    VertLine = _Height - (BaseMargin + TopMargin);
                    // Get the max value and then add half to have a gap
                    // between the highest point and top of chart
                    YValue = ChartData.Max() * 3 / 2;

                    double yMarkerValue = YValue / 11; // Values going up Y Axis

                    YScale = VertLine / YValue; // Scale for the yaxis

                    // Draw the Y-Axis Line
                    Line YAxis = new Line();
                    YAxis.X1 = LeftMargin;
                    YAxis.Y1 = TopMargin;
                    YAxis.X2 = LeftMargin;
                    YAxis.Y2 = TopMargin + VertLine;
                    YAxis.Stroke = new SolidColorBrush(Colors.DarkBlue);
                    ChartCanvas.Children.Add(YAxis);

                    // Draw the X-Axis
                    Line XAxis = new Line();
                    XAxis.X1 = LeftMargin;
                    XAxis.Y1 = YAxis.Y2;
                    XAxis.X2 = LeftMargin + BaseLine;
                    XAxis.Y2 = YAxis.Y2;
                    XAxis.Stroke = new SolidColorBrush(Colors.DarkBlue);
                    ChartCanvas.Children.Add(XAxis);

                    double YGap = VertLine / 11; // distance between each tick
                    double YPos = YAxis.Y2; // Starting position for the y-axis ticks

                    // Now draw the ticks for the yaxis and
                    // the labels
                    for (int i = 0; i <= 11; i++)
                    {
                        // Draw Yaxis Ticks
                        Line YTick = new Line();
                        YTick.X1 = LeftMargin - 4;
                        YTick.Y1 = YPos;
                        YTick.X2 = LeftMargin;
                        YTick.Y2 = YPos;
                        YTick.Stroke = new SolidColorBrush(Colors.DarkBlue);
                        ChartCanvas.Children.Add(YTick);

                        // Draw Y-Axis label with value
                        Label YValueText = new Label();
                        YValueText.Content = Math.Round((yMarkerValue * i), 2).ToString();
                        YValueText.FontSize = 10;
                        ChartCanvas.Children.Add(YValueText);

                        Canvas.SetTop(YValueText, YPos - 10);
                        Canvas.SetLeft(YValueText, LeftMargin - 35);

                        YPos -= YGap;
                    }

                    // Now draw the ticks for the Xaxis
                    double XPos = LeftMargin;

                    for (int i = 0; i <= 42; i++)
                    {
                        Line XTick = new Line();
                        XTick.X1 = XPos;
                        XTick.Y1 = YAxis.Y2;
                        XTick.X2 = XPos;

                        if (i % 6 != 0)
                            XTick.Y2 = YAxis.Y2 + 4;
                        else
                            XTick.Y2 = YAxis.Y2 + 10;

                        XTick.Stroke = new SolidColorBrush(Colors.DarkBlue);
                        ChartCanvas.Children.Add(XTick);

                        XPos += spacing;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error drawing graph outline.\n" + ex.Message);
                }
            }
        }
    }

    Thanks
    • Marked as answer by GaztheLegend Wednesday, October 22, 2008 8:17 AM
    Wednesday, October 22, 2008 6:19 AM
  • thanks for the reply. I will give it ago in a sec. Sorry for the poor coding and commenting, it was something that was all neat and tidy when first coded, but as I changed and tweaked things it soon became a mess. The idea was to get it working then go over it to tidy and make it more efficient. Anyway as I say I will give it ago and let you know.

    Cheers
    Wednesday, October 22, 2008 8:07 AM
  • it works thank you!!! now to go through the code see where I went wrong.

    1 other question which I think is more to do with drawing in WPF. If you look on the picture of the graph, to the right there is a dip that hits zero and then shoots up again. Why has it drawn below zero? it only seems to do it when there is a drop followed by a sharp rise.
    Wednesday, October 22, 2008 8:43 AM