The official Fatica Labs Blog! RSS 2.0
# Wednesday, May 25, 2011

In  the previous sample we shown how to present multiple chart by keeping them aligned in order to be comparable on the horizontal axis by using the WidthSpring feature. But D3 charting can automatically pan and zoom ( both by rect zoom by pressing control and left dragging the mouse on the chart, or by the wheel ) and in this case we loose the axis synchronization. Here below the undesired behavior:

image On the left we see the top chart zoomed, the other ones show the horizontal range thus there is no more relation among the three charts.

So we create an attached property as below:

<ddd:ChartPlotter x:Name="price" ddd:SynchroVisible.Axis="X" ddd:SynchroVisible.With="{Binding ElementName=volume}" …>
 

by applying this property we choose which axis synchronize ( X in the example, but can be Y or XY ) and which chart keep synchronized with, in this case we bind with the chart named “volume”. By repeating the property two chart by two chart:

<ddd:ChartPlotter x:Name="volume" ddd:SynchroVisible.Axis="X" ddd:SynchroVisible.With="{Binding ElementName=ma}" …>
<ddd:ChartPlotter    x:Name="ma"  ddd:SynchroVisible.Axis="X" ddd:SynchroVisible.With="{Binding ElementName=price}" …>

 

so we achieve this interesting result without any code behind:

image This is the result after panning the chart ( by dragging the mouse pointer into the chart area in any of the three chart ) as you can see the other chart properly follow…
image The same apply if we zoom on the chart area ( by pressing ctrl while dragging, or by the mouse wheel ) other chart amplify the resolution on the synchronized axis and the start position accordingly.

Check out the source for this example here.

Wednesday, May 25, 2011 9:49:37 PM (GMT Daylight Time, UTC+01:00)  #    Comments [5] - Trackback
Charting | D3 | WPF

# Monday, May 23, 2011

image

This is a new book written by Alberto Población I had a chance to read, and I want to say a few words about it. I think it cover the art of programming by a very practical point of view: you can find here some tricks to solve everyday troubles that other books simply forgot to talk about. There is a chapter on data access, explaining the common pitfalls and relative solutions, to make code faster, but even for having the application appearing faster ( ie, more reactive ) to the user. An entire chapter is about new features of Windows 7: what about the new taskbar? what’s in the new shell ? What can we do in C# with the new operating system features ? And then… a lot more in GDI+ graphics, since we don’t always need to buy a control library for customizing a control, as well as for creating a simple report. Chapter 4 talks about serialization, but explaining some advanced features that really can save our time, especially when we need to parse thirty party xml files.Chapter 5 is about reflection, but again it cover some usages in the usual annoying deploy troubles. Chapter 5 is a remainder for tips and tricks we really don’t need everyday, but that sometimes appear as very hard nuts to crack, for example, did you never experience problems in reading files with some exotic encoding? Last chapter is a recipe book about WinForm that we should read in order to know how to  achieve some special effect as non rectangular form, as well as some tips on task dialogs in Windows 7. 

So in conclusion, a very interesting and original book who real world programmer and consultant using .NET should have in the tool belt. Unfortunately nothing is said about pure Web Programming as well as WPF or WCF and networking: but many concept applies even for these frameworks since we are talking at a very low level approach.

Book has a code we can download online from here. Check out the book from Amazon.

Monday, May 23, 2011 8:58:04 PM (GMT Daylight Time, UTC+01:00)  #    Comments [1] - Trackback
Book | Reviews

Since contacting the original team for sending patches did not success, I decided to share my sources here. You find the bar chart and the candlestick I was talking in this previous post. Hope you find this useful.

Monday, May 23, 2011 11:53:56 AM (GMT Daylight Time, UTC+01:00)  #    Comments [3] - Trackback
Chart | Dynamic Data Display | WPF

# Thursday, May 19, 2011

A new community is born: sqlserver.it. It is a nedw community for Italian sql server developers, and data access technologies related to it, as the many frameworks used for data access.

Thursday, May 19, 2011 6:29:33 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback


# Tuesday, May 17, 2011

This post dig a little more into this blog post code about working with and customizing Dynamic Data Display charting. Since the default bar chart implementation does not work, it’s easy to create our own by simply deriving one chart from PointGraphBase.

 

public class BarChart : PointsGraphBase
{

Then we define the classical bounch of dependencies properties which code is not so fancy to see: Thickness, Stroke and fill brush, and so on. The core function we must implement is OnRenderCore. Let’s see how:

protected override void OnRenderCore(System.Windows.Media.DrawingContext dc, RenderState state)
       {
           if (DataSource == null) return;
           var transform = Plotter2D.Viewport.Transform;

           DataRect bounds = DataRect.Empty;
           using (IPointEnumerator enumerator = DataSource.GetEnumerator(GetContext()))
           {
               Point point = new Point();
               while (enumerator.MoveNext())
               {
                   enumerator.GetCurrent(ref point);
                   enumerator.ApplyMappings(this);

                   Point zero = new Point(point.X, 0);
                   Point screenPoint = point.DataToScreen(transform);
                   Point screenZero = zero.DataToScreen(transform);

                   double height = screenPoint.Y - screenZero.Y;

                   if (height >= 0)
                   {
                       dc.DrawRectangle(Fill, new Pen(Stroke, StrokeThickness)
, new Rect(screenPoint.X - BarWidth / 2, screenZero.Y, BarWidth, height));
                   }
                   else
                   {
                       dc.DrawRectangle(Fill, new Pen(Stroke, StrokeThickness), 
new Rect(screenPoint.X - BarWidth / 2, screenPoint.Y, BarWidth, -height));
                   }

                   bounds = DataRect.Union(bounds, point);
                   
               }
           }

           Viewport2D.SetContentBounds(this, bounds);
       }
So nothing really special, just some key point:
 
  • ApplyMapping is the function who map the model coordinates into the graphical X-Y coordinates.
  • DataToScreen/ScreenToData are the (extension) method who maps from world coordinates to screen coordinates.
 
The other code is just drawing the rectangles for each point at a certain bar width, and we obtain this:
image
 

 

 

As you can probably see, line are anti-aliased even if we specified SnapToDevicePixels. This is due to the fact that SnapToDevicePixels does not work when we manually draw onto a DrawingContext. If we want to remove this we need to follow these instructions and create some GudelineSet to force WPF align to physical bytes.

Tuesday, May 17, 2011 9:50:48 PM (GMT Daylight Time, UTC+01:00)  #    Comments [1] - Trackback
Charting | D3 | WPF

# Monday, May 16, 2011

By extending the generic helper class proposed for a generic CoRoutine in Caliburn Micro it is easy to create an helper that we can use this way:

public IEnumerable<IResult> SourceChange()
       {
           yield return new CoMethod(() => { PriceDS = CreateDataSource(SelectedSource); });
           NotifyOfPropertyChange(() => this.PriceDS);
           yield return new CoMethod(() => { VolumeDS = CreateDataSource(SelectedSource); });
           NotifyOfPropertyChange(() => this.VolumeDS);
           yield return new CoMethod(() => { Mavg5DS = CalculateMAverage(SelectedSource,5); });
           NotifyOfPropertyChange(() => this.Mavg5DS);
           yield return new CoMethod(() => { Mavg10DS = CalculateMAverage(SelectedSource, 10); });
           NotifyOfPropertyChange(() => this.Mavg10DS);
       }

In a single line we invoke asynchronously a method without having to crate a class implementing IResult. By the way this code is the one that creates the sources for the charts in this blog post.

The helper class is pretty easy:

public class CoMethod:AbstractBackgroundAction
    {
        Action toRun;
        public CoMethod(Action toRun)
        {
            this.toRun = toRun;
        }

        protected override void OnExecute(Caliburn.Micro.ActionExecutionContext context)
        {
            toRun();
        }
    }
It just invoke the action in the BackgroundWorker.
Monday, May 16, 2011 8:24:07 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
Caliburn | WPF

My Stack Overflow
Contacts

Send mail to the author(s) E-mail

Tags
profile for Felice Pollano at Stack Overflow, Q&A for professional and enthusiast programmers
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Felice Pollano
Sign In
Statistics
Total Posts: 157
This Year: 0
This Month: 0
This Week: 0
Comments: 123
This blog visits
All Content © 2014, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano