1

I am having difficulties with autoscaling with a c# WinForms chart. I thought by setting the Minimum and Maximum properties of ChartArea.AxisX it would take care of the scaling for me.

In the example below I show a column chart that tracks the number of accidents that occur in a workplace over a given year. Each month, an email will be sent out to the managers of the workplace showing them an updated chart with that month's entries added to it. When the first email is sent out, the columns are very large. Each month, as more workplace accidents occur, the columns of the chart begin to shrink.

In other words, if you make the fakeAccidentsData array have just one or two elements, then the columns are massive. If you have more elements in fakeAccidentsData then they are smaller. I want the columns in this chart to remain a fixed, narrow width so that as more elements are added the columns won't change in size. So there should be enough room to accomodate 365 columns (one for each day of the year). If 365 columns are too small to be seen then I am willing to change it to go by week instead (so I would need space for 52 columns in that case).

Can this be accomplished?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;

namespace MinimalExample
{
    class WorkplaceAccident
    {
        public DateTime Timestamp;
    }

    class ChartExample
    {
        private readonly Series _series;
        private readonly ChartArea _chartArea;
        private readonly Chart _chart;

        public IEnumerable<WorkplaceAccident> Accidents
        {
            set
            {
                var accidentCount = new Dictionary<DateTime, int>();

                foreach (var accident in value)
                {
                    DateTime accidentDate = accident.Timestamp.Date;
                    if (accidentCount.ContainsKey(accidentDate))
                    {
                        ++accidentCount[accidentDate];
                    }
                    else accidentCount.Add(accidentDate, 1);
                }

                foreach(KeyValuePair<DateTime, int> pair in accidentCount)
                {
                    Console.WriteLine("[Debug info] Adding " + pair.Key + ", " + pair.Value);
                    _series.Points.AddXY(pair.Key, pair.Value);
                }
            }
        }

        public ChartExample(int year)
        {
            _series = new Series();
            _series.ChartType = SeriesChartType.Column;

            _chartArea = new ChartArea();
            _chartArea.AxisY.Interval = 1;
            _chartArea.AxisY.IsMarginVisible = false;
            _chartArea.AxisY.Title = "Number of workplace accidents";
            _chartArea.AxisX.MajorGrid.Enabled = false;
            _chartArea.AxisX.Minimum = new DateTime(year, 1, 1).ToOADate();
            _chartArea.AxisX.Maximum = new DateTime(year, 12, 31).ToOADate();

            _chart = new Chart();
            _chart.ChartAreas.Add(_chartArea);
            _chart.Series.Add(_series);
            _chart.Size = new Size(800, 400);
        }

        public void SaveAsPng(string filename)
        {
            _chart.SaveImage(filename, ChartImageFormat.Png);
        }

        public static void Main()
        {
            WorkplaceAccident[] fakeAccidentsData = new WorkplaceAccident[]
            {
                new WorkplaceAccident { Timestamp = DateTime.Now.AddDays(-1) },
                new WorkplaceAccident { Timestamp = DateTime.Now.AddDays(-1) },
                new WorkplaceAccident { Timestamp = DateTime.Now.AddDays(-5) },
                new WorkplaceAccident { Timestamp = DateTime.Now.AddMonths(-1) }
            };
            var chart = new ChartExample(2020) { Accidents = fakeAccidentsData };
            chart.SaveAsPng(@"C:\Users\Home\Documents\chart.png");
            Console.Write("Press any key to terminate... "); Console.ReadKey();
        }
    }
}
  • You can set the [width of the columns](https://stackoverflow.com/questions/22556583/control-the-width-of-the-column-bar-in-a-chart-c-sharp) to a fixed size. Is that what you want? You can also zoom in and out and scroll in the chart. – TaW Dec 04 '20 at 11:42

0 Answers0