<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rob's Tech Blog &#187; Dot</title>
	<atom:link href="http://www.robfe.com/category/how-to/dot/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robfe.com</link>
	<description>A blog about coding by Rob Fonseca-Ensor</description>
	<lastBuildDate>Fri, 02 Dec 2011 09:03:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Timeago for C#</title>
		<link>http://www.robfe.com/2009/09/timeago-for-csharp/</link>
		<comments>http://www.robfe.com/2009/09/timeago-for-csharp/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 15:56:26 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Dot]]></category>
		<category><![CDATA[date formatting]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.robfe.com/?p=157</guid>
		<description><![CDATA[Sometimes you don&#8217;t want to make your users think. There&#8217;s the odd situation where you want to represent time in natural language: &#8220;about 4 hours ago&#8221; instead of just printing out a full timestamp. If you&#8217;re building a website, then the jQuery plugin Timeago is a pretty sweet way to do it (as long as [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you <a href="http://ayende.com/Blog/archive/2009/09/09/donrsquot-throw-the-information-at-me-i-expect-you-to.aspx">don&#8217;t want to make your users think</a>. There&#8217;s the odd situation where you want to represent time in natural language: &#8220;about 4 hours ago&#8221; instead of just printing out a full timestamp. If you&#8217;re building a website, then the jQuery plugin <a href="http://timeago.yarp.com/">Timeago</a> is a pretty sweet way to do it (as long as <a href="http://stackoverflow.uservoice.com/pages/1722-general/suggestions/96770-auto-update-the-fuzzy-timestamps-with-jquery-timeago-?ref=title">you can stand webpages that auto update text</a>).</p>
<p>Sucks for me, I&#8217;m working with WPF! (Not really sucks at all). So I needed a C# implementation of the same thing. Surely someone&#8217;s done this, right? Well my Google-fu failed me, and even when I Googled on bing I came up with nothing, so I built it myself. And I&#8217;m posting it here for you (and for me, later). If you&#8217;ve found a good one, please let me know.</p>
<p>First, the test cases, so you can see if the format <strong>I </strong>want is the format <strong>you </strong>want:</p>
<pre>[TestClass]
public class FriendlyTimeDescriptionTest
{
    private static string Run(TimeSpan span)
    {
        return FriendlyTimeDescription.Describe(span);
    }

    [TestMethod]
    public void TestNow()
    {
        Assert.AreEqual("now", Run(new TimeSpan(0, 0, 0, 0)));
    }

    [TestMethod]
    public void TestSeconds()
    {
        Assert.AreEqual("1 second ago", Run(new TimeSpan(0, 0, 0, 1)));
        Assert.AreEqual("2 seconds ago", Run(new TimeSpan(0, 0, 0, 2)));
        Assert.AreEqual("59 seconds ago", Run(new TimeSpan(0, 0, 0, 59)));
    }

    [TestMethod]
    public void TestMinutesAndSeconds()
    {
        Assert.AreEqual("about 1 minute ago", Run(new TimeSpan(0, 0, 1, 1)));
        Assert.AreEqual("about 3 minutes ago", Run(new TimeSpan(0, 0, 3, 1)));
    }

    [TestMethod]
    public void TestMinutesAndSecondsRounding()
    {
        Assert.AreEqual("about 4 minutes ago", Run(new TimeSpan(0, 0, 3, 31)));
    }

    [TestMethod]
    public void TestDaysHours()
    {
        Assert.AreEqual("about 3 hours ago", Run(new TimeSpan(0, 3, 3, 1)));
        Assert.AreEqual("about 2 days ago", Run(new TimeSpan(2, 0, 1, 1)));
    }
}</pre>
<p>I&#8217;ve conveniently wrapped all this up into an IValueConverter implementation, but if you&#8217;re not using WPF you can rip out the necessary methods. Please excuse the newline-enthused formatting &#8211; this blog theme has limited column width!</p>
<pre>[ValueConversion(typeof(DateTime), typeof(string))]
public class FriendlyTimeDescription : IValueConverter
{
    public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        var time = System.Convert.ToDateTime(value);
        return Describe(DateTime.Now - time);
    }

    static readonly string[] NAMES = {
                                         "day",
                                         "hour",
                                         "minute",
                                         "second"
                                     };

    public static string Describe(TimeSpan t)
    {
        int[] ints = {
                         t.Days,
                         t.Hours,
                         t.Minutes,
                         t.Seconds
                     };

        double[] doubles = {
                               t.TotalDays,
                               t.TotalHours,
                               t.TotalMinutes,
                               t.TotalSeconds
                           };

        var firstNonZero = ints
            .Select((value, index) =&gt; new { value, index })
            .FirstOrDefault(x =&gt; x.value != 0);
        if (firstNonZero == null)
        {
            return "now";
        }
        int i = firstNonZero.index;
        string prefix = (i &gt;= 3) ? "" : "about ";
        int quantity = (int)Math.Round(doubles[i]);
        return prefix + Tense(quantity, NAMES[i]) + " ago";
    }

    public static string Tense(int quantity, string noun)
    {
        return quantity == 1
            ? "1 " + noun
            : string.Format("{0} {1}s", quantity, noun);
    }

    public object ConvertBack(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}</pre>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfe.com/2009/09/timeago-for-csharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Visualising Data with Dot (Part 2 of 4)</title>
		<link>http://www.robfe.com/2009/03/visualising-data-with-dot-part-2-of-4/</link>
		<comments>http://www.robfe.com/2009/03/visualising-data-with-dot-part-2-of-4/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 21:04:06 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Dot]]></category>
		<category><![CDATA[Digraph]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://www.robfe.com/?p=100</guid>
		<description><![CDATA[Welcome back to my four part series on Dot. In this installment, I go deeper on some of the options available regarding layout of Dot graphs. Part 2: More Dot layout I&#8217;m not sure that I can add value beyond what&#8217;s already available in the documentation - its simple, clear and concise. What I will do, [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome back to my four part series on Dot. In this installment, I go deeper on some of the options available regarding layout of Dot graphs.</p>
<h1>Part 2: More Dot layout</h1>
<p>I&#8217;m not sure that I can add value beyond what&#8217;s <a href="http://www.graphviz.org/doc/info/attrs.html">already available in the documentation</a> - its simple, clear and concise. What I will do, however, is call out the things that I found most useful:</p>
<h2>Colours, fonts and shapes (oh my?)</h2>
<p>You have full control over the following layout attributes:</p>
<ul>
<li>Colour (of <a href="http://www.graphviz.org/doc/info/attrs.html#d:fontcolor">text</a>, <a href="http://www.graphviz.org/doc/info/attrs.html#d:color">outline </a>and <a href="http://www.graphviz.org/doc/info/attrs.html#d:bgcolor">background</a>)</li>
<li><a href="http://www.graphviz.org/doc/info/attrs.html#d:fontname">Font face</a></li>
<li><a href="http://www.graphviz.org/doc/info/attrs.html#d:fontsize">Font size</a></li>
<li><a href="http://www.graphviz.org/doc/info/attrs.html#d:shape">Node shape</a></li>
<li>Edge <a href="http://www.graphviz.org/doc/info/attrs.html#d:arrowhead">head </a>&amp; <a href="http://www.graphviz.org/doc/info/attrs.html#d:arrowtail">tail </a>shapes</li>
</ul>
<p>You could imagine modifying the<a href="http://www.robfe.com/2008/11/visualising-data-with-dot-part-1-of-4/"> sql that I demonstrated in part 1</a> to also select out some colour / formatting attributes into the nodes and edges. Another options is to generate your Dot code in an application layer &#8211; since ruby, C# or java are more expressive with SQL it&#8217;s a bit easier to generate some really interesting graphs.</p>
<h2>HyperDot: URLs and Tooltips</h2>
<p>Did you know that Dot can generate more than just image files? The Dot syntax supports adding tooltips and URLs into nodes, edges, and even arrowheads. However, there&#8217;s no way for this kind of information to be embedded into a gif, png or jpeg. Currently, the output formats that <strong>will</strong> let you see these are:</p>
<ul>
<li><a title="RTFW: SVG" href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics">SVG</a>, which is natively supported by all browsers except for the most important one, is a web-aware xml based vector format (that is soon to be superseded by XAML). In firefox, for example, you could be viewing a graph that will hyperlink you to the appropriate web page when you click on an item on the graph. </li>
<li><a href="http://en.wikipedia.org/wiki/Imagemap">HTML ImageMaps</a> are elements that you can use to tack urls and tooltips onto specific areas of an image in a webpage. If you run Dot twice against the same piece of Dot code (but with different output formats), you can create the imagemap code that corresponds perfectly to the pixels drawn on the image. In Part 4, I will be talking further about how to achieve this in an ASP.Net web server. This functionality replicates what SVG provides you (without the pretty vector scaling, but with superb browser compatibility).  </li>
</ul>
<p>Personally, I am hoping that someone will champion the Silverlight cause and write a native XAML output format for Dot <img src='http://www.robfe.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . I have experimented with a couple of SVG to XAML converters, both XSLT files, but neither of them get the layout just right.</p>
<p><strong>Next in this series:</strong> how to call into Dot from a .Net web server to dynamically generate server side images.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfe.com/2009/03/visualising-data-with-dot-part-2-of-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Visualising Data with Dot (Part 1 of 4)</title>
		<link>http://www.robfe.com/2008/11/visualising-data-with-dot-part-1-of-4/</link>
		<comments>http://www.robfe.com/2008/11/visualising-data-with-dot-part-1-of-4/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 21:17:29 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Dot]]></category>
		<category><![CDATA[Digraph]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://www.robfe.com/2008/11/visualising-data-with-dot-part-1-of-4/</guid>
		<description><![CDATA[Welcome to part one of a four part series on getting some sweet visualisations of workflows, using the open source digraph tool &#8220;Dot&#8221;. This first post will be about dot&#8217;s DSL (which rocks), and how to generate it. Part two will look at some advanced styling and layout. Parts 3 and 4 will be about [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to part one of a four part series on getting some sweet visualisations of workflows, using the open source digraph tool &#8220;Dot&#8221;. This first post will be about dot&#8217;s DSL (which rocks), and how to generate it. Part two will look at some advanced styling and layout. Parts 3 and 4 will be about ways to publish this data over the Internet (Dot is a command line tool).</p>
<p>Dot&#8217;s input language is very simple. Here&#8217;s an example of a digraph generated by dot:</p>
<p><a href="http://www.robfe.com/wp-content/uploads/2008/11/image2.png"><img class="alignnone size-full wp-image-85" title="image2" src="http://www.robfe.com/wp-content/uploads/2008/11/image2.png" alt="" width="381" height="270" /></a></p>
<p> </p>
<p>And the code to create this graph was:</p>
<p>digraph G{ <span style="color: #008000;">//declare a digraph, and give it a name of &#8220;G&#8221;</span><br />
    node[fontname=arial]; <span style="color: #008000;">//sets the default font for all nodes<br />
</span>    bismuth212-&gt;thallium208[label="36%"]; <span style="color: #008000;">//create a labelled edge</span><br />
    bismuth212-&gt;polonium212[label="64%"];<br />
    thallium208-&gt;lead208; <span style="color: #008000;">//create an unlabelled edge</span><br />
    polonium212-&gt;lead208;<br />
    lead208[color=gray] <span style="color: #008000;">//set the colour for the (already declared) lead 208 node<br />
</span>}</p>
<p>You can also give labels to nodes &#8211; the following code creates exactly the same graph:</p>
<p>digraph G{<br />
    node[fontname=arial];</p>
<p>    1[label=bismuth212]<br />
    2[label=thallium208]<br />
    3[label=polonium212]<br />
    4[label=lead208,color=gray]</p>
<p>    1-&gt;2[label="36%"];<br />
    1-&gt;3[label="64%"];<br />
    2-&gt;4;<br />
    3-&gt;4;<br />
}</p>
<p>You can get more information about the Dot language <a href="http://www.graphviz.org/Documentation.php">at the graphviz site</a>.</p>
<p>I have recently started working on a project that involves a simple workflow. This workflow is modelled in my database. I&#8217;ve simplified it a bit, but here&#8217;s my schema:</p>
<p> <img src="http://www.robfe.com/wp-content/uploads/2008/11/image1.png" border="0" alt="image" width="566" height="176" /></p>
<p>And here&#8217;s my data:</p>
<table border="1" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td width="249" valign="top">State</td>
<td width="249" valign="top">Transition</td>
</tr>
<tr>
<td width="249" valign="top">ID    Name<br />
1    Start<br />
2    Write test<br />
3    Run test (red stage)<br />
4    Write function<br />
5    Run test (green stage)<br />
6    Refactor<br />
7    Run test (refactor stage)<br />
8    Finish</td>
<td width="250" valign="top">ID    FromStateID    ToStateID    Description<br />
2    1    2   <br />
3    2    3   <br />
4    3    2    Passed<br />
5    3    4    Failed<br />
6    4    5   <br />
7    5    6    Passed<br />
8    5    4    Failed<br />
9    6    7   <br />
10    7    8    Passed<br />
11    7    4    Failed</td>
</tr>
</tbody>
</table>
<p> </p>
<p>Not that easy to follow, huh? And this is a <em>very </em>simple example.</p>
<p>However, if we run the following (simple) SQL:</p>
<p> </p>
<pre class="code"><span style="color: blue">DECLARE </span>@crlf <span style="color: blue">varchar</span><span style="color: gray">(</span>2<span style="color: gray">)
</span><span style="color: blue">SET </span>@crlf <span style="color: gray">= </span><span style="color: blue">char</span><span style="color: gray">(</span>13<span style="color: gray">)+</span><span style="color: blue">char</span><span style="color: gray">(</span>10<span style="color: gray">)

</span><span style="color: blue">DECLARE </span>@s <span style="color: blue">varchar</span><span style="color: gray">(</span><span style="color: magenta">max</span><span style="color: gray">)
</span><span style="color: blue">SET </span>@s <span style="color: gray">= </span><span style="color: red">'  node[fontname=arial];'</span><span style="color: gray">+</span>@crlf

<span style="color: blue">SELECT </span>@s<span style="color: gray">=</span>@s<span style="color: gray">+</span><span style="color: red">'  '
    </span><span style="color: gray">+</span><span style="color: magenta">convert</span><span style="color: gray">(</span><span style="color: blue">varchar</span><span style="color: gray">,</span>ID<span style="color: gray">)
    +</span><span style="color: red">'[label="'</span><span style="color: gray">+</span><span style="color: blue">Name</span><span style="color: gray">+</span><span style="color: red">'"];'
    </span><span style="color: gray">+</span>@crlf
<span style="color: blue">FROM </span>State

<span style="color: blue">SELECT </span>@s<span style="color: gray">=</span>@s<span style="color: gray">+</span><span style="color: red">'  '
    </span><span style="color: gray">+</span><span style="color: magenta">convert</span><span style="color: gray">(</span><span style="color: blue">varchar</span><span style="color: gray">,</span>FromStateID<span style="color: gray">)
    +</span><span style="color: red">'-&gt;'</span><span style="color: gray">+</span><span style="color: magenta">convert</span><span style="color: gray">(</span><span style="color: blue">varchar</span><span style="color: gray">,</span>ToStateID<span style="color: gray">)
    +</span><span style="color: red">'[label="'</span><span style="color: gray">+</span>Description<span style="color: gray">+</span><span style="color: red">'"];'
    </span><span style="color: gray">+</span>@crlf
<span style="color: blue">FROM </span>Transition

<span style="color: blue">SELECT </span><span style="color: red">'digraph G{'</span><span style="color: gray">+</span>@crlf<span style="color: gray">+</span>@s<span style="color: gray">+</span><span style="color: red">'}'</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>we&#8217;ll get the following output:</p>
<p>digraph G{</p>
<p>  node[fontname=arial];</p>
<p>  1[label="Start"];</p>
<p>  2[label="Write test"];</p>
<p>  3[label="Run test (red stage)"];</p>
<p>  4[label="Write function"];</p>
<p>  5[label="Run test (green stage)"];</p>
<p>  6[label="Refactor"];</p>
<p>  7[label="Run test (refactor stage)"];</p>
<p>  8[label="Finish"];</p>
<p>  1-&gt;2[label=""];</p>
<p>  2-&gt;3[label=""];</p>
<p>  3-&gt;2[label="Passed"];</p>
<p>  3-&gt;4[label="Failed"];</p>
<p>  4-&gt;5[label=""];</p>
<p>  5-&gt;6[label="Passed"];</p>
<p>  5-&gt;4[label="Failed"];</p>
<p>  6-&gt;7[label=""];</p>
<p>  7-&gt;8[label="Passed"];</p>
<p>  7-&gt;4[label="Failed"];</p>
<p>}</p>
<p>Which we can run through the dot tool:</p>
<p>C:\Program Files\Graphviz 2.21\bin&gt;dot -Tgif -ooutput.gif</p>
<p><img src="http://www.robfe.com/wp-content/uploads/2008/11/output.gif" border="0" alt="output" width="456" height="843" /></p>
<p>It&#8217;s not pretty (yet), but it&#8217;s so much easier to follow than looking at numbers. Hopefully I&#8217;ve got you thinking about how you could rewrite that SQL statement to work against your own database tables.</p>
<p>I&#8217;ll be posting a guide to styling and laying out this Dot diagram soon &#8211; stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfe.com/2008/11/visualising-data-with-dot-part-1-of-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

