Chart - Controlling axis interval, spacing and vis

2019-08-06 20:42发布

问题:

Using chart windows forms control to display:

STACKED COLUMN
Several series, ~70 datapoints per series. Let's say x: 0 - 70. Secondary Y-axis.

POINT/LINE
0-3 series displayed, ~20 datapoints per series. X-values are 0-20, so only covering part of the chartarea. Primary Y-axis.

I am adding custom labels to the x-axis. The user can choose to show 24, 48 or 72 datapoints. Depending on the number of datapoints, the interval on the x-axis is changed.

24 -> Interval 1  
48 -> Interval 2  
72 -> Interval 3  

FIRST PROBLEM
I do not want the primary Y-axis to be displayed when the point/line graphs are not visible. This is working, but when I add the graphs, the whole chartarea moves and resizes in order to fit in the axis. I would like the chartarea to remain still, just adding the axis to the area. See difference between images 24_Point and 24_NoPoint.

SECOND PROBLEM
The interval on the axis seems to work, but the numbers are scaled and moved up/down in order to fit in, even though there doesn't seem to be any space issues. Is there any way to force orientation, font and distance so they don't move? A problem I see with fixing the font, is that if the labels are to close, they will overlap or disappear. In this case though, the program will be in full control of the number of labels and size of the chartarea, so it should be able to control it. See difference between images 24_Point and 48_Point.

RELEVANT CODE

Chart.ChartAreas["Default"].AxisX.CustomLabels.Clear();
if (PlotHours == 24){
  interval = 1;
} else if (PlotHours == 48){
  interval = 2;
} else {
  interval = 3;
}

q = 0;

for (int i = 0; i=72; i++){
  if (q==0){
    Chart.ChartAreas["Default"].AxisX.CustomLabels.Add((i-0.5, (i+0.5), AxisLabels[i]);
  }
  if (q == (interval-1)){
    q = 0;
  } else {
    q++;
  }
}
Chart.ChartAreas["Default"].AxisX.Interval = interval;
Chart.ChartAreas["Default"].AxisX.IntervalOffset = 1;

REFERENCED IMAGES
The reason I have not removed the black areas of the pictures, is to keep them in scale.

24_Point
24_NoPoint
48_Point

回答1:

Problem 1: I don't think you can make the chart leave the empty space to the left. Instead a simple workaround should do: Display the Y-Axis but make the labels transparent! See here for a similar example! That will take the space without showing much more than maybe a few unobtrusive tick marks..

Problem 2: I don't think you can convince the labels that they have enough room, when the chart believes otherwise. It reserves the space for larger and for negative values, so I'm afraid you will have to allow for some extra space by

  • reducing font size or font width
  • reducing the number of labels
  • enlarging the chartarea


回答2:

Problem with labels is solved with a simple code change:

    Chart.ChartAreas["Default"].AxisX.CustomLabels.Add((i-(interval/2), (i+(interval/2)), AxisLabels[i]);

You are specifying a range for each label. This range not only gives information about which data it covers, but it also seems to specify what space the label is allowed to use. By increasing this space, based on which interval you want, the label will be able to be displayed properly, without rotating or moving up/down.