c# - 将月份放在 MajorGrid 上,将天数放在 MinorGrid 上(范围条形图)
解决方案
您可以像这样添加两行自定义标签:
ay.LabelStyle.Angle = 0;
ay.IsLabelAutoFit = true;
DateTime d1 = DateTime.FromOADate(ay.Minimum);
DateTime d2 = DateTime.FromOADate(ay.Maximum);
int dc = (int)(d2 - d1).TotalDays;
//double dspace = d2.ToOADate() - d1.ToOADate(); // we need a suitable number later (*)
dspace = 10; // seems to work better when zooming in..
for (int i = 0; i < dc; i++)
{
DateTime dt = d1.AddDays(i);
double dd = dt.ToOADate();
CustomLabel cl = new CustomLabel();
cl.Text = dt.Day + "";
cl.FromPosition = dd - dspace; //(*)
cl.ToPosition = dd + dspace; //(*)
cl.RowIndex = 0; // 1st row of labels
ay.CustomLabels.Add(cl);
if (dt.Day == 1) // place month name at the 1st day
{
cl = new CustomLabel();
string month = d1.AddDays(i).ToString("MMMM");
cl.Text = month;
cl.FromPosition = dd - dspace; //(*)
cl.ToPosition = dd + dspace; //(*)
cl.RowIndex = 1; // 2nd row
ay.CustomLabels.Add(cl);
}
}
哪里Axis ay = ca.AxisY;
和var ca = chart1.ChartAreas[0];
。
结果:
黄色矩形是在 PrePaint 事件中绘制的。例子:
private void chart1_PrePaint(object sender, ChartPaintEventArgs e)
{
Series s = chart1.Series[0];
if (s.Points.Count <= 0) return;
Graphics g = e.ChartGraphics.Graphics;
var ca = chart1.ChartAreas[0];
Axis ay = ca.AxisY;
DateTime d1 = DateTime.FromOADate(ay.Minimum);
DateTime d2 = DateTime.FromOADate(ay.Maximum);
int x1 = (int)ay.ValueToPixelPosition(ay.Minimum);
int x2 = (int)ay.ValueToPixelPosition(ay.Maximum);
int y = (int)ca.AxisX.ValueToPixelPosition(ca.AxisX.Minimum);
using (SolidBrush b = new SolidBrush(Color.FromArgb(11, 222, 222, 111)))
g.FillRectangle(b, x1, y, x2 - x1, 60); // 60 pixels large, calculate what you need!
}
我不得不承认我不知所措。也许一个(更多)涉及的油漆代码会接近你的例子..
推荐阅读
- flutter - 每次视图进入屏幕时如何在颤动中重建整个视图
- java - 从二进制堆转储中提取 JVM 正常运行时间
- python - Python argparse -h 选项点缀
- php - 使用修改过的 MustVerifyEmail 特征的 Laravel API 无法获取请求用户
- kotlin - Swagger-UI 和 Ktor 如何导入 swagger.json 或 .yaml 文件并启动 Swagger-UI?
- python - matplotlib 按钮在嵌套的 GridSpec 中不起作用
- php - 如何使用php ajax更新可排序的jquery ui中列表项的位置
- android - 从 env->functions 挂钩 JNI 函数在模拟器上不起作用
- api - Docusign:尝试从 UI5 应用程序的模板 ID 创建信封时出现 400 错误“无法解析多部分正文”
- java - Android 9.0 Activity 泄露了原本添加的窗口