As I wrote before I am using the MS Chart for the more conventional charts in the development of the next version of GeneXproTools. One particularly weak feature of this chart is the Zoom which is not very useful, has very cheesy scrollbars and also has  a few bugs here and there. One I came across recently was that when zooming in until the vertical line of the zoom cursor is not deleted when you stop dragging and release the mouse. It just stays there even if you zoom out completely. I don’t know if this is a side effect of hosting a Win32 component in WPF or if it is a bug but nevertheless I managed to come up with a very simple workaround. Maybe a bit too hacky for the more retentive but effective.

The solution was to handle the mouse down and mouse up events of the chart control and toggle the cursor colour between transparent and red (its default colour). Here is the XAML snippet:

<Charting:Chart
    x:Name="chart1"
    MouseDown="Chart1MouseDown"
    MouseUp="Chart1MouseUp" />

And here are the handlers:

private void Chart1MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
    {
        var cursorX = chart1.ChartAreas[0].CursorX;
        cursorX.LineColor = Color.Red;
        var cursorY = chart1.ChartAreas[0].CursorY;
        cursorY.LineColor = Color.Red;
    }

}

private void Chart1MouseUp(object sender, MouseEventArgs e)
{
    var cursorX = chart1.ChartAreas[0].CursorX;
    cursorX.LineColor = Color.Transparent;
    var cursorY = chart1.ChartAreas[0].CursorY;
    cursorY.LineColor = Color.Transparent;
}

I could not find a more elegant solution but this seems to work fine. The chart is a Scatter Chart and so far this is the only workaround. Let’s hope that the n in the title remains low.

Advertisements