04-30-2012 11:28 AM
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
Hope this helps!
04-30-2012 11:42 AM
I did try something along those lines but was getting some sloppy 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,
05-07-2012 06:12 AM
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:
Eventually I will need the plot to have varying colour as well as line size.
Thanks for your help!
05-07-2012 06:43 AM - edited 05-07-2012 06:44 AM
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...
Anyone knows why this is happening and if there's anything I should to to improve quality in ScatterPlot ?
06-04-2012 11:29 AM - edited 06-04-2012 11:32 AM
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)