Thursday, May 26, 2011

TreeMap and Zoom Control onto TreeGraph (WPF Toolkit and Extension)


WPF ToolKit and CodePlex WPF Extension

<Window x:Class="GMO.Monitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:dv="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:tc="clr-namespace:TreeContainer;assembly=TreeContainer"
xmlns:wpfec="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
x:Name="M"
>
<Grid >
<!--<WrapPanel>-->
<DockPanel>
<dv:TreeMap Name="tmp1" Height="Auto" Width="200" >
<dv:TreeMap.Interpolators>
<dv:SolidColorBrushInterpolator
TargetName="bdr"
TargetProperty="Background"
DataRangeBinding="{Binding Path=totalValue}"
From="Green"
To="White"/>
</dv:TreeMap.Interpolators>
<dv:TreeMap.ItemDefinition>
<dv:TreeMapItemDefinition
ItemsSource="{Binding}"
ValueBinding="{Binding Path=totalValue}"
>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" Name="bdr" MouseDown="bdr_MouseDown" ToolTip="{Binding Path=ToolTip}" >
<TextBlock Text="{Binding Path=shortname}" VerticalAlignment="Center" Margin="2,2,0,0" FontSize="10" />
</Border>
</DataTemplate>
</dv:TreeMapItemDefinition>
</dv:TreeMap.ItemDefinition>
</dv:TreeMap>
<wpfec:ZoomControl >

<Grid Width="1000" Height="1000" Background="Beige">
<Canvas>
<tc:TreeContainer tc:TreeContainer.Root="O" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="tc1" HorizontalBuffer="38" VerticalBuffer="48" HorizontalBufferSubtree="40" VerticalJustification="top">

</tc:TreeContainer>
</Canvas>
</Grid>
</wpfec:ZoomControl>
</DockPanel>
<!--</WrapPanel>-->
</Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Controls.DataVisualization;
using GMO.Monitor.Model;
using TreeContainer;
using System.Collections.ObjectModel;

namespace GMO.Monitor
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
ApexDevEntities ctx = new ApexDevEntities();
public MainWindow()
{
InitializeComponent();
var q = from t in ctx.GetDashboard() select new { t.shortname, t.totalValue, t.groupid, ToolTip = t.shortname + "," + t.groupid.ToString() };
tmp1.ItemsSource = q;
}

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
//vb.Width = this.ActualWidth - tmp1.ActualWidth; ;
//vb.Height = this.ActualHeight - tmp1.ActualHeight;
base.OnRenderSizeChanged(sizeInfo);
}

private void bdr_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount <= 1) return;

Border bdr = sender as Border;
string tip = bdr.ToolTip.ToString();
int GrpId = Convert.ToInt32(tip.Split(',')[1]);
TextBlock tb = bdr.Child as TextBlock;
tc1.Clear();
TreeNode root= tc1.AddRoot(tb.Text);
var q = from t in ctx.CustomGroups where t.RelatedGroupId == GrpId select t;
foreach (var q1 in q)
{
TextBlock tb1 = new TextBlock() { Text = q1.CustomGroupName};
TreeNode tn= tc1.AddNode(tb1, root);
tn.Tag = q.FirstOrDefault();
// tn.MouseDoubleClick += new MouseButtonEventHandler(tn_MouseDoubleClick);
// TreeNode tn = sender as TreeNode;
DateTime dt = new DateTime(2011, 2, 1);
string subCGstr = "a,0,b,1,c,2";
var q2 = from t in ctx.GetCustomGroupExposures(dt, q1.CustomGroupId, true, subCGstr) select t;
foreach (var q3 in q2)
{
TextBlock tb3 = new TextBlock() { Text = q3.Bet + "\r\n " + q3.Exposure.ToString() + "\r\n " + q3.PercentExposure.ToString() };
TreeNode tn1 = tc1.AddNode(tb3, tn);
}
}
}

void tn_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
tc1.Clear();
TreeNode tn = sender as TreeNode;
CustomGroup cg = tn.Tag as CustomGroup;
TextBlock tb= new TextBlock() { Text= cg.CustomGroupName};
TreeNode root = tc1.AddRoot(tb);
DateTime dt = new DateTime(2011,2,1);
var q = from t in ctx.GetCustomGroupExposures(dt, cg.CustomGroupId, true, "") select t;
foreach (var q1 in q)
{
TextBlock tb1 = new TextBlock() { Text = q1.Bet+" "+q1.Exposure.ToString() +" "+q1.PercentExposure.ToString() };
TreeNode tn1 = tc1.AddNode(tb1, root);
}
}
}

public class Data1
{
public string shortname { get; set; }
public double totalValue { get; set; }
}
}

Saturday, April 23, 2011

Generating C# code from Shader File (WIP)

Shazzam tools generate C# code

Sunday, April 17, 2011

Data Visualization



1. Quiet Status on Dashboard should have non-alerting color

2. Single Number should be communicated with alerting color, Icon graphic ( tiny, "SparkCon ???")

3. Large data set are good for Line Chart Period Analysis. Small data set mush rely on Bar Chart.

4. Period comparison should not use line chart overlay. Comparison= Delta of %

5. How to Breakdown a variable:
Human are good in visualizing Height no area plot, unit plot, Pie chart ( need to calculate area, counting, pizza size w/o number). Sorted Bar chart are better.

6.Breakdown over a period of time:
Bar chart side by side comparison with different color will show difference and trend
Stack line or bar are not clear.

7. How to visuaize Category:
Map, Shapes, Color, Lines

8. How to visualize Quantity:
Size, Count, Speed, Color Intensity, Height Comparison.

9. Typical usage: Cluster Point Chart, Bar with Median, Mean, Sorting, Markup Chain Workflow Graph. Highlighting GridCell, Projectin, Targeting

10. Other techniques: Click-through, Grouping, Multi-factor, Summary and Rating, Confidence Level to filter out outliers

Wednesday, March 30, 2011

App Fabric Caching Client Code



PorfolioExplorer.cs :: public void LoadDataFromRootFundComboBox()
{
...
LoadData(SelectedGroup);


cacheCluster = new DataCacheServerEndpoint[1];
cacheCluster[0] = new DataCacheServerEndpoint(
"jqdappfabric",
// "10.32.68.131",
22233);

DataCacheFactoryConfiguration cfg = new DataCacheFactoryConfiguration();
cfg.Servers = cacheCluster;
DataCacheFactory factory = new DataCacheFactory(cfg);
cache = factory.GetDefaultCache();
try
{
if (EventEngine.Instance.CurrentHoldingComposite != null)
cache.Put("CurrentHoldingComposite", EventEngine.Instance.CurrentHoldingComposite, new TimeSpan(240, 0, 0));
if (EventEngine.Instance.RootFund != null)
cache.Put("RootFund", EventEngine.Instance.RootFund, new TimeSpan(240, 0, 0));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

DataCacheFactory factory;
DataCache cache;
DataCacheServerEndpoint[] cacheCluster;

AppConfig
=========

<configSections>
<!-- required to read the <dataCacheClient> element -->
<section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowLocation="true" allowDefinition="Everywhere"/>

<dataCacheClient>
<transportProperties maxBufferPoolSize="7" maxBufferSize="2000000000"/>


<!-- (optional) specify local cache
<localCache
isEnabled="true"
sync="TimeoutBased"
objectCount="100000"
ttlValue="300" /> -->

<!--(optional) specify cache notifications poll interval
<clientNotification pollInterval="300" /> -->

<hosts>
<host name="jqdappfabric" cachePort="22233"/>
<!-- name="CacheServer2"
cachePort="22233"/>-->
</hosts>
</dataCacheClient>

Note that there are 2 DLL from server that can be used on XP ( only suppoed to installed to Win7 but it works
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll

The other two may be useful:
Microsoft.WindowsFabric.Common.dll
Microsoft.WindowsFabric.Data.Common.dll



Friday, February 25, 2011

WPF 300DPI High Resoluation Printing



PrintDialog dlg = new PrintDialog();
GeneralTransform transformToRoot = this.TransformToAncestor(win_root);

Rect rect = new Rect(transformToRoot.Transform(new Point(0, 0)), transformToRoot.Transform(new Point(this.ActualWidth, this.ActualHeight)));

double scale = 300 / 96;

RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(scale * (rect.Width + 1)), (int)(scale * (rect.Height + 1)), scale * 96, scale * 96, PixelFormats.Default);
bitmap.Render(g); // g=grid

Image img = new Image();
img.Source = bitmap;
img.Stretch = Stretch.None;

img.Measure(new Size(dlg.PrintableAreaWidth,
dlg.PrintableAreaHeight));
Size sizeImage = img.DesiredSize;
img.Arrange(new Rect(new Point(0, 0), sizeImage));

dlg.PrintVisual(img, "Test300DPI");

For 3D:

Rect = Viewport3DVisual.ViewPort and it comes from visual brush:

<Border>
<Border.Background>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<Viewport3DVisual x:Name="visual3d" Viewport="0 0 384 384">


Saturday, February 19, 2011

Create OData Producer


(1) Create a Web App and add .mdf file to App Data Folder
(2) Add EF edmx and drag drop the .mdf's table (SQL Express)
(3) Add new item Web ->WCF Data Service (Will generate Atom Data Feed Service)

(4)
public class WcfDataService1 : DataService<AdventureWorks_DataEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Products", EntitySetRights.AllRead);
config.SetEntitySetPageSize("Products", 2);
.. }

[QueryInterceptor("Products")]
public Expression<Func<Product,bool>> OnQueryCustomer()
{
return p => p.Name.Contains("Adjustable");
}
(5) Test in browser http://localhost/WcfDataService1/Products?$select=Name
...?$top=5
Feed Reader can be turned off Tool->Internet option->content->..

(6) Also test using Silverlight Client
...
AdventureWorks_DataEntities _svs;
DataServiceCollection<Product> _products;
public MainPage()
{
InitializeComponent();

_products.LoadCompleted += new EventHandler(_products_LoadCompleted);

Tuesday, February 15, 2011

WCF NamedPipeBinding in AppFabric



(1) Create anApp under default site running ASP.Net 4.0 AppPool
( net.pipe cannot have port number)
(2) Setting Binding for defulat site and App Advanced Setting to Enable net.pipe protocol

(3)Create a WCP Lib project by VS.net 2010 and publish into the App
(if svc file are missing, manually add one. It should be there if protocol include wsHttp*)
(4) VS.net 2001 ->Tool->WCF Config editor to edit App Web Config and add net.pipe endpoint and mexnetpipe endpoint. Note that Address for net.pipe can be empty but must set up mex for net.pipe.
(5) IIS Manager -> Manage WCF -> Config --> auto-start enabled

(6) svcutil net.pipe://localhost/TestNP/WcfServiceLibrary6.Service1.svc

(7) Client Code:
Service1Client c = new Service1Client("NetNamedPipeBinding_IService1");
c.Open();
string s= c.GetData(123));
c.Close();


<endpoint binding="netNamedPipeBinding" contract="WcfServiceLibrary6.IService1" />
<endpoint address="mex" binding="mexNamedPipeBinding" bindingConfiguration="" contract="IMetadataExchange" />
Note: binding config is optional
<bindings>
<netNamedPipeBinding>
<binding name="binding1">
<security mode="None" />
</binding>
</netNamedPipeBinding>
</bindings>