Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

ScatterPlot varying color

Solved!
Go to solution

Hi to all,

Has anyone ever implemented a derived class from ScatterPlot which could have varying color? What about varying linewidth?

Cheers,

Cyrille

0 Kudos
Message 1 of 8
(6,021 Views)

Hello there,

 

I just gave it a try and was able to use different colors for my graph.

Measurement studio ships with a ColorScale which can be integrated with some basic coding. See the attached source.zip file to see how it can be done.

 

And here is the screenshot I got with it 🙂

 

colorScatterGraph.png

 

Happy coding!!

Hope this helps!

 

Regards

 

Vijet Patankar

National Instruments

0 Kudos
Message 2 of 8
(6,016 Views)

Hi,

I did try something along those lines but was getting some sloppy graphics:

Graphics

For some reason I never managed to get things working with  g.DrawLine and I went into using  g.FillClosedCurve trying to make it smooth.

Mysteriously if I zoom in enough the track looks absolutely fine.

I will try again with your solution and see if I get any success (I like the fact that you are using colorScales).

Thanks for your prompt reply,

Cyrille

0 Kudos
Message 3 of 8
(6,013 Views)

Thanks for your reply, getting there slowly. On my side I absolutely need to derive from ScatterPlot and not ScatterGraph like in your example. Here is my code (in VB) to override the OnBeforeDraw:

 

 Protected Overrides Sub OnBeforeDraw(ByVal e As BeforeDrawXYPlotEventArgs)
    Dim plot As XYPlot = e.Plot
    Dim clippedXData() As Double = Nothing
    Dim clippedYData() As Double = Nothing
    plot.ClipDataPoints(clippedXData, clippedYData)

    If IsNothing(clippedXData) OrElse clippedXData.Length = 0 Then
      MyBase.OnBeforeDraw(e)
      Exit Sub
    End If

    e.Cancel = True

    Dim ptFirst As PointF = New PointF(clippedXData(0), clippedYData(0))
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    For i As Integer = 1 To clippedXData.Length - 1
      Dim ptNext As PointF = New PointF(clippedXData(i), clippedYData(i))

      ' skip ghost points
      If Double.IsNaN(clippedXData(i - 1)) OrElse Double.IsNaN(clippedYData(i - 1)) OrElse _
        Double.IsNaN(clippedXData(i)) OrElse Double.IsNaN(clippedYData(i)) Then
        GoTo DrawNextLine

      End If
   
    
      Dim col As Color = Me.LineColor
      If clippedYData(i) < 4 Then
        col = Color.Red
      ElseIf clippedYData(i) < 6 Then
        col = Color.Blue
      Else
        col = Color.Green
       End If

      Dim pt1 As PointF = plot.MapDataPoint(e.Bounds, ptFirst.X, ptFirst.Y)
      Dim pt2 As PointF = plot.MapDataPoint(e.Bounds, ptNext.X, ptNext.Y)

      e.Graphics.DrawLine(New Pen(col, 8), pt1, pt2)

DrawNextLine:
      ptFirst = ptNext
    Next
  End Sub

I added the 

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

as it made the result less sloppy, but it still sin't satisfactory.

 

The points I am plotting are the following (with ghost points):

    ReDim x(1000) : ReDim y(1000)
    For i = 0 To 1000
      If i > 300 And i < 450 Then
        x(i) = Double.NaN
        GoTo NextPt
      End If
      x(i) = i / 100
      y(i) = 5 + 2 * Math.Cos(4 * Math.PI * i / 1000) + 1 * Math.Cos(Math.PI * i / 50)
NextPt:
    Next

 

 

 This works but I am not happy with the result as I get a sloppy image:

Sloppy coloured line

 

Eventually I will need the plot to have varying colour as well as line size.

Thanks for your help!

0 Kudos
Message 4 of 8
(5,994 Views)

Digging some more into it, it seems to be some kind of GDI+ stuff which I am not expert in. To check this I am now also plotting the same line in a Picture object. I have been playing with the following:

 

  e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
    e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half

 Here is the result I get for a size 8 pen. Not perfect but better in the picturebox than in the ScatterGraph...

Compare GDI and ScatterPlot

 

Anyone knows why this is happening and if there's anything I should to to improve quality in ScatterPlot ?

Thanks,

CD

0 Kudos
Message 5 of 8
(5,991 Views)

Hello,

Has anyone from National Instruments had a look at my post to figure out a solution?

Thanks,

CD

0 Kudos
Message 6 of 8
(5,966 Views)
Solution
Accepted by topic author cdouillet

Hi CD,

 

The problem you are seeing is a result of the Pen configuration. The property EndCap is essentially the brush tip style you are using. I believe the default is Flat which causes your line to look like its zig zagging. Instead, use a Round EndCap and you will get a much smoother looking line.

 

Dim myPen As New Pen(col, 20)
myPen.EndCap = Drawing2D.LineCap.Round
e.Graphics.DrawLine(myPen, pt1, pt2)

 

National Instruments
0 Kudos
Message 7 of 8
(5,954 Views)

Brilliant, thank you!

CD

0 Kudos
Message 8 of 8
(5,945 Views)