Search code examples
c#imagechartsscaledata-visualization

How to adjust axis scales for datavisualization.charting after resizing a chart?


I updated a chart from essentially being 72dpi, to 300dpi. This is because I am using itextsharp to add an image to my pdf and the quality was poor. So I increased the size of the image by 3X and the image does look better, but here is the problem.

DPI has increased, but detail has become very hard to see.

Original Chart Image

Original Image

Refactored Chart Image enter image description here

Code

This is how I resized my chart.

private static System.Drawing.Bitmap GetChartBitmap()
        {


            System.Drawing.Rectangle targetBounds = new System.Drawing.Rectangle(0, 0, chart_runs.Width, chart_runs.Height);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(targetBounds.Width, targetBounds.Height);
            bitmap.SetResolution(1000, 1000);
            chart_runs.DrawToBitmap(bitmap, targetBounds);
            bitmap.Save(@"C:\Temp\OriginalChartImage.bmp");


            System.Drawing.Bitmap bitmap3 = new System.Drawing.Bitmap(1650, 990);
            bitmap3.SetResolution(300, 300);
            chart_runs.DrawToBitmap(bitmap3, new System.Drawing.Rectangle(0, 0, 1650, 990));
            bitmap3.Save(@"C:\Temp\RefactoredChartImage.png");



            //This stuff below is for my code elsewhere. Using bitmap3 to be added to pdf. 
            //chart_runs.DrawToBitmap(bitmap, targetBounds);
            string path = System.IO.Path.GetTempPath();

            bitmap1.Save(path + @"\Image.png");
            return bitmap1;
        }

I have looked at the Microsoft msdn examples and haven't found anything that addresses my problem. Namely, how can I either increase the size of my labels so people can read them again. OR, is there a way for me to increase the DPI and keep the same label x and label y scale that was used in the first picture? That is, have a larger image and 300DPI, but scale 0 to 300 by 20's and not 5's like my refactored picture?

Attempts to fix

  • Scaling the axis? See here. I don't think this is working right. Not much success here.
  • Been trying to find a way in Chart class to see if there is a way to specify strict scales. (20 on y scale vs 15 seconds on x scale).
  • Most online resources are pleased just to increase the scale of the picture and walk away. And things like this here.

I would greatly appreciate any help and assistance.


Solution

  • Couple different questions, with a couple different answers. The easiest would be to change the font size of your axis labels to be bigger. This can be done via

    chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font...;
    

    Without doing that, your labels won't be readable no matter what else you do, and that's just because you changed the DPI (that's exactly what changing the DPI does).

    If you want the labels to be displayed every 20 units on the y axis and every 15 on the x, you can use the Interval and IntervalType properties of the axis. The IntervalType is used when you have DateTime objects being displayed:

    chart1.ChartAreas[0].AxisX.Interval = 15;
    chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
    chart1.ChartAreas[0].AxisY.Interval = 20;
    

    Your first link about scaling the axis is essentially zooming in or out, which is why you haven't had success.