How to get the grid of my chart in white color?

Viewed 60

I've trying to set the grid of my chart to be in white color. Tried all the properties that contain the word color to set it to white but I didn't find the way.

I need the grid of the chart to be white so on a black background, it is visible correctly.

I'm using Net Library to generate these charts in a c# console application.

Image of the current chart: https://imgur.com/a/Pe1k21I

Code below:

static void GenerateChart(Dictionary<int, string> sizes)
    {
        Random random = new Random();
        Chart chart = new Chart();

        chart.Width = 584;
        chart.Height = 476;
        chart.BackColor = System.Drawing.Color.Transparent;
        chart.AntiAliasing = AntiAliasingStyles.Graphics;
        
        chart.Series.Clear();            
        //chart.ForeColor = System.Drawing.Color.White;
        //chart.BorderlineColor = System.Drawing.Color.Transparent;

        chart.ChartAreas.Add("Sizes");
        chart.ChartAreas["Sizes"].AxisX.Interval = 1;
        chart.ChartAreas["Sizes"].AxisY.Interval = 20;
        chart.ChartAreas["Sizes"].BackColor = System.Drawing.Color.Transparent;
        chart.ChartAreas["Sizes"].BorderColor = System.Drawing.Color.White;
        chart.ChartAreas["Sizes"].BorderWidth = 2;
        //chartArea.ShadowColor = System.Drawing.Color.White;

        chart.ChartAreas["Sizes"].AxisX.LineColor = System.Drawing.Color.White;
        chart.ChartAreas["Sizes"].AxisY.LineColor = System.Drawing.Color.White;

        Series chartSeries = chart.Series.Add("38.5");
        chartSeries.Legend = "Prices";
        chartSeries.LegendText = "Prices";
        chartSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
        chartSeries.XValueType = ChartValueType.DateTime;
        chartSeries.BorderWidth = 5;
        chartSeries.BorderColor = System.Drawing.Color.White;
        chartSeries.LabelForeColor = System.Drawing.Color.White;
        
        Dictionary<DateTime, int> sales = new Dictionary<DateTime, int>();
        sales.Add(DateTime.Now, random.Next(100, 200));
        sales.Add(DateTime.Now.AddDays(1), random.Next(100, 200));
        sales.Add(DateTime.Now.AddDays(2), random.Next(100, 200));
        sales.Add(DateTime.Now.AddDays(3), random.Next(100, 200));
        sales.Add(DateTime.Now.AddDays(4), random.Next(100, 200));
        sales.Add(DateTime.Now.AddDays(5), random.Next(100, 200));
        sales.Add(DateTime.Now.AddDays(6), random.Next(100, 200));

        foreach (var sale in sales)
        {
            chartSeries.Points.AddXY(sale.Key, sale.Value);
           
        }

        Debug.WriteLine(chart.ChartAreas["Sizes"].Axes.Count());
        for (int i = 0; i > chart.ChartAreas["Sizes"].Axes.Count(); i++)
            chart.ChartAreas["Sizes"].Axes[i].LineColor = System.Drawing.Color.White;

        string imageNameAndPath = Logic.programPath + "/chart.png";
        chart.SaveImage(imageNameAndPath, ChartImageFormat.Png);
    }
1 Answers

To change chart background grid color, simply set LineColor to MajorGrid Property:

chart.ChartAreas["Sizes"].AxisX.MajorGrid.LineColor = System.Drawing.Color.White;
chart.ChartAreas["Sizes"].AxisY.MajorGrid.LineColor = System.Drawing.Color.White;

Reference: link.

To change chart label color, simply set ForeColor to LabelStyle Property:

chart.ChartAreas["Sizes"].AxisX.LabelStyle.ForeColor = System.Drawing.Color.White;
chart.ChartAreas["Sizes"].AxisY.LabelStyle.ForeColor = System.Drawing.Color.White;

Reference: link.

Related