Sunday 31 January 2016

HTML Horizontal Bar Chart

Introduction

We are now going a step ahead in continuation with what we saw in the HTML Vertical Bar Chartarticle. In line with what we have seen, we are now going to see how we can generate colorful horizontal bar charts for our web application. As mentioned earlier, we can work with any of the wonderful tools and technologies like Microsoft Office web office components, some third party controls to generate charts, some great ways using System.Drawing things to display charts, etc. Again, I found that using plain HTML with some JavaScript is the simplest among all of them to do the same thing. Well, at first glance, it could seem to be a horrifying idea, but working with basics for achieving big requirements is never bad. Let me keep it very short and simple and see how this can be achieved.

Generating horizontal bar charts

Here we are going to look at the steps we need to follow to have effective HTML-based horizontal bar charts. We are first going to see how we can generate the chart for one data series pair, i.e. a chart for a series that has Y-axis values and corresponding X-axis values. Later we are going to take a step ahead and see a chart for the series that has Y-axis values and a corresponding "set" of X-axis values. So let us get into action.

Single horizontal bar chart

Our single horizontal chart is going to look somewhat like this:
As we have done with the HTML vertical bar chart, we are going to follow some very simple steps and see how this colorful chart is generated. The basic building blocks of this horizontal bar chart are going to be an HTML table, <DIV>s and <P> (the HTML paragraph tag). It is basically a set of <DIV>s and<P>s placed inside the <TD>s of HTML table <TR>s. This chart table needs only two columns in the desired number of rows. The first column will contain the axis values and the other will contain the<DIV>s with colorful backgrounds of the desired width to represent a bar in the chart. The values on each bar can be displayed with the <P></P> tags. Let us cook up this chart step-by-step.
  1. Take an HTML table. Your HTML code looks like this:
    <table></table>
    
  2. Determine how many rows you need in the table by determining the number of items you want to have in the chart. Let us say you got rowNum as the number of rows you need. In the example attached, it is 12.
  3. In each row, add two columns. With this, your HTML code should look something like this:
    <table>
        .
        .
        .
        <tr>
            <td> </td>
            <td> </td>
        </tr>
    </table>
    
  4. In the first column, add a <DIV> or <P> with labels for the Y-axis.
  5. In the second column, add a <DIV> with different background color and with fixed height and width the as same as the corresponding value of the X-axis. For any row with row number i, the code should look somewhat like this:
    <table>
        .
        .
        .
        <tr>
            <td><div> VerY[i] </div> </td>
            <td>
                <div style= 'background-color:blue; width:" + HorX[i] + "; />
                <p> HorX[i]</p>
            </td>
        </tr>
    </table>
    
The first <TD> contains the value for the axis. The second <TD> contains the <DIV> with background color as blue and with width the same as that of the X-axis value, along with a <P> tag with the value to display after the blue bar. This is the gist of the entire HTML horizontal bar chart. Steps 4 and 5 are repeated for each Y-axis value and then our bar chart is ready.
Well, HorX[i] is used directly as width in the attached demo, but as per requirement it can be reduced/increased proportionately. Say, for example, that you have only a limited area for displaying the chart. You may consider dividing the width of the blue <DIV> by, say, 2 or 3 in the loop for each row. Note that the bar chart is all about displaying the data proportionately. The bar for value 10 should look half the width of a 20-valued bar.

Double horizontal bar chart

Now let us extend our horizontal bar chart to cater to the second most required feature: comparing sets of values. The chart for the double horizontal bar chart would look something like this:
Now it won't be too hard to understand how to generate this double bar chart. In the same <TD> of Step 5, rather than adding one <DIV> and <P>, we need to place two <DIV>s and <P>s. The width of the second <DIV> should be guided by the second array of information.
That's it. To have better control over it, we can actually add another table inside the <TD> with two rows and one column, each column having a <DIV> representing the value. The HTML code would look somewhat like this:
<table>
   .
   .
   .
    <tr>
       <td><div> VerY[i] </div> </td>
       <td>
          <table>
             <tr>
                <td>
                   <div style='background-color:blue;width:"+HorX1[i]+";/>
                   <p> HorX1[i]</p>
                </td>
                <td>
                   <div style='background-color:green;width:"+HorX2[i]+";/>
                   <p> HorX2[i]</p>
                </td>
              </tr>
          </table>
       </td>
    </tr>
</table>

Benefits

Charts developed in such ways using basic HTML can be used very effectively in combination with web development tools and technology such as ASP, ASP.NET, JSP, etc. The only requirement is an array of values that needs to be displayed in the graphical manner. It works with Netscape as well, so scope is further increased automatically. HTML and JavaScript being the basic building blocks for this chart, it is easy to develop, easy to customize and, last but not least, easy to find people to maintain it.

Printing the charts

There is an Internet Explorer setting that determines whether the printing of images and <DIV>background colors is to be allowed. By default, IE settings do not allow the printing of background colors. To change this setting in IE 6, go to the "Tools" menu of IE, select "Internet Options..." and then select the "Advanced" tab. Now scroll down to the "Printing" section and check the checkbox for "Print Background Color and Images." We have the equivalent for this in IE 7 and other good browsers.
IE stores and picks this information from a registry. So, if you want to enable the printing of background colors and images with the script, add the following code in the HTML part of the landing page:
<script language="vbscript">
    on error resume next 
    'Change IE Print Settings to Print Background Color and Images 
    Set WSHShell = CreateObject("WScript.Shell") 
    WSHShell.RegWrite 
        "HKCU\Software\Microsoft\Internet Explorer\Main\Print_Background", 
        "yes"
    Set WSHShell = Nothing 
</script>

Wrapping up

Wonders can be created using the basic building blocks of HTML and DHTML in conjunction with scripts such as JavaScript and VBScript. User experience is best guided by UI and for UI to be fascinating, you don't always need to do hi-fi things. This is the second HTML chart that I am putting across; watch out in this space for more.
Please spare some time to rate and provide feedback about this article. Your couple of minutes can really help enhance its quality. If you're interested, click here to view all my CodeProject articles.
Read More »

Html Cleaner

Introduction

Cleaning html code by hand is a pain in the ...! But once in a while you just have to do it (well I do anyway). Unless you have a tool to do it for you. When finding the Html Agility Pack my prayers were answered. I quickly found the code for cleaning html code with this library (with a little help from google) at this place. I put it all in a Windows application which allows me to quickly access the function whenever I need it. Nothing fancy in the application (it was pretty much cut/paste from the two sources I have linked to, and then the simple interface.) No error checking... Nothing...!

Usage

Insert to html to be cleaned into the upper box. Modify the tags and the attributes to be deleted. Press the clean button. Copy the modified html code (the second box). That's about it...
Read More »

WYSIWYG HTML Editor

Introduction

I have found different code samples showing how to create a visual HTML editor in ASP using a text area. However, I needed a good editor for my WinForms application, but I couldn’t find a good example. Eventually, I wrote one from scratch after I converted some MFC code I found on the net. Based on the IE Browser control, you can create a powerful editor that allows you to manipulate HTMLtags visually and add any tags you want.

Using the code

The code is a simple form, and very easy to understand. In order to make it work, you first need to include two COM libraries:
  1. The Microsoft Web Control found in: c:\windows\system32\shdocvw.dll (add as a COM control).
  2. MSHTMLc:\windows\system32\MSHTML.tlb (add as a COM reference).
The reason for the tlb is that the mshtml.dll is used by the Windows system, and therefore is not accessible.
You don't need to worry about deployment: all Windows users will have these DLLs in their systemdirectory.
Read More »

A Professional HTML Renderer You Will Use

Introduction

This is a library of 100% managed code that draws beautifully formatted HTML. It comes along with three WinForms controls:
  • HtmlPanel
  • HtmlLabel
  • HtmlTooltip
And a static method ready to draw HTML:
  • HtmlRenderer.Render(Graphics g, string html, RectangleF area, bool clip) 
Note: The drawing engine is based on the CSS Level 2 specification.

Background

For years, I have been planning for a project like this. I prepared myself quite well. I went through the entire CSS Level 2 specification along with the HTML 4.01 specification.
One of the most interesting things I found is this: Drawing HTML is no more than laying out a bunch of boxes with borders margins and paddings. Once you overpass this paradigm, everything else is to help the code actually place the boxes on the right place, and then paint the string each box contains.
Imagine the power that drawing full-rich-formatted HTML on your controls can give to your applications. Use bold when you need it, italics on every message, and borders and fonts as you may like or need everywhere on the desktop application. One of the first projects where I will use it is on the tooltips of my Ribbon Project.
Although I have not tested it on mono yet, there should be no problem at all, since all of the code in the library is managed code and the methods it uses to paint are quite basic. It draws lines, rectangles, curves and text.
For now, the render looks really nice. Sometimes it can fool you to think you're using a real Web browser, trust me, download the demo, it is just an EXE and a DLL.

Using the Code

The library locates the code under the System.Drawing.Html namespace. The controls that renderHTML are under the System.Windows.Forms namespace.
The renderer follows the CSS Box Model. Box model is nothing but a tree of boxes, just as the tree ofHTML, each of these boxes is represented by a very used class called CssBox. The start node is represented by the class InitialContainer.
All the known CSS properties apply to each of these boxes. Each box may contain any number of child boxes and just one parent. The only box that has no parent at all is the so called Initial Container.
A typical use of an Initial Container to draw HTML would look like this:
//Create the InitialContainer
InitialContainer c = new InitialContainer("<html>");
 
//Give bounds to the container
c.SetBounds(ClientRectangle);
 
//Measure bounds of each box on the tree
c.MeasureBounds(graphics);

//Paint the HTML document
c.Paint(graphics);
renderer_002.jpg
First a label, then a panel and at last a ToolTip, all of which support HTML rendering.
You may never use it, since I provided controls and methods that create this object for you.

HtmlPanel

A panel that is ready to accept HTML code via its Text property. Its full name isSystem.Windows.Forms.HtmlPanel.
The only properties you need to know are:
  • AutoScroll. Activates/Deactivates the auto-scroll capabilities as you know. It is set to true by default.
  • Text. Gets/Sets the HTML source.
The panel will update the bounds of the elements as you scroll or resize the control.

HtmlLabel

A label that is ready to accept HTML code via its Text property. Its full name isSystem.Windows.Forms.HtmlLabel.
The only properties you need to know are:
  • AutoScroll. Activates/Deactivates the auto-scroll capabilities as you know. It is set to true by default.
  • AutoSize. Sets the size of the label automatically if activated.
  • Text. Gets/Sets the HTML source.
Some interesting things:
  • The label will update the bounds of the elements as you scroll or resize the control.
  • The label can be transparent.
  • The panel has better performance than the label.

HtmlToolTip

Works exactly like the ToolTip you already know, with the little difference that this tooltip will renderHTML on it. It's full name is System.Windows.Forms.HtmlToolTip.
There are no properties here to learn. Use it just the way you use the ToolTip that comes with the framework. Internally, it just handles the OwnerDraw event.

Some Features of my Own

I took the liberty of adding a couple of features:
  • Background gradients
  • Rounded corners
These are achieved through the following CSS properties:
  • background-gradient: (color)
  • background-gradient-angle: (number)
  • corner-ne-radius: (length)
  • corner-nw-radius: (length)
  • corner-se-radius: (length)
  • corner-se-radius: (length)
  • corner-radius: (length){1,4} (shorthand for all corners)

What's Currently Supported by the Renderer?

  • Most border, padding and margin and positioning CSSproperties (except for the heightproperty)
  • Text alignment horizontally and vertically, text indents too
  • Lists, ordered and unordered. Advanced numbering is not yet supported
  • Tables, almost all of it. Cell combinations work quite well as far as I tested them
  • Fonts (partially) and Colors
  • Backgrounds (just color)

Points of Interest

What can I say, this is one of the most fun projects I've ever been involved with. And so far, it runs beautifully and checks its original design goals.
I am planning to give it full rendering support, to the day that you may visualize a web page just as a good web browser would; and why not, make a WYSIWYG HTML editor to give amazing HTMLediting power to your applications.
I'm also planning to make sure it runs perfectly well on Mono and on Mobile platforms.
In the next few days, I'll publish a list of supported HTML tags and CSS properties.
Read More »

Scrollable HTML Table

Introduction

This JavaScript code can be used to convert tables in ordinary HTML into scrollable ones. No additional coding is necessary. All you need to do is put header rows in TBODY and give your table an ID field, include the ScrollableTable.js file and call makeScrollableTable in your document's onLoadmethod.
<html>
<head>
<script language="JavaScript" src=ScrollableTable.js></script>
</head>

<body onload="makeScrollableTable('tabela',true,'auto');">

<table border=0 id=tabela align=center 
  style="border-color: black; border-style: solid; border-width: 1;">
<thead>
<tr>
  <th bgcolor=blue style="color: white" rowspan=2 valign=bottom>ColA</th>
  <th bgcolor=blue style="color: white" colspan=2 align=center>ColBC</th>
  <th bgcolor=blue style="color: white" colspan=2 align=center>ColDE</th>
  <th bgcolor=blue style="color: white" rowspan=2 valign=bottom>ColF</th>
</tr>
<tr>
  <th bgcolor=blue style="color: white">ColB</th>
  <th bgcolor=blue style="color: white">ColC</th>
  <th bgcolor=blue style="color: white">ColD</th>
  <th bgcolor=blue style="color: white">ColE</th>
</tr>
</thead>
<tbody>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
<tr>
  <td>A</td>
  <td>BBBBBBBBBBBBBBBBBBBBBBBBB</td>
  <td>C</td>
  <td>D</td>
  <td>E</td>
  <td>F</td>
</tr>
</tbody>
<tfoot>
<tr>
  <td bgcolor=blue style="color: white" colspan=6 align=center>Footer</td>
</tr>
</tfoot>
</table>
</body>
</html>
Read More »