It may not be new but it is new to me.
Windows Azure Mobile Services (from Scott Gu)
Windows Azure Training Kit
Blogging Wishlist, Early September 2012
I’m less than two weeks into my new job at Microsoft as a Windows Phone Technical Evangelist and I’m learning an amazing number of new things (including a number of things that I can’t blog about). But I want to keep updating here with things I am learning and things I want to learn.
I Am Learning
How to save JPG images within a Windows Phone app specifically for display in Windows Phone tiles (hopefully a blog post forthcoming).
The Coding4Fun Windows Phone Toolkit is super awesome. A great set of tools and I’m disappointed I didn’t see these before.
I ran across this Hanseminutes podcast on technical presenting with Scott Hanselman and Drew Robbins. Not only are there a lot of great tips, it is inspiring to learn how much work and thought they put into their talks.
I’m getting more and more inspired by Iris Classon and her 365 Stupid Questions series. I’m learning a lot about smaller programming concepts I never thought to question.
I was skeptical about Windows 8 from the get-go. I was worried it was too much of a leap, many parts of it seemed foreign, it didn’t feel like a desktop OS but didn’t feel like a tablet OS either. But when I came to Microsoft, I got a multi-touch convertible tablet. And… wow. Using Windows 8 on this thing is a dream. I have a nano receiver for my mouse and I switch seamlessly between keyboard, mouse and touch interactions. With this hardware, I can clearly see the vision that was driving Windows 8. Microsoft sees a world in which everything is touch-enabled and Windows 8 is an amazing step toward enabling that world.
I Want To Learn
More about Azure. I’m working around some of the Azure team and there seems to be an ocean of things that Azure can enable.
Coding for devices. I was an early Pebble backer (I got the Hacker Special) and I want to be more comfortable jumping into developing for (and between) devices.
Creating Visual Studio project templates. I’d like to help developers jump-start their phone projects a little quicker and this seems like a good way to do so. (If you think I’m wrong or you have a better way, please let me know in the comments.)
More Windows 8 development. I’d be all over this right now but I’m totally consumed with my new job working on… um… you know… things.
Creating a Javascript Treemap using JIT (Tutorial)
I really like interactive treemaps, which I feel are a delightfully intuitive way to look at and dig into data (especially nested data). Used along with color, treemaps can communicate of information at a single glance. Great examples include the New York Times visualization of President Obama’s proposed 2012 budget and the classic “Map of the Market” visualization of the daily stock market.
They are, however, difficult to create from scratch. There is no one algorithm that you “should” use to create the sizes (although the squarified treemap (PDF) is my favorite). So I try to find a framework that I can plug into for building treemaps. As I’ve been working away from Silverlight and into Javascript recently, I decided on the Javascript InfoVis Toolkit (JIT) implementation.
Their static demo is pretty good, but I thought it could use a bit of a more clear explanation of the sample code.
The Data
First thing we look at is the data used to create our treemap. The basic object that is being used here is (in JSON format):
{
"id": "uniqueID",
"name": "Top Data Point 1",
"data":
{
"$color": "#1F281C",
"$area": 100,
"OptionalVar1": "Some text or HTML for the hover over"
},
"children":
[
{(TopData1_Subpoint1)},
{(TopData1_Subpoint2)},
{(TopData1_Subpoint3)}
]
}
Here is a quick description of how the data works:
- id – must be unique or elements will be confused and the treemap render won’t work.
- name – The text here will show up in the box as the data label.
- $color – the hexidecimal color code for the box square
- $area – this is the most vital bit of data. The JIT treemap will take the sum of the available $area values in a space and squarify that space using these values.
- OptionalVar1 – This isn’t at all necessary, but you can name any other variable you like in the data object and then use those variables to add more information to the hover object.
- children – an array of data points (same type of objects ) to nest inside this space. The way this treemap is calculated, however, does not include dependencies on parent area. That is to say: The area for the parent datapoint may be 100, but the sum of it’s children could be larger or smaller than 100 and it will still work (in the sense of not breaking). It will, however, be an inaccurate visual representation.
If the data point does have children, the $color will only apply to the header of the square. Here’s an example from a version of this treemap that maps out jobs in the US for the Trade/Transportation/Utilities sectors.
For data points that have children, only the headers are colored according to $color because the children take up the rest of the space. It is easer to see these relationships when interacting with the treemap, but it is a bit of a visual downside.
The Code
Since this is a tutorial on just getting your first treemap to work, we’re going to go through a tweak of the default code example.
When the treemap is initialized, it is done through
var tm = new $jit.TM.Squarified({
along with a bunch of “[property]:[value]” elements. One of those elements is “Tips” which contains an “onShow” event. This constructs the hover tip when the user hovers over a piece of data. Using our data above, we might build a hover that looks like this:
onShow: function (tip, node, isLeaf, domElement) { var html = "<div class=\"tip-title\">" + node.name + "</div><div class=\"tip-text\">"; var data = node.data; if (data.OptionalVar1) { html += "<br />" + data.OptionalVar1; } tip.innerHTML = html; }
That’s about it. You can see an example of this working here and download the relevant code here.
Beginning iOS: EXC_BAD_ACCESS
I’ve been playing around with learning iOS for a little while and just ran into this. My project builds, but then I run into an “EXC_BAD_ACCESS (code=1, address=blahblahblah)” error.
The Simple Version
I created a NSArray of strings but when I should have written it like this:
NSArray *myArray = [NSArray arrayWithObjects:@”One String”, @”Two String”, @”Red String”, @”Blue String”, nil];
I had written my string like this:
NSArray *myArray = [NSArray arrayWithObjects:@”One String”, “@Two String”, @”Red String”, @”Blue String”, nil];
This will still build successfully, but it won’t run. So, the simple version is I’m careless.
The Meaningful Version
You can get a more extended look at EXC_BAD_ACCESS in this post by Lou Franco, but the basics of it is this.
When adding a string (NSString) to the array (NSArray), the @ symbol is a short-hand way of allocating the string in memory. If the NSString has not been allocated, we’ve got a missing pointer that has never been property initalized and so we get a EXC_BAD_ACCESS error.
Calculating Color Values for Visualization (C#)
One of the vital elements of many visualizations is assigning a a color based on the correlated data. The most common way to think about this is that there is a color line representing values from a minimum to a maximum. Wherever the data point falls along that line determined the color assigned. This can be done using 2 colors (a minimum color and a maximum color) or 3 colors (an additional color representing the middle value).
Using these color scales, we can color data points as seen below:
Of course, the value doesn’t need to be 0-to-100. For example, if you’re visualizing grades, you may go from 60 – 100 (F-A). If you’re looking at stocks, you may go from –3% to 3%. So any algorithm we have that makes this conversion needs to do so from an arbitrary minimum to an arbitrary maximum. It will also need to “cap” the color translation at the min and max. For example, in the 105% sample above, we would color that value at the maximum instead of calculating a “more green” or “more blue” color.
Conceptually, this is the idea:
We separate the color elements (red, green, and blue) for our minimum color, maximum color, and (optionally) mid colors along a value line. We the find out where our value to convert to a color is along that value line and we can use the corresponding RGB values to generate our color. I think of it like this (RGB values taken from the red-to-yellow-to-green gradient above):
Here is the method that I use for 2 color conversion like the white-to-blue line (in C#):
public Color GetColorFrom2ColorScale(double value, double minValue, double maxValue, Color minColor, Color maxColor) { if (maxValue == minValue) return Color.FromArgb(255, 0, 0, 0); // in most cases the max value will be larger // than the min value if (maxValue > minValue) { // If the value is higher than the maxValue, // make sure we use the maxColor if (value > maxValue) value = maxValue; // If the value is lower than the minValue, // make sure we use the minColor else if (value < minValue) value = minValue; } // but in other cases (like golf scores) our // maxValue (better) might be lower than our // minValue (worse) else { if (value < maxValue) value = maxValue; else if (value > minValue) value = minValue; } // Find the R and G and B values individually by // 1) finding the position of the value along the // min-max line double valuePosition = (value - minValue) / (maxValue - minValue); // 2) finding the range between min and max for // each color element => (maxColor.R - minColor.R) // 3) multiplying that difference by the value position => // ((maxColor.R - minColor.R) * valuePosition) // 4) adding that color position to the minimum color value => // minColor.R + (etc) double valueR = minColor.R + ((maxColor.R - minColor.R) * valuePosition); double valueG = minColor.G + ((maxColor.G - minColor.G) * valuePosition); double valueB = minColor.B + ((maxColor.B - minColor.B) * valuePosition); // we use a simple method to convert the resulting double to a byte // so we can create a new color from it. return Color.FromArgb(255, byteColor(valueR), byteColor(valueG), byteColor(valueB)); ; } private byte byteColor(double value) { if (value > 255) value = 255; if (value < 0) value = 0; return Convert.ToByte(value); }
And here is the method I use for 3 color conversion (the red-to-yellow-to-green line) (in C#)
public Color GetColorFrom3ColorScale(double value, double minValue, double maxValue, Color minColor, Color midColor, Color maxColor) { if (maxValue == minValue) return Color.FromArgb(255, 0, 0, 0); // in most cases the max value will be larger // than the min value if (maxValue > minValue) { // If the value is higher than the maxValue, // make sure we use the maxColor if (value > maxValue) value = maxValue; // If the value is lower than the minValue, // make sure we use the minColor else if (value < minValue) value = minValue; } // but in other cases (like golf scores) our // maxValue (better) might be lower than our // minValue (worse) else { if (value < maxValue) value = maxValue; else if (value > minValue) value = minValue; } // Get the mid point so we know if the value is between // between the minColor and the midColor or the // midColor and the maxColor double midPointValue = ((maxValue - minValue) * .5) + minValue; bool isValueInHighEnd = false; if (maxValue > minValue && value > midPointValue) isValueInHighEnd = true; else if (maxValue < minValue && value < midPointValue) isValueInHighEnd = true; double valueR = 0; double valueG = 0; double valueB = 0; // if the value is "in the high end", we'll calculate between // the midColor and the maxColor if (isValueInHighEnd) { // Find the R and G and B values individually by // 1) finding the position of the value along the // mid-max line double valuePosition = (value - midPointValue) / (maxValue - midPointValue); // 2) finding the range between mid and max for // each color element => (maxColor.R - midColor.R) // 3) multiplying that difference by the value position => // ((maxColor.R - midColor.R) * valuePosition) // 4) adding that color position to the minimum color value => // midColor.R + (etc) valueR = midColor.R + ((maxColor.R - midColor.R) * valuePosition); valueG = midColor.G + ((maxColor.G - midColor.G) * valuePosition); valueB = midColor.B + ((maxColor.B - midColor.B) * valuePosition); } // otherwise we'll calculate between the minColor and the midColor else { // See above for details... the concept is the same double valuePosition = (value - minValue) / (midPointValue - minValue); valueR = minColor.R + ((midColor.R - minColor.R) * valuePosition); valueG = minColor.G + ((midColor.G - minColor.G) * valuePosition); valueB = minColor.B + ((midColor.B - minColor.B) * valuePosition); } // we use a simple method to convert the resulting double to a byte // so we can create a new color from it. return Color.FromArgb(255, byteColor(valueR), byteColor(valueG), byteColor(valueB)); ; } private byte byteColor(double value) { if (value > 255) value = 255; if (value < 0) value = 0; return Convert.ToByte(value); }
And if you need to convert that value into a hex, you can use the following hex conversion that I pulled from steve over at Cambia Research.
public string ColorToHex(Color c) { char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; byte[] bytes = new byte[3]; bytes[0] = c.R; bytes[1] = c.G; bytes[2] = c.B; char[] chars = new char[bytes.Length * 2]; for (int i = 0; i < bytes.Length; i++) { int b = bytes[i]; chars[i * 2] = hexDigits[b >> 4]; chars[i * 2 + 1] = hexDigits[b & 0xF]; } string hex = "#" + new string(chars); return hex; }
Logging Into Facebook With Windows Phone 7 (Update)
I covered this topic on my previous blog but then Facebook depreciated some stuff and the Facebook C# SDK changed so this is an update to that process.
I’ve tried to make this a full featured tutorial, so if you just want to get to the code, head down to the “Let the User Login To Facebook” section.
Get Your Facebook Key
Go to http://developers.facebook.com and log in with your Facebook account. Give them the permissions they need and go to the “Apps” tab. Click on “Create new App”
Fill in the App Display Name and the App Namespace and do the verification. Then you’ll get to a page that gives you your App ID and App Secret.
Don’t worry about anything else here… you don’t need to select anything in the “how your app integrates with Facebook” section to do this tutorial. If you’re using Facebook integration to do some basic login and simple posting, you could probably just use the Website option.
Also, click the “Graph API Explorer” on the left and keep that open. We’ll come back to that in a moment.
Add Facebook C# SDK
In Visual Studio go to Tools -> Library Package Manager -> Package Manager Console and type
Install-Package Facebook
And then
Install-Package Newtonsoft.Json
And *poof* you should have the Facebook C# SDK added to your project as well as a great JSON parser that will come in handy later.
Build Facebook Login UI
We’ll do this quick here (no MVVM, no bindings) but in later versions I’ll integrate this into a more formal project.
Open your project in Blend. For this quick-and-dirty tutorial, we’ll just add another page title and another panel for the UI we want to show when the user is logged in and set the Visibility to Collapsed on those. Our visual tree should look like this.
In our loginPanel, we’re going to add a button and a WebBrowser to our panel. Set the button (containing “sign in using facebook” in the content) to the top and the WebBrowser to fill the panel. For a little flare, I’m going to add “ShowFacebookLogin” and “HideFacebookLogin” animations. My login screen now looks like this (the WebBrowser will animate in when we press the button).
When we get our data back, we’re going to tell the user it worked by showing their name and their Facebook avatar. So we’ll add a Grid with an Image and a TextBlock to display the user, along with a friendly “Hello”. (Make sure to name all these things so that we can update them from the code.)
OK… out UI is simple, but ready to roll. Now let’s do something when the user clicks the sign-in button. Go to the “Click” event and add a method like “StartFacebookLogin”.
Let the User Login to Facebook
Add “using Facebook;” to your references at the top of your MainPage.xaml.cs file. Now, add the following properties:
private FacebookClient _asyncFbClient; private string _appID = "get from your facebook app page"; private string _appSecret = "get from your facebook app page";
Now go to your StartFacebookLogin method and add the following (explained at the end).
private void StartFacebookLogin(object sender, RoutedEventArgs e) { var uriElements = new Dictionary<string, object>(); uriElements.Add("client_id", _appID); uriElements.Add("redirect_uri", "https://www.facebook.com/connect/login_success.html"); uriElements.Add("response_type", "token"); var sb = new StringBuilder(); sb.Append("https://m.facebook.com/dialog/oauth?"); foreach(var kvp in dictionary) sb.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value.ToString())); sb.Length--; webBrowser.Navigated += new EventHandler<NavigationEventArgs>(CheckForAuth); webBrowser.Navigate(new Uri(sb.ToString())); }
This is a big change from the previous Facebook authentication process. Instead of adding our permissions, they are baked into our Facebook app. We just need to point to the mobile Facebook oauth page and let them know that we’d like to use the default redirect Uri.
At the point Facebook walks the user through their login (which should include identifying the device and giving the user info on what kind of sharing they can expect from our app) When they are done, it hands us back the access token we’ll need to get the user info and let the user make posts through our app.
This is why we attached the CheckForAuth event handler to our webBrowser. This is another big change from the previous version. We’ll be using our FacebookClient object
When we navigate to a new page, we’ll check to see if we got an access token using this code
private void CheckForAuth(object sender, System.Windows.Navigation.NavigationEventArgs e) { FacebookOAuthResult result; _asyncFbClient = new FacebookClient(); if (_asyncFbClient.TryParseOAuthCallbackUrl(returnUri, out result)) { if (result.IsSuccess) { IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings; if(Settings.Contains("MyFacebookAccessToken")) Settings["MyFacebookAccessToken"] = result.AccessToken; else Settings.Add("MyFacebookAccessToken", result.AccessToken); Settings.Save(); _asyncFbClient = new FacebookClient(result.AccessToken); _asyncFbClient.GetCompleted += new EventHandler<FacebookApiEventArgs>(_asyncFbClient_GetCompleted); _asyncFbClient.GetAsync("me"); } } }
If the Uri does contain a Facebook OAuth result and it is a success, we save the access token to our settings and then immediately use it to get the most basic user information.
The problem we have right now is that we have no class to structure the data that comes back. So get that all squared away.
First, right-click on “References” and select “Add Reference…”. Select “System.Runtime.Serialization”.
Next, add a class to your project (I added mine in a folder called “Models”) and name it FacebookUser.cs.
In that class, add the following code
[DataContractAttribute] public class FacebookUser { public FacebookUser() { } private string _id; [DataMember(Name = "id")] public string ID { get { return _id; } set { _id = value; } } private string _name; [DataMember(Name = "name")] public string Name { get { return _name; } set { _name = value; } } }
The sample project has a much larger (although still incomplete) version of this model. But this one will do for our purposes.
We’ll make our “GetCompleted” event handler (remember that?) look like this:
void _asyncFbClient_GetCompleted(object sender, FacebookApiEventArgs e) { FacebookUser _fbUser = JsonConvert.DeserializeObject<FacebookUser>(e.GetResultData().ToString()); Deployment.Current.Dispatcher.BeginInvoke(() => { fbName.Text = _fbUser.Name; BitmapImage bi = new BitmapImage(new Uri("https://graph.facebook.com/" + _fbUser.ID + "/picture")); fbAvatar.Source = bi; HideFacebookLogin.Begin(); }); _asyncFbClient.GetCompleted -= _asyncFbClient_GetCompleted; }
What we’ve done here is shove our data into a FacebookUser model. Then we use BeginInvoke to bring ourselves back to the UI thread and set all the properties we want.
Finally, we start the animation that hides the login data and shows that our Facebook login was a success.
Boom!
My New Bloggy Home
My last two blogs had names that were attached to specific technologies (Designer WPF and Designer Silverlight) and I think I’ve learned a bit of a lesson on that count. So this is my new bloggy home.
The conceit behind my blog name is the idea (hope) that I can learn something new every two weeks and blog about it. So this is very goal-oriented, but not technology oriented. Among my targets for things to learn are python (for the purposes of data analysis), visualization technologies and concepts, Lego Mindstorms, Microsoft technologies that aren’t a part of my job, and any mobile programming I can get my hands on (Android, iOS, WinPhone, HTML5, MonoTouch… anything I can manage to learn).
So welcome! I hope I can make amazing things and share them. Here we go.