Share via


How to set grid line type on Chart control?

Question

Wednesday, June 1, 2016 9:37 AM

I've tried to set MajorGrid.LineDashStyle,but it did't work and the grid line becomes thicker.

 Chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
 Chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

All replies (2)

Thursday, June 2, 2016 3:02 AM âś…Answered | 1 vote

Hi _HashTable,

Thank you for posting here.

According to your snippet code. I cannot find out something else. I have a test with the following code. It can draw the type of lines as we want. So I would suggest you can refer to code below to achieve your function. Or, you could share some code that could reproduce your issue.

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Graph = System.Windows.Forms.DataVisualization.Charting;

        Graph.Chart chart;
        public Form1()
        {
            InitializeComponent();
            this.ClientSize = new System.Drawing.Size(750, 750);
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            const int MaxX = 20;
            // Create new Graph
            chart = new Graph.Chart();
            chart.Location = new System.Drawing.Point(10, 10);
            chart.Size = new System.Drawing.Size(700, 700);
            // Add a chartarea called "draw", add axes to it and color the area black
            chart.ChartAreas.Add("draw");
            chart.ChartAreas["draw"].AxisX.Minimum = 0;
            chart.ChartAreas["draw"].AxisX.Maximum = MaxX;
            chart.ChartAreas["draw"].AxisX.Interval = 1;
            chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = Color.White;
            chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;
            chart.ChartAreas["draw"].AxisY.Minimum = -0.4;
            chart.ChartAreas["draw"].AxisY.Maximum = 1;
            chart.ChartAreas["draw"].AxisY.Interval = 0.2;
            chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.White;
            chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Solid;

            chart.ChartAreas["draw"].BackColor = Color.Black;

            // Create a new function series
            chart.Series.Add("MyFunc");
            // Set the type to line      
            chart.Series["MyFunc"].ChartType = Graph.SeriesChartType.Line;
            // Color the line of the graph light green and give it a thickness of 3
            chart.Series["MyFunc"].Color = Color.LightGreen;
            chart.Series["MyFunc"].BorderWidth = 3;
            //This function cannot include zero, and we walk through it in steps of 0.1 to add coordinates to our series
            for (double x = 0.1; x < MaxX; x += 0.1)
            {
                chart.Series["MyFunc"].Points.AddXY(x, Math.Sin(x) / x);
            }
            chart.Series["MyFunc"].LegendText = "sin(x) / x";
            // Create a new legend called "MyLegend".
            chart.Legends.Add("MyLegend");
            chart.Legends["MyLegend"].BorderColor = Color.Tomato; // I like tomato juice!
            Controls.Add(this.chart);
        }

Best Regards,

Albert Zhang


Friday, June 3, 2016 2:38 AM

Thank you very much.

The code in the first two line will work just fine in normal case. I think some of my code have reset the grid line style but I can't find them.

Chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.DashDotDot;
Chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
Chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
Chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
Chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
Chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 3600;
Chart1.ChartAreas[0].AxisX.Interval = 3600;
Chart1.ChartAreas[0].AxisX.Maximum = 3600 * ((int)m / 3600 + 1);
Chart1.ChartAreas[0].AxisX.Minimum = 0;
Chart1.Series.Add("Latitude");
Chart1.ChartAreas[0].AxisX.Title = "Epoch[h]";
Chart1.ChartAreas[0].AxisX.TitleFont = new Font("TimesNewRoman", 10);
Chart1.ChartAreas[0].Position.Width = 45;
Chart1.ChartAreas[0].Position.Height = 30;
Chart1.Series[0].ChartType = SeriesChartType.Line;
Chart1.Series[0].Points.DataBindXY(time,rms_x);  
Chart1.ChartAreas[0].AxisY.Title = "Latitude[m]";
Chart1.ChartAreas[0].AxisY.TitleFont = new Font("TimesNewRoman", 10);
Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false;
//Chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
Chart1.ChartAreas[0].Name = "Latitude";
Chart1.ChartAreas[0].Position.X = 0;
Chart1.ChartAreas[0].Position.Y = 0;
Chart1.Series[0].ChartArea = "Latitude";