NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Member
cdouillet
Posts: 28
0 Kudos
Accepted Solution

ScatterPlot varying color

Hi to all,

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

Cheers,

Cyrille

Member vcp
Member
vcp
Posts: 86
0 Kudos

Re: ScatterPlot varying color

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 :smileyhappy:

 

colorScatterGraph.png

 

Happy coding!!

Hope this helps!

 

Regards

 

Vijet Patankar

National Instruments

Member
cdouillet
Posts: 28
0 Kudos

Re: ScatterPlot varying color

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

Member
cdouillet
Posts: 28
0 Kudos

Re: ScatterPlot varying color

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!

Member
cdouillet
Posts: 28
0 Kudos

Re: ScatterPlot varying color

[ Edited ]

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

Member
cdouillet
Posts: 28
0 Kudos

Re: ScatterPlot varying color

Hello,

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

Thanks,

CD

Active Participant
D_Biel
Posts: 639
0 Kudos

Re: ScatterPlot varying color

[ Edited ]

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
Product Support Engineer
Member
cdouillet
Posts: 28
0 Kudos

Re: ScatterPlot varying color

Brilliant, thank you!

CD

By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page