2012-02-13 78 views
1

我详尽地使用了搜索,但一直未能找到令人满意的解决方案来解决我的问题。窗体窗体:在C#中的图表上创建透明图形

我使用图表(datavisualization.charting.chart)对数据可视化进行了编程。由于它显示了某种模拟结果,因此该图表稳步改变。 我想在图表上画一条线(取决于鼠标的位置),以便显示上下文相关的信息。

因此,我尝试了两种实现方式,既没有作品正是我想它:

情节主线上称为MouseMove-事件

GGRAPH从底层图使用创建:chartCreateGraphics() ;

protected void plotLine(object sender, System.EventArgs e) 
{ 
    if (this.chart.Series.Count > 0) //ensure that the chart shows data 
    { 
    plotChart(); // this plots the underlying chart 
    penGraph = new Pen(Color.Black); 
    Point point1 = new Point(Form1.MousePosition.X - chart.Location.X, 0); 
    Point point2 = new Point(Form1.MousePosition.X - chart.Location.X,  
    chart.Location.Y + chart.Size.Height); 
    gGraph.DrawLine(penGraph, point1, point2); 
    penGraph.Dispose();  
    } 
} 

在这里,每行绘制后立即消失,但应保持,只要鼠标不移动。

protected void plotLine(object sender, System.EventArgs e) 
{ 
    penGraph = new Pen(Color.Black); 
    Point point1 = new Point(Form1.MousePosition.X - chart.Location.X, 0); 
    Point point2 = new Point(Form1.MousePosition.X - chart.Location.X,  
    chart.Location.Y + chart.Size.Height); 
    gGraph.DrawLine(penGraph, point1, point2); 
    penGraph.Dispose();   
} 

这里,只要图表没有被新的图表绘制,所有绘制的线仍保留在图形表面中。 (只有最后一行应该保留为了表示鼠标位置)

任何人都可以帮我吗?

+0

_gGraph是使用下面的图形创建的:chart.CreateGraphics(); _这是错误。 – TaW 2016-08-27 08:01:37

回答

1

您应该在OnPaint事件中绘制。您正在规避更新模型并正在看到这样做的效果。当然,你在MouseMove中做了一些绘图,但是当Paint事件触发时,它只会被删除。

首先将您的代码放在OnPaint中。在鼠标移动时只需记录所需的任何数据(即鼠标位置),然后在图表上调用Invalidate()。当你这样做时,Paint事件将被调用并且你的绘图代码将被触发。

经验法则;除了Paint事件之外,不能从任何地方绘制。

+0

不幸的是,同样的情况发生...... – 2012-02-14 08:52:45

+0

我在底层窗体的OnPaint方法内执行了绘图(使用Invalidate()调用了MouseMove事件)。这导致了和以前一样的行为......我不太明白这个问题 – 2012-02-14 09:10:41

+0

在PrePaint或PostPaint中绘图只会起作用。尽管使用__only__ Graphics形式的参数:'e.ChartGraphics.Graphics'! – TaW 2016-08-27 08:00:45