我想在的背景上绘制图像DataVisualization.Charting.Chart
。由于该ChartArea.BackImage
属性仅接受映像的路径,因此无法将此值设置为运行时映像。
因此,我PrePaint Event
将图表的绘制在图表图形上(我删除了部分代码,并将图像替换为蓝色矩形):
private void chart1_PrePaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
double xMax = e.ChartGraphics.GetPositionFromAxis("ChartArea1", AxisName.X, chart1.ChartAreas[0].AxisX.Maximum);
double xMin = e.ChartGraphics.GetPositionFromAxis("ChartArea1", AxisName.X, chart1.ChartAreas[0].AxisX.Minimum);
double yMax = e.ChartGraphics.GetPositionFromAxis("ChartArea1", AxisName.Y, chart1.ChartAreas[0].AxisY.Minimum);
double yMin = e.ChartGraphics.GetPositionFromAxis("ChartArea1", AxisName.Y, chart1.ChartAreas[0].AxisY.Maximum);
double width = xMax-xMin;
double heigth = yMax- yMin;
RectangleF myRect = new RectangleF((float)xMin,(float)yMin,(float)width,(float)heigth);
myRect = e.ChartGraphics.GetAbsoluteRectangle(myRect);
e.ChartGraphics.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), myRect);
}
问题在于,这种方式会覆盖图表的网格(请参见左图)。但是我希望网格可见(见左图)。有任何想法吗?
由于ChartArea.BackImage属性仅接受图像的路径,因此您不能将此值设置为运行时图像。
实际上,您可以通过使用晦涩的NamedImage
类:
// here you can use any image..
Bitmap bmp = ... insert your image creation code!
// create a named image from it
NamedImage ni = new NamedImage("test", bmp);
// add it to the chart's collection of images
chart1.Images.Add(ni);
// now we can use it at any place we seemingly can only use a path:
chart1.BackImage = "test";
同样的技巧也适用DataPoint.BackImage
!
性能是另一个问题,但是它应该比随时写入磁盘都要好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句