1

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://i.stack.imgur.com/Rld8L.jpg

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);
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

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.

NcXNaV
  • 1,657
  • 4
  • 14
  • 23
  • Thanks that did the work! One question more, what about the X and Y intervals strings? I mean the bottom datetime and the Y values, how I could change them of color? – Emanuel Rodriguez Jul 17 '21 at 20:23
  • Thanks a lot, have a great day sir! Have to say that LineColor doesn't exist in LabelStyle, the ForeColor property is the one that needs to be set. – Emanuel Rodriguez Jul 17 '21 at 20:50